A lightweight library of core TypeScript utilities.
@metreeca/core is a zero-dependency foundation for type-safe operations, data validation, and functional programming, providing utilities for type guards, safe casts, comparisons, immutability, data manipulation, and error handling.
npm install @metreeca/core
TypeScript consumers must use "moduleResolution": "bundler" (or "node16"/"nodenext") in tsconfig.json. The
legacy "node" resolver is not supported.
Type guards for runtime JavaScript types and protocols.
import { isDefined, isEmpty, isFunction, isPromise, isIterable } from '@metreeca/core';
if ( isDefined(value) ) { /* value is T */ }
isEmpty({}); // true
isEmpty([]); // true
isFunction(() => {}); // true
isPromise(Promise.resolve(42)); // true
isIterable([1, 2, 3]); // true
Type guards for JSON-like values and data structures.
import { isBoolean, isNumber, isString, isObject, isArray } from '@metreeca/core';
isBoolean(true); // true
isNumber(42); // true (excludes NaN, Infinity)
isString('hello'); // true
isObject({ a: 1 }); // true
isObject(new Date()); // false
isArray([1, 2, 3], isNumber); // true
isArray([1, 'two'], isNumber); // false
Safe type casts returning undefined instead of throwing.
import { asNumber, asString, asObject, asArray } from '@metreeca/core';
asNumber(42); // 42
asNumber('42'); // undefined
Deep operations on complex types.
import { equals, immutable } from '@metreeca/core';
equals({ a: [1, 2] }, { a: [1, 2] }); // true
immutable({ a: [1, 2, 3] }); // deep frozen
Throw errors in expression contexts.
import { error } from '@metreeca/core';
isValid(input) ? input : error('Invalid input');
findUser(id) ?? error(`User ${id} not found`);
This project is licensed under the Apache 2.0 License – see LICENSE file for details.