From 68e482ea27367ffcf98b35b4e102681f642710a7 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 3 Oct 2025 15:21:18 -0300 Subject: sugar: onlyItem --- src/common-util/sugar.js | 10 ++++++++++ src/data/composite/things/track/withMainReleaseTrack.js | 9 +-------- 2 files changed, 11 insertions(+), 8 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, diff --git a/src/data/composite/things/track/withMainReleaseTrack.js b/src/data/composite/things/track/withMainReleaseTrack.js index fa678161..871eba7d 100644 --- a/src/data/composite/things/track/withMainReleaseTrack.js +++ b/src/data/composite/things/track/withMainReleaseTrack.js @@ -5,6 +5,7 @@ // is specified by reference and that reference doesn't resolve to anything. import {input, templateCompositeFrom} from '#composite'; +import {onlyItem} from '#sugar'; import {getKebabCase} from '#wiki-data'; import {exitWithoutDependency, withResultOfAvailabilityCheck} @@ -19,14 +20,6 @@ import { import withMainRelease from './withMainRelease.js'; -function onlyItem(array) { - if (array.length === 1) { - return array[0]; - } else { - return null; - } -} - export default templateCompositeFrom({ annotation: `withMainReleaseTrack`, -- cgit 1.3.0-6-gf8a5