Elegant methods for deriving sub-stores from existing stores.
Supports deriving Writable stores.
# pnpm
$ pnpm add @crikey/stores-select
# npm
$ npm add @crikey/stores-select
# yarn
$ yarn add @crikey/stores-select
import {by_key, by_property, by_size, select} from "@crikey/stores-select";
const state = writable({
user: {
id: 5,
username: 'Joe Blogs'
},
accounts: new Map([
[2, { id: 2, name: 'First National' }],
[3, { id: 3, name: 'Bank of mum and dad'}]
])
});
// Create nested derived stores to access user information
const user = select(state, by_property('user'));
const user_id = select(user, by_property('id'));
const user_username = select(user, by_property('username'));
console.log(get(user_id)); // 5
console.log(get(user_username)); // Joe Blogs
// These are stores, so everything is reactive
user_username.set('Joe Middlename Blogs');
console.log(get(user)); // { id: 5, username: 'Joe Middlename Blogs' }
// Create derived account stores
const accounts = select(state, by_property('accounts'));
const n_accounts = select(accounts, by_size());
console.log(get(n_accounts)); // 2
// Selectors can be chained to access deeply nested values
const first_national = select(state, by_property('accounts'), by_key(2));
console.log(get(first_national)); // { id: 2, name: 'First National' }
key of property to update
function which returns a default value to use if key does not exist. defaults to a function which throws a property not found
error
WriteSelector
Create a derived writable/deletable store
Note: loses type saftey
Create a derived writable store
Note: loses type saftey
Create a derived readable/deletable store
Note: loses type saftey
Create a derived readable store
Note: loses type saftey
Create a derived writable/deletable store
Note: loses type saftey
Create a derived writable store
Note: loses type saftey
Create a derived readable store
Note: loses type saftey
Create a derived writable/deletable store
Note: loses type saftey
Create a derived writable store
Note: loses type saftey
Create a derived readable store
Note: loses type saftey
Create a derived writable/deletable store
Note: loses type saftey
Create a derived writable store
Note: loses type saftey
Create a derived readable store
Note: loses type saftey
Generated using TypeDoc
Creates a selector which selects/updates a property of the input object