Collection Utilities Installation
The @tolki/collection package provides a fluent Collection class inspired by Laravel's Collection. You can install it via npm, yarn, or pnpm:
npm install @tolki/collectionyarn add @tolki/collectionpnpm add @tolki/collectionCreating collections
There are three ways to create a collection, and they build the same thing. Read the items back with all() or toArray():
import { Collection, collect } from "@tolki/collection";
collect([1, 2, 3]).all();
// => [1, 2, 3]
Collection.make([1, 2, 3]).all();
// => [1, 2, 3]
new Collection([1, 2, 3]).all();
// => [1, 2, 3]collect() and Collection.make() also type results that the constructor cannot, such as the keys of a Map. Prefer one of them in TypeScript.
Lists and keyed collections
A collection holds either a list or a keyed record. An array makes a list, and a plain object or a Map makes a keyed collection:
import { collect } from "@tolki/collection";
collect(["a", "b"]).all();
// => ["a", "b"]
collect({ first: "a", second: "b" }).all();
// => { first: "a", second: "b" }In TypeScript the class is Collection<TValue, TKey, TShape>. TValue is the item type and TKey the key type. TShape is "list", "keyed" or "partial": a list, a record that holds every key its type names, or one that may lack some. It defaults from the key type, so number keys give a list and any other key gives a keyed record. You rarely write it yourself, because collect() infers it.
Methods that change the collection in place
Most methods return a new collection. The few that change the collection they are called on are listed here. Most also return it. getOrPut returns the value it found or stored, pull the item it removed, pop and shift the item or items they removed, splice the items it removed, and offsetSet and offsetUnset return nothing.
addescapeWhenCastingToStringforgetgetOrPutoffsetSetoffsetUnsetpopprependpullpushputshiftsplicetransformunshift
More documentation
- The Methods List page documents every method with a live example.
- The Differences from Laravel page lists where the class departs from Laravel's, and why.
- Methods that take a path such as
pluckread it as "dot" notation. The Dot Notation page of Arrays & Objects explains the rules.