index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. exports.get = function (path, o, special, map) {
  29. if ('function' == typeof special) {
  30. map = special;
  31. special = undefined;
  32. }
  33. map || (map = K);
  34. var parts = 'string' == typeof path
  35. ? path.split('.')
  36. : path
  37. if (!Array.isArray(parts)) {
  38. throw new TypeError('Invalid `path`. Must be either string or array');
  39. }
  40. var obj = o
  41. , part;
  42. for (var i = 0; i < parts.length; ++i) {
  43. part = parts[i];
  44. if (Array.isArray(obj) && !/^\d+$/.test(part)) {
  45. // reading a property from the array items
  46. var paths = parts.slice(i);
  47. return obj.map(function (item) {
  48. return item
  49. ? exports.get(paths, item, special, map)
  50. : map(undefined);
  51. });
  52. }
  53. obj = special && obj[special]
  54. ? obj[special][part]
  55. : obj[part];
  56. if (!obj) return map(obj);
  57. }
  58. return map(obj);
  59. }
  60. /**
  61. * Sets the `val` at the given `path` of object `o`.
  62. *
  63. * @param {String} path
  64. * @param {Anything} val
  65. * @param {Object} o
  66. * @param {String} [special] When this property name is present on any object in the path, walking will continue on the value of this property.
  67. * @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.
  68. */
  69. exports.set = function (path, val, o, special, map, _copying) {
  70. if ('function' == typeof special) {
  71. map = special;
  72. special = undefined;
  73. }
  74. map || (map = K);
  75. var parts = 'string' == typeof path
  76. ? path.split('.')
  77. : path
  78. if (!Array.isArray(parts)) {
  79. throw new TypeError('Invalid `path`. Must be either string or array');
  80. }
  81. if (null == o) return;
  82. // the existance of $ in a path tells us if the user desires
  83. // the copying of an array instead of setting each value of
  84. // the array to the one by one to matching positions of the
  85. // current array.
  86. var copy = _copying || /\$/.test(path)
  87. , obj = o
  88. , part
  89. for (var i = 0, len = parts.length - 1; i < len; ++i) {
  90. part = parts[i];
  91. if ('$' == part) {
  92. if (i == len - 1) {
  93. break;
  94. } else {
  95. continue;
  96. }
  97. }
  98. if (Array.isArray(obj) && !/^\d+$/.test(part)) {
  99. var paths = parts.slice(i);
  100. if (!copy && Array.isArray(val)) {
  101. for (var j = 0; j < obj.length && j < val.length; ++j) {
  102. // assignment of single values of array
  103. exports.set(paths, val[j], obj[j], special, map, copy);
  104. }
  105. } else {
  106. for (var j = 0; j < obj.length; ++j) {
  107. // assignment of entire value
  108. exports.set(paths, val, obj[j], special, map, copy);
  109. }
  110. }
  111. return;
  112. }
  113. obj = special && obj[special]
  114. ? obj[special][part]
  115. : obj[part];
  116. if (!obj) return;
  117. }
  118. // process the last property of the path
  119. part = parts[len];
  120. // use the special property if exists
  121. if (special && obj[special]) {
  122. obj = obj[special];
  123. }
  124. // set the value on the last branch
  125. if (Array.isArray(obj) && !/^\d+$/.test(part)) {
  126. if (!copy && Array.isArray(val)) {
  127. for (var item, j = 0; j < obj.length && j < val.length; ++j) {
  128. item = obj[j];
  129. if (item) {
  130. if (item[special]) item = item[special];
  131. item[part] = map(val[j]);
  132. }
  133. }
  134. } else {
  135. for (var j = 0; j < obj.length; ++j) {
  136. item = obj[j];
  137. if (item) {
  138. if (item[special]) item = item[special];
  139. item[part] = map(val);
  140. }
  141. }
  142. }
  143. } else {
  144. obj[part] = map(val);
  145. }
  146. }
  147. /*!
  148. * Returns the value passed to it.
  149. */
  150. function K (v) {
  151. return v;
  152. }