Collection Utilities
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
-
TypeError: (0 , collection_utils__WEBPACK_IMPORTED_MODULE_0__.map) is not a function
cause This typically indicates that the module was imported incorrectly, or that tree-shaking removed the function because it appeared unused, or a bundler issue.fixEnsure you are using named imports: `import { map } from 'collection-utils';`. Verify your bundler (e.g., Webpack, Rollup) is configured for ES module resolution and tree-shaking correctly, especially in environments where CommonJS is prevalent. -
Property 'map' does not exist on type 'typeof import("collection-utils")'.cause This TypeScript error means that the imported `collection-utils` module, when treated as a single object (e.g., `import * as utils from 'collection-utils'; utils.map(...)`), does not expose `map` as a property directly on that root object.fixUse named imports for individual functions: `import { map } from 'collection-utils';`. The library uses named exports for its utilities.
Warnings
- gotcha Many collection utility functions, especially those interacting with objects, might operate on shallow copies or mutate inputs if not explicitly designed for immutability. Always check function documentation or test for mutation if immutability is critical.
- gotcha Performance considerations: While convenient, using utility functions on extremely large collections (thousands or millions of items) in tight loops can introduce overhead compared to raw JavaScript loops. Profile your code if performance becomes a bottleneck.
- gotcha The library is relatively minimal. Developers coming from larger utility libraries like Lodash or Ramda might expect a broader array of specialized functions (e.g., deep merge, debounce, throttle, advanced string manipulation) which are not present here.
Install
-
npm install collection-utils -
yarn add collection-utils -
pnpm add collection-utils
Imports
- map
const map = require('collection-utils').map;import { map } from 'collection-utils'; - filter
import collectionUtils from 'collection-utils'; collectionUtils.filter(...);
import { filter } from 'collection-utils'; - isEmpty
import { isEmpty } from 'collection-utils';
Quickstart
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