Skip to content

Commit 33e1574

Browse files
author
sttk
committed
Initial implementation
1 parent 8fbbba3 commit 33e1574

9 files changed

Lines changed: 481 additions & 0 deletions

File tree

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Coverage directory used by tools like istanbul
2+
coverage

.eslintrc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"env": {
3+
"node": true
4+
},
5+
"rules": {
6+
"array-bracket-spacing": [2, "never"],
7+
"block-scoped-var": 2,
8+
"brace-style": [2, "1tbs"],
9+
"camelcase": 1,
10+
"computed-property-spacing": [2, "never"],
11+
"curly": 2,
12+
"eol-last": 2,
13+
"eqeqeq": [2, "smart"],
14+
"max-depth": [1, 3],
15+
"max-len": [1, 80],
16+
"max-statements": [1, 20],
17+
"new-cap": 1,
18+
"no-extend-native": 2,
19+
"no-mixed-spaces-and-tabs": 2,
20+
"no-trailing-spaces": 2,
21+
"no-unused-vars": 1,
22+
"no-use-before-define": [2, "nofunc"],
23+
"object-curly-spacing": [2, "always"],
24+
"quotes": [2, "single", "avoid-escape"],
25+
"semi": [2, "always"],
26+
"keyword-spacing": [2, { "before": true, "after": true }],
27+
"space-unary-ops": 2
28+
}
29+
}

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Coverage directory used by tools like istanbul
7+
coverage
8+
9+
# Dependency directories
10+
node_modules
11+
12+
# Optional npm cache directory
13+
.npm
14+
15+
# Files generated by platform.
16+
.DS_Store

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
3+
language: node_js
4+
5+
node_js:
6+
- '6'
7+
- '5'
8+
- '4'
9+
- '0.12'
10+
- '0.10'
11+
12+
os:
13+
- linux
14+
- osx

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# each-props [![Build Status][travis-img]][travis-url] [![Build Status][appveyor-img]][appveyor-url]
2+
3+
Process object properties deeply.
4+
5+
[![NPM][npm-img]][npm-url]
6+
7+
[npm-img]: https://nodei.co/npm/each-props.png
8+
[npm-url]: https://nodei.co/npm/each-props/
9+
[travis-img]: https://travis-ci.org/sttk/each-props.svg?branch=master
10+
[travis-url]: https://travis-ci.org/sttk/each-props
11+
[appveyor-img]: https://ci.appveyor.com/api/projects/status/github/sttk/each-props?branch=master&svg=true
12+
[appveyor-url]: https://ci.appveyor.com/project/sttk/each-props
13+
14+
Install
15+
-------
16+
17+
```
18+
$ npm i each-props --save
19+
```
20+
21+
Usage
22+
-----
23+
24+
* Load this module :
25+
26+
```js
27+
const eachProps = require('each-props');
28+
```
29+
30+
* Apply a function to all (= non plain object) properties.
31+
32+
```js
33+
var obj = { a: 1, b: { c: 'CCC', d: { e: 'EEE' } } };
34+
35+
eachProps(obj, function(value, keychain, nodeinfo) {
36+
if (keychain === 'a') {
37+
nodeinfo.parent['a'] = value * 2;
38+
} else if (keychain === 'b.c') {
39+
nodeinfo.parent['c'] = value.toLowerCase();
40+
} else if (keychain === 'b.d') {
41+
return true; // stop to dig
42+
} else i f(keychain === 'b.d.e') {
43+
nodeinfo.parent['e'] = value.toLowerCase();
44+
}
45+
});
46+
console.log(obj);
47+
// => { a: 2, b: { c: 'ccc', d: { e: 'EEE' } } };
48+
```
49+
50+
API
51+
---
52+
53+
### eachProps(obj, callback [, opts]) => void
54+
55+
Executes the *callback* function for all properties.
56+
57+
##### **Arguments :**
58+
59+
* **obj** [object] : A plain object to be treated.
60+
* **callback** [function] : A function to treat the plain object.
61+
* **opts** [object] : An object to be able to has options.
62+
63+
##### **API of *callback* function**
64+
65+
* ***callback(value, key, info) => object***
66+
67+
* **Arguments :**
68+
* **value** [any] : The property value.
69+
* **keychain** [string] : A string concatenated the hierarchical keys with dots.
70+
* **nodeinfo** [object] : An object which contains properties: `index`, `count`, `depth`, `parent`, `sort`, and can contains more properties by specifying in **eachProps**'s *opts*.
71+
72+
* **Returns :**
73+
* [boolean] : Stops digging child properties if `true`.
74+
75+
##### **Properties of *nodeinfo* :**
76+
77+
* ***nodeinfo*** *[object]*
78+
* **index** [number] : The index of the property among the sibling properties.
79+
* **count** [number] : The count of the sibling properties.
80+
* **depth** [number] : The depth in the property hierarchy.
81+
* **parent** [object] : The parent property.
82+
* **sort** [function] : A sort function which orders the child properties. (Not sort If null.)
83+
84+
##### **Properties of *opts* :**
85+
86+
* ***opts*** *[object]*
87+
* **sort** [function] : A sort function which orders the same level properties. (Not sort if null.)
88+
* ... and any properties you want to pass to each node.
89+
90+
License
91+
-------
92+
93+
MIT

