« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util
diff options
context:
space:
mode:
Diffstat (limited to 'src/common-util')
-rw-r--r--src/common-util/sugar.js10
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,