commit 72eb377d18f558aac7da7ec13a07cfda9cd63f24 Author: Chris Date: Sat Sep 20 22:56:48 2025 +0000 Add json.js diff --git a/json.js.md b/json.js.md new file mode 100644 index 0000000..8dae89d --- /dev/null +++ b/json.js.md @@ -0,0 +1,31 @@ +```javascript +import { jsonQuery, jsonPatch, tokenizePath } from './json.js'; +``` + +## jsonQuery(model, path, ?default) → mixed + +Lookup a key, or an array of keys from the structure in `model`. The path uses a simplified syntax to select the nodes: + +- `.foo` - the node **foo** in the root. +- `.items[].description` - an array of the the **description** values from each of the items in the items array. +- `.users[0].username` - the username of the first item in the array users. + +```json +let model = { foo: true, bar: 42, baz: "hello" }; +let bar = jsonQuery(model, '.bar', 43); +// bar = 42 +``` + +## jsonPatch(model, path, value) → object + +Patch a key in the model with the provided value. + +```json +let model = { foo: true, bar: 42, baz: "hello" }; +let updated = jsonPatch(model, '.baz', 'hello world'); +// updated = { foo: true, bar: 42, baz: "hello world" } +``` + +## tokenizePath(path) → array + +This is used internally to parse paths, but it may be useful for some other purpose. There is no backward compatibility promise for this function however. \ No newline at end of file