Skip to content

Commit

Permalink
refactor: rename arguments named list to array
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 2, 2024
1 parent 0358acb commit de23cb4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/array/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* // [[1, 2], [3, 4], [5, 6]]
* ```
*/
export function cluster<T>(list: readonly T[], size = 2): T[][] {
const clusterCount = Math.ceil(list.length / size)
export function cluster<T>(array: readonly T[], size = 2): T[][] {
const clusterCount = Math.ceil(array.length / size)
return new Array(clusterCount).fill(null).map((_c: null, i: number) => {
return list.slice(i * size, i * size + size)
return array.slice(i * size, i * size + size)
})
}
16 changes: 8 additions & 8 deletions src/array/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
* ```
*/
export function replace<T>(
list: readonly T[],
array: readonly T[],
newItem: T,
match: (item: T, idx: number) => boolean,
): T[] {
if (!list) {
if (!array) {
return []
}
if (newItem === undefined) {
return [...list]
return [...array]
}
for (let idx = 0; idx < list.length; idx++) {
const item = list[idx]
for (let idx = 0; idx < array.length; idx++) {
const item = array[idx]
if (match(item, idx)) {
return [
...list.slice(0, idx),
...array.slice(0, idx),
newItem,
...list.slice(idx + 1, list.length),
...array.slice(idx + 1, array.length),
]
}
}
return [...list]
return [...array]
}
18 changes: 9 additions & 9 deletions src/array/replaceOrAppend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
* ```
*/
export function replaceOrAppend<T>(
list: readonly T[],
array: readonly T[],
newItem: T,
match: (a: T, idx: number) => boolean,
): T[] {
if (!list && !newItem) {
if (!array && !newItem) {
return []
}
if (!newItem) {
return [...list]
return [...array]
}
if (!list) {
if (!array) {
return [newItem]
}
for (let idx = 0; idx < list.length; idx++) {
const item = list[idx]
for (let idx = 0; idx < array.length; idx++) {
const item = array[idx]
if (match(item, idx)) {
return [
...list.slice(0, idx),
...array.slice(0, idx),
newItem,
...list.slice(idx + 1, list.length),
...array.slice(idx + 1, array.length),
]
}
}
return [...list, newItem]
return [...array, newItem]
}
4 changes: 2 additions & 2 deletions src/array/sift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ type Falsy = null | undefined | false | '' | 0 | 0n
* // => [1, 2, 3]
* ```
*/
export function sift<T>(list: readonly (T | Falsy)[]): T[] {
return (list?.filter(x => !!x) as T[]) ?? []
export function sift<T>(array: readonly (T | Falsy)[]): T[] {
return (array?.filter(x => !!x) as T[]) ?? []
}
16 changes: 8 additions & 8 deletions src/array/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ```
*/
export function toggle<T>(
list: readonly T[],
array: readonly T[],
item: T,
/**
* Converts an item of type T item into a value that can be checked
Expand All @@ -30,25 +30,25 @@ export function toggle<T>(
strategy?: 'prepend' | 'append'
},
): T[] {
if (!list && !item) {
if (!array && !item) {
return []
}
if (!list) {
if (!array) {
return [item]
}
if (!item) {
return [...list]
return [...array]
}
const matcher = toKey
? (x: T, idx: number) => toKey(x, idx) === toKey(item, idx)
: (x: T) => x === item
const existing = list.find(matcher)
const existing = array.find(matcher)
if (existing) {
return list.filter((x, idx) => !matcher(x, idx))
return array.filter((x, idx) => !matcher(x, idx))
}
const strategy = options?.strategy ?? 'append'
if (strategy === 'append') {
return [...list, item]
return [...array, item]
}
return [item, ...list]
return [item, ...array]
}

0 comments on commit de23cb4

Please sign in to comment.