appveyor.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# http://www.appveyor.com/docs/appveyor-yml
2+
# http://www.appveyor.com/docs/lang/nodejs-iojs
3+
4+
environment:
5+
matrix:
6+
# node.js
7+
- nodejs_version: "6"
8+
- nodejs_version: "5"
9+
- nodejs_version: "4"
10+
- nodejs_version: "0.12"
11+
- nodejs_version: "0.10"
12+
13+
install:
14+
- ps: Install-Product node $env:nodejs_version
15+
- npm install
16+
17+
test_script:
18+
- node --version
19+
- npm --version
20+
- cmd: npm test
21+
22+
build: off
23+
24+
version: "{build}"

index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
var inspect = require('util').inspect;
4+
var isPlainObject = require('lodash.isplainobject');
5+
6+
module.exports = function(obj, callback, opts) {
7+
if (!isPlainObject(obj)) {
8+
throw new TypeError('1st argument needs to be a plain object: ' +
9+
inspect(obj));
10+
}
11+
12+
if (typeof callback !== 'function') {
13+
throw new TypeError('2nd argument needs to be a function: ' +
14+
inspect(callback));
15+
}
16+
17+
if (opts == null) {
18+
opts = {};
19+
} else if (!isPlainObject(opts)) {
20+
throw new TypeError('3rd argument needs to be an object: ' +
21+
inspect(opts));
22+
}
23+
24+
if (opts.sort != null && typeof opts.sort !== 'function') {
25+
throw new TypeError('The `sort` property in 3rd argument needs to be a ' +
26+
'function: ' + inspect(opts));
27+
}
28+
29+
opts.depth = 0;
30+
31+
foreachNode(obj, '', callback, opts);
32+
};
33+
34+
function foreachNode(node, basekey, callback, info) {
35+
var keys = Object.keys(node);
36+
if (typeof info.sort === 'function') {
37+
keys = info.sort(keys);
38+
}
39+
40+
for (var i = 0, n = keys.length; i < n; i++) {
41+
var key = keys[i];
42+
var keychain = basekey + '.' + key;
43+
var value = node[key];
44+
45+
var childinfo = createChildInfo(info, node, i, n);
46+
47+
if (callback(value, keychain.slice(1), childinfo)) {
48+
continue;
49+
}
50+
51+
if (isPlainObject(value)) {
52+
foreachNode(value, keychain, callback, childinfo);
53+
}
54+
}
55+
}
56+
57+
function createChildInfo(info, node, i, n) {
58+
var childinfo = {};
59+
60+
var keys = Object.keys(info);
61+
for (var j = 0; j < keys.length; j++) {
62+
var key = keys[j];
63+
childinfo[key] = info[key];
64+
}
65+
66+
childinfo.index = i;
67+
childinfo.count = n;
68+
childinfo.depth = info.depth + 1;
69+
childinfo.parent = node;
70+
71+
return childinfo;
72+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "each-props",
3+
"version": "0.1.0",
4+
"description": "Process each properties deeply.",
5+
"main": "index.js",
6+
"scripts": {
7+
"lint": "eslint .",
8+
"test": "mocha",
9+
"coverage": "istanbul cover _mocha"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/sttk/each-props.git"
14+
},
15+
"keywords": [
16+
"object",
17+
"property",
18+
"deep"
19+
],
20+
"author": "Takayuki Sato",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/sttk/each-props/issues"
24+
},
25+
"homepage": "https://github.com/sttk/each-props#readme",
26+
"dependencies": {
27+
"lodash.isplainobject": "^4.0.6"
28+
},
29+
"devDependencies": {
30+
"chai": "^3.5.0",
31+
"eslint": "^3.6.1",
32+
"istanbul": "^0.4.5",
33+
"mocha": "^3.1.0",
34+
"testrun": "^0.7.0"
35+
}
36+
}

0 commit comments

Comments
 (0)