Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Commit 0a38098

Browse files
committed
integration with new path expression parser
1 parent 3b4ab21 commit 0a38098

12 files changed

+206
-429
lines changed

.eslint

-21
This file was deleted.

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
"extends": "standard"
3+
};

index.js

+2-74
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,3 @@
1-
const _get = require('lodash/get');
2-
const _set = require('lodash/set');
3-
const _isUndefined = require('lodash/isUndefined');
4-
const _isArray = require('lodash/isArray');
5-
const _isPlainObject = require('lodash/isPlainObject');
6-
const compactArrays = require('./lib/compactArrays');
7-
const expandPathExpression = require('./lib/expandPathExpression');
1+
const Sieve = require('./lib/sieve')
82

9-
function Sieve(paths) {
10-
if (!(this instanceof Sieve)) {
11-
return new Sieve(paths);
12-
}
13-
this._paths = paths || {};
14-
this._paths.include = this._paths.include || [];
15-
this._paths.exclude = this._paths.exclude || [];
16-
}
17-
18-
Sieve.prototype.include = function include(path) {
19-
this._paths.include.push(path);
20-
return this;
21-
};
22-
23-
Sieve.prototype.exclude = function include(path) {
24-
this._paths.exclude.push(path);
25-
return this;
26-
};
27-
28-
Sieve.prototype.toJSON = function () {
29-
return this._paths;
30-
};
31-
32-
Sieve.prototype.apply = function (obj) {
33-
const output = {};
34-
for (const pathEpression of this._paths.include) {
35-
for (const path of expandPathExpression(pathEpression, obj)) {
36-
const value = _get(obj, path);
37-
if (!_isUndefined(value)) {
38-
_set(output, path, value);
39-
}
40-
}
41-
}
42-
43-
for (const pathEpression of this._paths.exclude) {
44-
for (const path of expandPathExpression(pathEpression, output)) {
45-
const parentObj = _get(output, path.slice(0, -1));
46-
delete parentObj[path[path.length - 1]];
47-
}
48-
}
49-
compactArrays(output);
50-
return output;
51-
};
52-
53-
const strOrArray = (s) => _isArray(s) ? s : s.split(',');
54-
55-
Sieve.filter = function filter(paths, obj) {
56-
let includeList;
57-
let excludeList;
58-
if (_isPlainObject(paths)) {
59-
includeList = strOrArray(paths.include || []);
60-
excludeList = strOrArray(paths.exclude || []);
61-
} else {
62-
includeList = strOrArray(paths);
63-
excludeList = [];
64-
}
65-
const sieve = new Sieve();
66-
for (const path of includeList) {
67-
sieve.include(path);
68-
}
69-
for (const path of excludeList) {
70-
sieve.exclude(path);
71-
}
72-
return sieve.apply(obj);
73-
}
74-
75-
module.exports = Sieve;
3+
module.exports = Sieve

lib/compactArrays.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
const _isArray = require('lodash/isArray');
2-
const _isPlainObject = require('lodash/isPlainObject');
3-
const _pull = require('lodash/pull');
1+
const _isArray = require('lodash/isArray')
2+
const _isPlainObject = require('lodash/isPlainObject')
3+
const _pull = require('lodash/pull')
44

