Hello, if this is your first time reading the Arcaelas Insiders documentation, let me tell you that you have found a good place to learn.
Our team and community are happy to write and make methods simple to implement and understand, but I think you already know that.
The documentation for this tool is open to edits and suggestions.
Let's start with the basic implementation steps.
> npm i --save @arcaelas/collection
> yarn add --save @arcaelas/collection
// Class Import Statement
import Collection from "@arcaelas/Collection";
// Function import statement
import { Collection } from "@arcaelas/collection";
// EsModule
const Collection = require("@arcaelas/collection");
In object-oriented programming we find common situations, such as those where we want to order, filter and modify elements of a list, however the "Array Prototypes" are not very complete in some cases, for these situations the Arcaelas Insiders team has designed useful tools that allow these actions within "Collections".
As an interesting part of this tool, we have the B-JSON notation that Atlas implements in its MongoDB database engine, only some of them are implemented here, but we will explain how to extend them and create your own validators.
import Collection from "@arcaelas/collection"
const collection = new Collection([ ... ])
Filters elements by a callback or Query expression.
collection.filter((item) => item.age >= 18);
collection.filter({ age: { $gte: 18 } });
collection.filter({
name: /Alejandro/,
skills: { $contains: "Liberty" },
gender: { $not: { $in: ["animal", "fruit"] } },
work: { $not: { $in: ["work", "without", "coffee"] } },
});
Returns elements that do NOT match the expression.
collection.not({ online: false });
Returns the first or last element that matches a Query or function.
collection.first({ age: { $gte: 18 } });
collection.last((item) => item.name === "Joe");
Shorthand for querying with operator and value.
collection.where("online", false);
collection.where("age", ">=", 21);
collection.whereNot("role", "admin");
collection.whereNot("price", "<", 100);
Updates elements based on query or globally.
collection.update({ online: false }, { deletedAt: new Date() });
collection.update({ email: /gmail\.com$/ }, (item) => ({
prevEmail: item.email,
email: null,
}));
collection.update((item) => ({ name: item.name.toUpperCase() }));
Removes matched elements. Mutates collection.
collection.delete({ deletedAt: { $exists: true } });
Creates a new
Collection
instance preserving prototype.
collection.collect([ ... ]);
Debug methods to print and exit or continue.
collection.dump();
collection.dd();
Performs aggregate calculations by key or function.
collection.max("score");
collection.min("price");
collection.sum("amount");
collection.sum((item) => item.price * 1.18);
Returns or mutates with random order.
collection.random(2);
collection.shuffle();
Splits into chunks or paginates.
collection.chunk(100);
collection.paginate(1, 50);
Aggregates by key or iterator.
collection.countBy("type");
collection.countBy((item) => item.group);
collection.groupBy("status");
collection.groupBy((item) => item.role);
Iterates over elements.
return false
to break.
collection.each((item) => {
if (!item.active) return false;
});
Removes specific fields from each item.
collection.forget("password", "token");
Filters to unique values by key or callback.
collection.unique("email");
collection.unique((item) => item.id);
Adds custom methods.
Collection.macro("pluck", function (key) {
return this.map((item) => item[key]);
});
collection.pluck("name");
Sorts the collection by key, direction or comparator function.
collection.sort("age", "asc");
collection.sort("name", "desc");
collection.sort((a, b) => a.score - b.score);
Verifies that all elements satisfy the expression.
collection.every("active");
collection.every("score", ">", 0);
collection.every({ verified: true });
collection.every((item) => item.age >= 18);
Converts collection to JSON string.
collection.stringify();
collection.stringify(null, 2);
concat
,map
,pop
,slice
,splice
,shift
,unshift
are fully supported.
Collection<T, V>;
// T = item type
// V = shape for query validation
This ensures type-safe autocompletion and query shape consistency.
Alias | Mongo DSL | Meaning |
---|---|---|
= |
$eq |
equal |
!= |
$not |
not equal |
> |
$gt |
greater than |
< |
$lt |
less than |
>= |
$gte |
greater or equal |
<= |
$lte |
less or equal |
in |
$in |
included in array |
includes |
$includes |
contains substring or value |
- Underlying logic delegates to native
Array
methods. - Query filtering is optimized using precompiled functions.
- Avoid frequent mutation in large collections; prefer
.collect()
clones. - For secure randomness, override
random()
with your own comparator.
- Fork and clone this repo.
- Run
npm install
. - Run tests:
npm run test
. - Follow conventional commits.
Script | Description |
---|---|
npm run build |
Compile TS and bundle |
npm run test |
Run unit tests |
npm run lint |
Run ESLint and Prettier |
- LazyCollection using generators
- AsyncCollection support for promises/streams
- Drop lodash dependency
- YAML/CSV serialization
MIT © 2025 Miguel Alejandro (Miguel Guevara) miguel.guevara@arcaelas.com
Want to discuss any of my open source projects, or something else? Send me a direct message on Twitter.
If you already use these libraries and want to support us to continue development, you can sponsor us at Github Sponsors.