Skip to content

Commit

Permalink
feat: deep clone mini array
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Mar 25, 2024
1 parent d3b2c7f commit a298b95
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions lib/helpers/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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
}
}

Expand Down

0 comments on commit a298b95

Please sign in to comment.