Immutable, optimized and optionally typed path-based object property / array accessors with structural sharing
npm install @thi.ng/paths
!npm downloads

> [!NOTE]
> This is one of 214 standalone projects, maintained as part
> of the @thi.ng/umbrella monorepo
> and anti-framework.
>
> 🚀 Please help me to work full-time on these projects by sponsoring me on
> GitHub. Thank you! ❤️
- About
- Status
- Breaking changes
- 4.0.0
- Naming convention
- Type checked accessors
- Related packages
- Installation
- Dependencies
- Usage examples
- API
- Type checked paths
- Optional property handling
- Higher-order accessors
- First order operators
- Deletions
- Prototype pollution
- Structural sharing
- Mutable setter
- Path checking
- Authors
- License
Immutable, optimized and optionally typed path-based object property / array accessors with structural sharing.
STABLE - used in production
Search or submit any issues for this package
#### Naming convention
As part of a larger effort to enforce more consistent naming conventions
across various umbrella packages, all higher-order operators in this
package are now using the def prefix: e.g. getterT() =>defGetter(), setterT() => defSetter().
#### Type checked accessors
**Type checked accessors are now the default and those functions expect
paths provided as tuples**. To continue using string based paths (e.g."a.b.c"), alternative Unsafe versions are provided. E.g. getIn()
(type checked) vs. getInUnsafe() (unchecked). Higher-order versions
also provide fallbacks (e.g. getter() => defGetterUnsafe()).
Type checking for paths is currently "only" supported for the first 8
levels of nesting. Deeper paths are supported but only partially checked
and their value type inferred as any.
- @thi.ng/atom - Mutable wrappers for nested immutable values with optional undo/redo history and transaction support
``bash`
yarn add @thi.ng/paths
ESM import:
`ts`
import * as paths from "@thi.ng/paths";
Browser ESM import:
`html`
For Node.js REPL:
`js`
const paths = await import("@thi.ng/paths");
Package sizes (brotli'd, pre-treeshake): ESM: 1.13 KB
- @thi.ng/api
- @thi.ng/checks
- @thi.ng/errors
Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
Eight projects in this repo's
/examples
directory are using this package:
| Screenshot | Description | Live demo | Source |
|:--------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------|:----------------------------------------------------------|:---------------------------------------------------------------------------------------|
| | Using hdom in an Elm-like manner | Demo | Source |
| | UI component w/ local state stored in hdom context | Demo | Source |
| | Example for themed components proposal | Demo | Source |
| | Event handling w/ interceptors and side effects | Demo | Source |
|
| Minimal demo of using rstream constructs to form an interceptor-style event loop | Demo | Source |
|
| Declarative component-based system with central rstream-based pubsub event bus | Demo | Source |
|
| Obligatory to-do list example with undo/redo | Demo | Source |
|
| Triple store query results & sortable table | Demo | Source |
As stated in the breaking changes section, since
v4.0.0 paths are now type checked by default. These new functions use
Typescript generics to validate a given path against the type structure
of the target state object. Since string paths cannot be checked, only
path tuples are supported. **Type checking & inference supports path
lengths up to 8** (i.e. levels of hierarchy) before reverting back to
any for longer/deeper paths (there's no depth limit per se).
Due to missing type information of the not-yet-known state value, using
the typed checked higher-order versions (e.g. defGetter, defSettergetIn()
etc.) is slightly more verbose compared to their immediate use,
first-order versions (e.g. , setIn() etc.), where everything
can be inferred directly. However, (re)using the HOF-constructed
accessors can be somewhat faster and more convenient... YMMV! More details below.
#### Optional property handling
When accessing data structures with optional properties, not only the
leaf value type targeted by a lookup path is important, but any
intermediate optional properties need to be considered too. Furthermore,
we need to distinguish between read (get) and write (update) use cases
for correct type inference.
For example, given these types:
`ts
type Foo1 = { a: { b: { c?: number; } } };
type Foo2 = { a?: { b: { c: number; } } };
`
For get/read purposes the inferred type for c will both be number |
undefined. Even though c in Foo2 is not marked as optional, the ac
property is optional and so attempting to lookup can yieldundefined...
For set/update/write purposes, the type for c is inferred verbatim.undefined
I.e. if a property is marked as optional, a setter will allow as new value as well.
The defGetter(), defSetter() and defUpdater() functions compile a
lookup path tuple into an optimized function, operating directly at the
value the path points to in a nested object given later. For getters,
this essentially compiles to:
`ts
import { defGetter } from "@thi.ng/paths";
defGetter(["a","b","c"]) => (obj) => obj.a.b.c;
`
...with the important difference that the function returns undefined
if any intermediate values along the lookup path are undefined (and
doesn't throw an error).
For setters / updaters, the resulting function too accepts a single
object (or array) to operate on and when called, immutably replaces
the value at the given path, i.e. it produces a selective deep copy of
obj up until given path. If any intermediate key is not present in the
given object, it creates a plain empty object for that missing key and
descends further along the path.
`ts
import { defGetter } from "@thi.ng/paths";
// define state structure (see above example)
interface State {
a: {
b?: number;
c: string[];
}
}
const state: State = { a: { b: 1, c: ["c1", "c2"] } };
// build type checked getter for b & c
const getB = defGetter
const getFirstC = defGetter
const b = getB(state); // b inferred as number | undefinedstring
const c1 = getFirstC(state); // c1 inferred as `
Paths can also be defined as dot-separated strings, however cannot be
type checked and MUST use the Unsafe version of each operation:
`ts
import { defSetterUnsafe } from "@thi.ng/paths";
s = defSetterUnsafe("a.b.c");
s({ a: { b: { c: 23 } } }, 24)
// { a: { b: { c: 24 } } }
s({ x: 23 }, 24)
// { x: 23, a: { b: { c: 24 } } }
s(null, 24)
// { a: { b: { c: 24 } } }
`
Nested value updaters follow a similar pattern, but also take a user
supplied function to apply to the existing value (incl. any other
arguments passed):
`ts
import { defUpdater } from "@thi.ng/paths";
type State = { a?: { b?: number; } };
const incAB = defUpdater
["a","b"],
// x inferred as number | undefined
(x) => x !== undefined ? x + 1 : 1
);
incAB({ a: { b: 10 } });
// { a: { b: 11 } }
incAB({});
// { a: { b: 1 } }
// with additional arguments
const add = defUpdater("a.b", (x, n) => x + n);
add({ a: { b: 10 } }, 13);
// { a: { b: 23 } }
`
In addition to these higher-order functions, the module also provides
immediate-use wrappers: getIn(), setIn(), updateIn() anddeleteIn(). These functions are using defGetter / defSetter internally, so come with the same contracts/disclaimers...
`ts
import { deleteIn, getIn, setIn, updateIn } from "@thi.ng/paths";
const state = { a: { b: { c: 23 } } };
const cPath =
getIn(state, cPath)
// 23
setIn(state, cPath, 24)
// { a: { b: { c: 24 } } }
// apply given function to path value
// Note: New c is 24, since above setIn() didn't mutate orig
updateIn(state, cPath, (x) => x + 1)
// { a: { b: { c: 24 } } }
// immutably remove path key
deleteIn(state, cPath)
// { a: { b: {} } }
`
Since deleteIn immutably removes a key from the given state object, itWithout{1-8}<...>
also returns a new type from which the key has been explicitly removed.
Those return types come in the form of interfaces.
`ts
import { deleteIn } from "@thi.ng/paths";
// again using state from above examplea.c
// remove nested key
const state2 = deleteIn(state, ["a","b","c"]);
// compile error: "Property c does not exist
state2.a.b.c;
``
Mainly a potential concern for the non-typechecked versions - currently, only
the mutation functions (i.e. mutIn, mutInUnsafe(), deleteIn,deleteInUnsafe etc.) explicitly disallow updating an object's __proto__,prototype or constructor properties. The package provides thedisallowProtoPath()
helper which can be used in conjunction with the other setters in situations
where it's advisable to do so.
`ts
import { disallowProtoPath, muIn, setIn } from "@thi.ng/paths";
// always checked for pollution
mutIn({}, ["__proto__", "polluted"], true);
// Uncaught Error: illegal argument(s): illegal path: ["__proto__","polluted"]
// manually checked
setIn({}, disallowProtoPath("__proto__.polluted"), true);
// Uncaught Error: illegal argument(s): illegal path: "__proto__.polluted"
`
Only keys in the path will be updated, all other keys present in the
given object retain their original/identical values to provide efficient
structural sharing / re-use. This is the same behavior as in Clojure's
immutable maps or those provided by ImmutableJS (albeit those
implementation are completely different - they're using trees, we're
using the ES6 spread op (for objects, slice() for arrays) and dynamic
functional composition to produce the setter/updater).
`ts
import { defSetterUnsafe } from "@thi.ng/paths";
const s = defSetterUnsafe("a.b.c");
// original
const a = { x: { y: { z: 1 } }, u: { v: 2 } };
// updated version
const b = s(a, 3);
// { x: { y: { z: 1 } }, u: { v: 2 }, a: { b: { c: 3 } } }
// verify anything under keys x & u is still identical`
a.x === b.x // true
a.x.y === b.x.y // true
a.u === b.u; // true
defMutator()/defMutatorUnsafe() are the mutable alternatives todefSetter()/defSetterUnsafe(). Each returns a function, which whenundefined
called, mutates given object / array at given path location and bails if
any intermediate path values are non-indexable (only the very last path
element can be missing in the actual target object structure). If
successful, returns original (mutated) object, else . This
function too provides optimized versions for path lengths <= 4.
As with setIn, mutIn is the immediate use mutator, i.e. the same as:defMutator(path)(state, val).
`ts
import { mutIn, mutInUnsafe } from "@thi.ng/paths";
mutIn({ a: { b: [10, 20] } }, ["a", "b", 1], 23);
// or
mutInUnsafe({ a: { b: [10, 20] } }, "a.b.1", 23);
// { a: { b: [ 10, 23 ] } }
// no-op (because of missing path structure in target object)
mutInUnsafe({}, "a.b.c", 23);
// undefined
`
The exists() function takes an arbitrary object and lookup pathnull
(string or tuple). Descends into object along path and returns true if
the full path exists (even if final leaf value is orundefined). Checks are performed using hasOwnProperty().
`ts
import { exists } from "@thi.ng/paths";
exists({ a: { b: { c: [null] } } }, "a.b.c.0");
// true
exists({ a: { b: { c: [null] } } }, ["a", "b", "c", 1]);
// false
`
If this project contributes to an academic publication, please cite it as:
`bibtex``
@misc{thing-paths,
title = "@thi.ng/paths",
author = "Karsten Schmidt",
note = "https://thi.ng/paths",
year = 2016
}
© 2016 - 2026 Karsten Schmidt // Apache License 2.0