|
| 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 |
0 commit comments