diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-10-03 15:21:18 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-10-03 15:21:18 -0300 |
commit | 68e482ea27367ffcf98b35b4e102681f642710a7 (patch) | |
tree | baa04e4a5cdad1e4abe835632727a7dada055497 /src/common-util/sugar.js | |
parent | 9e7807a8500a0208f8f8cbded89dfb06f1e21061 (diff) |
sugar: onlyItem
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r-- | src/common-util/sugar.js | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index e931ad59..d6ce1410 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -70,6 +70,16 @@ export function pick(array) { return array[Math.floor(Math.random() * array.length)]; } +// Gets the only item in a single-item array (strictly, length === 1). +// If the array has more than one item, or is empty, this is null. +export function onlyItem(array) { + if (array.length === 1) { + return array[0]; + } else { + return null; + } +} + // Gets the item at an index relative to another index. export function atOffset(array, index, offset, { wrap = false, |