Skip to content

Commit

Permalink
Update the filter **: it can also be applied to a specific property.
Browse files Browse the repository at this point in the history
Update target: "ES6" to "ES5".
Add "CHANGELOG.md".
  • Loading branch information
cyrilschumacher committed May 28, 2016
1 parent 2f04d8c commit 205944c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 80 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.8] - 2015-05-28
### Changed
- Update the filter `**`: it can also be applied to a specific property.

[0.0.8]: https://github.com/cyrilschumacher/json-property-filter/compare/v0.0.6...v0.0.8
90 changes: 31 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# json-property-filter

[![MIT License][license-image]][license-url]
[![npm version][npmjs-image]][npmjs-url]
[![Build Status][travis-image]][travis-url]
[![typescript-standard-style][standard-image]][standard-url]
[![david-dm Status][david-image]][david-url]
[![david-dm devDependency Status][david-dev-dependencies-image]][david-dev-dependencies-url]
[![MIT License][license-image]][license-url] [![npm version][npmjs-image]][npmjs-url] [![Build Status][travis-image]][travis-url] [![typescript-standard-style][standard-image]][standard-url] [![david-dm Status][david-image]][david-url] [![david-dm devDependency Status][david-dev-dependencies-image]][david-dev-dependencies-url]

A library to filter a JSON object by including/excluding properties.

Expand All @@ -18,65 +13,42 @@ $ npm install json-property-filter
## Usage

