Ohm-Management - Projektarbeit B-ME
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.

README.md 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #mpath
  2. {G,S}et javascript object values using MongoDB-like path notation.
  3. ###Getting
  4. ```js
  5. var mpath = require('mpath');
  6. var obj = {
  7. comments: [
  8. { title: 'funny' },
  9. { title: 'exciting!' }
  10. ]
  11. }
  12. mpath.get('comments.1.title', obj) // 'exciting!'
  13. ```
  14. `mpath.get` supports array property notation as well.
  15. ```js
  16. var obj = {
  17. comments: [
  18. { title: 'funny' },
  19. { title: 'exciting!' }
  20. ]
  21. }
  22. mpath.get('comments.title', obj) // ['funny', 'exciting!']
  23. ```
  24. Array property and indexing syntax, when used together, are very powerful.
  25. ```js
  26. var obj = {
  27. array: [
  28. { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
  29. , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }}
  30. , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
  31. , { o: { array: [{x: null }] }}
  32. , { o: { array: [{y: 3 }] }}
  33. , { o: { array: [3, 0, null] }}
  34. , { o: { name: 'ha' }}
  35. ];
  36. }
  37. var found = mpath.get('array.o.array.x.b.1', obj);
  38. console.log(found); // prints..
  39. [ [6, undefined]
  40. , [2, undefined, undefined]
  41. , [null, 1]
  42. , [null]
  43. , [undefined]
  44. , [undefined, undefined, undefined]
  45. , undefined
  46. ]
  47. ```
  48. #####Field selection rules:
  49. The following rules are iteratively applied to each `segment` in the passed `path`. For example:
  50. ```js
  51. var path = 'one.two.14'; // path
  52. 'one' // segment 0
  53. 'two' // segment 1
  54. 14 // segment 2
  55. ```
  56. - 1) when value of the segment parent is not an array, return the value of `parent.segment`
  57. - 2) when value of the segment parent is an array
  58. - a) if the segment is an integer, replace the parent array with the value at `parent[segment]`
  59. - 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.
  60. #####Maps
  61. `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.
  62. ```js
  63. var obj = {
  64. comments: [
  65. { title: 'funny' },
  66. { title: 'exciting!' }
  67. ]
  68. }
  69. mpath.get('comments.title', obj, function (val) {
  70. return 'funny' == val
  71. ? 'amusing'
  72. : val;
  73. });
  74. // ['amusing', 'exciting!']
  75. ```
  76. ###Setting
  77. ```js
  78. var obj = {
  79. comments: [
  80. { title: 'funny' },
  81. { title: 'exciting!' }
  82. ]
  83. }
  84. mpath.set('comments.1.title', 'hilarious', obj)
  85. console.log(obj.comments[1].title) // 'hilarious'
  86. ```
  87. `mpath.set` supports the same array property notation as `mpath.get`.
  88. ```js
  89. var obj = {
  90. comments: [
  91. { title: 'funny' },
  92. { title: 'exciting!' }
  93. ]
  94. }
  95. mpath.set('comments.title', ['hilarious', 'fruity'], obj);
  96. console.log(obj); // prints..
  97. { comments: [
  98. { title: 'hilarious' },
  99. { title: 'fruity' }
  100. ]}
  101. ```
  102. Array property and indexing syntax can be used together also when setting.
  103. ```js
  104. var obj = {
  105. array: [
  106. { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
  107. , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }}
  108. , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
  109. , { o: { array: [{x: null }] }}
  110. , { o: { array: [{y: 3 }] }}
  111. , { o: { array: [3, 0, null] }}
  112. , { o: { name: 'ha' }}
  113. ]
  114. }
  115. mpath.set('array.1.o', 'this was changed', obj);
  116. console.log(require('util').inspect(obj, false, 1000)); // prints..
  117. {
  118. array: [
  119. { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
  120. , { o: 'this was changed' }
  121. , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
  122. , { o: { array: [{x: null }] }}
  123. , { o: { array: [{y: 3 }] }}
  124. , { o: { array: [3, 0, null] }}
  125. , { o: { name: 'ha' }}
  126. ];
  127. }
  128. mpath.set('array.o.array.x', 'this was changed too', obj);
  129. console.log(require('util').inspect(obj, false, 1000)); // prints..
  130. {
  131. array: [
  132. { o: { array: [{x: 'this was changed too'}, { y: 10, x: 'this was changed too'} ] }}
  133. , { o: 'this was changed' }
  134. , { o: { array: [{x: 'this was changed too'}, { x: 'this was changed too'}] }}
  135. , { o: { array: [{x: 'this was changed too'}] }}
  136. , { o: { array: [{x: 'this was changed too', y: 3 }] }}
  137. , { o: { array: [3, 0, null] }}
  138. , { o: { name: 'ha' }}
  139. ];
  140. }
  141. ```
  142. ####Setting arrays
  143. 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.
  144. ```js
  145. var obj = {
  146. comments: [
  147. { title: 'funny' },
  148. { title: 'exciting!' }
  149. ]
  150. }
  151. mpath.set('comments.title', ['hilarious', 'fruity'], obj);
  152. console.log(obj); // prints..
  153. { comments: [
  154. { title: 'hilarious' },
  155. { title: 'fruity' }
  156. ]}
  157. ```
  158. 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.
  159. ```js
  160. var obj = {
  161. comments: [
  162. { title: 'funny' },
  163. { title: 'exciting!' }
  164. ]
  165. }
  166. mpath.set('comments.$.title', ['hilarious', 'fruity'], obj);
  167. console.log(obj); // prints..
  168. { comments: [
  169. { title: ['hilarious', 'fruity'] },
  170. { title: ['hilarious', 'fruity'] }
  171. ]}
  172. ```
  173. ####Field assignment rules
  174. 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.
  175. #####Maps
  176. `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.
  177. ```js
  178. var obj = {
  179. comments: [
  180. { title: 'funny' },
  181. { title: 'exciting!' }
  182. ]
  183. }
  184. mpath.set('comments.title', ['hilarious', 'fruity'], obj, function (val) {
  185. return val.length;
  186. });
  187. console.log(obj); // prints..
  188. { comments: [
  189. { title: 9 },
  190. { title: 6 }
  191. ]}
  192. ```
  193. ### Custom object types
  194. 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:
  195. ```js
  196. var mpath = require('mpath');
  197. var obj = {
  198. comments: [
  199. { title: 'exciting!', _doc: { title: 'great!' }}
  200. ]
  201. }
  202. mpath.get('comments.0.title', obj, '_doc') // 'great!'
  203. mpath.set('comments.0.title', 'nov 3rd', obj, '_doc')
  204. mpath.get('comments.0.title', obj, '_doc') // 'nov 3rd'
  205. mpath.get('comments.0.title', obj) // 'exciting'
  206. ```
  207. When used with a `map`, the `map` argument comes last.
  208. ```js
  209. mpath.get(path, obj, '_doc', map);
  210. mpath.set(path, val, obj, '_doc', map);
  211. ```
  212. [LICENSE](https://github.com/aheckmann/mpath/blob/master/LICENSE)