diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-06-23 20:11:52 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-06-23 20:11:52 -0300 |
commit | c023b206b4c9a72b935042bf61ce42067ce1a2f0 (patch) | |
tree | 2b8743cfccaa8f770e7248b5fea1fe89d06a4bf2 | |
parent | c8332bb4e9b719eae0bc8f758ab1835ea1b4d0e8 (diff) |
sugar: stitchArrays: accept null
-rw-r--r-- | src/util/sugar.js | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js index 5f54c3b9..11ff7f01 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -80,24 +80,27 @@ export function stitchArrays(keyToArray) { const errors = []; for (const [key, value] of Object.entries(keyToArray)) { - if (!Array.isArray(value)) { - errors.push(new TypeError(`(${key}) Expected array, got ${value}`)); - } + if (value === null) continue; + if (Array.isArray(value)) continue; + errors.push(new TypeError(`(${key}) Expected array or null, got ${value}`)); } if (!empty(errors)) { - throw new AggregateError(errors, `Expected all values to be arrays`); + throw new AggregateError(errors, `Expected arrays or null`); } const keys = Object.keys(keyToArray); - const arrays = Object.values(keyToArray); + const arrays = Object.values(keyToArray).filter(val => Array.isArray(val)); const length = Math.max(...arrays.map(({length}) => length)); const results = []; for (let i = 0; i < length; i++) { const object = {}; for (const key of keys) { - object[key] = keyToArray[key][i]; + object[key] = + (Array.isArray(keyToArray[key]) + ? keyToArray[key][i] + : null); } results.push(object); } |