Skip to content

Commit abd71ec

Browse files
Add unit tests for arrays support.
Simplify unit tests for "JsonPropertyFilter". Update package version. Update CHANGELOG.
1 parent d57f290 commit abd71ec

File tree

6 files changed

+374
-212
lines changed

6 files changed

+374
-212
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [Unreleased]
5+
## [1.1.0]
66
### Added
77
- Add arrays support.
88

@@ -30,7 +30,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3030
### Changed
3131
- Update the filter `**`: it can also be applied to a specific property.
3232

33-
[Unreleased]: https://github.com/cyrilschumacher/json-property-filter/compare/1.0.2...HEAD
33+
[1.1.0]: https://github.com/cyrilschumacher/json-property-filter/compare/1.0.2...1.1.0
3434
[1.0.2]: https://github.com/cyrilschumacher/json-property-filter/compare/1.0.1...1.0.2
3535
[1.0.1]: https://github.com/cyrilschumacher/json-property-filter/compare/1.0.0...1.0.1
3636
[1.0.0]: https://github.com/cyrilschumacher/json-property-filter/compare/0.0.8...1.0.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-property-filter",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"keywords": [
55
"json-property-filter",
66
"filter",

src/jsonExcludePropertyFilter.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class JsonExcludePropertyFilter {
2929
private static ALL_PROPERTIES_REGEX = /\*\*$/g;
3030
private static ALL_ELEMENT_PROPERTIES_REGEX = /\*$/g;
3131
private static ARRAY_INDEX = /\[[0-9]+\]/g;
32+
private static ARRAY_INDEX_START = /^\[([0-9]+)\]./;
3233
private static PATH_SEPARATOR = ".";
3334
private static STRING_EMPTY = "";
3435

@@ -60,6 +61,10 @@ export default class JsonExcludePropertyFilter {
6061
return source;
6162
}
6263

64+
private _cleanupPath(path: string) {
65+
return path.replace(JsonExcludePropertyFilter.ARRAY_INDEX_START, "");
66+
}
67+
6368
private _exclude(rule: string, source: Array<string>) {
6469
if (rule.match(JsonExcludePropertyFilter.ALL_PROPERTIES_REGEX)) {
6570
this._excludeProperties(rule, source);
@@ -71,9 +76,10 @@ export default class JsonExcludePropertyFilter {
7176
}
7277

7378
private _excludeProperty(rule: string, source: Array<string>, path: string) {
79+
const cleanPath = this._cleanupPath(path);
7480
const regexp = `^${rule}`;
7581

76-
if (path.match(regexp)) {
82+
if (cleanPath.match(regexp)) {
7783
delete source[path];
7884
}
7985
}
@@ -92,7 +98,10 @@ export default class JsonExcludePropertyFilter {
9298
const pathWithoutIndex = path.replace(JsonExcludePropertyFilter.ARRAY_INDEX, JsonExcludePropertyFilter.STRING_EMPTY);
9399

94100
if (rule === JsonExcludePropertyFilter.STRING_EMPTY) {
95-
if (path.split(JsonExcludePropertyFilter.PATH_SEPARATOR).length === 1) {
101+
const cleanPath = this._cleanupPath(path);
102+
const splittedPath = cleanPath.split(JsonExcludePropertyFilter.PATH_SEPARATOR);
103+
104+
if (splittedPath.length === 1) {
96105
delete source[path];
97106
}
98107
} else {
@@ -123,7 +132,8 @@ export default class JsonExcludePropertyFilter {
123132
const regexp = `^${rule}`;
124133
for (const path in source) {
125134
if (path) {
126-
const pathWithoutIndex = path.replace(JsonExcludePropertyFilter.ARRAY_INDEX, JsonExcludePropertyFilter.STRING_EMPTY);
135+
const cleanPath = this._cleanupPath(path);
136+
const pathWithoutIndex = cleanPath.replace(JsonExcludePropertyFilter.ARRAY_INDEX, JsonExcludePropertyFilter.STRING_EMPTY);
127137

128138
if (pathWithoutIndex.match(regexp)) {
129139
const pathWithoutIndexItems = pathWithoutIndex.split(".");

src/jsonIncludePropertyFilter.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class JsonIncludePropertyFilter {
2929
private static ALL_PROPERTIES_REGEX = /\*\*$/g;
3030
private static ALL_ELEMENT_PROPERTIES_REGEX = /\*$/g;
3131
private static ARRAY_INDEX = /\[[0-9]+\]/g;
32+
private static ARRAY_INDEX_START = /^\[([0-9]+)\]./;
3233
private static PATH_SEPARATOR = ".";
3334
private static STRING_EMPTY = "";
3435

@@ -62,6 +63,10 @@ export default class JsonIncludePropertyFilter {
6263
}
6364
}
6465

66+
private _cleanupPath(path: string) {
67+
return path.replace(JsonIncludePropertyFilter.ARRAY_INDEX_START, "");
68+
}
69+
6570
private _include(rule: string, source: Array<string>, destination: Array<string>) {
6671
if (rule.match(JsonIncludePropertyFilter.ALL_PROPERTIES_REGEX)) {
6772
this._includeProperties(rule, source, destination);
@@ -72,11 +77,12 @@ export default class JsonIncludePropertyFilter {
7277
}
7378
}
7479

75-
private _includeProperty(rule: string, source: string, value: string, destination: Array<string>) {
80+
private _includeProperty(rule: string, path: string, value: string, destination: Array<string>) {
81+
const cleanPath = this._cleanupPath(path);
7682
const regexp = `^${rule}`;
7783

78-
if (source.match(regexp)) {
79-
destination[source] = value;
84+
if (cleanPath.match(regexp)) {
85+
destination[path] = value;
8086
}
8187
}
8288

@@ -95,7 +101,10 @@ export default class JsonIncludePropertyFilter {
95101
const pathWithoutIndex = path.replace(JsonIncludePropertyFilter.ARRAY_INDEX, JsonIncludePropertyFilter.STRING_EMPTY);
96102

97103
if (rule === JsonIncludePropertyFilter.STRING_EMPTY) {
98-
if (path.split(JsonIncludePropertyFilter.PATH_SEPARATOR).length === 1) {
104+
const cleanPath = this._cleanupPath(path);
105+
const splittedPath = cleanPath.split(JsonIncludePropertyFilter.PATH_SEPARATOR);
106+
107+
if (splittedPath.length === 1) {
99108
destination[path] = value;
100109
}
101110
} else {
@@ -126,7 +135,8 @@ export default class JsonIncludePropertyFilter {
126135
private _includeSpecificPath(rule: string, source: Array<string>, destination: Array<string>) {
127136
for (const path in source) {
128137
if (path) {
129-
const pathWithoutIndex = path.replace(JsonIncludePropertyFilter.ARRAY_INDEX, JsonIncludePropertyFilter.STRING_EMPTY);
138+
const cleanPath = this._cleanupPath(path);
139+
const pathWithoutIndex = cleanPath.replace(JsonIncludePropertyFilter.ARRAY_INDEX, JsonIncludePropertyFilter.STRING_EMPTY);
130140
const regexp = `^${rule}`;
131141

132142
if (pathWithoutIndex.match(regexp)) {

src/serializer/serializeToObject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function _isArray(arr) {
2626
const firstKey = keys[0];
2727

2828
if (firstKey) {
29-
const isArray = /^\[([0-9]+)\]/;
30-
return isArray.test(firstKey);
29+
const isArrayRegexp = /^\[([0-9]+)\]/;
30+
return isArrayRegexp.test(firstKey);
3131
}
3232

3333
return false;

0 commit comments

Comments
 (0)