Skip to content

Commit

Permalink
Add class to handle exclude.
Browse files Browse the repository at this point in the history
Fix bug.
  • Loading branch information
cyrilschumacher committed May 22, 2016
1 parent 14b239d commit 2f04d8c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 38 deletions.
51 changes: 26 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
{
"name": "json-property-filter",
"version": "0.0.6",
"description": "A library to filter a JSON object by including/excluding properties.",
"main": "lib/jsonPropertyFilter.js",
"scripts": {
"prepublish": "tsc",
"test": "mocha --require ts-node/register test/*.ts"
},
"repository": {
"type": "git",
"url": "https://github.com/cyrilschumacher/json-property-filter.git"
},
"bugs": {
"url": "https://github.com/cyrilschumacher/json-property-filter/issues"
},
"homepage": "https://github.com/cyrilschumacher/json-property-filter",
"typings": "lib/jsonPropertyFilter.d.ts",
"author": "Cyril Schumacher",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"ts-node": "^0.7.3",
"typescript": "^1.8.10"
}
"name": "json-property-filter",
"version": "0.0.7",
"keywords": ["json-property-filter", "filter", "json", "property", "path", "object", "element"],
"description": "A library to filter a JSON object by including/excluding properties.",
"main": "lib/jsonPropertyFilter.js",
"scripts": {
"prepublish": "tsc",
"test": "mocha --require ts-node/register test/*.ts"
},
"repository": {
"type": "git",
"url": "https://github.com/cyrilschumacher/json-property-filter.git"
},
"bugs": {
"url": "https://github.com/cyrilschumacher/json-property-filter/issues"
},
"homepage": "https://github.com/cyrilschumacher/json-property-filter",
"typings": "lib/jsonPropertyFilter.d.ts",
"author": "Cyril Schumacher",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"ts-node": "^0.7.3",
"typescript": "^1.8.10"
}
}
48 changes: 48 additions & 0 deletions src/jsonExcludePropertyFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* The MIT License (MIT)
*
* Copyright (c) 2016 Cyril Schumacher.fr
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Filter include JSON property.
* @class
*/
export default class JsonExcludePropertyFilter {
private _properties: Array<string>;

/**
* Constructor.
* @constructors
* @param {string[]} properties Properties.
*/
public constructor(properties: Array<string>) {
this._properties = properties;
}

/**
* Apply filter on a JSON object.
* @param {Object} source A JSON object.
* @return {Object} The filtered JSON object.
*/
public apply = (source: Array<string>): Array<string> => {
return source;
}
}
8 changes: 6 additions & 2 deletions src/jsonIncludePropertyFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ export default class JsonIncludePropertyFilter {
for (const propertySourcePath in source) {
const formattedPropertySourcePath = propertySourcePath.replace(JsonIncludePropertyFilter.ARRAY_INDEX, JsonIncludePropertyFilter.STRING_EMPTY);
const propertySourceValue = source[propertySourcePath];

if (formattedRule === JsonIncludePropertyFilter.STRING_EMPTY) {
if (propertySourcePath.split(JsonIncludePropertyFilter.PATH_SEPARATOR).length === 1) {
destination[propertySourcePath] = propertySourceValue;
}
} else {
if (formattedPropertySourcePath.match(`^${formattedRule}`)) {
destination[propertySourcePath] = propertySourceValue;
const splittedFormattedPropertySourcePath = formattedPropertySourcePath.split(".");
const splittedFormattedRule = formattedRule.split(".");

if (splittedFormattedPropertySourcePath.length === splittedFormattedRule.length) {
destination[propertySourcePath] = propertySourceValue;
}
}
}
}
Expand Down
33 changes: 22 additions & 11 deletions src/jsonPropertyFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

import JsonSerializer from "./jsonSerializer";
import JsonExcludePropertyFilter from "./jsonExcludePropertyFilter";
import JsonIncludePropertyFilter from "./jsonIncludePropertyFilter";

/**
Expand All @@ -30,7 +31,9 @@ import JsonIncludePropertyFilter from "./jsonIncludePropertyFilter";
*/
export class JsonPropertyFilter {
private static INCLUDE_SYMBOL = "+";
private static EXCLUDE_SYMBOL = "-";

private _exclude: JsonExcludePropertyFilter;
private _include: JsonIncludePropertyFilter;

/**
Expand All @@ -40,8 +43,10 @@ export class JsonPropertyFilter {
*/
public constructor(args: string | Array<string>) {
const properties = this._formatProperties(args);
const propertiesToInclude = this._extractProperties(properties);
const propertiesToInclude = this._extractProperties(properties, [JsonPropertyFilter.INCLUDE_SYMBOL, ""]);
const propertiesToExclude = this._extractProperties(properties, [JsonPropertyFilter.EXCLUDE_SYMBOL]);

this._exclude = new JsonExcludePropertyFilter(propertiesToExclude);
this._include = new JsonIncludePropertyFilter(propertiesToInclude);
}

Expand All @@ -52,31 +57,37 @@ export class JsonPropertyFilter {
*/
public apply(source: Object): Object {
const keys = JsonSerializer.serializeToArray(source);
const destination = this._include.apply(keys);
let destination = new Array<string>();
destination = this._include.apply(keys);
destination = this._exclude.apply(destination);

const filtered = JsonSerializer.serializeToObject(destination);

return filtered;
}

private _extractProperties(properties: Array<string>): Array<string> {
private _extractProperties(properties: Array<string>, symbols: Array<string>): Array<string> {
let include = new Array<string>();

for (let property of properties) {
const formattedProperty = this._extractProperty(property);
include.push(formattedProperty);
const formattedProperty = this._extractProperty(property, symbols);
if (formattedProperty) {
include.push(formattedProperty);
}
}

return include;
}

private _extractProperty(property: string) {
const isIncludeProperty = property.indexOf(JsonPropertyFilter.INCLUDE_SYMBOL) === 0;

if (isIncludeProperty) {
return property.substring(1);
private _extractProperty(property: string, symbols: Array<string>): string {
for (const symbol of symbols) {
const inProperty = property.indexOf(symbol);
if (inProperty === 0) {
return property.substring(symbol.length);
}
}

return property;
return undefined;
}

private _formatProperties(properties: string | Array<string>): Array<string> {
Expand Down
9 changes: 9 additions & 0 deletions test/jsonPropertyFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ describe("JsonPropertyFilter", () => {

assert.deepEqual(filtered, expected);
});

it("should return the 'id' property of 'content' array", () => {
const properties = ["commit.*"];
const filter = new JsonPropertyFilter(properties);
const filtered = filter.apply(source);
const expected = { commit: { sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", url: "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" } }

assert.deepEqual(filtered, expected);
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"!node_modules/**/*.ts"
],
"files": [
"./src/jsonExcludePropertyFilter.ts",
"./src/jsonIncludePropertyFilter.ts",
"./src/jsonPropertyFilter.ts",
"./src/jsonSerializer.ts"
Expand Down

0 comments on commit 2f04d8c

Please sign in to comment.