From c023b206b4c9a72b935042bf61ce42067ce1a2f0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 23 Jun 2023 20:11:52 -0300 Subject: sugar: stitchArrays: accept null --- src/util/sugar.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') 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); } -- cgit 1.3.0-6-gf8a5