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.

154 lines
4.8 KiB

4 years ago
  1. # map-visit [![NPM version](https://img.shields.io/npm/v/map-visit.svg?style=flat)](https://www.npmjs.com/package/map-visit) [![NPM monthly downloads](https://img.shields.io/npm/dm/map-visit.svg?style=flat)](https://npmjs.org/package/map-visit) [![NPM total downloads](https://img.shields.io/npm/dt/map-visit.svg?style=flat)](https://npmjs.org/package/map-visit) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/map-visit.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/map-visit)
  2. > Map `visit` over an array of objects.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save map-visit
  7. ```
  8. ## Usage
  9. ```js
  10. var mapVisit = require('map-visit');
  11. ```
  12. ## What does this do?
  13. **Assign/Merge/Extend vs. Visit**
  14. Let's say you want to add a `set` method to your application that will:
  15. * set key-value pairs on a `data` object
  16. * extend objects onto the `data` object
  17. * extend arrays of objects onto the data object
  18. **Example using `extend`**
  19. Here is one way to accomplish this using Lo-Dash's `extend` (comparable to `Object.assign`):
  20. ```js
  21. var _ = require('lodash');
  22. var obj = {
  23. data: {},
  24. set: function (key, value) {
  25. if (Array.isArray(key)) {
  26. _.extend.apply(_, [obj.data].concat(key));
  27. } else if (typeof key === 'object') {
  28. _.extend(obj.data, key);
  29. } else {
  30. obj.data[key] = value;
  31. }
  32. }
  33. };
  34. obj.set('a', 'a');
  35. obj.set([{b: 'b'}, {c: 'c'}]);
  36. obj.set({d: {e: 'f'}});
  37. console.log(obj.data);
  38. //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}
  39. ```
  40. The above approach works fine for most use cases. However, **if you also want to emit an event** each time a property is added to the `data` object, or you want more control over what happens as the object is extended, a better approach would be to use `visit`.
  41. **Example using `visit`**
  42. In this approach:
  43. * when an array is passed to `set`, the `mapVisit` library calls the `set` method on each object in the array.
  44. * when an object is passed, `visit` calls `set` on each property in the object.
  45. As a result, the `data` event will be emitted every time a property is added to `data` (events are just an example, you can use this approach to perform any necessary logic every time the method is called).
  46. ```js
  47. var mapVisit = require('map-visit');
  48. var visit = require('object-visit');
  49. var obj = {
  50. data: {},
  51. set: function (key, value) {
  52. if (Array.isArray(key)) {
  53. mapVisit(obj, 'set', key);
  54. } else if (typeof key === 'object') {
  55. visit(obj, 'set', key);
  56. } else {
  57. // simulate an event-emitter
  58. console.log('emit', key, value);
  59. obj.data[key] = value;
  60. }
  61. }
  62. };
  63. obj.set('a', 'a');
  64. obj.set([{b: 'b'}, {c: 'c'}]);
  65. obj.set({d: {e: 'f'}});
  66. obj.set({g: 'h', i: 'j', k: 'l'});
  67. console.log(obj.data);
  68. //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }, g: 'h', i: 'j', k: 'l'}
  69. // events would look something like:
  70. // emit a a
  71. // emit b b
  72. // emit c c
  73. // emit d { e: 'f' }
  74. // emit g h
  75. // emit i j
  76. // emit k l
  77. ```
  78. ## About
  79. ### Related projects
  80. * [collection-visit](https://www.npmjs.com/package/collection-visit): Visit a method over the items in an object, or map visit over the objects… [more](https://github.com/jonschlinkert/collection-visit) | [homepage](https://github.com/jonschlinkert/collection-visit "Visit a method over the items in an object, or map visit over the objects in an array.")
  81. * [object-visit](https://www.npmjs.com/package/object-visit): Call a specified method on each value in the given object. | [homepage](https://github.com/jonschlinkert/object-visit "Call a specified method on each value in the given object.")
  82. ### Contributing
  83. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  84. ### Contributors
  85. | **Commits** | **Contributor** |
  86. | --- | --- |
  87. | 15 | [jonschlinkert](https://github.com/jonschlinkert) |
  88. | 7 | [doowb](https://github.com/doowb) |
  89. ### Building docs
  90. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  91. To generate the readme, run the following command:
  92. ```sh
  93. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  94. ```
  95. ### Running tests
  96. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  97. ```sh
  98. $ npm install && npm test
  99. ```
  100. ### Author
  101. **Jon Schlinkert**
  102. * [github/jonschlinkert](https://github.com/jonschlinkert)
  103. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  104. ### License
  105. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  106. Released under the [MIT License](LICENSE).
  107. ***
  108. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 09, 2017._