Provides a managed array with non-destructive reducing / filtering / sorting capabilities with subscription / Svelte store support.

Note: In constructing a DynArrayReducer instance that arrays are treated as a special case. When an array is passed in as data in the constructor it will be used as the host array and not copied. All non-array iterables otherwise create a new array / copy.

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:
// Using external array data as reducer host data.
const data = ['a', 'b', 'c'];
const reducer = new DynArrayReducer<string>(data);

// Add new data externally.
data.push('d');

// Update the index.
reducer.index.update();
// Explicit type specification.
const reducer = new DynArrayReducer<string>(['a', 'b', 'c']);
// Using the default type.
const reducer = new DynArrayReducer(); // Defaults to DynArrayReducer<unknown>

Type Parameters

  • T = unknown

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

Hierarchy

Constructors

  • Initializes DynArrayReducer. 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

    • T = unknown

      unknown - Type of data.

    Parameters

    Returns DynArrayReducer<T>

Accessors

  • get data(): T[]
  • Returns the internal data of this instance. Be careful!

    Note: if an array is set as initial data then that array is used as the internal data. If any changes are performed to the data externally do invoke update via DynArrayReducer.index with true to recalculate the index and notify all subscribers.

    Returns T[]

    The internal data.

  • get destroyed(): boolean
  • Returns boolean

    Returns whether this instance is destroyed.

  • get index(): Index<number>
  • Returns Index<number>

    Returns the Indexer public API; is also iterable.

  • 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: T[] | Iterable<T>

      New data to set to internal data.

    • Optionalreplace: boolean

      New data to set to internal data.

    Returns void

  • Add a subscriber to this DynArrayReducer instance.

    Parameters

    • handler: (value: this) => void

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

    Returns () => void

    Unsubscribe function.