Skip to content

Commit bc082f9

Browse files
Fix bugs.
Fix example descriptions. Reorganize tests.
1 parent 0bfc906 commit bc082f9

File tree

9 files changed

+11
-9
lines changed

9 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1616
### Fixed
1717
- Fix CLI version.
1818
- Fix CLI.
19+
- Fix Regular expression on including and excluding filters.
1920

2021
## [1.2.0] - 2016-06-20
2122
### Added

example/array/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Example that excludes given JSON keys.
2+
* Example with array type.
33
*/
44

55
const fs = require("fs");

example/complex/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Example that excludes given JSON keys.
2+
* Example that include and excludes given JSON keys.
33
*/
44

55
const fs = require("fs");
@@ -8,7 +8,7 @@ const lib = require("../../lib/jsonPropertyFilter");
88
const buffer = fs.readFileSync("source.json");
99
const source = JSON.parse(buffer.toString());
1010

11-
const filter = new lib.JsonPropertyFilter(["**", "-type", "-properties.id.type"]);
11+
const filter = new lib.JsonPropertyFilter(["**", "-$schema", "-type", "-properties.id.type"]);
1212
const filtered = filter.apply(source);
1313

1414
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "test",
23
"title": "Product",
34
"description": "A product from Acme's catalog",
45
"type": "object",

example/exclude/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const lib = require("../../lib/jsonPropertyFilter");
88
const buffer = fs.readFileSync("source.json");
99
const source = JSON.parse(buffer.toString());
1010

11-
const filter = new lib.JsonPropertyFilter(["-key2.key3", "-key2.key5.key6"]);
11+
const filter = new lib.JsonPropertyFilter(["-$schema", "-key2.key3", "-key2.key5.key6"]);
1212
const filtered = filter.apply(source);
1313

1414
console.log(filtered); // { key1: 'value1', key2: { key4: 'value3' } }

src/filter/jsonExcludePropertyFilter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class JsonExcludePropertyFilter {
7373

7474
private _excludeProperty(rule: string, source: string[], path: string) {
7575
const pathWithoutArrayIndexStart = this._removeArrayIndexStart(path);
76-
const regexp = `^${rule}`;
76+
const regexp = `^${rule.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")}`;
7777

7878
if (pathWithoutArrayIndexStart.match(regexp)) {
7979
delete source[path];
@@ -101,7 +101,7 @@ export class JsonExcludePropertyFilter {
101101
delete source[path];
102102
}
103103
} else {
104-
const regexp = `^${rule}`;
104+
const regexp = `^${rule.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")}`;
105105

106106
if (pathWithoutIndex.match(regexp)) {
107107
const pathItems = pathWithoutIndex.split(JsonExcludePropertyFilter.PATH_SEPARATOR);
@@ -125,7 +125,7 @@ export class JsonExcludePropertyFilter {
125125
}
126126

127127
private _excludeSpecificPath(rule: string, source: string[]) {
128-
const regexp = `^${rule}`;
128+
const regexp = `^${rule.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")}`;
129129
for (const path in source) {
130130
if (path) {
131131
const pathWithoutArrayIndexStart = this._removeArrayIndexStart(path);

src/filter/jsonIncludePropertyFilter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class JsonIncludePropertyFilter {
7575

7676
private _includeProperty(rule: string, path: string, value: string, destination: string[]) {
7777
const cleanPath = this._removeArrayIndexStart(path);
78-
const regexp = `^${rule}`;
78+
const regexp = `^${rule.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")}`;
7979

8080
if (cleanPath.match(regexp)) {
8181
destination[path] = value;
@@ -103,7 +103,7 @@ export class JsonIncludePropertyFilter {
103103
destination[path] = value;
104104
}
105105
} else {
106-
const regexp = `^${rule}`;
106+
const regexp = `^${rule.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")}`;
107107

108108
if (pathWithoutArrayIndexStart.match(regexp)) {
109109
const pathItems = pathWithoutArrayIndexStart.split(JsonIncludePropertyFilter.PATH_SEPARATOR);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)