Skip to content

Commit 8e38e48

Browse files
authored
Merge pull request #1 from bryanmylee/feat/nested-keys
Keypaths for nested keys
2 parents 7162c96 + c6d9585 commit 8e38e48

File tree

6 files changed

+267
-177
lines changed

6 files changed

+267
-177
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ module.exports = {
2222
"no-use-before-define": ["off"],
2323
"no-return-assign": ["off"],
2424
"no-param-reassign": ["off"],
25-
"no-extra-parens": ["error", "all"],
2625
},
2726
};

README.md

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,13 @@
1111

1212
A **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

5237
If 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

package-lock.json

Lines changed: 30 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-keyed",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A writable derived store for objects and arrays",
55
"main": "dist/index.js",
66
"module": "dist/index.es.js",
@@ -46,6 +46,7 @@
4646
"rollup": "^2.61.0",
4747
"ts-jest": "^27.1.1",
4848
"tslib": "^2.0.0",
49+
"type-fest": "^2.8.0",
4950
"typescript": "^4.5.2"
5051
}
5152
}

0 commit comments

Comments
 (0)