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.

index.js 62KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. 'use strict';
  2. /**
  3. * Test dependencies.
  4. */
  5. var mpath = require('../')
  6. var assert = require('assert')
  7. /**
  8. * logging helper
  9. */
  10. function log (o) {
  11. console.log();
  12. console.log(require('util').inspect(o, false, 1000));
  13. }
  14. /**
  15. * special path for override tests
  16. */
  17. var special = '_doc';
  18. /**
  19. * Tests
  20. */
  21. describe('mpath', function(){
  22. /**
  23. * test doc creator
  24. */
  25. function doc () {
  26. var o = { first: { second: { third: [3,{ name: 'aaron' }, 9] }}};
  27. o.comments = [
  28. { name: 'one' }
  29. , { name: 'two', _doc: { name: '2' }}
  30. , { name: 'three'
  31. , comments: [{},{ comments: [{val: 'twoo'}]}]
  32. , _doc: { name: '3', comments: [{},{ _doc: { comments: [{ val: 2 }] }}] }}
  33. ];
  34. o.name = 'jiro';
  35. o.array = [
  36. { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }}
  37. , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: {b: 'hi'}}] }}
  38. , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }}
  39. , { o: { array: [{x: null }] }}
  40. , { o: { array: [{y: 3 }] }}
  41. , { o: { array: [3, 0, null] }}
  42. , { o: { name: 'ha' }}
  43. ];
  44. o.arr = [
  45. { arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  46. , { yep: true }
  47. ]
  48. return o;
  49. }
  50. describe('get', function(){
  51. var o = doc();
  52. it('`path` must be a string or array', function(done){
  53. assert.throws(function () {
  54. mpath.get({}, o);
  55. }, /Must be either string or array/);
  56. assert.throws(function () {
  57. mpath.get(4, o);
  58. }, /Must be either string or array/);
  59. assert.throws(function () {
  60. mpath.get(function(){}, o);
  61. }, /Must be either string or array/);
  62. assert.throws(function () {
  63. mpath.get(/asdf/, o);
  64. }, /Must be either string or array/);
  65. assert.throws(function () {
  66. mpath.get(Math, o);
  67. }, /Must be either string or array/);
  68. assert.throws(function () {
  69. mpath.get(Buffer, o);
  70. }, /Must be either string or array/);
  71. assert.doesNotThrow(function () {
  72. mpath.get('string', o);
  73. });
  74. assert.doesNotThrow(function () {
  75. mpath.get([], o);
  76. });
  77. done();
  78. })
  79. describe('without `special`', function(){
  80. it('works', function(done){
  81. assert.equal('jiro', mpath.get('name', o));
  82. assert.deepEqual(
  83. { second: { third: [3,{ name: 'aaron' }, 9] }}
  84. , mpath.get('first', o)
  85. );
  86. assert.deepEqual(
  87. { third: [3,{ name: 'aaron' }, 9] }
  88. , mpath.get('first.second', o)
  89. );
  90. assert.deepEqual(
  91. [3,{ name: 'aaron' }, 9]
  92. , mpath.get('first.second.third', o)
  93. );
  94. assert.deepEqual(
  95. 3
  96. , mpath.get('first.second.third.0', o)
  97. );
  98. assert.deepEqual(
  99. 9
  100. , mpath.get('first.second.third.2', o)
  101. );
  102. assert.deepEqual(
  103. { name: 'aaron' }
  104. , mpath.get('first.second.third.1', o)
  105. );
  106. assert.deepEqual(
  107. 'aaron'
  108. , mpath.get('first.second.third.1.name', o)
  109. );
  110. assert.deepEqual([
  111. { name: 'one' }
  112. , { name: 'two', _doc: { name: '2' }}
  113. , { name: 'three'
  114. , comments: [{},{ comments: [{val: 'twoo'}]}]
  115. , _doc: { name: '3', comments: [{},{ _doc: { comments: [{ val: 2 }] }}]}}],
  116. mpath.get('comments', o));
  117. assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o));
  118. assert.deepEqual('one', mpath.get('comments.0.name', o));
  119. assert.deepEqual('two', mpath.get('comments.1.name', o));
  120. assert.deepEqual('three', mpath.get('comments.2.name', o));
  121. assert.deepEqual([{},{ comments: [{val: 'twoo'}]}]
  122. , mpath.get('comments.2.comments', o));
  123. assert.deepEqual({ comments: [{val: 'twoo'}]}
  124. , mpath.get('comments.2.comments.1', o));
  125. assert.deepEqual('twoo', mpath.get('comments.2.comments.1.comments.0.val', o));
  126. done();
  127. })
  128. it('handles array.property dot-notation', function(done){
  129. assert.deepEqual(
  130. ['one', 'two', 'three']
  131. , mpath.get('comments.name', o)
  132. );
  133. done();
  134. })
  135. it('handles array.array notation', function(done){
  136. assert.deepEqual(
  137. [undefined, undefined, [{}, {comments:[{val:'twoo'}]}]]
  138. , mpath.get('comments.comments', o)
  139. );
  140. done();
  141. })
  142. it('handles prop.prop.prop.arrayProperty notation', function(done){
  143. assert.deepEqual(
  144. [undefined, 'aaron', undefined]
  145. , mpath.get('first.second.third.name', o)
  146. );
  147. assert.deepEqual(
  148. [1, 'aaron', 1]
  149. , mpath.get('first.second.third.name', o, function (v) {
  150. return undefined === v ? 1 : v;
  151. })
  152. );
  153. done();
  154. })
  155. it('handles array.prop.array', function(done){
  156. assert.deepEqual(
  157. [ [{x: {b: [4,6,8]}}, { y: 10} ]
  158. , [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: {b: 'hi'}}]
  159. , [{x: {b: null }}, { x: { b: [null, 1]}}]
  160. , [{x: null }]
  161. , [{y: 3 }]
  162. , [3, 0, null]
  163. , undefined
  164. ]
  165. , mpath.get('array.o.array', o)
  166. );
  167. done();
  168. })
  169. it('handles array.prop.array.index', function(done){
  170. assert.deepEqual(
  171. [ {x: {b: [4,6,8]}}
  172. , {x: {b: [1,2,3]}}
  173. , {x: {b: null }}
  174. , {x: null }
  175. , {y: 3 }
  176. , 3
  177. , undefined
  178. ]
  179. , mpath.get('array.o.array.0', o)
  180. );
  181. done();
  182. })
  183. it('handles array.prop.array.index.prop', function(done){
  184. assert.deepEqual(
  185. [ {b: [4,6,8]}
  186. , {b: [1,2,3]}
  187. , {b: null }
  188. , null
  189. , undefined
  190. , undefined
  191. , undefined
  192. ]
  193. , mpath.get('array.o.array.0.x', o)
  194. );
  195. done();
  196. })
  197. it('handles array.prop.array.prop', function(done){
  198. assert.deepEqual(
  199. [ [undefined, 10 ]
  200. , [undefined, undefined, undefined]
  201. , [undefined, undefined]
  202. , [undefined]
  203. , [3]
  204. , [undefined, undefined, undefined]
  205. , undefined
  206. ]
  207. , mpath.get('array.o.array.y', o)
  208. );
  209. assert.deepEqual(
  210. [ [{b: [4,6,8]}, undefined]
  211. , [{b: [1,2,3]}, {z: 10 }, {b: 'hi'}]
  212. , [{b: null }, { b: [null, 1]}]
  213. , [null]
  214. , [undefined]
  215. , [undefined, undefined, undefined]
  216. , undefined
  217. ]
  218. , mpath.get('array.o.array.x', o)
  219. );
  220. done();
  221. })
  222. it('handles array.prop.array.prop.prop', function(done){
  223. assert.deepEqual(
  224. [ [[4,6,8], undefined]
  225. , [[1,2,3], undefined, 'hi']
  226. , [null, [null, 1]]
  227. , [null]
  228. , [undefined]
  229. , [undefined, undefined, undefined]
  230. , undefined
  231. ]
  232. , mpath.get('array.o.array.x.b', o)
  233. );
  234. done();
  235. })
  236. it('handles array.prop.array.prop.prop.index', function(done){
  237. assert.deepEqual(
  238. [ [6, undefined]
  239. , [2, undefined, 'i'] // undocumented feature (string indexing)
  240. , [null, 1]
  241. , [null]
  242. , [undefined]
  243. , [undefined, undefined, undefined]
  244. , undefined
  245. ]
  246. , mpath.get('array.o.array.x.b.1', o)
  247. );
  248. assert.deepEqual(
  249. [ [6, 0]
  250. , [2, 0, 'i'] // undocumented feature (string indexing)
  251. , [null, 1]
  252. , [null]
  253. , [0]
  254. , [0, 0, 0]
  255. , 0
  256. ]
  257. , mpath.get('array.o.array.x.b.1', o, function (v) {
  258. return undefined === v ? 0 : v;
  259. })
  260. );
  261. done();
  262. })
  263. it('handles array.index.prop.prop', function(done){
  264. assert.deepEqual(
  265. [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: {b: 'hi'}}]
  266. , mpath.get('array.1.o.array', o)
  267. );
  268. assert.deepEqual(
  269. ['hi','hi','hi']
  270. , mpath.get('array.1.o.array', o, function (v) {
  271. if (Array.isArray(v)) {
  272. return v.map(function (val) {
  273. return 'hi';
  274. })
  275. }
  276. return v;
  277. })
  278. );
  279. done();
  280. })
  281. it('handles array.array.index', function(done){
  282. assert.deepEqual(
  283. [{ a: { c: 48 }}, undefined]
  284. , mpath.get('arr.arr.1', o)
  285. );
  286. assert.deepEqual(
  287. ['woot', undefined]
  288. , mpath.get('arr.arr.1', o, function (v) {
  289. if (v && v.a && v.a.c) return 'woot';
  290. return v;
  291. })
  292. );
  293. done();
  294. })
  295. it('handles array.array.index.prop', function(done){
  296. assert.deepEqual(
  297. [{ c: 48 }, 'woot']
  298. , mpath.get('arr.arr.1.a', o, function (v) {
  299. if (undefined === v) return 'woot';
  300. return v;
  301. })
  302. );
  303. assert.deepEqual(
  304. [{ c: 48 }, undefined]
  305. , mpath.get('arr.arr.1.a', o)
  306. );
  307. mpath.set('arr.arr.1.a', [{c:49},undefined], o)
  308. assert.deepEqual(
  309. [{ c: 49 }, undefined]
  310. , mpath.get('arr.arr.1.a', o)
  311. );
  312. mpath.set('arr.arr.1.a', [{c:48},undefined], o)
  313. done();
  314. })
  315. it('handles array.array.index.prop.prop', function(done){
  316. assert.deepEqual(
  317. [48, undefined]
  318. , mpath.get('arr.arr.1.a.c', o)
  319. );
  320. assert.deepEqual(
  321. [48, 'woot']
  322. , mpath.get('arr.arr.1.a.c', o, function (v) {
  323. if (undefined === v) return 'woot';
  324. return v;
  325. })
  326. );
  327. done();
  328. })
  329. })
  330. describe('with `special`', function(){
  331. describe('that is a string', function(){
  332. it('works', function(done){
  333. assert.equal('jiro', mpath.get('name', o, special));
  334. assert.deepEqual(
  335. { second: { third: [3,{ name: 'aaron' }, 9] }}
  336. , mpath.get('first', o, special)
  337. );
  338. assert.deepEqual(
  339. { third: [3,{ name: 'aaron' }, 9] }
  340. , mpath.get('first.second', o, special)
  341. );
  342. assert.deepEqual(
  343. [3,{ name: 'aaron' }, 9]
  344. , mpath.get('first.second.third', o, special)
  345. );
  346. assert.deepEqual(
  347. 3
  348. , mpath.get('first.second.third.0', o, special)
  349. );
  350. assert.deepEqual(
  351. 4
  352. , mpath.get('first.second.third.0', o, special, function (v) {
  353. return 3 === v ? 4 : v;
  354. })
  355. );
  356. assert.deepEqual(
  357. 9
  358. , mpath.get('first.second.third.2', o, special)
  359. );
  360. assert.deepEqual(
  361. { name: 'aaron' }
  362. , mpath.get('first.second.third.1', o, special)
  363. );
  364. assert.deepEqual(
  365. 'aaron'
  366. , mpath.get('first.second.third.1.name', o, special)
  367. );
  368. assert.deepEqual([
  369. { name: 'one' }
  370. , { name: 'two', _doc: { name: '2' }}
  371. , { name: 'three'
  372. , comments: [{},{ comments: [{val: 'twoo'}]}]
  373. , _doc: { name: '3', comments: [{},{ _doc: { comments: [{ val: 2 }] }}]}}],
  374. mpath.get('comments', o, special));
  375. assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o, special));
  376. assert.deepEqual('one', mpath.get('comments.0.name', o, special));
  377. assert.deepEqual('2', mpath.get('comments.1.name', o, special));
  378. assert.deepEqual('3', mpath.get('comments.2.name', o, special));
  379. assert.deepEqual('nice', mpath.get('comments.2.name', o, special, function (v) {
  380. return '3' === v ? 'nice' : v;
  381. }));
  382. assert.deepEqual([{},{ _doc: { comments: [{ val: 2 }] }}]
  383. , mpath.get('comments.2.comments', o, special));
  384. assert.deepEqual({ _doc: { comments: [{val: 2}]}}
  385. , mpath.get('comments.2.comments.1', o, special));
  386. assert.deepEqual(2, mpath.get('comments.2.comments.1.comments.0.val', o, special));
  387. done();
  388. })
  389. it('handles array.property dot-notation', function(done){
  390. assert.deepEqual(
  391. ['one', '2', '3']
  392. , mpath.get('comments.name', o, special)
  393. );
  394. assert.deepEqual(
  395. ['one', 2, '3']
  396. , mpath.get('comments.name', o, special, function (v) {
  397. return '2' === v ? 2 : v
  398. })
  399. );
  400. done();
  401. })
  402. it('handles array.array notation', function(done){
  403. assert.deepEqual(
  404. [undefined, undefined, [{}, {_doc: { comments:[{val:2}]}}]]
  405. , mpath.get('comments.comments', o, special)
  406. );
  407. done();
  408. })
  409. it('handles array.array.index.array', function(done){
  410. assert.deepEqual(
  411. [undefined, undefined, [{val:2}]]
  412. , mpath.get('comments.comments.1.comments', o, special)
  413. );
  414. done();
  415. })
  416. it('handles array.array.index.array.prop', function(done){
  417. assert.deepEqual(
  418. [undefined, undefined, [2]]
  419. , mpath.get('comments.comments.1.comments.val', o, special)
  420. );
  421. assert.deepEqual(
  422. ['nil', 'nil', [2]]
  423. , mpath.get('comments.comments.1.comments.val', o, special, function (v) {
  424. return undefined === v ? 'nil' : v;
  425. })
  426. );
  427. done();
  428. })
  429. })
  430. describe('that is a function', function(){
  431. var special = function (obj, key) {
  432. return obj[key]
  433. }
  434. it('works', function(done){
  435. assert.equal('jiro', mpath.get('name', o, special));
  436. assert.deepEqual(
  437. { second: { third: [3,{ name: 'aaron' }, 9] }}
  438. , mpath.get('first', o, special)
  439. );
  440. assert.deepEqual(
  441. { third: [3,{ name: 'aaron' }, 9] }
  442. , mpath.get('first.second', o, special)
  443. );
  444. assert.deepEqual(
  445. [3,{ name: 'aaron' }, 9]
  446. , mpath.get('first.second.third', o, special)
  447. );
  448. assert.deepEqual(
  449. 3
  450. , mpath.get('first.second.third.0', o, special)
  451. );
  452. assert.deepEqual(
  453. 4
  454. , mpath.get('first.second.third.0', o, special, function (v) {
  455. return 3 === v ? 4 : v;
  456. })
  457. );
  458. assert.deepEqual(
  459. 9
  460. , mpath.get('first.second.third.2', o, special)
  461. );
  462. assert.deepEqual(
  463. { name: 'aaron' }
  464. , mpath.get('first.second.third.1', o, special)
  465. );
  466. assert.deepEqual(
  467. 'aaron'
  468. , mpath.get('first.second.third.1.name', o, special)
  469. );
  470. assert.deepEqual([
  471. { name: 'one' }
  472. , { name: 'two', _doc: { name: '2' }}
  473. , { name: 'three'
  474. , comments: [{},{ comments: [{val: 'twoo'}]}]
  475. , _doc: { name: '3', comments: [{},{ _doc: { comments: [{ val: 2 }] }}]}}],
  476. mpath.get('comments', o, special));
  477. assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o, special));
  478. assert.deepEqual('one', mpath.get('comments.0.name', o, special));
  479. assert.deepEqual('two', mpath.get('comments.1.name', o, special));
  480. assert.deepEqual('three', mpath.get('comments.2.name', o, special));
  481. assert.deepEqual('nice', mpath.get('comments.2.name', o, special, function (v) {
  482. return 'three' === v ? 'nice' : v;
  483. }));
  484. assert.deepEqual([{},{ comments: [{ val: 'twoo' }] }]
  485. , mpath.get('comments.2.comments', o, special));
  486. assert.deepEqual({ comments: [{val: 'twoo'}]}
  487. , mpath.get('comments.2.comments.1', o, special));
  488. assert.deepEqual('twoo', mpath.get('comments.2.comments.1.comments.0.val', o, special));
  489. var overide = false;
  490. assert.deepEqual('twoo', mpath.get('comments.8.comments.1.comments.0.val', o, function (obj, path) {
  491. if (Array.isArray(obj) && 8 == path) {
  492. overide = true;
  493. return obj[2];
  494. }
  495. return obj[path];
  496. }));
  497. assert.ok(overide);
  498. done();
  499. })
  500. it('in combination with map', function(done){
  501. var special = function (obj, key) {
  502. if (Array.isArray(obj)) return obj[key];
  503. return obj.mpath;
  504. }
  505. var map = function (val) {
  506. return 'convert' == val
  507. ? 'mpath'
  508. : val;
  509. }
  510. var o = { mpath: [{ mpath: 'converse' }, { mpath: 'convert' }] }
  511. assert.equal('mpath', mpath.get('something.1.kewl', o, special, map));
  512. done();
  513. })
  514. })
  515. })
  516. })
  517. describe('set', function() {
  518. it('prevents writing to __proto__', function() {
  519. var obj = {};
  520. mpath.set('__proto__.x', 'foobar', obj);
  521. assert.ok(!({}.x));
  522. mpath.set('constructor.prototype.x', 'foobar', obj);
  523. assert.ok(!({}.x));
  524. });
  525. describe('without `special`', function() {
  526. var o = doc();
  527. it('works', function(done) {
  528. mpath.set('name', 'a new val', o, function(v) {
  529. return 'a new val' === v ? 'changed' : v;
  530. });
  531. assert.deepEqual('changed', o.name);
  532. mpath.set('name', 'changed', o);
  533. assert.deepEqual('changed', o.name);
  534. mpath.set('first.second.third', [1,{name:'x'},9], o);
  535. assert.deepEqual([1,{name:'x'},9], o.first.second.third);
  536. mpath.set('first.second.third.1.name', 'y', o)
  537. assert.deepEqual([1,{name:'y'},9], o.first.second.third);
  538. mpath.set('comments.1.name', 'ttwwoo', o);
  539. assert.deepEqual({ name: 'ttwwoo', _doc: { name: '2' }}, o.comments[1]);
  540. mpath.set('comments.2.comments.1.comments.0.expand', 'added', o);
  541. assert.deepEqual(
  542. { val: 'twoo', expand: 'added'}
  543. , o.comments[2].comments[1].comments[0]);
  544. mpath.set('comments.2.comments.1.comments.2', 'added', o);
  545. assert.equal(3, o.comments[2].comments[1].comments.length);
  546. assert.deepEqual(
  547. { val: 'twoo', expand: 'added'}
  548. , o.comments[2].comments[1].comments[0]);
  549. assert.deepEqual(
  550. undefined
  551. , o.comments[2].comments[1].comments[1]);
  552. assert.deepEqual(
  553. 'added'
  554. , o.comments[2].comments[1].comments[2]);
  555. done();
  556. })
  557. describe('array.path', function(){
  558. describe('with single non-array value', function(){
  559. it('works', function(done){
  560. mpath.set('arr.yep', false, o, function (v) {
  561. return false === v ? true: v;
  562. });
  563. assert.deepEqual([
  564. { yep: true, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  565. , { yep: true }
  566. ], o.arr);
  567. mpath.set('arr.yep', false, o);
  568. assert.deepEqual([
  569. { yep: false, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  570. , { yep: false }
  571. ], o.arr);
  572. done();
  573. })
  574. })
  575. describe('with array of values', function(){
  576. it('that are equal in length', function(done){
  577. mpath.set('arr.yep', ['one',2], o, function (v) {
  578. return 'one' === v ? 1 : v;
  579. });
  580. assert.deepEqual([
  581. { yep: 1, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  582. , { yep: 2 }
  583. ], o.arr);
  584. mpath.set('arr.yep', ['one',2], o);
  585. assert.deepEqual([
  586. { yep: 'one', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  587. , { yep: 2 }
  588. ], o.arr);
  589. done();
  590. })
  591. it('that is less than length', function(done){
  592. mpath.set('arr.yep', [47], o, function (v) {
  593. return 47 === v ? 4 : v;
  594. });
  595. assert.deepEqual([
  596. { yep: 4, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  597. , { yep: 2 }
  598. ], o.arr);
  599. mpath.set('arr.yep', [47], o);
  600. assert.deepEqual([
  601. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  602. , { yep: 2 }
  603. ], o.arr);
  604. done();
  605. })
  606. it('that is greater than length', function(done){
  607. mpath.set('arr.yep', [5,6,7], o, function (v) {
  608. return 5 === v ? 'five' : v;
  609. });
  610. assert.deepEqual([
  611. { yep: 'five', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  612. , { yep: 6 }
  613. ], o.arr);
  614. mpath.set('arr.yep', [5,6,7], o);
  615. assert.deepEqual([
  616. { yep: 5, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  617. , { yep: 6 }
  618. ], o.arr);
  619. done();
  620. })
  621. })
  622. })
  623. describe('array.$.path', function(){
  624. describe('with single non-array value', function(){
  625. it('copies the value to each item in array', function(done){
  626. mpath.set('arr.$.yep', {xtra: 'double good'}, o, function (v) {
  627. return v && v.xtra ? 'hi' : v;
  628. });
  629. assert.deepEqual([
  630. { yep: 'hi', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  631. , { yep: 'hi'}
  632. ], o.arr);
  633. mpath.set('arr.$.yep', {xtra: 'double good'}, o);
  634. assert.deepEqual([
  635. { yep: {xtra:'double good'}, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  636. , { yep: {xtra:'double good'}}
  637. ], o.arr);
  638. done();
  639. })
  640. })
  641. describe('with array of values', function(){
  642. it('copies the value to each item in array', function(done){
  643. mpath.set('arr.$.yep', [15], o, function (v) {
  644. return v.length === 1 ? [] : v;
  645. });
  646. assert.deepEqual([
  647. { yep: [], arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  648. , { yep: []}
  649. ], o.arr);
  650. mpath.set('arr.$.yep', [15], o);
  651. assert.deepEqual([
  652. { yep: [15], arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  653. , { yep: [15]}
  654. ], o.arr);
  655. done();
  656. })
  657. })
  658. })
  659. describe('array.index.path', function(){
  660. it('works', function(done){
  661. mpath.set('arr.1.yep', 0, o, function (v) {
  662. return 0 === v ? 'zero' : v;
  663. });
  664. assert.deepEqual([
  665. { yep: [15] , arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  666. , { yep: 'zero' }
  667. ], o.arr);
  668. mpath.set('arr.1.yep', 0, o);
  669. assert.deepEqual([
  670. { yep: [15] , arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  671. , { yep: 0 }
  672. ], o.arr);
  673. done();
  674. })
  675. })
  676. describe('array.index.array.path', function(){
  677. it('with single value', function(done){
  678. mpath.set('arr.0.arr.e', 35, o, function (v) {
  679. return 35 === v ? 3 : v;
  680. });
  681. assert.deepEqual([
  682. { yep: [15], arr: [{ a: { b: 47 }, e: 3}, { a: { c: 48 }, e: 3}, { d: 'yep', e: 3 }] }
  683. , { yep: 0 }
  684. ], o.arr);
  685. mpath.set('arr.0.arr.e', 35, o);
  686. assert.deepEqual([
  687. { yep: [15], arr: [{ a: { b: 47 }, e: 35}, { a: { c: 48 }, e: 35}, { d: 'yep', e: 35 }] }
  688. , { yep: 0 }
  689. ], o.arr);
  690. done();
  691. })
  692. it('with array', function(done){
  693. mpath.set('arr.0.arr.e', ['a','b'], o, function (v) {
  694. return 'a' === v ? 'x' : v;
  695. });
  696. assert.deepEqual([
  697. { yep: [15], arr: [{ a: { b: 47 }, e: 'x'}, { a: { c: 48 }, e: 'b'}, { d: 'yep', e: 35 }] }
  698. , { yep: 0 }
  699. ], o.arr);
  700. mpath.set('arr.0.arr.e', ['a','b'], o);
  701. assert.deepEqual([
  702. { yep: [15], arr: [{ a: { b: 47 }, e: 'a'}, { a: { c: 48 }, e: 'b'}, { d: 'yep', e: 35 }] }
  703. , { yep: 0 }
  704. ], o.arr);
  705. done();
  706. })
  707. })
  708. describe('array.index.array.path.path', function(){
  709. it('with single value', function(done){
  710. mpath.set('arr.0.arr.a.b', 36, o, function (v) {
  711. return 36 === v ? 3 : v;
  712. });
  713. assert.deepEqual([
  714. { yep: [15], arr: [{ a: { b: 3 }, e: 'a'}, { a: { c: 48, b: 3 }, e: 'b'}, { d: 'yep', e: 35 }] }
  715. , { yep: 0 }
  716. ], o.arr);
  717. mpath.set('arr.0.arr.a.b', 36, o);
  718. assert.deepEqual([
  719. { yep: [15], arr: [{ a: { b: 36 }, e: 'a'}, { a: { c: 48, b: 36 }, e: 'b'}, { d: 'yep', e: 35 }] }
  720. , { yep: 0 }
  721. ], o.arr);
  722. done();
  723. })
  724. it('with array', function(done){
  725. mpath.set('arr.0.arr.a.b', [1,2,3,4], o, function (v) {
  726. return 2 === v ? 'two' : v;
  727. });
  728. assert.deepEqual([
  729. { yep: [15], arr: [{ a: { b: 1 }, e: 'a'}, { a: { c: 48, b: 'two' }, e: 'b'}, { d: 'yep', e: 35 }] }
  730. , { yep: 0 }
  731. ], o.arr);
  732. mpath.set('arr.0.arr.a.b', [1,2,3,4], o);
  733. assert.deepEqual([
  734. { yep: [15], arr: [{ a: { b: 1 }, e: 'a'}, { a: { c: 48, b: 2 }, e: 'b'}, { d: 'yep', e: 35 }] }
  735. , { yep: 0 }
  736. ], o.arr);
  737. done();
  738. })
  739. })
  740. describe('array.index.array.$.path.path', function(){
  741. it('with single value', function(done){
  742. mpath.set('arr.0.arr.$.a.b', '$', o, function (v) {
  743. return '$' === v ? 'dolla billz' : v;
  744. });
  745. assert.deepEqual([
  746. { yep: [15], arr: [{ a: { b: 'dolla billz' }, e: 'a'}, { a: { c: 48, b: 'dolla billz' }, e: 'b'}, { d: 'yep', e: 35 }] }
  747. , { yep: 0 }
  748. ], o.arr);
  749. mpath.set('arr.0.arr.$.a.b', '$', o);
  750. assert.deepEqual([
  751. { yep: [15], arr: [{ a: { b: '$' }, e: 'a'}, { a: { c: 48, b: '$' }, e: 'b'}, { d: 'yep', e: 35 }] }
  752. , { yep: 0 }
  753. ], o.arr);
  754. done();
  755. })
  756. it('with array', function(done){
  757. mpath.set('arr.0.arr.$.a.b', [1], o, function (v) {
  758. return Array.isArray(v) ? {} : v;
  759. });
  760. assert.deepEqual([
  761. { yep: [15], arr: [{ a: { b: {} }, e: 'a'}, { a: { c: 48, b: {} }, e: 'b'}, { d: 'yep', e: 35 }] }
  762. , { yep: 0 }
  763. ], o.arr);
  764. mpath.set('arr.0.arr.$.a.b', [1], o);
  765. assert.deepEqual([
  766. { yep: [15], arr: [{ a: { b: [1] }, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  767. , { yep: 0 }
  768. ], o.arr);
  769. done();
  770. })
  771. })
  772. describe('array.array.index.path', function(){
  773. it('with single value', function(done){
  774. mpath.set('arr.arr.0.a', 'single', o, function (v) {
  775. return 'single' === v ? 'double' : v;
  776. });
  777. assert.deepEqual([
  778. { yep: [15], arr: [{ a: 'double', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  779. , { yep: 0 }
  780. ], o.arr);
  781. mpath.set('arr.arr.0.a', 'single', o);
  782. assert.deepEqual([
  783. { yep: [15], arr: [{ a: 'single', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  784. , { yep: 0 }
  785. ], o.arr);
  786. done();
  787. })
  788. it('with array', function(done){
  789. mpath.set('arr.arr.0.a', [4,8,15,16,23,42], o, function (v) {
  790. return 4 === v ? 3 : v;
  791. });
  792. assert.deepEqual([
  793. { yep: [15], arr: [{ a: 3, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  794. , { yep: false }
  795. ], o.arr);
  796. mpath.set('arr.arr.0.a', [4,8,15,16,23,42], o);
  797. assert.deepEqual([
  798. { yep: [15], arr: [{ a: 4, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  799. , { yep: false }
  800. ], o.arr);
  801. done();
  802. })
  803. })
  804. describe('array.array.$.index.path', function(){
  805. it('with single value', function(done){
  806. mpath.set('arr.arr.$.0.a', 'singles', o, function (v) {
  807. return 0;
  808. });
  809. assert.deepEqual([
  810. { yep: [15], arr: [{ a: 0, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  811. , { yep: 0 }
  812. ], o.arr);
  813. mpath.set('arr.arr.$.0.a', 'singles', o);
  814. assert.deepEqual([
  815. { yep: [15], arr: [{ a: 'singles', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  816. , { yep: 0 }
  817. ], o.arr);
  818. mpath.set('$.arr.arr.0.a', 'single', o);
  819. assert.deepEqual([
  820. { yep: [15], arr: [{ a: 'single', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  821. , { yep: 0 }
  822. ], o.arr);
  823. done();
  824. })
  825. it('with array', function(done){
  826. mpath.set('arr.arr.$.0.a', [4,8,15,16,23,42], o, function (v) {
  827. return 'nope'
  828. });
  829. assert.deepEqual([
  830. { yep: [15], arr: [{ a: 'nope', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  831. , { yep: 0}
  832. ], o.arr);
  833. mpath.set('arr.arr.$.0.a', [4,8,15,16,23,42], o);
  834. assert.deepEqual([
  835. { yep: [15], arr: [{ a: [4,8,15,16,23,42], e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  836. , { yep: 0}
  837. ], o.arr);
  838. mpath.set('arr.$.arr.0.a', [4,8,15,16,23,42,108], o);
  839. assert.deepEqual([
  840. { yep: [15], arr: [{ a: [4,8,15,16,23,42,108], e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] }
  841. , { yep: 0}
  842. ], o.arr);
  843. done();
  844. })
  845. })
  846. describe('array.array.path.index', function(){
  847. it('with single value', function(done){
  848. mpath.set('arr.arr.a.7', 47, o, function (v) {
  849. return 1
  850. });
  851. assert.deepEqual([
  852. { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,1], e: 'a'}, { a: { c: 48, b: [1], '7': 1 }, e: 'b'}, { d: 'yep', e: 35 }] }
  853. , { yep: 0}
  854. ], o.arr);
  855. mpath.set('arr.arr.a.7', 47, o);
  856. assert.deepEqual([
  857. { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,47], e: 'a'}, { a: { c: 48, b: [1], '7': 47 }, e: 'b'}, { d: 'yep', e: 35 }] }
  858. , { yep: 0}
  859. ], o.arr);
  860. done();
  861. })
  862. it('with array', function(done){
  863. o.arr[1].arr = [{ a: [] }, { a: [] }, { a: null }];
  864. mpath.set('arr.arr.a.7', [[null,46], [undefined, 'woot']], o);
  865. var a1 = [];
  866. var a2 = [];
  867. a1[7] = undefined;
  868. a2[7] = 'woot';
  869. assert.deepEqual([
  870. { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,null], e: 'a'}, { a: { c: 48, b: [1], '7': 46 }, e: 'b'}, { d: 'yep', e: 35 }] }
  871. , { yep: 0, arr: [{a:a1},{a:a2},{a:null}] }
  872. ], o.arr);
  873. done();
  874. })
  875. })
  876. describe('handles array.array.path', function(){
  877. it('with single', function(done){
  878. o.arr[1].arr = [{},{}];
  879. assert.deepEqual([{},{}], o.arr[1].arr);
  880. o.arr.push({ arr: 'something else' });
  881. o.arr.push({ arr: ['something else'] });
  882. o.arr.push({ arr: [[]] });
  883. o.arr.push({ arr: [5] });
  884. var weird = [];
  885. weird.e = 'xmas';
  886. // test
  887. mpath.set('arr.arr.e', 47, o, function (v) {
  888. return 'xmas'
  889. });
  890. assert.deepEqual([
  891. { yep: [15], arr: [
  892. { a: [4,8,15,16,23,42,108,null], e: 'xmas'}
  893. , { a: { c: 48, b: [1], '7': 46 }, e: 'xmas'}
  894. , { d: 'yep', e: 'xmas' }
  895. ]
  896. }
  897. , { yep: 0, arr: [{e: 'xmas'}, {e:'xmas'}] }
  898. , { arr: 'something else' }
  899. , { arr: ['something else'] }
  900. , { arr: [weird] }
  901. , { arr: [5] }
  902. ]
  903. , o.arr);
  904. weird.e = 47;
  905. mpath.set('arr.arr.e', 47, o);
  906. assert.deepEqual([
  907. { yep: [15], arr: [
  908. { a: [4,8,15,16,23,42,108,null], e: 47}
  909. , { a: { c: 48, b: [1], '7': 46 }, e: 47}
  910. , { d: 'yep', e: 47 }
  911. ]
  912. }
  913. , { yep: 0, arr: [{e: 47}, {e:47}] }
  914. , { arr: 'something else' }
  915. , { arr: ['something else'] }
  916. , { arr: [weird] }
  917. , { arr: [5] }
  918. ]
  919. , o.arr);
  920. done();
  921. })
  922. it('with arrays', function(done){
  923. mpath.set('arr.arr.e', [[1,2,3],[4,5],null,[],[6], [7,8,9]], o, function (v) {
  924. return 10;
  925. });
  926. var weird = [];
  927. weird.e = 10;
  928. assert.deepEqual([
  929. { yep: [15], arr: [
  930. { a: [4,8,15,16,23,42,108,null], e: 10}
  931. , { a: { c: 48, b: [1], '7': 46 }, e: 10}
  932. , { d: 'yep', e: 10 }
  933. ]
  934. }
  935. , { yep: 0, arr: [{e: 10}, {e:10}] }
  936. , { arr: 'something else' }
  937. , { arr: ['something else'] }
  938. , { arr: [weird] }
  939. , { arr: [5] }
  940. ]
  941. , o.arr);
  942. mpath.set('arr.arr.e', [[1,2,3],[4,5],null,[],[6], [7,8,9]], o);
  943. weird.e = 6;
  944. assert.deepEqual([
  945. { yep: [15], arr: [
  946. { a: [4,8,15,16,23,42,108,null], e: 1}
  947. , { a: { c: 48, b: [1], '7': 46 }, e: 2}
  948. , { d: 'yep', e: 3 }
  949. ]
  950. }
  951. , { yep: 0, arr: [{e: 4}, {e:5}] }
  952. , { arr: 'something else' }
  953. , { arr: ['something else'] }
  954. , { arr: [weird] }
  955. , { arr: [5] }
  956. ]
  957. , o.arr);
  958. done();
  959. })
  960. })
  961. })
  962. describe('with `special`', function(){
  963. var o = doc();
  964. it('works', function(done){
  965. mpath.set('name', 'chan', o, special, function (v) {
  966. return 'hi';
  967. });
  968. assert.deepEqual('hi', o.name);
  969. mpath.set('name', 'changer', o, special);
  970. assert.deepEqual('changer', o.name);
  971. mpath.set('first.second.third', [1,{name:'y'},9], o, special);
  972. assert.deepEqual([1,{name:'y'},9], o.first.second.third);
  973. mpath.set('first.second.third.1.name', 'z', o, special)
  974. assert.deepEqual([1,{name:'z'},9], o.first.second.third);
  975. mpath.set('comments.1.name', 'ttwwoo', o, special);
  976. assert.deepEqual({ name: 'two', _doc: { name: 'ttwwoo' }}, o.comments[1]);
  977. mpath.set('comments.2.comments.1.comments.0.expander', 'adder', o, special, function (v) {
  978. return 'super'
  979. });
  980. assert.deepEqual(
  981. { val: 2, expander: 'super'}
  982. , o.comments[2]._doc.comments[1]._doc.comments[0]);
  983. mpath.set('comments.2.comments.1.comments.0.expander', 'adder', o, special);
  984. assert.deepEqual(
  985. { val: 2, expander: 'adder'}
  986. , o.comments[2]._doc.comments[1]._doc.comments[0]);
  987. mpath.set('comments.2.comments.1.comments.2', 'set', o, special);
  988. assert.equal(3, o.comments[2]._doc.comments[1]._doc.comments.length);
  989. assert.deepEqual(
  990. { val: 2, expander: 'adder'}
  991. , o.comments[2]._doc.comments[1]._doc.comments[0]);
  992. assert.deepEqual(
  993. undefined
  994. , o.comments[2]._doc.comments[1]._doc.comments[1]);
  995. assert.deepEqual(
  996. 'set'
  997. , o.comments[2]._doc.comments[1]._doc.comments[2]);
  998. done();
  999. })
  1000. describe('array.path', function(){
  1001. describe('with single non-array value', function(){
  1002. it('works', function(done){
  1003. o.arr[1]._doc = { special: true }
  1004. mpath.set('arr.yep', false, o, special, function (v) {
  1005. return 'yes';
  1006. });
  1007. assert.deepEqual([
  1008. { yep: 'yes', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1009. , { yep: true, _doc: { special: true, yep: 'yes'}}
  1010. ], o.arr);
  1011. mpath.set('arr.yep', false, o, special);
  1012. assert.deepEqual([
  1013. { yep: false, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1014. , { yep: true, _doc: { special: true, yep: false }}
  1015. ], o.arr);
  1016. done();
  1017. })
  1018. })
  1019. describe('with array of values', function(){
  1020. it('that are equal in length', function(done){
  1021. mpath.set('arr.yep', ['one',2], o, special, function (v) {
  1022. return 2 === v ? 20 : v;
  1023. });
  1024. assert.deepEqual([
  1025. { yep: 'one', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1026. , { yep: true, _doc: { special: true, yep: 20}}
  1027. ], o.arr);
  1028. mpath.set('arr.yep', ['one',2], o, special);
  1029. assert.deepEqual([
  1030. { yep: 'one', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1031. , { yep: true, _doc: { special: true, yep: 2}}
  1032. ], o.arr);
  1033. done();
  1034. })
  1035. it('that is less than length', function(done){
  1036. mpath.set('arr.yep', [47], o, special, function (v) {
  1037. return 80;
  1038. });
  1039. assert.deepEqual([
  1040. { yep: 80, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1041. , { yep: true, _doc: { special: true, yep: 2}}
  1042. ], o.arr);
  1043. mpath.set('arr.yep', [47], o, special);
  1044. assert.deepEqual([
  1045. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1046. , { yep: true, _doc: { special: true, yep: 2}}
  1047. ], o.arr);
  1048. // add _doc to first element
  1049. o.arr[0]._doc = { yep: 46, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] }
  1050. mpath.set('arr.yep', [20], o, special);
  1051. assert.deepEqual([
  1052. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }], _doc: { yep: 20, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1053. , { yep: true, _doc: { special: true, yep: 2}}
  1054. ], o.arr);
  1055. done();
  1056. })
  1057. it('that is greater than length', function(done){
  1058. mpath.set('arr.yep', [5,6,7], o, special, function () {
  1059. return 'x';
  1060. });
  1061. assert.deepEqual([
  1062. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }], _doc: { yep: 'x', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1063. , { yep: true, _doc: { special: true, yep: 'x'}}
  1064. ], o.arr);
  1065. mpath.set('arr.yep', [5,6,7], o, special);
  1066. assert.deepEqual([
  1067. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }], _doc: { yep: 5, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1068. , { yep: true, _doc: { special: true, yep: 6}}
  1069. ], o.arr);
  1070. done();
  1071. })
  1072. })
  1073. })
  1074. describe('array.$.path', function(){
  1075. describe('with single non-array value', function(){
  1076. it('copies the value to each item in array', function(done){
  1077. mpath.set('arr.$.yep', {xtra: 'double good'}, o, special, function (v) {
  1078. return 9;
  1079. });
  1080. assert.deepEqual([
  1081. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1082. , _doc: { yep: 9, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1083. , { yep: true, _doc: { special: true, yep: 9}}
  1084. ], o.arr);
  1085. mpath.set('arr.$.yep', {xtra: 'double good'}, o, special);
  1086. assert.deepEqual([
  1087. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1088. , _doc: { yep: {xtra:'double good'}, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1089. , { yep: true, _doc: { special: true, yep: {xtra:'double good'}}}
  1090. ], o.arr);
  1091. done();
  1092. })
  1093. })
  1094. describe('with array of values', function(){
  1095. it('copies the value to each item in array', function(done){
  1096. mpath.set('arr.$.yep', [15], o, special, function (v) {
  1097. return 'array'
  1098. });
  1099. assert.deepEqual([
  1100. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1101. , _doc: { yep: 'array', arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1102. , { yep: true, _doc: { special: true, yep: 'array'}}
  1103. ], o.arr);
  1104. mpath.set('arr.$.yep', [15], o, special);
  1105. assert.deepEqual([
  1106. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1107. , _doc: { yep: [15], arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1108. , { yep: true, _doc: { special: true, yep: [15]}}
  1109. ], o.arr);
  1110. done();
  1111. })
  1112. })
  1113. })
  1114. describe('array.index.path', function(){
  1115. it('works', function(done){
  1116. mpath.set('arr.1.yep', 0, o, special, function (v) {
  1117. return 1;
  1118. });
  1119. assert.deepEqual([
  1120. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1121. , _doc: { yep: [15], arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1122. , { yep: true, _doc: { special: true, yep: 1}}
  1123. ], o.arr);
  1124. mpath.set('arr.1.yep', 0, o, special);
  1125. assert.deepEqual([
  1126. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1127. , _doc: { yep: [15], arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }] } }
  1128. , { yep: true, _doc: { special: true, yep: 0}}
  1129. ], o.arr);
  1130. done();
  1131. })
  1132. })
  1133. describe('array.index.array.path', function(){
  1134. it('with single value', function(done){
  1135. mpath.set('arr.0.arr.e', 35, o, special, function (v) {
  1136. return 30
  1137. });
  1138. assert.deepEqual([
  1139. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1140. , _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 30}, { a: { c: 48 }, e: 30}, { d: 'yep', e: 30 }] } }
  1141. , { yep: true, _doc: { special: true, yep: 0}}
  1142. ], o.arr);
  1143. mpath.set('arr.0.arr.e', 35, o, special);
  1144. assert.deepEqual([
  1145. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1146. , _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 35}, { a: { c: 48 }, e: 35}, { d: 'yep', e: 35 }] } }
  1147. , { yep: true, _doc: { special: true, yep: 0}}
  1148. ], o.arr);
  1149. done();
  1150. })
  1151. it('with array', function(done){
  1152. mpath.set('arr.0.arr.e', ['a','b'], o, special, function (v) {
  1153. return 'a' === v ? 'A' : v;
  1154. });
  1155. assert.deepEqual([
  1156. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1157. , _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 'A'}, { a: { c: 48 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1158. , { yep: true, _doc: { special: true, yep: 0}}
  1159. ], o.arr);
  1160. mpath.set('arr.0.arr.e', ['a','b'], o, special);
  1161. assert.deepEqual([
  1162. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1163. , _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 'a'}, { a: { c: 48 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1164. , { yep: true, _doc: { special: true, yep: 0}}
  1165. ], o.arr);
  1166. done();
  1167. })
  1168. })
  1169. describe('array.index.array.path.path', function(){
  1170. it('with single value', function(done){
  1171. mpath.set('arr.0.arr.a.b', 36, o, special, function (v) {
  1172. return 20
  1173. });
  1174. assert.deepEqual([
  1175. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1176. , _doc: { yep: [15], arr: [{ a: { b: 20 }, e: 'a'}, { a: { c: 48, b: 20 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1177. , { yep: true, _doc: { special: true, yep: 0}}
  1178. ], o.arr);
  1179. mpath.set('arr.0.arr.a.b', 36, o, special);
  1180. assert.deepEqual([
  1181. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1182. , _doc: { yep: [15], arr: [{ a: { b: 36 }, e: 'a'}, { a: { c: 48, b: 36 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1183. , { yep: true, _doc: { special: true, yep: 0}}
  1184. ], o.arr);
  1185. done();
  1186. })
  1187. it('with array', function(done){
  1188. mpath.set('arr.0.arr.a.b', [1,2,3,4], o, special, function (v) {
  1189. return v*2;
  1190. });
  1191. assert.deepEqual([
  1192. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1193. , _doc: { yep: [15], arr: [{ a: { b: 2 }, e: 'a'}, { a: { c: 48, b: 4 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1194. , { yep: true, _doc: { special: true, yep: 0}}
  1195. ], o.arr);
  1196. mpath.set('arr.0.arr.a.b', [1,2,3,4], o, special);
  1197. assert.deepEqual([
  1198. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1199. , _doc: { yep: [15], arr: [{ a: { b: 1 }, e: 'a'}, { a: { c: 48, b: 2 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1200. , { yep: true, _doc: { special: true, yep: 0}}
  1201. ], o.arr);
  1202. done();
  1203. })
  1204. })
  1205. describe('array.index.array.$.path.path', function(){
  1206. it('with single value', function(done){
  1207. mpath.set('arr.0.arr.$.a.b', '$', o, special, function (v) {
  1208. return 'dollaz'
  1209. });
  1210. assert.deepEqual([
  1211. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1212. , _doc: { yep: [15], arr: [{ a: { b: 'dollaz' }, e: 'a'}, { a: { c: 48, b: 'dollaz' }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1213. , { yep: true, _doc: { special: true, yep: 0}}
  1214. ], o.arr);
  1215. mpath.set('arr.0.arr.$.a.b', '$', o, special);
  1216. assert.deepEqual([
  1217. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1218. , _doc: { yep: [15], arr: [{ a: { b: '$' }, e: 'a'}, { a: { c: 48, b: '$' }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1219. , { yep: true, _doc: { special: true, yep: 0}}
  1220. ], o.arr);
  1221. done();
  1222. })
  1223. it('with array', function(done){
  1224. mpath.set('arr.0.arr.$.a.b', [1], o, special, function (v) {
  1225. return {};
  1226. });
  1227. assert.deepEqual([
  1228. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1229. , _doc: { yep: [15], arr: [{ a: { b: {} }, e: 'a'}, { a: { c: 48, b: {} }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1230. , { yep: true, _doc: { special: true, yep: 0}}
  1231. ], o.arr);
  1232. mpath.set('arr.0.arr.$.a.b', [1], o, special);
  1233. assert.deepEqual([
  1234. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1235. , _doc: { yep: [15], arr: [{ a: { b: [1] }, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1236. , { yep: true, _doc: { special: true, yep: 0}}
  1237. ], o.arr);
  1238. done();
  1239. })
  1240. })
  1241. describe('array.array.index.path', function(){
  1242. it('with single value', function(done){
  1243. mpath.set('arr.arr.0.a', 'single', o, special, function (v) {
  1244. return 88;
  1245. });
  1246. assert.deepEqual([
  1247. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1248. , _doc: { yep: [15], arr: [{ a: 88, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1249. , { yep: true, _doc: { special: true, yep: 0}}
  1250. ], o.arr);
  1251. mpath.set('arr.arr.0.a', 'single', o, special);
  1252. assert.deepEqual([
  1253. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1254. , _doc: { yep: [15], arr: [{ a: 'single', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1255. , { yep: true, _doc: { special: true, yep: 0}}
  1256. ], o.arr);
  1257. done();
  1258. })
  1259. it('with array', function(done){
  1260. mpath.set('arr.arr.0.a', [4,8,15,16,23,42], o, special, function (v) {
  1261. return v*2;
  1262. });
  1263. assert.deepEqual([
  1264. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1265. , _doc: { yep: [15], arr: [{ a: 8, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1266. , { yep: true, _doc: { special: true, yep: 0}}
  1267. ], o.arr);
  1268. mpath.set('arr.arr.0.a', [4,8,15,16,23,42], o, special);
  1269. assert.deepEqual([
  1270. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1271. , _doc: { yep: [15], arr: [{ a: 4, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1272. , { yep: true, _doc: { special: true, yep: 0}}
  1273. ], o.arr);
  1274. done();
  1275. })
  1276. })
  1277. describe('array.array.$.index.path', function(){
  1278. it('with single value', function(done){
  1279. mpath.set('arr.arr.$.0.a', 'singles', o, special, function (v) {
  1280. return v.toUpperCase();
  1281. });
  1282. assert.deepEqual([
  1283. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1284. , _doc: { yep: [15], arr: [{ a: 'SINGLES', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1285. , { yep: true, _doc: { special: true, yep: 0}}
  1286. ], o.arr);
  1287. mpath.set('arr.arr.$.0.a', 'singles', o, special);
  1288. assert.deepEqual([
  1289. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1290. , _doc: { yep: [15], arr: [{ a: 'singles', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1291. , { yep: true, _doc: { special: true, yep: 0}}
  1292. ], o.arr);
  1293. mpath.set('$.arr.arr.0.a', 'single', o, special);
  1294. assert.deepEqual([
  1295. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1296. , _doc: { yep: [15], arr: [{ a: 'single', e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1297. , { yep: true, _doc: { special: true, yep: 0}}
  1298. ], o.arr);
  1299. done();
  1300. })
  1301. it('with array', function(done){
  1302. mpath.set('arr.arr.$.0.a', [4,8,15,16,23,42], o, special, function (v) {
  1303. return Array
  1304. });
  1305. assert.deepEqual([
  1306. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1307. , _doc: { yep: [15], arr: [{ a: Array, e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1308. , { yep: true, _doc: { special: true, yep: 0}}
  1309. ], o.arr);
  1310. mpath.set('arr.arr.$.0.a', [4,8,15,16,23,42], o, special);
  1311. assert.deepEqual([
  1312. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1313. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42], e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1314. , { yep: true, _doc: { special: true, yep: 0}}
  1315. ], o.arr);
  1316. mpath.set('arr.$.arr.0.a', [4,8,15,16,23,42,108], o, special);
  1317. assert.deepEqual([
  1318. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1319. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42,108], e: 'a'}, { a: { c: 48, b: [1] }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1320. , { yep: true, _doc: { special: true, yep: 0}}
  1321. ], o.arr);
  1322. done();
  1323. })
  1324. })
  1325. describe('array.array.path.index', function(){
  1326. it('with single value', function(done){
  1327. mpath.set('arr.arr.a.7', 47, o, special, function (v) {
  1328. return Object;
  1329. });
  1330. assert.deepEqual([
  1331. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1332. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,Object], e: 'a'}, { a: { c: 48, b: [1], '7': Object }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1333. , { yep: true, _doc: { special: true, yep: 0}}
  1334. ], o.arr);
  1335. mpath.set('arr.arr.a.7', 47, o, special);
  1336. assert.deepEqual([
  1337. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1338. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,47], e: 'a'}, { a: { c: 48, b: [1], '7': 47 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1339. , { yep: true, _doc: { special: true, yep: 0}}
  1340. ], o.arr);
  1341. done();
  1342. })
  1343. it('with array', function(done){
  1344. o.arr[1]._doc.arr = [{ a: [] }, { a: [] }, { a: null }];
  1345. mpath.set('arr.arr.a.7', [[null,46], [undefined, 'woot']], o, special, function (v) {
  1346. return undefined === v ? 'nope' : v;
  1347. });
  1348. var a1 = [];
  1349. var a2 = [];
  1350. a1[7] = 'nope';
  1351. a2[7] = 'woot';
  1352. assert.deepEqual([
  1353. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1354. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,null], e: 'a'}, { a: { c: 48, b: [1], '7': 46 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1355. , { yep: true, _doc: { arr: [{a:a1},{a:a2},{a:null}], special: true, yep: 0}}
  1356. ], o.arr);
  1357. mpath.set('arr.arr.a.7', [[null,46], [undefined, 'woot']], o, special);
  1358. a1[7] = undefined;
  1359. assert.deepEqual([
  1360. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1361. , _doc: { yep: [15], arr: [{ a: [4,8,15,16,23,42,108,null], e: 'a'}, { a: { c: 48, b: [1], '7': 46 }, e: 'b'}, { d: 'yep', e: 35 }] } }
  1362. , { yep: true, _doc: { arr: [{a:a1},{a:a2},{a:null}], special: true, yep: 0}}
  1363. ], o.arr);
  1364. done();
  1365. })
  1366. })
  1367. describe('handles array.array.path', function(){
  1368. it('with single', function(done){
  1369. o.arr[1]._doc.arr = [{},{}];
  1370. assert.deepEqual([{},{}], o.arr[1]._doc.arr);
  1371. o.arr.push({ _doc: { arr: 'something else' }});
  1372. o.arr.push({ _doc: { arr: ['something else'] }});
  1373. o.arr.push({ _doc: { arr: [[]] }});
  1374. o.arr.push({ _doc: { arr: [5] }});
  1375. // test
  1376. mpath.set('arr.arr.e', 47, o, special);
  1377. var weird = [];
  1378. weird.e = 47;
  1379. assert.deepEqual([
  1380. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1381. , _doc: {
  1382. yep: [15]
  1383. , arr: [
  1384. { a: [4,8,15,16,23,42,108,null], e: 47}
  1385. , { a: { c: 48, b: [1], '7': 46 }, e: 47}
  1386. , { d: 'yep', e: 47 }
  1387. ]
  1388. }
  1389. }
  1390. , { yep: true
  1391. , _doc: {
  1392. arr: [
  1393. {e:47}
  1394. , {e:47}
  1395. ]
  1396. , special: true
  1397. , yep: 0
  1398. }
  1399. }
  1400. , { _doc: { arr: 'something else' }}
  1401. , { _doc: { arr: ['something else'] }}
  1402. , { _doc: { arr: [weird] }}
  1403. , { _doc: { arr: [5] }}
  1404. ]
  1405. , o.arr);
  1406. done();
  1407. })
  1408. it('with arrays', function(done){
  1409. mpath.set('arr.arr.e', [[1,2,3],[4,5],null,[],[6], [7,8,9]], o, special);
  1410. var weird = [];
  1411. weird.e = 6;
  1412. assert.deepEqual([
  1413. { yep: 47, arr: [{ a: { b: 47 }}, { a: { c: 48 }}, { d: 'yep' }]
  1414. , _doc: {
  1415. yep: [15]
  1416. , arr: [
  1417. { a: [4,8,15,16,23,42,108,null], e: 1}
  1418. , { a: { c: 48, b: [1], '7': 46 }, e: 2}
  1419. , { d: 'yep', e: 3 }
  1420. ]
  1421. }
  1422. }
  1423. , { yep: true
  1424. , _doc: {
  1425. arr: [
  1426. {e:4}
  1427. , {e:5}
  1428. ]
  1429. , special: true
  1430. , yep: 0
  1431. }
  1432. }
  1433. , { _doc: { arr: 'something else' }}
  1434. , { _doc: { arr: ['something else'] }}
  1435. , { _doc: { arr: [weird] }}
  1436. , { _doc: { arr: [5] }}
  1437. ]
  1438. , o.arr);
  1439. done();
  1440. })
  1441. })
  1442. describe('that is a function', function(){
  1443. describe('without map', function(){
  1444. it('works on array value', function(done){
  1445. var o = { hello: { world: [{ how: 'are' }, { you: '?' }] }};
  1446. var special = function (obj, key, val) {
  1447. if (val) {
  1448. obj[key] = val;
  1449. } else {
  1450. return 'thing' == key
  1451. ? obj.world
  1452. : obj[key]
  1453. }
  1454. }
  1455. mpath.set('hello.thing.how', 'arrrr', o, special);
  1456. assert.deepEqual(o, { hello: { world: [{ how: 'arrrr' }, { you: '?', how: 'arrrr' }] }});
  1457. done();
  1458. })
  1459. it('works on non-array value', function(done){
  1460. var o = { hello: { world: { how: 'are you' }}};
  1461. var special = function (obj, key, val) {
  1462. if (val) {
  1463. obj[key] = val;
  1464. } else {
  1465. return 'thing' == key
  1466. ? obj.world
  1467. : obj[key]
  1468. }
  1469. }
  1470. mpath.set('hello.thing.how', 'RU', o, special);
  1471. assert.deepEqual(o, { hello: { world: { how: 'RU' }}});
  1472. done();
  1473. })
  1474. })
  1475. it('works with map', function(done){
  1476. var o = { hello: { world: [{ how: 'are' }, { you: '?' }] }};
  1477. var special = function (obj, key, val) {
  1478. if (val) {
  1479. obj[key] = val;
  1480. } else {
  1481. return 'thing' == key
  1482. ? obj.world
  1483. : obj[key]
  1484. }
  1485. }
  1486. var map = function (val) {
  1487. return 'convert' == val
  1488. ? 'ºº'
  1489. : val
  1490. }
  1491. mpath.set('hello.thing.how', 'convert', o, special, map);
  1492. assert.deepEqual(o, { hello: { world: [{ how: 'ºº' }, { you: '?', how: 'ºº' }] }});
  1493. done();
  1494. })
  1495. })
  1496. })
  1497. describe('get/set integration', function(){
  1498. var o = doc();
  1499. it('works', function(done){
  1500. var vals = mpath.get('array.o.array.x.b', o);
  1501. vals[0][0][2] = 10;
  1502. vals[1][0][1] = 0;
  1503. vals[1][1] = 'Rambaldi';
  1504. vals[1][2] = [12,14];
  1505. vals[2] = [{changed:true}, [null, ['changed','to','array']]];
  1506. mpath.set('array.o.array.x.b', vals, o);
  1507. var t = [
  1508. { o: { array: [{x: {b: [4,6,10]}}, { y: 10} ] }}
  1509. , { o: { array: [{x: {b: [1,0,3]}}, { x: {b:'Rambaldi',z: 10 }}, { x: {b: [12,14]}}] }}
  1510. , { o: { array: [{x: {b: {changed:true}}}, { x: { b: [null, ['changed','to','array']]}}]}}
  1511. , { o: { array: [{x: null }] }}
  1512. , { o: { array: [{y: 3 }] }}
  1513. , { o: { array: [3, 0, null] }}
  1514. , { o: { name: 'ha' }}
  1515. ];
  1516. assert.deepEqual(t, o.array);
  1517. done();
  1518. });
  1519. it('array.prop', function(done){
  1520. mpath.set('comments.name', ['this', 'was', 'changed'], o);
  1521. assert.deepEqual([
  1522. { name: 'this' }
  1523. , { name: 'was', _doc: { name: '2' }}
  1524. , { name: 'changed'
  1525. , comments: [{},{ comments: [{val: 'twoo'}]}]
  1526. , _doc: { name: '3', comments: [{},{ _doc: { comments: [{ val: 2 }] }}] }}
  1527. ], o.comments);
  1528. mpath.set('comments.name', ['also', 'changed', 'this'], o, special);
  1529. assert.deepEqual([
  1530. { name: 'also' }
  1531. , { name: 'was', _doc: { name: 'changed' }}
  1532. , { name: 'changed'
  1533. , comments: [{},{ comments: [{val: 'twoo'}]}]
  1534. , _doc: { name: 'this', comments: [{},{ _doc: { comments: [{ val: 2 }] }}] }}
  1535. ], o.comments);
  1536. done();
  1537. });
  1538. it('nested array', function(done) {
  1539. const obj = { arr: [[{ test: 41 }]] };
  1540. mpath.set('arr.test', [[42]], obj);
  1541. assert.deepEqual(obj.arr, [[{ test: 42 }]]);
  1542. done();
  1543. });
  1544. });
  1545. describe('multiple $ use', function(){
  1546. var o = doc();
  1547. it('is ok', function(done){
  1548. assert.doesNotThrow(function () {
  1549. mpath.set('arr.$.arr.$.a', 35, o);
  1550. });
  1551. done();
  1552. });
  1553. });
  1554. it('has', function(done) {
  1555. assert.ok(mpath.has('a', { a: 1 }));
  1556. assert.ok(mpath.has('a', { a: undefined }));
  1557. assert.ok(!mpath.has('a', {}));
  1558. assert.ok(!mpath.has('a', null));
  1559. assert.ok(mpath.has('a.b', { a: { b: 1 } }));
  1560. assert.ok(mpath.has('a.b', { a: { b: undefined } }));
  1561. assert.ok(!mpath.has('a.b', { a: 1 }));
  1562. assert.ok(!mpath.has('a.b', { a: null }));
  1563. done();
  1564. });
  1565. it('underneath a map', function(done) {
  1566. if (!global.Map) {
  1567. done();
  1568. return;
  1569. }
  1570. assert.equal(mpath.get('a.b', { a: new Map([['b', 1]]) }), 1);
  1571. var m = new Map([['b', 1]]);
  1572. var obj = { a: m };
  1573. mpath.set('a.c', 2, obj);
  1574. assert.equal(m.get('c'), 2);
  1575. done();
  1576. });
  1577. it('unset', function(done) {
  1578. var o = { a: 1 };
  1579. mpath.unset('a', o);
  1580. assert.deepEqual(o, {});
  1581. o = { a: { b: 1 } };
  1582. mpath.unset('a.b', o);
  1583. assert.deepEqual(o, { a: {} });
  1584. o = { a: null };
  1585. mpath.unset('a.b', o);
  1586. assert.deepEqual(o, { a: null });
  1587. done();
  1588. });
  1589. it('unset with __proto__', function(done) {
  1590. // Should refuse to set __proto__
  1591. function Clazz() {}
  1592. Clazz.prototype.foobar = true;
  1593. mpath.unset('__proto__.foobar', new Clazz());
  1594. assert.ok(Clazz.prototype.foobar);
  1595. mpath.unset('constructor.prototype.foobar', new Clazz());
  1596. assert.ok(Clazz.prototype.foobar);
  1597. done();
  1598. });
  1599. it('get() underneath subclassed array', function(done) {
  1600. class MyArray extends Array {}
  1601. const obj = {
  1602. arr: new MyArray()
  1603. };
  1604. obj.arr.push({ test: 2 });
  1605. const arr = mpath.get('arr.test', obj);
  1606. assert.equal(arr.constructor.name, 'Array');
  1607. assert.ok(!(arr instanceof MyArray));
  1608. done();
  1609. });
  1610. it('ignores setting a nested path that doesnt exist', function(done){
  1611. var o = doc();
  1612. assert.doesNotThrow(function(){
  1613. mpath.set('thing.that.is.new', 10, o);
  1614. })
  1615. done();
  1616. });
  1617. });
  1618. });