Class DynMapReducer<K, T>

    Provides a managed Map with non-destructive reducing / filtering / sorting capabilities with subscription / Svelte store support allowing for a Map to be treated like an iterable list.

    Note:

    • The default type unknown ensures stricter type checking, preventing unintended operations on the data.
    • If the type of data is known, explicitly specify the generic type to improve clarity and maintainability:
    const mapReducer = new DynMapReducer<number, string>(
    new Map([
    [1, 'banana'],
    [2, 'apple'],
    [3, 'cherry'],
    ])
    );

    console.log([...mapReducer]); // Output: ['banana', 'apple', 'cherry']

    // Sort values alphabetically.
    mapReducer.sort.set((a, b) => a.localeCompare(b));

    console.log([...mapReducer]); // Output: ['apple', 'banana', 'cherry']

    Type Parameters

    • K = unknown

      unknown - Key type. Defaults to unknown to enforce type safety when no type is specified.

    • T = unknown

      unknown - Type of data. Defaults to unknown to enforce type safety when no type is specified.

    Constructors

    • Initializes DynMapReducer. Any iterable is supported for initial data. Take note that if data is an array it will be used as the host array and not copied. All non-array iterables otherwise create a new array / copy.

      Type Parameters

      • K = unknown

        unknown - Key type.

      • T = unknown

        unknown - Type of data.

      Parameters

      • Optionaldata: Map<K, T> | MapReducer<K, T>

        Data iterable to store if array or copy otherwise.

      Returns DynMapReducer<K, T>

    Accessors

    • get data(): Map<K, T>
    • Returns the internal data of this instance. Be careful!

      Note: When a map is set as data then that map is used as the internal data. If any changes are performed to the data externally do invoke update via DynMapReducer.index with true to recalculate the index and notify all subscribers.

      Returns Map<K, T>

      The internal data.

    • get length(): number
    • Returns number

      Returns the main data items or indexed items length.

    • get reversed(): boolean
    • Returns boolean

      Returns current reversed state.

    • set reversed(reversed: boolean): void
    • Sets reversed state and notifies subscribers.

      Parameters

      • reversed: boolean

        New reversed state.

      Returns void

    Methods

    • Removes all derived reducers, subscriptions, and cleans up all resources.

      Returns void

    • Protected

      Provides a callback for custom reducers to initialize any data / custom configuration. Depending on the consumer of dynamic-reducer this may be utilized allowing child classes to avoid implementing the constructor.

      Parameters

      Returns void

    • Removes internal data and pushes new data. This does not destroy any initial array set to internal data unless replace is set to true.

      Parameters

      • data: Map<K, T>

        New data to set to internal data.

      • Optionalreplace: boolean = false

        New data to set to internal data.

      Returns void

    • Add a subscriber to this DynMapReducer instance.

      Parameters

      • handler: (value: this) => void

        Callback function that is invoked on update / changes. Receives this reference.

      Returns () => void

      Unsubscribe function.