1111
1212A ** writable** derived store for objects and arrays!
1313
14- ### Objects are ` keyed `
15-
1614``` js
17- const name = writable ({ first: " Rich" , last: " Harris" });
18- const firstName = keyed (name , " first" );
15+ const user = writable ({ name : { first: " Rich" , last: " Harris" } });
16+ const firstName = keyed (user , " name. first" );
1917
2018$firstName = " Bryan" ;
2119
22- console .log ($name); // { first: 'Bryan', last: 'Harris' };
23- ```
24-
25- ### Arrays are ` indexed `
26-
27- ``` js
28- const history = writable ([" one" , " two" , " three" ]);
29- const previousEdit = indexed (history, 1 );
30-
31- $previousEdit = " four" ;
32-
33- console .log ($history); // ['one', 'four', 'three'];
20+ console .log ($user); // { name: { first: 'Bryan', last: 'Harris' } };
3421```
3522
3623## Installation
@@ -43,27 +30,25 @@ Since Svelte automatically bundles all required dependencies, you only need to i
4330
4431## API
4532
46- ` keyed ` takes a writable object store and a property name, while ` indexed ` takes a writable array store and an index value.
47-
48- Both return a writable store whose ** changes are reflected on the original store** .
33+ ` keyed ` takes a writable object store and a ** keypath** , and returns a writable store whose _ changes are reflected on the original store_ .
4934
5035### Nullable parents
5136
5237If the parent store is nullable, then the child store will also be nullable.
5338
54- Due to Typescript limitations, if the parent store is nullable, specify the full type for ` keyed ` and ` indexed ` .
55-
5639``` ts
57- const name = writable <Name | undefined >(undefined );
58- const firstName = keyed < Name >( name , " first" );
40+ const user = writable <User | undefined >(undefined );
41+ const firstName = keyed ( user , " name. first" ); // string | undefined
5942```
6043
6144### Nested objects
6245
63- ` keyed ` and ` indexed ` only derive one depth of properties or elements. To access nested objects, nest multiple ` keyed ` or ` indexed ` calls.
46+ To access a nested object, provide a keypath.
47+
48+ Properties are accessed with dot notation, and arrays can be indexed with bracket notation.
6449
6550``` js
66- const email = keyed (indexed ( keyed ( settings, " profiles" ), 0 ), " email" );
51+ const email = keyed (settings, " profiles[0]. email" );
6752```
6853
6954## Motivations
0 commit comments