프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / mpath / README.md

이력 | 보기 | 이력해설 | 다운로드 (6.78 KB)

1
#mpath
2

    
3
{G,S}et javascript object values using MongoDB-like path notation.
4

    
5
###Getting
6

    
7
```js
8
var mpath = require('mpath');
9

    
10
var obj = {
11
    comments: [
12
      { title: 'funny' },
13
      { title: 'exciting!' }
14
    ]
15
}
16

    
17
mpath.get('comments.1.title', obj) // 'exciting!'
18
```
19

    
20
`mpath.get` supports array property notation as well.
21

    
22
```js
23
var obj = {
24
    comments: [
25
      { title: 'funny' },
26
      { title: 'exciting!' }
27
    ]
28
}
29

    
30
mpath.get('comments.title', obj) // ['funny', 'exciting!']
31
```
32

    
33
Array property and indexing syntax, when used together, are very powerful.
34

    
35
```js
36
var obj = {
37
  array: [
38
      { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
39
    , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }}
40
    , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
41
    , { o: { array: [{x: null }] }}
42
    , { o: { array: [{y: 3 }] }}
43
    , { o: { array: [3, 0, null] }}
44
    , { o: { name: 'ha' }}
45
  ];
46
}
47

    
48
var found = mpath.get('array.o.array.x.b.1', obj);
49

    
50
console.log(found); // prints..
51

    
52
    [ [6, undefined]
53
    , [2, undefined, undefined]
54
    , [null, 1]
55
    , [null]
56
    , [undefined]
57
    , [undefined, undefined, undefined]
58
    , undefined
59
    ]
60

    
61
```
62

    
63
#####Field selection rules:
64

    
65
The following rules are iteratively applied to each `segment` in the passed `path`. For example:
66

    
67
```js
68
var path = 'one.two.14'; // path
69
'one' // segment 0
70
'two' // segment 1
71
14    // segment 2
72
```
73

    
74
- 1) when value of the segment parent is not an array, return the value of `parent.segment`
75
- 2) when value of the segment parent is an array
76
  - a) if the segment is an integer, replace the parent array with the value at `parent[segment]`
77
  - b) if not an integer, keep the array but replace each array `item` with the value returned from calling `get(remainingSegments, item)` or undefined if falsey.
