@mehdiasadov/superarray
v1.0.1
Published
A better Array
Downloads
3
Readme
@mehdiasadov/superarray
An enhanced List implementation with advanced array-like functionality.
Installation
npm install @mehdiasadov/superarray
Usage
import { List } from '@mehdiasadov/superarray';
const list = new List([1, 2, 3, 4, 5]);
console.log(list.sum); // 15
API Reference
Constructor
new List<T>(initialItems?: T[] | Set<T>)
: Create a new Listnew List<T>(...initialItems: T[])
: Create a new List from individual items
Properties
length: number
: Number of elements in the Listfirst: T | undefined
: First element of the Listlast: T | undefined
: Last element of the Listrandom: T | undefined
: Random element from the Listunique: List<T>
: New List with unique elementssum: number
: Sum of all elements (for numeric Lists)average: number
: Average of all elements (for numeric Lists)median: number
: Median of all elements (for numeric Lists)mode: T | undefined
: Mode (most frequent element) of the Listrange: number
: Range (difference between max and min) for numeric ListscumulativeSum: List<number>
: Cumulative sum of the ListisSorted: boolean
: Check if the List is sorted in ascending order
Methods
push(...elements: T[]): number
: Add elements to the end of the Listinsert(element: T, index: number): void
: Insert an element at a specific indexpop(): T | undefined
: Remove and return the last elementremove(index: number): T | undefined
: Remove and return element at specific indexshift(): T | undefined
: Remove and return the first elementunshift(...elements: T[]): number
: Add elements to the beginning of the Listat(index: number | ((length: number) => number)): T | undefined
: Get element at indexjoin(separator: string, options?: { trailing?: boolean; starting?: boolean }): string
: Join elements into a stringslice(start?: number, end?: number): List<T>
: Extract a section of the Listconcat(...lists: List<T>[]): List<T>
: Concatenate Listsclone(): List<T>
: Create a shallow copy of the ListtoArray(): T[]
: Convert List to arraytoSet(): Set<T>
: Convert List to Seteach(callback: EachCallback<T, void>, options?: EachOptions): void
: Iterate over elementsmap<U>(callback: EachCallback<T, U>, options?: EachOptions): List<U>
: Create a new List with results of callbackfilter(predicate: EachCallback<T, boolean>, options?: EachOptions): List<T>
: Filter elementsfind(predicate: EachCallback<T, boolean>, options?: EachOptions): T | undefined
: Find an elementfindIndex(predicate: EachCallback<T, boolean>, options?: EachOptions): number
: Find index of an elementevery(predicate: EachCallback<T, boolean>, options?: EachOptions): boolean
: Test if all elements pass the testsome(predicate: EachCallback<T, boolean>, options?: EachOptions): boolean
: Test if any element passes the testreduce<U>(callback: (acc: U, value: T, index: number, context: EachContext<T>) => U, initialValue: U, options?: EachOptions): U
: Reduce the List to a single valuesort(directionOrCompare?: SortDirection | ((a: T, b: T) => number), method?: SortMethod): List<T>
: Sort the Listshuffle(): List<T>
: Randomly shuffle the Listchunk(size: number): List<List<T>>
: Split the List into chunksrotate(k: number): List<T>
: Rotate the List by k positionsinterleave(other: List<T>): List<T>
: Interleave with another ListgroupBy<K>(keyFn: (item: T) => K): Map<K, List<T>>
: Group elements by a key functionpartition(predicate: (item: T) => boolean): [List<T>, List<T>]
: Split the List based on a predicatezip<U>(other: List<U>): List<[T, U]>
: Combine corresponding elements from two ListsadjacentReduce<U>(reducer: (prev: T, curr: T) => U): List<U>
: Combine adjacent elementspermutations(): List<List<T>>
: Generate all permutations of the ListslidingWindow(windowSize: number): List<List<T>>
: Generate sliding windows of the Listcombinations(k: number): List<List<T>>
: Generate all combinations of k elementsdivideInto(n: number, fill?: T): List<List<T>>
: Divide the List into n sublistshistogram(bins: number = 10): Map<string, number>
: Create a histogram of the List elements
For detailed usage examples of each method, please refer to the source code or the detailed API documentation.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.