Min-dash: Minimal Utility Toolbelt
Min-dash is a lean, battle-tested utility belt providing a curated selection of essential functions for JavaScript and TypeScript development, particularly within the bpmn.io ecosystem. It differentiates itself by its minimal bundle footprint (under 2 kB gzipped), ES2015 compatibility, and performance-optimized utilities for common operations like sorting and unions. The library ships with comprehensive TypeScript type definitions, ensuring a robust development experience. The current stable version is `5.0.0`, published in February 2024. The project maintains an active release cadence, addressing bug fixes and improvements in minor versions, with major releases introducing significant architectural shifts, such as the transition to an ESM-only distribution. It encourages the use of modern module bundlers for optimal tree-shaking.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an environment where min-dash v5.0.0+ is loaded as an ES Module.fixSwitch to ESM `import` statements: `import { utility } from 'min-dash';`. -
SyntaxError: Named export 'find' not found. The requested module 'min-dash' does not provide an export named 'find'.
cause This error occurs if your project is configured as CommonJS, but attempts to import `min-dash@^5.0.0` using ESM `import` syntax, or if a bundler isn't correctly resolving ESM exports for a CJS project.fixEnsure your project is configured for ESM (`"type": "module"` in `package.json`) when using `min-dash@^5.0.0`. Alternatively, if you must use CommonJS, downgrade to `min-dash@^4.0.0`. -
TypeError: 'this' is undefined or TypeError: Cannot read properties of undefined (reading 'property')
cause This can occur when passing `null` or `undefined` as a target object to `assign` or other object manipulation functions, especially if not explicitly handling such cases or if expecting a specific object structure.fixAlways provide a valid object as the first argument to `assign` (e.g., `assign({}, source)`). Ensure inputs to other utilities are of the expected type, or add explicit checks (`if (input == null) return defaultValue;`) where necessary.
Warnings
- breaking Min-dash v5.0.0 and later are exclusively ESM (ECMAScript Modules). Projects using CommonJS (`require()`) must either migrate to ESM or stick to min-dash v4.x.x.
- gotcha When using `min-dash` with module bundlers, ensure your setup correctly applies tree-shaking to only include the specific utilities your application requires. Unoptimized bundling can lead to larger than necessary bundle sizes.
- gotcha Prior to v4.2.2, some language utilities might not gracefully handle `undefined` inputs, potentially leading to runtime errors. Specifically, `findIndex` had a type definition fix in v4.2.2 and `isNil`/`isArray` in v4.2.1.
Install
-
npm install min-dash -
yarn add min-dash -
pnpm add min-dash
Imports
- find
const { find } = require('min-dash');import { find } from 'min-dash'; - * as minDash
const minDash = require('min-dash');import * as minDash from 'min-dash';
- assign
import assign from 'min-dash/lib/assign';
import { assign } from 'min-dash';
Quickstart
import { find, sortBy, assign } from 'min-dash';
const users = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 }
];
// Find a user based on a condition
const oldestUser = find(users, user => user.age > 30);
console.log('Oldest user:', oldestUser); // Output: Oldest user: { id: 3, name: 'Charlie', age: 35 }
// Sort users by a property
const sortedUsers = sortBy(users, 'age');
console.log('Sorted users:', sortedUsers); // Output: Sorted users: [ { id: 2, name: 'Bob', age: 25 }, { id: 1, name: 'Alice', age: 30 }, { id: 3, name: 'Charlie', age: 35 } ]
// Assign properties from source objects to a target object
const defaultOptions = { theme: 'dark', logging: true };
const userOptions = { logging: false, debug: true };
const mergedOptions = assign({}, defaultOptions, userOptions);
console.log('Merged options:', mergedOptions); // Output: Merged options: { theme: 'dark', logging: false, debug: true }