PageRenderTime 58ms CodeModel.GetById 17ms app.highlight 36ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/mpath/lib/index.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 280 lines | 177 code | 41 blank | 62 comment | 82 complexity | 03894b38579834b76f634c7bce16812a MD5 | raw file
  1/**
  2 * Returns the value of object `o` at the given `path`.
  3 *
  4 * ####Example:
  5 *
  6 *     var obj = {
  7 *         comments: [
  8 *             { title: 'exciting!', _doc: { title: 'great!' }}
  9 *           , { title: 'number dos' }
 10 *         ]
 11 *     }
 12 *
 13 *     mpath.get('comments.0.title', o)         // 'exciting!'
 14 *     mpath.get('comments.0.title', o, '_doc') // 'great!'
 15 *     mpath.get('comments.title', o)           // ['exciting!', 'number dos']
 16 *
 17 *     // summary
 18 *     mpath.get(path, o)
 19 *     mpath.get(path, o, special)
 20 *     mpath.get(path, o, map)
 21 *     mpath.get(path, o, special, map)
 22 *
 23 * @param {String} path
 24 * @param {Object} o
 25 * @param {String} [special] When this property name is present on any object in the path, walking will continue on the value of this property.
 26 * @param {Function} [map] Optional function which receives each individual found value. The value returned from `map` is used in the original values place.
 27 */
 28
 29exports.get = function (path, o, special, map) {
 30  var lookup;
 31
 32  if ('function' == typeof special) {
 33    if (special.length < 2) {
 34      map = special;
 35      special = undefined;
 36    } else {
 37      lookup = special;
 38      special = undefined;
 39    }
 40  }
 41
 42  map || (map = K);
 43
 44  var parts = 'string' == typeof path
 45    ? path.split('.')
 46    : path
 47
 48  if (!Array.isArray(parts)) {
 49    throw new TypeError('Invalid `path`. Must be either string or array');
 50  }
 51
 52  var obj = o
 53    , part;
 54
 55  for (var i = 0; i < parts.length; ++i) {
 56    part = parts[i];
 57
 58    if (Array.isArray(obj) && !/^\d+$/.test(part)) {
 59      // reading a property from the array items
 60      var paths = parts.slice(i);
 61
 62      return obj.map(function (item) {
 63        return item
 64          ? exports.get(paths, item, special || lookup, map)
 65          : map(undefined);
 66      });
 67    }
 68
 69    if (lookup) {
 70      obj = lookup(obj, part);
 71    } else {
 72      var _from = special && obj[special] ? obj[special] : obj;
 73      obj = _from instanceof Map ?
 74        _from.get(part) :
 75        _from[part];
 76    }
 77
 78    if (!obj) return map(obj);
 79  }
 80
 81  return map(obj);
 82};
 83
 84/**
 85 * Returns true if `in` returns true for every piece of the path
 86 *
 87 * @param {String} path
 88 * @param {Object} o
 89 */
 90
 91exports.has = function (path, o) {
 92  var parts = typeof path === 'string' ?
 93    path.split('.') :
 94    path;
 95
 96  if (!Array.isArray(parts)) {
 97    throw new TypeError('Invalid `path`. Must be either string or array');
 98  }
 99
100  var len = parts.length;
101  var cur = o;
102  for (var i = 0; i < len; ++i) {
103    if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
104      return false;
105    }
106    cur = cur[parts[i]];
107  }
108
109  return true;
110};
111
112/**
113 * Deletes the last piece of `path`
114 *
115 * @param {String} path
116 * @param {Object} o
117 */
118
119exports.unset = function (path, o) {
120  var parts = typeof path === 'string' ?
121    path.split('.') :
122    path;
123
124  if (!Array.isArray(parts)) {
125    throw new TypeError('Invalid `path`. Must be either string or array');
126  }
127
128  var len = parts.length;
129  var cur = o;
130  for (var i = 0; i < len; ++i) {
131    if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
132      return false;
133    }
134    if (i === len - 1) {
135      delete cur[parts[i]];
136      return true;
137    }
138    cur = cur instanceof Map ? cur.get(parts[i]) : cur[parts[i]];
139  }
140
141  return true;
142};
143
144/**
145 * Sets the `val` at the given `path` of object `o`.
146 *
147 * @param {String} path
148 * @param {Anything} val
149 * @param {Object} o
150 * @param {String} [special] When this property name is present on any object in the path, walking will continue on the value of this property.
151 * @param {Function} [map] Optional function which is passed each individual value before setting it. The value returned from `map` is used in the original values place.
152 */
153
154exports.set = function (path, val, o, special, map, _copying) {
155  var lookup;
156
157  if ('function' == typeof special) {
158    if (special.length < 2) {
159      map = special;
160      special = undefined;
161    } else {
162      lookup = special;
163      special = undefined;
164    }
165  }
166
167  map || (map = K);
168
169  var parts = 'string' == typeof path
170    ? path.split('.')
171    : path
172
173  if (!Array.isArray(parts)) {
174    throw new TypeError('Invalid `path`. Must be either string or array');
175  }
176
177  if (null == o) return;
178
179  // the existance of $ in a path tells us if the user desires
180  // the copying of an array instead of setting each value of
181  // the array to the one by one to matching positions of the
182  // current array. Unless the user explicitly opted out by passing
183  // false, see Automattic/mongoose#6273
184  var copy = _copying || (/\$/.test(path) && _copying !== false)
185    , obj = o
186    , part
187
188  for (var i = 0, len = parts.length - 1; i < len; ++i) {
189    part = parts[i];
190
191    if ('$' == part) {
192      if (i == len - 1) {
193        break;
194      } else {
195        continue;
196      }
197    }
198
199    if (Array.isArray(obj) && !/^\d+$/.test(part)) {
200      var paths = parts.slice(i);
201      if (!copy && Array.isArray(val)) {
202        for (var j = 0; j < obj.length && j < val.length; ++j) {
203          // assignment of single values of array
204          exports.set(paths, val[j], obj[j], special || lookup, map, copy);
205        }
206      } else {
207        for (var j = 0; j < obj.length; ++j) {
208          // assignment of entire value
209          exports.set(paths, val, obj[j], special || lookup, map, copy);
210        }
211      }
212      return;
213    }
214
215    if (lookup) {
216      obj = lookup(obj, part);
217    } else {
218      var _to = special && obj[special] ? obj[special] : obj;
219      obj = _to instanceof Map ?
220        _to.get(part) :
221        _to[part];
222    }
223
224    if (!obj) return;
225  }
226
227  // process the last property of the path
228
229  part = parts[len];
230
231  // use the special property if exists
232  if (special && obj[special]) {
233    obj = obj[special];
234  }
235
236  // set the value on the last branch
237  if (Array.isArray(obj) && !/^\d+$/.test(part)) {
238    if (!copy && Array.isArray(val)) {
239      for (var item, j = 0; j < obj.length && j < val.length; ++j) {
240        item = obj[j];
241        if (item) {
242          if (lookup) {
243            lookup(item, part, map(val[j]));
244          } else {
245            if (item[special]) item = item[special];
246            item[part] = map(val[j]);
247          }
248        }
249      }
250    } else {
251      for (var j = 0; j < obj.length; ++j) {
252        item = obj[j];
253        if (item) {
254          if (lookup) {
255            lookup(item, part, map(val));
256          } else {
257            if (item[special]) item = item[special];
258            item[part] = map(val);
259          }
260        }
261      }
262    }
263  } else {
264    if (lookup) {
265      lookup(obj, part, map(val));
266    } else if (obj instanceof Map) {
267      obj.set(part, map(val));
268    } else {
269      obj[part] = map(val);
270    }
271  }
272}
273
274/*!
275 * Returns the value passed to it.
276 */
277
278function K (v) {
279  return v;
280}