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.
# pnpm
$ pnpm add @crikey/stores-selectable
# npm
$ npm add @crikey/stores-selectable
# yarn
$ yarn add @crikey/stores-selectable
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
Callback used to resolve a selector into a path
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
.
Callback used to traverse root using path and delete/set undefined the final leaf node
Callback used to traverse root using path and return the final node
Callback used to traverse root using path and update the final node using a callback
Symbol used as storage for finding the current proxy path
the current path for the proxy
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.
callback which takes the root type and returns a leaf member
path An array of members/elements which were used to select the returned member
Create a selectable Selectable store with custom pathing semantics.
Use SelectableSelect.select to derive a new child store from the current store.
base store to add selection semantics to
path resolution and trigger semantics
Create a selectable Selectable store with standard json pathing and strict inequality trigger semantics.
Use SelectableSelect.select to derive a new child store from the current store.
path resolution and trigger semantics
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.
the root node to start from
array of members/elements
Traverses root using the given path, returning the final leaf node.
the root node to start from
array of members/elements
any the final leaf node, or undefined if it is inaccessible/does not exist
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.
the root node to start from
array of members/elements
callback used to replace the leaf node value
any the (possibly updated) root node
Generated using TypeDoc
A simple proxy type which emulates the T type with an added property for storing the path