DEV Community

Cover image for Introducing @small-project/collection
sebk69
sebk69

Posted on

Introducing @small-project/collection

Managing arrays and maps in JavaScript can quickly become messy, especially when you want type safety and functional APIs like map, filter, or groupBy.

That’s why I built @small-project/collection, a minimal yet powerful library that brings typed, fluent collections to JavaScript and TypeScript — similar to Laravel Collections, but fully modernized.


Installation

npm install @small-project/collection
Enter fullscreen mode Exit fullscreen mode

Then import what you need:

import { Collection, NumberCollection, StringCollection } from "@small-project/collection";
Enter fullscreen mode Exit fullscreen mode

What is a Collection?

A Collection wraps a native Map but gives you a clean, fluent, and type-safe API to manipulate data.

import { Collection } from "@small-project/collection";

const users = new Collection({
  a: { name: "Alice", age: 25 },
  b: { name: "Bob", age: 30 },
});

console.log(users.keys()); // ['a', 'b']
console.log(users.where({ age: 30 }).values()); // [{ name: 'Bob', age: 30 }]
Enter fullscreen mode Exit fullscreen mode

It supports:

  • Typed keys and values (Collection<K,V>)
  • map, filter, reduce, groupBy, sortBy
  • Conversions to arrays or plain objects
  • Deep selection via select('users.*.email')

NumberCollection — Math made simple

The NumberCollection extends Collection<number, number> with math utilities:

import { NumberCollection } from "@small-project/collection";

const numbers = new NumberCollection([10, 20, 30, 40]);

console.log(numbers.sum());  // 100
console.log(numbers.mean()); // 25
console.log(numbers.std());  // 11.18
console.log(numbers.normalize().values()); // [0, 0.33, 0.66, 1]
Enter fullscreen mode Exit fullscreen mode

Features:

  • sum(), mean(), median()
  • variance(), std()
  • abs(), multiply(), divide()
  • greaterThan(), between()
  • normalize(), uniqueValues(), sortAsc(), sortDesc()

Perfect for datasets, statistics, and numeric analysis in Node or browser.


StringCollection — manipulate text fluently

The StringCollection extends Collection<number, string> and gives you elegant text manipulation.

import { StringCollection } from "@small-project/collection";

const words = new StringCollection(["  hello ", "WORLD ", "déjà vu"]);

console.log(words.trim().capitalize().values());
// ["Hello", "World", "Déjà vu"]

console.log(words.removeAccents().toSlug().values());
// ["hello", "world", "deja-vu"]
Enter fullscreen mode Exit fullscreen mode

Features:

  • Case handling: toUpper(), toLower(), capitalize()
  • Trimming & filtering: trim(), nonEmpty(), lengthBetween()
  • Transformations: replaceAll(), prepend(), append()
  • Conversion: toSlug(), toPascalCase(), toCamelCase()
  • Utilities: join(), joinLines(), uniqueIgnoreCase()

Why another collection library?

Because none of the existing ones were:

  • Fully typed with generics
  • ES module & CommonJS compatible
  • Minimal (no dependencies)
  • Extendable with domain-specific logic (like NumberCollection)

This library is designed for TypeScript-first development and clean, functional codebases.


Documentation


GitHub & npm

Top comments (0)