diff --git a/lib/helpers/array.js b/lib/helpers/array.js index 1b14344..e902034 100644 --- a/lib/helpers/array.js +++ b/lib/helpers/array.js @@ -1,30 +1,3 @@ -// Mutates original array -function deepRemove(arr, values) { - for (let i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - deepRemove(arr[i], values) - } else if (values.includes(arr[i])) { - arr.splice(i, 1) - i-- - } - } -} - -// Non-mutating deepRemove -function deepSubtract(arr, values) { - const newArray = [] - - for (let i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - newArray.push(deepSubtract(arr[i], values)) - } else if (!values.includes(arr[i])) { - newArray.push(arr[i]) - } - } - - return newArray -} - function deepSearch(arr, queries, isSubItem = false) { const newArray = [] @@ -47,13 +20,8 @@ function deepSearch(arr, queries, isSubItem = false) { return newArray } -function flattenArgs(args) { - return args - .map((arg) => (Array.isArray(arg) ? arg.flat(Infinity) : [arg])) - .flat() -} - function deepEquality(arr1, arr2) { + if (!Array.isArray(arr1) || !Array.isArray(arr2)) return false if (arr1.length !== arr2.length) return false for (let i = 0; i < arr1.length; i++) { @@ -65,11 +33,14 @@ function deepEquality(arr1, arr2) { return true } -function getNextArrayItem(arr, targetArray, nextIndexCallback) { - const index = arr.findIndex( - (subArray) => Array.isArray(subArray) && deepEquality(subArray, targetArray) +function getArrayItemIndex(arr, targetItem) { + return arr.findIndex( + (item) => Array.isArray(item) ? deepEquality(item, targetItem) : item === targetItem ) +} +function getNextArrayItem(arr, targetArray, nextIndexCallback) { + const index = getArrayItemIndex(arr, targetArray) if (index === -1) return null const nextIndex = nextIndexCallback(index, arr.length) @@ -184,23 +155,38 @@ export class MiniArray extends Array { } toggle(...args) { - const values = flattenArgs(args) - const flattenArray = this.deepFlat() - const toAddValues = values.filter((value) => !flattenArray.includes(value)) + const toAddValues = [] + + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + const index = getArrayItemIndex(this, arg) - deepRemove(this, values) - this.push(...toAddValues) + if (index === -1) + toAddValues.push(arg) + else + this.splice(index, 1); + } + + this.push(...new MiniArray(...toAddValues)) return this } add(...args) { - this.push(...args) + this.push(...new MiniArray(...args)) return this } remove(...args) { - deepRemove(this, flattenArgs(args)) + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + const index = getArrayItemIndex(this, arg) + + if (index === -1) continue + + this.splice(index, 1); + } + return this } @@ -210,7 +196,16 @@ export class MiniArray extends Array { } subtract(...args) { - return new MiniArray(...deepSubtract(this, flattenArgs(args))) + const newArray = [] + + for (let i = 0; i < this.length; i++) { + const item = this[i] + const index = getArrayItemIndex(args, item) + + if (index === -1) newArray.push(item) + } + + return new MiniArray(...newArray) } search(...args) {