Skip to content

Commit

Permalink
fix: do not flatten arrays for array toggle, remove, and subtract
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Mar 25, 2024
1 parent 9be8153 commit 10b496a
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions lib/helpers/array.js
Original file line number Diff line number Diff line change
@@ -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 = []

Expand All @@ -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++) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down

0 comments on commit 10b496a

Please sign in to comment.