78

    
79
#####Maps
80

    
81
`mpath.get` also accepts an optional `map` argument which receives each individual found value. The value returned from the `map` function will be used in the original found values place.
82

    
83
```js
84
var obj = {
85
    comments: [
86
      { title: 'funny' },
87
      { title: 'exciting!' }
88
    ]
89
}
90

    
91
mpath.get('comments.title', obj, function (val) {
92
  return 'funny' == val
93
    ? 'amusing'
94
    : val;
95
});
96
// ['amusing', 'exciting!']
97
```
98

    
99
###Setting
100

    
101
```js
102
var obj = {
103
    comments: [
104
      { title: 'funny' },
105
      { title: 'exciting!' }
106
    ]
107
}
108

    
109
mpath.set('comments.1.title', 'hilarious', obj)
110
console.log(obj.comments[1].title) // 'hilarious'
111
```
112

    
113
`mpath.set` supports the same array property notation as `mpath.get`.
114

    
115
```js
116
var obj = {
117
    comments: [
118
      { title: 'funny' },
119
      { title: 'exciting!' }
120
    ]
121
}
122

    
123
mpath.set('comments.title', ['hilarious', 'fruity'], obj);
124

    
125
console.log(obj); // prints..
126

    
127
  { comments: [
128
      { title: 'hilarious' },
129
      { title: 'fruity' }
130
  ]}
131
```
132

    
133
Array property and indexing syntax can be used together also when setting.
134

    
135
```js
136
var obj = {
137
  array: [
138
      { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
139
    , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }}
140
    , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
141
    , { o: { array: [{x: null }] }}
142
    , { o: { array: [{y: 3 }] }}
143
    , { o: { array: [3, 0, null] }}
144
    , { o: { name: 'ha' }}
145
  ]
146
}
147

    
148
mpath.set('array.1.o', 'this was changed', obj);
149

    
150
console.log(require('util').inspect(obj, false, 1000)); // prints..
151

    
152
{
153
  array: [
154
      { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
155
    , { o: 'this was changed' }
156
    , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
157
    , { o: { array: [{x: null }] }}
158
    , { o: { array: [{y: 3 }] }}
159
    , { o: { array: [3, 0, null] }}
160
    , { o: { name: 'ha' }}
161
  ];
162
}
163

    
164
mpath.set('array.o.array.x', 'this was changed too', obj);
165

    
166
console.log(require('util').inspect(obj, false, 1000)); // prints..
167

    
168
{
169
  array: [
170
      { o: { array: [{x: 'this was changed too'}, { y: 10, x: 'this was changed too'} ] }}
171
    , { o: 'this was changed' }
172
    , { o: { array: [{x: 'this was changed too'}, { x: 'this was changed too'}] }}
173
    , { o: { array: [{x: 'this was changed too'}] }}
174
    , { o: { array: [{x: 'this was changed too', y: 3 }] }}
175
    , { o: { array: [3, 0, null] }}
176
    , { o: { name: 'ha' }}
177
  ];
178
}
179
```
180

    
181
####Setting arrays
182

    
183
By default, setting a property within an array to another array results in each element of the new array being set to the item in the destination array at the matching index. An example is helpful.
184

    
185
```js
186
var obj = {
187
    comments: [
188
      { title: 'funny' },
189
      { title: 'exciting!' }
190
    ]
191
}
192

    
193
mpath.set('comments.title', ['hilarious', 'fruity'], obj);
194

    
195
console.log(obj); // prints..
196

    
197
  { comments: [
198
      { title: 'hilarious' },
199
      { title: 'fruity' }
200
  ]}
201
```
202

    
203
If we do not desire this destructuring-like assignment behavior we may instead specify the `$` operator in the path being set to force the array to be copied directly.
204

    
205
```js
206
var obj = {
207
    comments: [
208
      { title: 'funny' },
209
      { title: 'exciting!' }
210
    ]
211
}
212

    
213
mpath.set('comments.$.title', ['hilarious', 'fruity'], obj);
214

    
215
console.log(obj); // prints..
216

    
217
  { comments: [
218
      { title: ['hilarious', 'fruity'] },
219
      { title: ['hilarious', 'fruity'] }
220
  ]}
221
```
222

    
223
####Field assignment rules
224

    
225
The rules utilized mirror those used on `mpath.get`, meaning we can take values returned from `mpath.get`, update them, and reassign them using `mpath.set`. Note that setting nested arrays of arrays can get unweildy quickly. Check out the [tests](https://github.com/aheckmann/mpath/blob/master/test/index.js) for more extreme examples.
226

    
227
#####Maps
228

    
229
`mpath.set` also accepts an optional `map` argument which receives each individual value being set. The value returned from the `map` function will be used in the original values place.
230

    
231
```js
232
var obj = {
233
    comments: [
234
      { title: 'funny' },
235
      { title: 'exciting!' }
236
    ]
237
}
238

    
239
mpath.set('comments.title', ['hilarious', 'fruity'], obj, function (val) {
240
  return val.length;
241
});
242

    
243
console.log(obj); // prints..
244

    
245
  { comments: [
246
      { title: 9 },
247
      { title: 6 }
248
  ]}
249
```
250

    
251
### Custom object types
252

    
253
Sometimes you may want to enact the same functionality on custom object types that store all their real data internally, say for an ODM type object. No fear, `mpath` has you covered. Simply pass the name of the property being used to store the internal data and it will be traversed instead:
254

    
255
```js
256
var mpath = require('mpath');
257

    
258
var obj = {
259
    comments: [
260
      { title: 'exciting!', _doc: { title: 'great!' }}
261
    ]
262
}
263

    
264
mpath.get('comments.0.title', obj, '_doc')            // 'great!'
265
mpath.set('comments.0.title', 'nov 3rd', obj, '_doc')
266
mpath.get('comments.0.title', obj, '_doc')            // 'nov 3rd'
267
mpath.get('comments.0.title', obj)                    // 'exciting'
268
```
269

    
270
When used with a `map`, the `map` argument comes last.
271

    
272
```js
273
mpath.get(path, obj, '_doc', map);
274
mpath.set(path, val, obj, '_doc', map);
275
```
276

    
277
[LICENSE](https://github.com/aheckmann/mpath/blob/master/LICENSE)
278