« get me outta code hell

data: validateArrayItems (etc): pass through index, array - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-11-20 13:48:52 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-20 13:48:52 -0400
commit2d58b70d0bd5bbc7cdd8789332a31a220c78da01 (patch)
tree0be4d3083d05b79293a59f42fa58502f53b81cdb /src/data
parentee02bc3efebf992c47694ec4065f658473b1f904 (diff)
data: validateArrayItems (etc): pass through index, array
Diffstat (limited to 'src/data')
-rw-r--r--src/data/things/validators.js16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index e213e933..2893e7fd 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -170,9 +170,9 @@ export function is(...values) {
 }
 
 function validateArrayItemsHelper(itemValidator) {
-  return (item, index) => {
+  return (item, index, array) => {
     try {
-      const value = itemValidator(item);
+      const value = itemValidator(item, index, array);
 
       if (value !== true) {
         throw new Error(`Expected validator to return true`);
@@ -197,13 +197,15 @@ function validateArrayItemsHelper(itemValidator) {
 }
 
 export function validateArrayItems(itemValidator) {
-  const fn = validateArrayItemsHelper(itemValidator);
+  const helper = validateArrayItemsHelper(itemValidator);
 
   return (array) => {
     isArray(array);
 
-    withAggregate({message: 'Errors validating array items'}, ({wrap}) => {
-      array.forEach(wrap(fn));
+    withAggregate({message: 'Errors validating array items'}, ({call}) => {
+      for (let index = 0; index < array.length; index++) {
+        call(helper, array[index], index, array);
+      }
     });
 
     return true;
@@ -215,12 +217,12 @@ export function strictArrayOf(itemValidator) {
 }
 
 export function sparseArrayOf(itemValidator) {
-  return validateArrayItems(item => {
+  return validateArrayItems((item, index, array) => {
     if (item === false || item === null) {
       return true;
     }
 
-    return itemValidator(item);
+    return itemValidator(item, index, array);
   });
 }