Skip to content

Definition

Create a map of key/value pairs used to define definitions and their values.

Instantiate

ts
function useDefinitionMap<T extends [string, unknown][]>(items: T): UseDefinitionMap<InferType<T>>;
function useDefinitionMap<T>(items: Record<string, T>): UseDefinitionMap<T>;

Returns

ts
type UseDefinitionMap<T> = {
    /**
     * A map of key/value pairs.
     */
    map: Map<string, T>;
    /**
     * Define a new definition.
     */
    define(key: string, value: T): void;
    define(key: [string, T][]): void;
    define(key: Record<string, T>): void;
    define(key: string | [string, T][] | Record<string, T>, value?: T): void;
    /**
     * Removes a definition.
     */
    unset(keys: string): void;
    unset(keys: string[]): void;
    unset(keys: string | string[]): void;
};

Usage

ts
import { useDefinition } from 'flipclock';

const { define, unset } = useDefinition([
    ['January', 'Enero'],
    ['February', 'Febrero'],
]);

define('March', 'Marzo');

define({
    April: 'Abril',
    May: 'Mayo'
});

unset('January');
unset(['February', 'March']);

TIP

These are just a few examples and far from complete. If you want to see a feature-complete example, check tests/helpers/dictionary.test.ts in the repo.

Released under the MIT License.