@metreeca/core - v0.9.1
    Preparing search index...

    Metreeca/Core

    npm

    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.

    Installation

    npm install @metreeca/core
    
    Warning

    TypeScript consumers must use "moduleResolution": "bundler" (or "node16"/"nodenext") in tsconfig.json. The legacy "node" resolver is not supported.

    Usage

    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`);

    Support

    • open an issue to report a problem or to suggest a new feature
    • start a discussion to ask a how-to question or to share an idea

    License

    This project is licensed under the Apache 2.0 License – see LICENSE file for details.