Skip to content

Commit 0bfc906

Browse files
Add examples.
1 parent 7343e02 commit 0bfc906

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

example/array/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Example that excludes given JSON keys.
3+
*/
4+
5+
const fs = require("fs");
6+
const lib = require("../../lib/jsonPropertyFilter");
7+
8+
const buffer = fs.readFileSync("source.json");
9+
const source = JSON.parse(buffer.toString());
10+
11+
const filter = new lib.JsonPropertyFilter(["description", "-_private"]);
12+
const filtered = filter.apply(source);
13+
14+
console.log(filtered); // [ { description: 'First item' }, { description: 'Second item' } ]

example/array/source.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"id": 1,
4+
"description": "First item",
5+
"_private": {
6+
"type": "integer"
7+
}
8+
},
9+
{
10+
"id": 2,
11+
"description": "Second item",
12+
"_private": {
13+
"type": "string"
14+
}
15+
}
16+
]

example/complex/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Example that excludes given JSON keys.
3+
*/
4+
5+
const fs = require("fs");
6+
const lib = require("../../lib/jsonPropertyFilter");
7+
8+
const buffer = fs.readFileSync("source.json");
9+
const source = JSON.parse(buffer.toString());
10+
11+
const filter = new lib.JsonPropertyFilter(["**", "-type", "-properties.id.type"]);
12+
const filtered = filter.apply(source);
13+
14+
console.log(filtered); // { title: 'Product', description: 'A product from Acme\'s catalog', properties: { id: { description: 'The unique identifier for a product' } }, required: [ 'id' ] }

example/complex/source.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"title": "Product",
3+
"description": "A product from Acme's catalog",
4+
"type": "object",
5+
"properties": {
6+
"id": {
7+
"description": "The unique identifier for a product",
8+
"type": "integer"
9+
}
10+
},
11+
"required": ["id"]
12+
}

example/include/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ const source = JSON.parse(buffer.toString());
1111
const filter = new lib.JsonPropertyFilter(["-key2.key3", "-key2.key5"]);
1212
const filtered = filter.apply(source);
1313

14-
console.log(filtered); // { key1: 'value1', key3: 'value3' }
14+
console.log(filtered); // { key1: 'value1', key2: { key4: 'value3' } }

example/root/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ const source = JSON.parse(buffer.toString());
1111
const filter = new lib.JsonPropertyFilter(["*", "key2.*"]);
1212
const filtered = filter.apply(source);
1313

14-
console.log(filtered); // { key1: 'value1', key3: 'value3' }
14+
console.log(filtered); // { key1: 'value1', key2: { key3: 'value2', key4: 'value3' } }

0 commit comments

Comments
 (0)