Adrià Vilanova MartÃnez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 1 | /* eslint-disable no-sparse-arrays */ |
| 2 | import * as protojs from './protojs.js'; |
| 3 | |
| 4 | const objectLike1 = { |
| 5 | '2': [1, 2, 3], |
| 6 | }; |
| 7 | |
| 8 | const arrayLike1 = [ |
| 9 | undefined, |
| 10 | [1, 2, 3], |
| 11 | ]; |
| 12 | |
| 13 | const rObjectLike1 = [ |
| 14 | , |
| 15 | undefined, |
| 16 | [, 1, 2, 3], |
| 17 | ]; |
| 18 | |
| 19 | const objectLike2 = { |
| 20 | '1': { |
| 21 | '1': 12, |
| 22 | '2': [ |
| 23 | { |
| 24 | '6': [false, false, false], |
| 25 | '7': { |
| 26 | '1': false, |
| 27 | '2': 'Hola', |
| 28 | }, |
| 29 | }, |
| 30 | { |
| 31 | '6': [true, true, false], |
| 32 | '7': { |
| 33 | '1': false, |
| 34 | '2': 'Test', |
| 35 | }, |
| 36 | }, |
| 37 | { |
| 38 | '6': [], |
| 39 | '7': { |
| 40 | '1': true, |
| 41 | '2': 'Bye', |
| 42 | }, |
| 43 | }, |
| 44 | ], |
| 45 | '3': 1663, |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | const arrayLike2 = [[ |
| 50 | 12, |
| 51 | [ |
| 52 | [ |
| 53 | undefined, undefined, undefined, undefined, undefined, |
| 54 | [false, false, false], |
| 55 | [ |
| 56 | false, |
| 57 | 'Hola', |
| 58 | ] |
| 59 | ], |
| 60 | [ |
| 61 | undefined, undefined, undefined, undefined, undefined, |
| 62 | [true, true, false], |
| 63 | [ |
| 64 | false, |
| 65 | 'Test', |
| 66 | ] |
| 67 | ], |
| 68 | [ |
| 69 | undefined, undefined, undefined, undefined, undefined, [], |
| 70 | [ |
| 71 | true, |
| 72 | 'Bye', |
| 73 | ] |
| 74 | ], |
| 75 | ], |
| 76 | 1663, |
| 77 | ]]; |
| 78 | |
| 79 | const rObjectLike2 = [, [ |
| 80 | , |
| 81 | 12, |
| 82 | [ |
| 83 | , |
| 84 | [ |
| 85 | , undefined, undefined, undefined, undefined, undefined, |
| 86 | [, false, false, false], |
| 87 | [ |
| 88 | , |
| 89 | false, |
| 90 | 'Hola', |
| 91 | ] |
| 92 | ], |
| 93 | [ |
| 94 | , undefined, undefined, undefined, undefined, undefined, |
| 95 | [, true, true, false], |
| 96 | [ |
| 97 | , |
| 98 | false, |
| 99 | 'Test', |
| 100 | ] |
| 101 | ], |
| 102 | [ |
| 103 | , undefined, undefined, undefined, undefined, undefined, [], |
| 104 | [ |
| 105 | , |
| 106 | true, |
| 107 | 'Bye', |
| 108 | ] |
| 109 | ], |
| 110 | ], |
| 111 | 1663, |
| 112 | ]]; |
| 113 | |
| 114 | test('can convert object-like to array-like', () => { |
| 115 | // [ object-like input, array-like verified output ] |
| 116 | const entries = [ |
| 117 | [objectLike1, arrayLike1], |
| 118 | [rObjectLike1, arrayLike1], |
| 119 | [objectLike2, arrayLike2], |
| 120 | [rObjectLike2, arrayLike2], |
| 121 | ]; |
| 122 | |
| 123 | entries.forEach(([input, verifiedOutput]) => { |
| 124 | const converted = protojs.inverseCorrectArrayKeys(input); |
| 125 | expect(converted).toStrictEqual(verifiedOutput); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | test('can convert array-like to object-like', () => { |
| 130 | // [ array-like input, object-like verified output ] |
| 131 | const entries = [ |
| 132 | [arrayLike1, rObjectLike1], |
| 133 | [arrayLike2, rObjectLike2], |
| 134 | ]; |
| 135 | |
| 136 | entries.forEach(([input, verifiedOutput]) => { |
| 137 | const converted = protojs.correctArrayKeys(input); |
| 138 | expect(converted).toStrictEqual(verifiedOutput); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | test('empty object can be converted to array-like form', () => { |
| 143 | const object = { |
| 144 | 1: {}, |
| 145 | }; |
| 146 | const converted = protojs.inverseCorrectArrayKeys(object); |
| 147 | expect(converted).toStrictEqual([[]]); |
| 148 | }); |