index.js 54 KB

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