package/package.json 000644 0000002664 13275662340 011605 0 ustar 00 000000 000000 {
"name": "each-props",
"version": "1.3.2",
"description": "Processes each properties of an object deeply.",
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"lint": "eslint .",
"test": "mocha",
"coverage": "nyc --reporter=lcov --reporter=text-summary npm test",
"coveralls": "nyc --reporter=text-lcov npm test | coveralls",
"web:build": "browserify index.js --standalone eachProps -o web/each-props.js && cd web && uglifyjs each-props.js --compress --mangle -o each-props.min.js --source-map url=each-props.min.js.map",
"chrome:install": "npm i --no-save mocha-chrome",
"chrome:test": "mocha-chrome test/web/browser-test.html",
"build": "npm run lint && npm run coverage && npm run web:build && node test/web/make.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sttk/each-props.git"
},
"keywords": [
"deep",
"each",
"object",
"property",
"properties",
"props"
],
"author": "Takayuki Sato",
"license": "MIT",
"bugs": {
"url": "https://github.com/sttk/each-props/issues"
},
"homepage": "https://github.com/sttk/each-props#readme",
"dependencies": {
"is-plain-object": "^2.0.1",
"object.defaults": "^1.1.0"
},
"devDependencies": {
"browserify": "^16.2.2",
"chai": "^3.5.0",
"coveralls": "^3.0.1",
"eslint": "^4.19.1",
"mocha": "^3.2.0",
"nyc": "^11.7.2",
"uglify-js": "^3.3.24"
}
}
package/index.js 000644 0000002234 13275662340 010755 0 ustar 00 000000 000000 'use strict';
var isPlainObject = require('is-plain-object');
var defaults = require('object.defaults/immutable');
module.exports = function(obj, fn, opts) {
if (!isObject(obj)) {
return;
}
if (typeof fn !== 'function') {
return;
}
if (!isPlainObject(opts)) {
opts = {};
}
forEachChild(obj, '', fn, 0, opts);
};
function forEachChild(node, baseKey, fn, depth, opts) {
var keys = Object.keys(node);
if (typeof opts.sort === 'function') {
var sortedKeys = opts.sort(keys);
if (Array.isArray(sortedKeys)) {
keys = sortedKeys;
}
}
depth += 1;
for (var i = 0, n = keys.length; i < n; i++) {
var key = keys[i];
var keyChain = baseKey + '.' + key;
var value = node[key];
var nodeInfo = defaults(opts);
nodeInfo.name = key;
nodeInfo.index = i;
nodeInfo.count = n;
nodeInfo.depth = depth;
nodeInfo.parent = node;
var notDigg = fn(value, keyChain.slice(1), nodeInfo);
if (notDigg || !isPlainObject(value)) {
continue;
}
forEachChild(value, keyChain, fn, depth, opts);
}
}
function isObject(v) {
return Object.prototype.toString.call(v) === '[object Object]';
}
package/LICENSE 000644 0000002056 13275662340 010317 0 ustar 00 000000 000000 MIT License
Copyright (c) 2016 Takayuki Sato
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.
package/README.md 000644 0000010237 13275662340 010571 0 ustar 00 000000 000000 # [each-props][repo-url] [![NPM][npm-img]][npm-url] [![MIT License][mit-img]][mit-url] [![Build Status][travis-img]][travis-url] [![Build Status][appveyor-img]][appveyor-url] [![Coverage Status][coverage-img]][coverage-url]
Processes each properties of an object deeply.
## Install
To install from npm:
```sh
$ npm i each-props --save
```
## Load this module
For Node.js:
```js
const eachProps = require('each-props');
```
For Web browser:
```html
```
## Usage
Apply a function to all (non plain object) properties.
```js
var obj = { a: 1, b: { c: 'CCC', d: { e: 'EEE' } } };
eachProps(obj, function(value, keyChain, nodeInfo) {
if (keyChain === 'a') {
nodeInfo.parent['a'] = value * 2;
} else if (keyChain === 'b.c') {
nodeInfo.parent['c'] = value.toLowerCase();
} else if (keyChain === 'b.d') {
return true; // stop to dig
} else if (keyChain === 'b.d.e') {
nodeInfo.parent['e'] = value.toLowerCase();
}
});
console.log(obj);
// => { a: 2, b: { c: 'ccc', d: { e: 'EEE' } } };
```
## API
### eachProps(obj, fn [, opts]) : void
Executes the *fn* function for all properties.
#### Parameters:
| Parameter | Type | Description |
|:------------|:------:|:-----------------------------------------------|
| *obj* | object | A plain object to be treated. |
| *fn* |function| A function to operate each properties. |
| *opts* | object | An object to pass any data to each properties. |
* **API of *fn* function**
#### fn(value, keyChain, nodeInfo) : boolean
This function is applied to all properties in an object.
##### Parameters:
| Parameter | Type | Description |
|:------------|:------:|:-----------------------------------------------|
| *value* | any | A property value. |
| *keyChain* | string | A string concatenating the hierarchical keys with dots. |
| *nodeInfo* | object | An object which contains node informations (See [below](#nodeinfo)). |
##### Returns:
True, if stops digging child properties.
**Type:** boolean
* **Properties of nodeInfo**
| Properties | Type | Description |
|:-------------|:------:|:-----------------------------------------|
| *name* | string | The property name of this node. |
| *index* | number | The index of the property among the sibling properties. |
| *count* | number | The count of the sibling properties. |
| *depth* | number | The depth of the property. |
| *parent* | object | The parent node of the property. |
| *sort* |function| A sort function which orders the child properties. This function is inherited from *opts*, if be specified. |
... and any properties inherited from *opts*.
* **Properties of opts**
| Properties | Type | Description |
|:-------------|:------:|:-----------------------------------------|
| *sort* |function| A sort function which orders the same level properties. (Optional) |
... and any properties you want to pass to each node.
## License
Copyright (C) 2016-2018 Takayuki Sato
This program is free software under [MIT][mit-url] License.
See the file LICENSE in this distribution for more details.
[repo-url]: https://github.com/sttk/each-props/
[npm-img]: https://img.shields.io/badge/npm-v1.3.2-blue.svg
[npm-url]: https://www.npmjs.org/package/each-props/
[mit-img]: https://img.shields.io/badge/license-MIT-green.svg
[mit-url]: https://opensource.org/licenses.MIT
[travis-img]: https://travis-ci.org/sttk/each-props.svg?branch=master
[travis-url]: https://travis-ci.org/sttk/each-props
[appveyor-img]: https://ci.appveyor.com/api/projects/status/github/sttk/each-props?branch=master&svg=true
[appveyor-url]: https://ci.appveyor.com/project/sttk/each-props
[coverage-img]: https://coveralls.io/repos/github/sttk/each-props/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/sttk/each-props?branch=master