Builtins
Every method that has a matching Array.prototype is documented here. They all delegate to the underlying array and produce a new collection (for transforms) or a scalar value (for reads).
Transforms (return a new collection)
c.map<U>(cb: (item: T, index: number) => U): ImmutableCollection<U>c.filter(cb: (item: T, index: number) => boolean): ImmutableCollection<T>c.flat(depth?: number): ImmutableCollection<any>c.flatMap(cb: (item: T, index: number) => any): ImmutableCollection<any>c.takeWhile(cb): ImmutableCollection<T> // alias for filterc.removeAll(cb): ImmutableCollection<T> // alias for filter (confusing name)collect([1, 2, 3]).map(n => n * 2); // [2, 4, 6]collect([1, 2, 3]).filter(n => n > 1); // [2, 3]collect([1, [2, 3], [4]]).flat(); // [1, 2, 3, 4]collect([1, 2, 3]).flatMap(n => [n, n + 100]); // [1, 101, 2, 102, 3, 103]
removeAllis not a remove operation — it’s a filter that KEEPS the matching items. Despite the name, it’s the same asfilter. Usereject(...)for the inverse.
Reads (return a scalar)
c.reduce<Acc>(cb, initialValue?): Accc.reduceRight(cb, initialValue?): anyc.find(cb): T | undefinedc.findIndex(cb): numberc.indexOf(item, fromIndex?): numberc.lastIndexOf(item, fromIndex?): numberc.includes(item): booleanc.contains(item): boolean // aliasc.every(cb): booleanc.some(cb): booleanc.join(separator?): stringc.implode(separator?): string // aliascollect([1, 2, 3, 4]).reduce((acc, n) => acc + n, 0); // 10collect([1, 2, 3, 4]).reduce((acc, n) => acc + n); // 10 — also workscollect([1, 2, 3]).find(n => n > 1); // 2collect([1, 2, 3]).every(n => n > 0); // truecollect([1, 2, 3]).join("-"); // "1-2-3"reduce(cb) (no initialValue) preserves native Array.prototype.reduce semantics — the wrapper uses arguments.length to decide whether to forward initialValue, so items[0] is used as the accumulator when none is supplied. Calling reduce on an empty collection with no initial value still throws TypeError, matching the native behavior.
Iteration / shape
c.forEach(cb): this // executes cb for each item, returns the collectionc.each(cb): this // alias for forEachc.keys(): ImmutableCollection<number> // [0, 1, 2, ...]c.values(): ImmutableCollection<T>c.entries(): ImmutableCollection<[number, T]>c.indexes(): ImmutableCollection<number> // same as keys()c.length: number // gettercollect(["a", "b", "c"]).keys().all(); // [0, 1, 2]collect(["a", "b", "c"]).entries().all(); // [[0, "a"], [1, "b"], [2, "c"]]for-of / spread / Array.from
A collection IS an Iterable.
const c = collect([1, 2, 3]);for (const n of c) /* ... */;[...c]; // [1, 2, 3]Array.from(c); // [1, 2, 3]Conversion
c.toArray(): T[] // returns the live reference (mutating it mutates the collection)c.toArray(mapper): U[] // maps before returningc.all(): T[] // alias for toArray()c.toString(): string // Array.prototype.toString semanticsc.toJson(): string // JSON.stringify(items)c.join(sep?): string