To using [TypeScript](https://www.typescriptlang.org/) or JavaScript (ES6 support) language:

```javascript
import {JsonPropertyFilter} from "json-property-filter";

var filter = new JsonPropertyFilter("**");
var filter = new JsonPropertyFilter(["**"]);
filter.apply({ key: "value" });
```

If you use JavaScript (ES5 support) language:

```javascript
var JsonPropertyFilter = require("JsonPropertyFilter");
var JsonPropertyFilter = require("json-property-filter");

var filter = new JsonPropertyFilter.JsonPropertyFilter("**");
var filter = new JsonPropertyFilter.JsonPropertyFilter(["**"]);
filter.apply({ key: "value" });
```

### `include` filters

- all properties: `**`

```json
{
"key":"value",
"property":{
"key":"value"
}
}
```

- root properties only: `*`

```json
{
"key":"value"
}
```

- specific path: `root.property.element`, `root.array.property.element`

```json
{
"property":{
"element":"value"
}
}
```

```json
{
"array":[
{
"property":{
"element":"value"
}
}
]
}
```

### `exclude` filters
### Filters

- `**`

All properties. Include all the properties children and their children.

- `*`

Root properties only. Include only properties located in the root.

- `root.property.element`

Specific property. Include a specific property.

### TypeScript definition

If you use the TypeScript language on your project, you can use the following definition from that path:

> lib/jsonPropertyFilter.d.ts
### Test

Expand All @@ -91,23 +63,23 @@ $ npm test
## License

> 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.
[npmjs-image]:https://badge.fury.io/js/json-property-filter.svg
[npmjs-url]:https://www.npmjs.com/package/json-property-filter
[david-dev-dependencies-image]: https://david-dm.org/cyrilschumacher/json-property-filter/dev-status.svg
[david-dev-dependencies-url]: https://david-dm.org/cyrilschumacher/json-property-filter#info=devDependencies
[david-image]: https://david-dm.org/cyrilschumacher/json-property-filter.svg
[david-url]: https://david-dm.org/cyrilschumacher/json-property-filter
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[license-url]: LICENSE
[npmjs-image]: https://badge.fury.io/js/json-property-filter.svg
[npmjs-url]: https://www.npmjs.com/package/json-property-filter
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat
[standard-url]: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
[travis-image]: https://travis-ci.org/cyrilschumacher/json-property-filter.svg
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-property-filter",
"version": "0.0.7",
"version": "0.0.8",
"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",
Expand All @@ -21,7 +21,7 @@
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"mocha": "^2.5.3",
"ts-node": "^0.7.3",
"typescript": "^1.8.10"
}
Expand Down
37 changes: 21 additions & 16 deletions src/jsonIncludePropertyFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* @class
*/
export default class JsonIncludePropertyFilter {
private static ALL_PROPERTIES = "**";
private static ALL_ELEMENT_PROPERTIES = /\*$/g;
private static ALL_PROPERTIES_REGEX = /\*\*$/g;
private static ALL_ELEMENT_PROPERTIES_REGEX = /\*$/g;
private static ARRAY_INDEX = /\[[0-9]+\]/g;
private static PATH_SEPARATOR = ".";
private static STRING_EMPTY = "";
Expand All @@ -50,30 +50,35 @@ export default class JsonIncludePropertyFilter {
*/
public apply = (source: Array<string>): Array<string> => {
const destination = new Array<string>();
if (this._containsAllPropertiesFilter()) {
return source;
}

for (let rule of this._properties) {
this._include(rule, source, destination);
}

return destination;
}

private _containsAllPropertiesFilter() {
return this._properties.indexOf(JsonIncludePropertyFilter.ALL_PROPERTIES) > -1;
}

private _include(rule: string, source: Array<string>, destination: Array<string>) {
if (rule.match(JsonIncludePropertyFilter.ALL_ELEMENT_PROPERTIES)) {
if (rule.match(JsonIncludePropertyFilter.ALL_PROPERTIES_REGEX)) {
this._includeProperties(rule, source, destination);
} else if (rule.match(JsonIncludePropertyFilter.ALL_ELEMENT_PROPERTIES_REGEX)) {
this._includeRootProperties(rule, source, destination);
} else {
this._includeSpecificPath(rule, source, destination);
}
}

private _includeProperties(rule: string, source: Array<string>, destination: Array<string>) {
const formattedRule = rule.substr(0, rule.length - 2);
for (const propertySourcePath in source) {
const propertySourceValue = source[propertySourcePath];

if (propertySourcePath.match(`^${formattedRule}`)) {
destination[propertySourcePath] = propertySourceValue;
}
}
}

private _includeRootProperties(rule: string, source: Array<string>, destination: Array<string>) {
const formattedRule = rule.substr(0, rule.length - 1);

for (const propertySourcePath in source) {
Expand All @@ -85,12 +90,12 @@ export default class JsonIncludePropertyFilter {
}
} else {
if (formattedPropertySourcePath.match(`^${formattedRule}`)) {
const splittedFormattedPropertySourcePath = formattedPropertySourcePath.split(".");
const splittedFormattedRule = formattedRule.split(".");
const splittedFormattedPropertySourcePath = formattedPropertySourcePath.split(".");
const splittedFormattedRule = formattedRule.split(".");

if (splittedFormattedPropertySourcePath.length === splittedFormattedRule.length) {
destination[propertySourcePath] = propertySourceValue;
}
if (splittedFormattedPropertySourcePath.length === splittedFormattedRule.length) {
destination[propertySourcePath] = propertySourceValue;
}
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions test/jsonPropertyFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe("JsonPropertyFilter", () => {
const properties = ["commit.parents.*"];
const filter = new JsonPropertyFilter(properties);
const filtered = filter.apply(source);
const expected = { commit: { parents: [{ sha: "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e", url: "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e" }, { sha: "762941318ee16e59dabbacb1b4049eec22f0d303", url: "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303" }] } }
const expected = { commit: { parents: [{ sha: "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e", url: "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e" }, { sha: "762941318ee16e59dabbacb1b4049eec22f0d303", url: "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303" }] } };

assert.deepEqual(filtered, expected);
});
Expand All @@ -188,7 +188,16 @@ describe("JsonPropertyFilter", () => {
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" } }
const expected = { commit: { sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", url: "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" } };

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

it("should return the all elements of 'protection' property and their children.", () => {
const properties = ["protection.**"];
const filter = new JsonPropertyFilter(properties);
const filtered = filter.apply(source);
const expected = { protection: { enabled: false, required_status_checks: { enforcement_level: "off", contexts: [] } } };

assert.deepEqual(filtered, expected);
});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"outDir": "lib",
"preserveConstEnums": true,
"removeComments": true,
"target": "ES6"
"target": "ES5"
},
"filesGlob": [
"./src/**/*.ts",
Expand Down

0 comments on commit 205944c

Please sign in to comment.