Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @crikey/stores-selectable

@crikey/stores-selectable

Types and functions for creating Svelte compatible stores.

@crikey/stores-selectable further extends the Readable and Writable interfaces with methods for generating derived stores by referencing values within the original store.

API

Store creation functions:

Utility functions:

Installation

# pnpm
$ pnpm add @crikey/stores-selectable

# npm
$ npm add @crikey/stores-selectable

# yarn
$ yarn add @crikey/stores-selectable

Usage

  • From ./packages/stores-selectable/examples/select.test.ts#10~89

        interface Person {
    full_name: {
    first_name?: string;
    last_name?: string;
    };
    age: number;
    }

    const person$ = selectable(writable<Person>({
    age: 30,
    full_name: {
    first_name: 'John'
    }
    }));

    const age$ = person$
    .select(person => person.age)
    const full_name$ = person$
    .select(person => person.full_name);
    const first_name$ = person$
    .select(person => person.full_name.first_name); // can do a deep selection
    const last_name$ = full_name$
    .select(full_name => full_name.last_name); // can chain selections

    console.log(get(person$));
    console.log(get(age$));
    console.log(get(full_name$));
    console.log(get(first_name$));
    console.log(get(last_name$));

    // > { age: 30, full_name: { first_name: 'John' } }
    // > 30
    // > { first_name: 'John' }
    // > John
    // > undefined

    age$.update(age => age + 1);
    last_name$.set('Smith');

    console.log(get(person$));
    console.log(get(age$));
    console.log(get(full_name$));
    console.log(get(first_name$));
    console.log(get(last_name$));

    // > { age: 30, full_name: { first_name: 'John', last_name: 'Smith' } }
    // > 31
    // > { first_name: 'John', last_name: 'Smith' }
    // > John
    // > Smith

    console.log(first_name$.path);

    // ['full_name', 'first_name']

    const first_name_via_full_path$ = person$
    .select(['full_name', 'first_name']); // ≡ first_name$
    const first_name_via_path_segments$ = person$
    .select('full_name').select<string>('first_name'); // ≡ first_name$
    const full_name_via_relative_path$ = first_name$
    .select([], 1); // ≡ full_name$

    console.log(get(first_name_via_full_path$));
    console.log(get(first_name_via_path_segments$));
    console.log(get(full_name_via_relative_path$));

    // > John
    // > John
    // > { first_name: 'John', last_name: 'Smith' }

    last_name$.delete();

    console.log(get(person$));
    console.log(get(last_name$));

    // > { age: 30, full_name: { first_name: 'John' } }
    // > undefined

Index

Type aliases

PathProxy<T>: { [sym_path]: PropertyKey[] } & (T extends object ? { [ P in keyof T]: PathProxy<T[P]> } : T)

A simple proxy type which emulates the T type with an added property for storing the path

Type parameters

  • T

ReadOrWrite<T, S>: S extends Writable ? Writable : Readable

Type parameters

ResolveSelector<P>: <T, R>(selector: (root: T) => R) => P[]

Type parameters

  • P

Type declaration

    • <T, R>(selector: (root: T) => R): P[]
    • Callback used to resolve a selector into a path

      Type parameters

      • T

      • R

      Parameters

      • selector: (root: T) => R
          • (root: T): R
          • Parameters

            • root: T

            Returns R

      Returns P[]

Selectable<T, S, P>: S & SelectablePath<P> & (S extends Writable ? SelectableSelect<T, Writable, P> & (undefined extends T ? SelectableDelete : {}) : SelectableSelect<T, Readable, P>)

Extends an existing store with select semantics.

Readable stores are extended with SelectablePath and SelectableSelect.

Writable stores are additionally extended with SelectableDelete if their value can be set to undefined.

Type parameters

TraverseDelete<T, P>: (root: T, path: readonly P[]) => T

Type parameters

  • T

  • P

Type declaration

    • (root: T, path: readonly P[]): T
    • Callback used to traverse root using path and delete/set undefined the final leaf node

      Parameters

      • root: T
      • path: readonly P[]

      Returns T

TraverseGet<T, P>: (root: T, path: readonly P[]) => unknown

Type parameters

  • T

  • P

Type declaration

    • (root: T, path: readonly P[]): unknown
    • Callback used to traverse root using path and return the final node

      Parameters

      • root: T
      • path: readonly P[]

      Returns unknown

TraverseUpdate<T, P>: <U>(root: T, path: readonly P[], update: (old_value: any) => U) => T

Type parameters

  • T

  • P

Type declaration

    • <U>(root: T, path: readonly P[], update: (old_value: any) => U): T
    • Callback used to traverse root using path and update the final node using a callback

      Type parameters

      • U

      Parameters

      • root: T
      • path: readonly P[]
      • update: (old_value: any) => U
          • (old_value: any): U
          • Parameters

            • old_value: any

            Returns U

      Returns T

Variables

sym_path: typeof sym_path = ...

Symbol used as storage for finding the current proxy path

Functions

  • create_path_proxy<T>(path?: PropertyKey[]): PathProxy<T>
  • resolve_selector<T, R>(selector: (root: T) => R): PropertyKey[]
  • Returns the path generated by referencing root members and their children.

    Note that the root provided to the callback is only a proxy for finding paths and is not actually of the typescript specified type.

    Type parameters

    • T

    • R

    Parameters

    • selector: (root: T) => R

      callback which takes the root type and returns a leaf member

        • (root: T): R
        • Parameters

          • root: T

          Returns R

    Returns PropertyKey[]

    path An array of members/elements which were used to select the returned member

  • traverse_delete<T>(root: T, path: readonly PropertyKey[]): T
  • Traverses root using the given path, deleting the final leaf node if it exists. Note that type safety cannot be strictly enforced since path is dynamic.

    Type parameters

    • T

    Parameters

    • root: T

      the root node to start from

    • path: readonly PropertyKey[]

      array of members/elements

    Returns T

  • traverse_get<T>(root: T, path: readonly PropertyKey[]): any
  • Traverses root using the given path, returning the final leaf node.

    Type parameters

    • T

    Parameters

    • root: T

      the root node to start from

    • path: readonly PropertyKey[]

      array of members/elements

    Returns any

    any the final leaf node, or undefined if it is inaccessible/does not exist

  • traverse_update<T, U>(root: T, path: readonly PropertyKey[], update: (old_value: unknown) => U): T
  • Traverses root using the given path, adding nodes as necessary, replacing the final leaf node using the provided callback. When adding nodes, the type of node to be added is determined by the subsequent path segment - array for numbers and objects for strings/symbols. When accessing an array element, the array is padded with undefined elements to avoid sparse arrays. Note that type safety cannot be strictly enforced since path is dynamic.

    throws

    TypeError if an attempt is made to access a member/element of a primitive type

    throws

    TypeError if an attempt is made to access an array using something other than a number segment

    Type parameters

    • T

    • U

    Parameters

    • root: T

      the root node to start from

    • path: readonly PropertyKey[]

      array of members/elements

    • update: (old_value: unknown) => U

      callback used to replace the leaf node value

        • (old_value: unknown): U
        • Parameters

          • old_value: unknown

          Returns U

    Returns T

    any the (possibly updated) root node

Generated using TypeDoc