5-
function compactArrays(object) {
6-
const alreadySeen = [];
7-
function _compactArrays(obj) {
5+
function compactArrays (object) {
6+
const alreadySeen = []
7+
function _compactArrays (obj) {
88
if (alreadySeen.indexOf(obj) !== -1) {
9-
return;
9+
return
1010
}
11-
alreadySeen.push(obj);
11+
alreadySeen.push(obj)
1212
if (_isArray(obj)) {
13-
_pull(obj, undefined);
13+
_pull(obj, undefined)
1414
for (var i = 0; i < obj.length; i++) {
15-
_compactArrays(obj[i]);
15+
_compactArrays(obj[i])
1616
}
17-
} else if (_isPlainObject(obj)){
17+
} else if (_isPlainObject(obj)) {
1818
for (var k in obj) {
19-
_compactArrays(obj[k]);
19+
_compactArrays(obj[k])
2020
}
2121
}
2222
}
23-
_compactArrays(object);
23+
_compactArrays(object)
2424
}
2525

26-
module.exports = compactArrays;
26+
module.exports = compactArrays

lib/expandPathExpression.js

-63
This file was deleted.

lib/sieve.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const _get = require('lodash/get')
2+
const _set = require('lodash/set')
3+
const _isUndefined = require('lodash/isUndefined')
4+
const _isArray = require('lodash/isArray')
5+
const _isPlainObject = require('lodash/isPlainObject')
6+
const compactArrays = require('./compactArrays')
7+
8+
const expressionParser = require('obj-path-expression-parser')
9+
10+
function Sieve (paths) {
11+
if (!(this instanceof Sieve)) {
12+
return new Sieve(paths)
13+
}
14+
this._paths = paths || {}
15+
this._paths.include = this._paths.include || []
16+
this._paths.exclude = this._paths.exclude || []
17+
}
18+
19+
Sieve.prototype.include = function include (path) {
20+
this._paths.include.push(path)
21+
return this
22+
}
23+
24+
Sieve.prototype.exclude = function include (path) {
25+
this._paths.exclude.push(path)
26+
return this
27+
}
28+
29+
Sieve.prototype.toJSON = function () {
30+
return this._paths
31+
}
32+
33+
Sieve.prototype.apply = function (obj) {
34+
const output = {}
35+
for (const pathEpression of this._paths.include) {
36+
for (const path of expressionParser(pathEpression, obj)) {
37+
const value = _get(obj, path)
38+
if (!_isUndefined(value)) {
39+
_set(output, path, value)
40+
}
41+
}
42+
}
43+
44+
for (const pathEpression of this._paths.exclude) {
45+
for (const path of expressionParser(pathEpression, output)) {
46+
const parentObj = _get(output, path.slice(0, -1))
47+
delete parentObj[path[path.length - 1]]
48+
}
49+
}
50+
compactArrays(output)
51+
return output
52+
}
53+
54+
const strOrArray = (s) => _isArray(s) ? s : s.split(',')
55+
56+
Sieve.filter = function filter (paths, obj) {
57+
let includeList
58+
let excludeList
59+
if (_isPlainObject(paths)) {
60+
includeList = strOrArray(paths.include || [])
61+
excludeList = strOrArray(paths.exclude || [])
62+
} else {
63+
includeList = strOrArray(paths)
64+
excludeList = []
65+
}
66+
const sieve = new Sieve()
67+
for (const path of includeList) {
68+
sieve.include(path)
69+
}
70+
for (const path of excludeList) {
71+
sieve.exclude(path)
72+
}
73+
return sieve.apply(obj)
74+
}
75+
76+
module.exports = Sieve

lib/toPath.js

-19
This file was deleted.

package.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@
66
"scripts": {
77
"test": "mocha tests/*.js",
88
"watch": "npm run test -- -w",
9-
"lint": "eslint --ext .js ./src ./tests",
9+
"lint": "eslint --fix --ext .js ./lib ./tests",
1010
"release:major": "npm-release major",
1111
"release:minor": "npm-release minor",
1212
"release:patch": "npm-release patch",
1313
"precommit": "npm run lint",
1414
"prepush": "npm run test"
1515
},
16-
"keyword": [
17-
"redux",
18-
"immutable",
19-
"diff",
20-
"patch"
21-
],
16+
"keyword": [],
2217
"repository": "[email protected]:sithmel/obj-sieve.git",
2318
"author": "Maurizio Lupo <[email protected]>",
2419
"license": "MIT",
2520
"dependencies": {
2621
"iter-tools": "^1.4.1",
2722
"lodash": "^4.17.4",
28-
"micromatch": "^3.1.4"
23+
"micromatch": "^3.1.4",
24+
"obj-path-expression-parser": "0.0.2"
2925
},
3026
"devDependencies": {
3127
"chai": "^4.1.2",
3228
"eslint": "^4.9.0",
29+
"eslint-config-standard": "^10.2.1",
30+
"eslint-plugin-import": "^2.8.0",
31+
"eslint-plugin-node": "^5.2.1",
32+
"eslint-plugin-promise": "^3.6.0",
33+
"eslint-plugin-standard": "^3.0.1",
3334
"husky": "^0.14.3",
3435
"mocha": "^4.0.1",
3536
"npm-release": "^1.0.0"

0 commit comments

Comments
 (0)