You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # [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]
  2. Processes each properties of an object deeply.
  3. ## Install
  4. To install from npm:
  5. ```sh
  6. $ npm i each-props --save
  7. ```
  8. ## Load this module
  9. For Node.js:
  10. ```js
  11. const eachProps = require('each-props');
  12. ```
  13. For Web browser:
  14. ```html
  15. <script src="each-props.min.js"></script>
  16. ```
  17. ## Usage
  18. Apply a function to all (non plain object) properties.
  19. ```js
  20. var obj = { a: 1, b: { c: 'CCC', d: { e: 'EEE' } } };
  21. eachProps(obj, function(value, keyChain, nodeInfo) {
  22. if (keyChain === 'a') {
  23. nodeInfo.parent['a'] = value * 2;
  24. } else if (keyChain === 'b.c') {
  25. nodeInfo.parent['c'] = value.toLowerCase();
  26. } else if (keyChain === 'b.d') {
  27. return true; // stop to dig
  28. } else if (keyChain === 'b.d.e') {
  29. nodeInfo.parent['e'] = value.toLowerCase();
  30. }
  31. });
  32. console.log(obj);
  33. // => { a: 2, b: { c: 'ccc', d: { e: 'EEE' } } };
  34. ```
  35. ## API
  36. ### <u>eachProps(obj, fn [, opts]) : void</u>
  37. Executes the *fn* function for all properties.
  38. #### Parameters:
  39. | Parameter | Type | Description |
  40. |:------------|:------:|:-----------------------------------------------|
  41. | *obj* | object | A plain object to be treated. |
  42. | *fn* |function| A function to operate each properties. |
  43. | *opts* | object | An object to pass any data to each properties. |
  44. * **API of *fn* function**
  45. #### <u>fn(value, keyChain, nodeInfo) : boolean</u>
  46. This function is applied to all properties in an object.
  47. ##### Parameters:
  48. | Parameter | Type | Description |
  49. |:------------|:------:|:-----------------------------------------------|
  50. | *value* | any | A property value. |
  51. | *keyChain* | string | A string concatenating the hierarchical keys with dots. |
  52. | *nodeInfo* | object | An object which contains node informations (See [below](#nodeinfo)). |
  53. ##### Returns:
  54. True, if stops digging child properties.
  55. **Type:** boolean
  56. <a name="nodeinfo"></a>
  57. * **Properties of <i>nodeInfo</i>**
  58. | Properties | Type | Description |
  59. |:-------------|:------:|:-----------------------------------------|
  60. | *name* | string | The property name of this node. |
  61. | *index* | number | The index of the property among the sibling properties. |
  62. | *count* | number | The count of the sibling properties. |
  63. | *depth* | number | The depth of the property. |
  64. | *parent* | object | The parent node of the property. |
  65. | *sort* |function| A sort function which orders the child properties. This function is inherited from *opts*, if be specified. |
  66. ... and any properties inherited from *opts*.
  67. * **Properties of <i>opts</i>**
  68. | Properties | Type | Description |
  69. |:-------------|:------:|:-----------------------------------------|
  70. | *sort* |function| A sort function which orders the same level properties. (Optional) |
  71. ... and any properties you want to pass to each node.
  72. ## License
  73. Copyright (C) 2016-2018 Takayuki Sato
  74. This program is free software under [MIT][mit-url] License.
  75. See the file LICENSE in this distribution for more details.
  76. [repo-url]: https://github.com/sttk/each-props/
  77. [npm-img]: https://img.shields.io/badge/npm-v1.3.2-blue.svg
  78. [npm-url]: https://www.npmjs.org/package/each-props/
  79. [mit-img]: https://img.shields.io/badge/license-MIT-green.svg
  80. [mit-url]: https://opensource.org/licenses.MIT
  81. [travis-img]: https://travis-ci.org/sttk/each-props.svg?branch=master
  82. [travis-url]: https://travis-ci.org/sttk/each-props
  83. [appveyor-img]: https://ci.appveyor.com/api/projects/status/github/sttk/each-props?branch=master&svg=true
  84. [appveyor-url]: https://ci.appveyor.com/project/sttk/each-props
  85. [coverage-img]: https://coveralls.io/repos/github/sttk/each-props/badge.svg?branch=master
  86. [coverage-url]: https://coveralls.io/github/sttk/each-props?branch=master