Skip to main content
On this page

@std/collections

Overview Jump to heading

Pure functions for common tasks around collection types like arrays and objects.

Inspired by Kotlin's Collections package and Lodash.

import { intersect, sample, pick } from "@std/collections";
import { assertEquals, assertArrayIncludes } from "@std/assert";

const lisaInterests = ["Cooking", "Music", "Hiking"];
const kimInterests = ["Music", "Tennis", "Cooking"];

assertEquals(intersect(lisaInterests, kimInterests), ["Cooking", "Music"]);

assertArrayIncludes(lisaInterests, [sample(lisaInterests)]);

const cat = { name: "Lulu", age: 3, breed: "Ragdoll" };

assertEquals(pick(cat, ["name", "breed"]), { name: "Lulu", breed: "Ragdoll"});

Add to your project Jump to heading

deno add jsr:@std/collections

See all symbols in @std/collections on

What are collection types? Jump to heading

Collection types are data structures that hold multiple values, such as arrays and objects. They allow you to group related data together and perform operations on them, like filtering, transforming, and aggregating.

This package provides small, focused functions to work with these types, allowing you to compose complex operations from simple building blocks.

Why use @std/collections? Jump to heading

The collections package provides small, pure helpers (intersect, pick, groupBy, partition) to use instead of pulling a large utility library.

Examples Jump to heading

Distinct and distinctBy Jump to heading

import { distinct, distinctBy } from "@std/collections";

const tags = ["a", "b", "a", "c"];
console.log(distinct(tags)); // ["a", "b", "c"]

const people = [
  { id: 1, name: "Alice" },
  { id: 1, name: "Alice v2" },
  { id: 2, name: "Bob" },
];
console.log(distinctBy(people, (p) => p.id));
// [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]

Map records: mapValues, mapKeys Jump to heading

import { mapKeys, mapValues } from "@std/collections";

const rec = { a: 1, b: 2 };
console.log(mapValues(rec, (v) => v * 2)); // { a: 2, b: 4 }
console.log(mapKeys(rec, (k) => k.toUpperCase())); // { A: 1, B: 2 }

Deep merge nested objects Jump to heading

import { deepMerge } from "@std/collections/deep-merge";

const a = { cfg: { retries: 2, mode: "fast" } };
const b = { cfg: { retries: 3 } };
console.log(deepMerge(a, b));
// { cfg: { retries: 3, mode: "fast" } }

Sliding windows (with options) Jump to heading

import { slidingWindows } from "@std/collections";

console.log(slidingWindows([1, 2, 3, 4], 3));
// [[1,2,3],[2,3,4]]

console.log(slidingWindows([1, 2, 3, 4, 5], 3, { step: 2, partial: true }));
// [[1,2,3],[3,4,5],[5]]

Sort by derived keys Jump to heading

import { sortBy } from "@std/collections";

const items = [{ v: 2 }, { v: 5 }, { v: 1 }];
console.log(sortBy(items, (i) => i.v));
// [{ v: 1 }, { v: 2 }, { v: 5 }]
console.log(sortBy(items, (i) => i.v, { order: "desc" }));
// [{ v: 5 }, { v: 2 }, { v: 1 }]

Partition entries of an object Jump to heading

import { partitionEntries } from "@std/collections";

const user = { id: 1, name: "Sam", active: true, score: 42 };
const [numbers, rest] = partitionEntries(
  user,
  ([, v]) => typeof v === "number",
);
// numbers: { id: 1, score: 42 }
// rest: { name: "Sam", active: true }

Produce joined strings Jump to heading

import { joinToString } from "@std/collections";

console.log(
  joinToString([1, 2, 3], { prefix: "[", separator: ", ", suffix: "]" }),
);
// "[1, 2, 3]"

Tips Jump to heading

  • Functions are pure and data-first; they don’t mutate your inputs.
  • Prefer these primitives to keep dependencies light and code clear.

Did you find what you needed?

Privacy policy