From c0b483682f4fef86fadf4be9d65dbfc932ed7a44 Mon Sep 17 00:00:00 2001 From: Pierre-Nicolas Watin-Augouard Date: Tue, 24 Sep 2024 16:19:54 +0200 Subject: [PATCH] feat: add list class --- src/arrays/index.ts | 1 + src/arrays/list.ts | 134 ++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + 3 files changed, 136 insertions(+) create mode 100644 src/arrays/list.ts diff --git a/src/arrays/index.ts b/src/arrays/index.ts index ac1c1b7..662c1f7 100644 --- a/src/arrays/index.ts +++ b/src/arrays/index.ts @@ -17,3 +17,4 @@ export { toFilled } from './to-filled'; export { union } from './union'; export { uniq } from './uniq'; export { xor } from './xor'; +export { List } from './list'; diff --git a/src/arrays/list.ts b/src/arrays/list.ts new file mode 100644 index 0000000..a783a81 --- /dev/null +++ b/src/arrays/list.ts @@ -0,0 +1,134 @@ +import { average } from './average'; +import { chunk } from './chunk'; +import { countOccurrences } from './count-occurences'; +import { difference } from './difference'; +import { group } from './group'; +import { intersection } from './intersection'; +import { last, lastIndex } from './last'; +import { pick, sample } from './pick'; +import { randomIndex } from './random-index'; +import { remove } from './remove'; +import { select } from './select'; +import { shuffle } from './shuffle'; +import { sortNumbers } from './sort-numbers'; +import { union } from './union'; +import { uniq } from './uniq'; +import { xor } from './xor'; + +// TODO: document methods +export class List extends Array { + static fromArray(arr: S[]) { + return new List(...arr); + } + + append(item: T) { + this.push(item); + + return this; + } + + compact() { + return List.fromArray( + this.filter( + (item): item is NonNullable => item != null && item !== undefined, + ), + ); + } + + keep( + predicate: (value: T, index: number, array: T[]) => value is S, + ): List; + + keep(predicate: (value: T, index: number, array: T[]) => unknown): List; + + keep(predicate: (value: T, index: number, array: T[]) => unknown): List { + return List.fromArray(this.filter(predicate)); + } + + reject( + predicate: (value: T, index: number, array: T[]) => value is S, + ): List>; + + reject(predicate: (value: T, index: number, array: T[]) => unknown): List; + + reject(predicate: (value: T, index: number, array: T[]) => unknown): List { + return List.fromArray(this.filter((...args) => !predicate(...args))); + } + + shuffle() { + return List.fromArray(shuffle(this)); + } + + remove(value: T) { + return List.fromArray(remove(this, value)); + } + + last() { + return last(this); + } + + lastIndex() { + return lastIndex(this); + } + + pick() { + return pick(this); + } + + sample(size: number) { + return sample(this, size); + } + + uniq() { + return uniq(this); + } + + union(other: List) { + return union(this, other); + } + + intersection(...others: Array>) { + return intersection(this, ...others); + } + + xor(other: List) { + return xor(this, other); + } + + difference(...others: Array>) { + return difference(this, ...others); + } + + select( + mapper: (item: T, index: number) => K, + condition: (item: T, index: number) => boolean, + ) { + return select(this, mapper, condition); + } + + group( + getGroupId: (item: T) => Key, + ): Partial<{ [key in Key]: T[] }> { + return group(this, getGroupId); + } + + randomIndex() { + return randomIndex(this); + } + + countOccurrences(value: T) { + return countOccurrences(this, value); + } + + chunk(size: number) { + return chunk(this, size); + } + + average(this: List) { + return average(this); + } + + sortNumbers(this: List, dir: 'asc' | 'desc') { + return sortNumbers(this, dir); + } +} diff --git a/src/index.ts b/src/index.ts index 72c0f5f..37ae063 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ export { union, uniq, xor, + List, } from './arrays'; export {