From a298b955bbbf15deff2fbce07db6166f434efa26 Mon Sep 17 00:00:00 2001 From: Joeylene <23741509+jorenrui@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:16:50 +0800 Subject: [PATCH] feat: deep clone mini array --- lib/helpers/array.js | 75 +++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/lib/helpers/array.js b/lib/helpers/array.js index 7fc7846..30379bd 100644 --- a/lib/helpers/array.js +++ b/lib/helpers/array.js @@ -32,17 +32,14 @@ function deepSearch(arr, queries, isSubItem = false) { if (Array.isArray(arr[i])) { const subArray = deepSearch(arr[i], queries, true) - if (subArray.length > 0) - newArray.push(subArray) + if (subArray.length > 0) newArray.push(subArray) } else { const lowercaseItem = arr[i].toString().trim().toLowerCase() const matches = queries.some((query) => lowercaseItem.includes(query)) if (matches) { - if (isSubItem) - return arr - else - newArray.push(arr[i]) + if (isSubItem) return arr + else newArray.push(arr[i]) } } } @@ -68,18 +65,17 @@ function deepEquality(arr1, arr2) { return true } -function getNextIndex(arr, targetArray, nextIndexCallback) { - const index = arr.findIndex(subArray => - Array.isArray(subArray) && deepEquality(subArray, targetArray) - ); +function getNextArrayItem(arr, targetArray, nextIndexCallback) { + const index = arr.findIndex( + (subArray) => Array.isArray(subArray) && deepEquality(subArray, targetArray) + ) + + if (index === -1) return null - if (index === -1) return null; - const nextIndex = nextIndexCallback(index, arr.length) const difference = Math.abs(nextIndex - index) - if (difference === 0) - return arr[index] + if (difference === 0) return arr[index] else if (nextIndex >= arr.length) { return difference === 1 ? arr.first : arr[difference - 1] } else if (nextIndex < 0) { @@ -106,8 +102,25 @@ export class MiniArray extends Array { 'replaceAt', ] - constructor(...args) { - super(...args) + static deepConvert = (arr) => { + const newArray = new MiniArray() + + for (let i = 0; i < arr.length; i++) + if (Array.isArray(arr[i])) newArray.push(MiniArray.deepConvert(arr[i])) + else newArray.push(arr[i]) + + return newArray + } + + constructor(...arr) { + const newArray = [] + + for (let i = 0; i < arr.length; i++) + if (Array.isArray(arr[i])) + newArray.push(MiniArray.deepConvert(arr[i])) + else newArray.push(arr[i]) + + super(...newArray) } get first() { @@ -132,23 +145,41 @@ export class MiniArray extends Array { nextItem(item) { if (Array.isArray(item)) { - return getNextIndex(this, item, (index) => index + 1) + const nextItem = getNextArrayItem(this, item, (index) => index + 1) + if (Array.isArray(nextItem)) return new MiniArray(...nextItem) + return nextItem } else { const flatArray = this.deepFlat() const nextIndex = flatArray.indexOf(item) + 1 - if (nextIndex === -1) return flatArray.first - return nextIndex >= flatArray.length ? flatArray.first : flatArray.at(nextIndex) + let nextItem + + if (nextIndex === -1) nextItem = flatArray.first + else + nextItem = + nextIndex >= flatArray.length + ? flatArray.first + : flatArray.at(nextIndex) + + if (Array.isArray(nextItem)) return new MiniArray(...nextItem) + return nextItem } } previousItem(item) { if (Array.isArray(item)) { - return getNextIndex(this, item, (index) => index - 1) + return getNextArrayItem(this, item, (index) => index - 1) } else { const flatArray = this.deepFlat() const previousIndex = flatArray.indexOf(item) - 1 - if (previousIndex === -1) return flatArray.last - return previousIndex < 0 ? flatArray.last : flatArray.at(previousIndex) + let previousItem + + if (previousIndex === -1) previousItem = flatArray.last + else + previousItem = + previousIndex < 0 ? flatArray.last : flatArray.at(previousIndex) + + if (Array.isArray(previousItem)) return new MiniArray(...previousItem) + return previousItem } }