Collection Utilities

1.0.1 · active · verified Sun Apr 19

The `collection-utils` package, at version `1.0.1`, provides a set of fundamental utility functions designed to simplify common operations on JavaScript collections, primarily arrays and objects. Published by `quicktype`, a project focused on generating type bindings from JSON schemas, this library likely serves as an internal dependency or a lightweight, standalone offering for basic collection manipulation. It ships with TypeScript types, facilitating type-safe usage in TypeScript projects. Given its current version, it appears stable but may have a focused, rather than expansive, feature set, with a release cadence potentially tied to the `quicktype` ecosystem's needs. Key differentiators, beyond its small footprint and TypeScript support, are not explicitly detailed in public documentation, suggesting a design for efficiency and straightforward use rather than broad, complex functionality found in larger utility libraries like Lodash.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `map`, `filter`, and `isEmpty` functions on both arrays and objects, showcasing type-safe operations.

import { map, filter, isEmpty } from 'collection-utils';

interface User { id: number; name: string; isActive: boolean; roles: string[]; }

const users: User[] = [
  { id: 1, name: 'Alice', isActive: true, roles: ['admin', 'editor'] },
  { id: 2, name: 'Bob', isActive: false, roles: ['viewer'] },
  { id: 3, name: 'Charlie', isActive: true, roles: ['editor'] },
  { id: 4, name: 'David', isActive: false, roles: [] }
];

// Use 'map' to transform a collection
const userNames = map(users, user => user.name);
console.log('User Names:', userNames); // Expected: ['Alice', 'Bob', 'Charlie', 'David']

// Use 'filter' to select elements based on a condition
const activeEditors = filter(users, user => user.isActive && user.roles.includes('editor'));
console.log('Active Editors:', activeEditors.map(u => u.name)); // Expected: ['Alice', 'Charlie']

// Use 'isEmpty' to check if a collection is empty
const hasActiveUsers = !isEmpty(filter(users, user => user.isActive));
console.log('Are there any active users?', hasActiveUsers); // Expected: true

const emptyArray: any[] = [];
console.log('Is emptyArray empty?', isEmpty(emptyArray)); // Expected: true

const emptyObject = {};
console.log('Is emptyObject empty?', isEmpty(emptyObject)); // Expected: true

view raw JSON →