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

Commit 9322a17

Browse files
committed
first commit
0 parents  commit 9322a17

7 files changed

+138
-0
lines changed

.eslint

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"rules": {
3+
"indent": [2, 2],
4+
"quotes": [2, "single"],
5+
"linebreak-style": [2, "unix"],
6+
"semi": [2, "always"],
7+
"no-underscore-dangle": 0,
8+
"curly": 0,
9+
"no-use-before-define": [2, "nofunc"],
10+
"spaced-comment": [2, "always"],
11+
"space-before-function-paren": [2, { "anonymous": "always", "named": "never" }],
12+
"space-after-keywords": [2, "always"],
13+
"space-before-blocks": [2, "always"],
14+
"semi-spacing": [2, {"before": false, "after": true}],
15+
},
16+
"env": {
17+
"browser": true,
18+
"node": true,
19+
"mocha": true
20+
}
21+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
bower_components

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Maurizio Lupo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var _get = require('lodash/get');
2+
var _set = require('lodash/set');
3+
var _isUndefined = require('lodash/isUndefined');
4+
var expandPathExpression = require('./lib/expandPathExpression');
5+
6+
function Sieve(paths) {
7+
this._paths = paths || [];
8+
}
9+
10+
Sieve.prototype.pass = function pass(path) {
11+
this._paths.push(path);
12+
};
13+
14+
Sieve.prototype.toJSON = function () {
15+
return JSON.stringify(this._paths);
16+
};
17+
18+
Sieve.prototype.apply = function (obj) {
19+
const output = {};
20+
for (const pathEpression of this._paths) {
21+
for (const path of expandPathExpression(pathEpression, obj)) {
22+
const value = _get(obj, path);
23+
if (!_isUndefined(value)) {
24+
_set(output, path, value);
25+
}
26+
}
27+
}
28+
return output;
29+
};

lib/expandPathExpression.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var _toPath = require('lodash/toPath');
2+
3+
function expandExpressionFragment(path, expression, obj) {
4+
yield path.concat(expression);
5+
}
6+
7+
function* iterOverPath(pathIter, expression, obj) {
8+
if (pathIter) {
9+
for (const path of pathIter) {
10+
yield* expandExpressionFragment(path, expression, obj);
11+
}
12+
} else {
13+
yield* expandExpressionFragment([], expression, obj);
14+
}
15+
}
16+
17+
function* expandPathExpression(pathExpression, obj) {
18+
let iterPath;
19+
const pathArray = _toPath(pathExpression);
20+
for (const pathFragment of pathArray) {
21+
iterPath = iterOverPath(iterPath, pathFragment, obj);
22+
}
23+
yield* iterPath;
24+
}
25+
26+
// [3:6] [:2] [3:] [5:-2] slice
27+
// * all
28+
// [key1|key2]
29+
30+
module.exports = expandPathExpression;

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "obj-sieve",
3+
"version": "0.0.0",
4+
"description": "Skim an object of unnecessary data",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha tests/*.js",
8+
"watch": "npm run test -- -w",
9+
"lint": "eslint --ext .js ./src ./tests",
10+
"release:major": "npm-release major",
11+
"release:minor": "npm-release minor",
12+
"release:patch": "npm-release patch",
13+
"precommit": "npm run lint",
14+
"prepush": "npm run test"
15+
},
16+
"keyword": [
17+
"redux",
18+
"immutable",
19+
"diff",
20+
"patch"
21+
],
22+
"repository": "[email protected]:sithmel/obj-sieve.git",
23+
"author": "Maurizio Lupo <[email protected]>",
24+
"license": "MIT",
25+
"dependencies": {
26+
"lodash": "^4.17.4"
27+
},
28+
"devDependencies": {
29+
"chai": "^4.1.2",
30+
"eslint": "^4.9.0",
31+
"husky": "^0.14.3",
32+
"mocha": "^4.0.1",
33+
"npm-release": "^1.0.0"
34+
}
35+
}

0 commit comments

Comments
 (0)