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 |
|
---|
3 | > Map `visit` over an array of objects.
|
---|
4 |
|
---|
5 | ## Install
|
---|
6 |
|
---|
7 | Install with [npm](https://www.npmjs.com/):
|
---|
8 |
|
---|
9 | ```sh
|
---|
10 | $ npm install --save map-visit
|
---|
11 | ```
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | var mapVisit = require('map-visit');
|
---|
17 | ```
|
---|
18 |
|
---|
19 | ## What does this do?
|
---|
20 |
|
---|
21 | **Assign/Merge/Extend vs. Visit**
|
---|
22 |
|
---|
23 | Let's say you want to add a `set` method to your application that will:
|
---|
24 |
|
---|
25 | * set key-value pairs on a `data` object
|
---|
26 | * extend objects onto the `data` object
|
---|
27 | * extend arrays of objects onto the data object
|
---|
28 |
|
---|
29 | **Example using `extend`**
|
---|
30 |
|
---|
31 | Here is one way to accomplish this using Lo-Dash's `extend` (comparable to `Object.assign`):
|
---|
32 |
|
---|
33 | ```js
|
---|
34 | var _ = require('lodash');
|
---|
35 |
|
---|
36 | var obj = {
|
---|
37 | data: {},
|
---|
38 | set: function (key, value) {
|
---|
39 | if (Array.isArray(key)) {
|
---|
40 | _.extend.apply(_, [obj.data].concat(key));
|
---|
41 | } else if (typeof key === 'object') {
|
---|
42 | _.extend(obj.data, key);
|
---|
43 | } else {
|
---|
44 | obj.data[key] = value;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | obj.set('a', 'a');
|
---|
50 | obj.set([{b: 'b'}, {c: 'c'}]);
|
---|
51 | obj.set({d: {e: 'f'}});
|
---|
52 |
|
---|
53 | console.log(obj.data);
|
---|
54 | //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}
|
---|
55 | ```
|
---|
56 |
|
---|
57 | 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`.
|
---|
58 |
|
---|
59 | **Example using `visit`**
|
---|
60 |
|
---|
61 | In this approach:
|
---|
62 |
|
---|
63 | * when an array is passed to `set`, the `mapVisit` library calls the `set` method on each object in the array.
|
---|
64 | * when an object is passed, `visit` calls `set` on each property in the object.
|
---|
65 |
|
---|
66 | 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).
|
---|
67 |
|
---|
68 | ```js
|
---|
69 | var mapVisit = require('map-visit');
|
---|
70 | var visit = require('object-visit');
|
---|
71 |
|
---|
72 | var obj = {
|
---|
73 | data: {},
|
---|
74 | set: function (key, value) {
|
---|
75 | if (Array.isArray(key)) {
|
---|
76 | mapVisit(obj, 'set', key);
|
---|
77 | } else if (typeof key === 'object') {
|
---|
78 | visit(obj, 'set', key);
|
---|
79 | } else {
|
---|
80 | // simulate an event-emitter
|
---|
81 | console.log('emit', key, value);
|
---|
82 | obj.data[key] = value;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | };
|
---|
86 |
|
---|
87 | obj.set('a', 'a');
|
---|
88 | obj.set([{b: 'b'}, {c: 'c'}]);
|
---|
89 | obj.set({d: {e: 'f'}});
|
---|
90 | obj.set({g: 'h', i: 'j', k: 'l'});
|
---|
91 |
|
---|
92 | console.log(obj.data);
|
---|
93 | //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }, g: 'h', i: 'j', k: 'l'}
|
---|
94 |
|
---|
95 | // events would look something like:
|
---|
96 | // emit a a
|
---|
97 | // emit b b
|
---|
98 | // emit c c
|
---|
99 | // emit d { e: 'f' }
|
---|
100 | // emit g h
|
---|
101 | // emit i j
|
---|
102 | // emit k l
|
---|
103 | ```
|
---|
104 |
|
---|
105 | ## About
|
---|
106 |
|
---|
107 | ### Related projects
|
---|
108 |
|
---|
109 | * [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.")
|
---|
110 | * [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.")
|
---|
111 |
|
---|
112 | ### Contributing
|
---|
113 |
|
---|
114 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
---|
115 |
|
---|
116 | ### Contributors
|
---|
117 |
|
---|
118 | | **Commits** | **Contributor** |
|
---|
119 | | --- | --- |
|
---|
120 | | 15 | [jonschlinkert](https://github.com/jonschlinkert) |
|
---|
121 | | 7 | [doowb](https://github.com/doowb) |
|
---|
122 |
|
---|
123 | ### Building docs
|
---|
124 |
|
---|
125 | _(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.)_
|
---|
126 |
|
---|
127 | To generate the readme, run the following command:
|
---|
128 |
|
---|
129 | ```sh
|
---|
130 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
---|
131 | ```
|
---|
132 |
|
---|
133 | ### Running tests
|
---|
134 |
|
---|
135 | 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:
|
---|
136 |
|
---|
137 | ```sh
|
---|
138 | $ npm install && npm test
|
---|
139 | ```
|
---|
140 |
|
---|
141 | ### Author
|
---|
142 |
|
---|
143 | **Jon Schlinkert**
|
---|
144 |
|
---|
145 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
---|
146 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
---|
147 |
|
---|
148 | ### License
|
---|
149 |
|
---|
150 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
---|
151 | Released under the [MIT License](LICENSE).
|
---|
152 |
|
---|
153 | ***
|
---|
154 |
|
---|
155 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 09, 2017._ |
---|