« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/sugar.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/sugar.js')
-rw-r--r--src/util/sugar.js15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index a0c1ed62..7dd173a0 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -48,15 +48,22 @@ export function empty(value) {
 
 // Repeats all the items of an array a number of times.
 export function repeat(times, array) {
-  if (typeof array === 'string') return repeat(times, [array]);
-  if (empty(array)) return [];
   if (times === 0) return [];
-  if (times === 1) return array.slice();
+  if (array === null || array === undefined) return [];
+  if (Array.isArray(array) && empty(array)) return [];
 
   const out = [];
+
   for (let n = 1; n <= times; n++) {
-    out.push(...array);
+    const value =
+      (typeof array === 'function'
+        ? array()
+        : array);
+
+    if (Array.isArray(value)) out.push(...value);
+    else out.push(value);
   }
+
   return out;
 }