From ba2751eae00c4bcf10403edbd549ca70dc316cab Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 21 Nov 2022 21:48:31 -0400 Subject: new "empty" sugar.js util --- src/util/sugar.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/util/sugar.js') diff --git a/src/util/sugar.js b/src/util/sugar.js index c836d0ce..24ae8639 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -30,6 +30,20 @@ export function* splitArray(array, fn) { } } +// Null-accepting function to check if an array is empty. Accepts null (and +// treats as empty) as a shorthand for "hey, check if this property is an array +// with/without stuff in it" for objects where properties that are PRESENT but +// don't currently have a VALUE are null (instead of undefined). +export function empty(arrayOrNull) { + if (arrayOrNull === null) { + return true; + } else if (Array.isArray(arrayOrNull)) { + return arrayOrNull.length === 0; + } else { + throw new Error(`Expected array or null`); + } +} + // Sums the values in an array, optionally taking a function which maps each // item to a number (handy for accessing a certain property on an array of like // objects). This also coalesces null values to zero, so if the mapping function -- cgit 1.3.0-6-gf8a5