keypath.any.js (3585B)
1 // META: global=window,worker 2 // META: title=Keypath 3 // META: script=resources/support.js 4 5 // Spec: 6 // http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#key-path-construct 7 8 'use strict'; 9 10 let global_db = createdb_for_multiple_tests(); 11 12 function keypath(keypath, objects, expected_keys, desc) { 13 let db; 14 let t = async_test(self.title + ' - ' + (desc ? desc : keypath)); 15 let store_name = 'store-' + (Date.now()) + Math.random(); 16 17 let open_rq = global_db.setTest(t); 18 open_rq.onupgradeneeded = function(e) { 19 db = e.target.result; 20 let objStore = db.createObjectStore(store_name, {keyPath: keypath}); 21 22 for (let i = 0; i < objects.length; i++) 23 objStore.add(objects[i]); 24 }; 25 26 open_rq.onsuccess = function(e) { 27 let actual_keys = []; 28 let rq = db.transaction(store_name).objectStore(store_name).openCursor(); 29 30 rq.onsuccess = t.step_func(function(e) { 31 let cursor = e.target.result; 32 33 if (cursor) { 34 actual_keys.push(cursor.key.valueOf()); 35 cursor.continue(); 36 } else { 37 assert_key_equals(actual_keys, expected_keys, 'keyorder array'); 38 t.done(); 39 } 40 }); 41 }; 42 } 43 44 keypath('my.key', [{my: {key: 10}}], [10]); 45 46 keypath('my.køi', [{my: {køi: 5}}], [5]); 47 48 keypath('my.key_ya', [{my: {key_ya: 10}}], [10]); 49 50 keypath('public.key$ya', [{public: {key$ya: 10}}], [10]); 51 52 keypath('true.$', [{true: {$: 10}}], [10]); 53 54 keypath('my._', [{my: {_: 10}}], [10]); 55 56 keypath('delete.a7', [{delete: {a7: 10}}], [10]); 57 58 keypath( 59 'p.p.p.p.p.p.p.p.p.p.p.p.p.p', 60 [{p: {p: {p: {p: {p: {p: {p: {p: {p: {p: {p: {p: {p: {p: 10}}}}}}}}}}}}}}], 61 [10]); 62 63 keypath( 64 'str.length', [{str: 'pony'}, {str: 'my'}, {str: 'little'}, {str: ''}], 65 [0, 2, 4, 6]); 66 67 keypath( 68 'arr.length', 69 [ 70 {arr: [0, 0, 0, 0]}, {arr: [{}, 0, 'hei', 'length', Infinity, []]}, 71 {arr: [10, 10]}, {arr: []} 72 ], 73 [0, 2, 4, 6]); 74 75 keypath('length', [[10, 10], '123', {length: 20}], [2, 3, 20]); 76 77 keypath( 78 '', [['bags'], 'bean', 10], [10, 'bean', ['bags']], 79 '\'\' uses value as key'); 80 81 keypath( 82 [''], [['bags'], 'bean', 10], [[10], ['bean'], [['bags']]], 83 '[\'\'] uses value as [key]'); 84 85 keypath( 86 ['x', 'y'], [{x: 10, y: 20}, {y: 1.337, x: 100}], [[10, 20], [100, 1.337]], 87 '[\'x\', \'y\']'); 88 89 keypath( 90 [['x'], ['y']], [{x: 10, y: 20}, {y: 1.337, x: 100}], 91 [[10, 20], [100, 1.337]], '[[\'x\'], \'y\'] (stringifies)'); 92 93 keypath( 94 [ 95 'x', { 96 toString: function() { 97 return 'y' 98 } 99 } 100 ], 101 [{x: 10, y: 20}, {y: 1.337, x: 100}], [[10, 20], [100, 1.337]], 102 '[\'x\', {toString->\'y\'}] (stringifies)'); 103 104 if (false) { 105 let myblob = Blob(['Yoda'], {type: 'suprawsum'}); 106 keypath( 107 ['length', 'type'], [myblob], [4, 'suprawsum'], 108 '[Blob.length, Blob.type]'); 109 } 110 111 // File.name and File.lastModified is not testable automatically 112 113 keypath( 114 ['name', 'type'], 115 [ 116 {name: 'orange', type: 'fruit'}, 117 {name: 'orange', type: ['telecom', 'french']} 118 ], 119 [['orange', 'fruit'], ['orange', ['telecom', 'french']]]); 120 121 keypath( 122 ['name', 'type.name'], 123 [ 124 {name: 'orange', type: {name: 'fruit'}}, 125 {name: 'orange', type: {name: 'telecom'}} 126 ], 127 [['orange', 'fruit'], ['orange', 'telecom']]); 128 129 keypath( 130 ['type'], 131 [{name: 'orange', type: 'fruit'}, {name: 'cucumber', type: 'vegetable'}], 132 [['fruit'], ['vegetable']], 'list with 1 field'); 133 134 let loop_array = []; 135 loop_array.push(loop_array); 136 keypath( 137 loop_array, ['a', 1, ['k']], [[1], ['a'], [['k']]], 138 'array loop -> stringify becomes [\'\']');