Gathering detailed insights and metrics for dot-prop-immutable
Gathering detailed insights and metrics for dot-prop-immutable
Gathering detailed insights and metrics for dot-prop-immutable
Gathering detailed insights and metrics for dot-prop-immutable
object-path-immutable
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
@types/dot-prop-immutable
TypeScript definitions for dot-prop-immutable
@dot-store/dot-prop-immutable
Immutable version of dot-prop with some extensions
@invrs/dot-prop-immutable
Immutable version of dot-prop with some extensions
npm install dot-prop-immutable
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
569 Stars
105 Commits
39 Forks
29 Watchers
14 Branches
37 Contributors
Updated on May 28, 2025
Latest Version
2.1.1
Package Id
dot-prop-immutable@2.1.1
Size
4.71 kB
NPM Version
6.14.11
Node Version
14.15.4
Published on
Jul 17, 2021
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
Immutable version of dot-prop with some extensions.
1npm install dot-prop-immutable
The motivation for this module is to have a simple utility for changing state in a React-Redux application without mutating the existing state of plain JavaScript objects. If you are going for real immutable data collections take a look at the cool library Immutable.js. A good practice is not to mix the immutable data collections with mutable objects because it can lead to confusion. Immutable objects are not accessed by the default semantics, but implemented by setters and getters.
This library implements 3 helper functions:
1get(object, path) --> value 2set(object, path, value) --> object 3delete(object, path) --> object
None of the functions mutate the input object. For efficiency, the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path.
1const dotProp = require('dot-prop-immutable'); 2let state = { todos: [] }, index = 0; 3 4// Add todo: 5state = dotProp.set(state, 'todos', list => [...list, {text: 'cleanup', complete: false}]) 6// or with destructuring assignment 7state = {...state, todos: [...state.todos, {text: 'cleanup', complete: false}]}; 8//=> { todos: [{text: 'cleanup', complete: false}] } 9 10// Complete todo: 11state = dotProp.set(state, `todos.${index}.complete`, true) 12// or with destructuring assignment 13state = {...state, todos: [ 14 ...state.todos.slice(0, index), 15 {...state.todos[index], complete: true}, 16 ...state.todos.slice(index + 1) 17]}; 18//=> { todos: [{text: 'cleanup', complete: true}] } 19 20// Delete todo: 21state = dotProp.delete(state, `todos.${index}`) 22// or with destructuring assignment 23state = {...state, todos: [ 24 ...state.todos.slice(0, index), 25 ...state.todos.slice(index + 1) 26]}; 27//=> { todos: [] }
Access a nested property by a dot path
1// Getter 2dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar') 3//=> 'unicorn' 4 5dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep') 6//=> undefined 7 8dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value') 9//=> default value 10 11dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot') 12//=> 'unicorn'
or use a property array as a path.
1// Use an array as get path 2dotProp.get({foo: {'dot.dot': 'unicorn'}}, ['foo', 'dot.dot']) 3//=> 'unicorn'
It is also possible to index into an array where the special index $end
refers to the last element of the array.
1const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}; 2 3// Index into array 4dotProp.get(obj, 'foo.1') 5//=> 'white-unicorn' 6 7dotProp.get(obj, 'foo.0.bar') 8//=> 'gold-unicorn' 9 10// Index into array with $end 11dotProp.get(obj, 'foo.$end') 12//=> 'silver-unicorn' 13 14// If obj is an array 15dotProp.get([{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn'], '0.bar') 16//=> 'gold-unicorn' 17
Modify a nested property by a dot path
1// Setter 2const obj = {foo: {bar: 'a'}}; 3 4const obj1 = dotProp.set(obj, 'foo.bar', 'b'); 5//obj1 => {foo: {bar: 'b'}} 6 7const obj2 = dotProp.set(obj1 , 'foo.baz', 'x'); 8//obj2 => {foo: {bar: 'b', baz: 'x'}}
where obj
, obj1
, obj2
, obj3
all are different objects.
Use a function to modify the selected property, where first argument is the old value.
1// Setter where value is a function (get and set current value) 2dotProp.set({foo: {bar: 'a'}}, 'foo.bar', v => v + 'bc') 3//=> {foo: {bar: 'abc'}}
Modify a nested array
1const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}; 2 3// Index into array 4dotProp.set(obj, 'foo.1', 'platin-unicorn') 5//=> {foo: [{bar: 'gold-unicorn'}, 'platin-unicorn', 'silver-unicorn']} 6 7dotProp.set(obj, 'foo.0.bar', 'platin-unicorn') 8//=> {foo: [{bar: 'platin-unicorn'}, 'white-unicorn', 'silver-unicorn']} 9 10// Index into array with $end 11dotProp.set(obj, 'foo.$end', 'platin-unicorn') 12//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'platin-unicorn']} 13
Delete a nested property/array by a dot path
1const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}; 2 3// delete 4dotProp.delete(obj, 'foo.$end'); 5//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn']} 6 7dotProp.delete(obj, 'foo.0.bar'); 8//=> {foo: [{}, 'white-unicorn', 'silver-unicorn']}
Toggle a boolean a value by a dot path.
1const obj = {foo: { bar: true } }; 2 3// toggle 4dotProp.toggle(obj, 'foo.bar'); 5//=> {foo: { bar: false } }
Merge a value by a dot path.
The target value must be an object, array, null, or undefined.
1const obj = {foo: { bar: {a:1, b:2 } }; 2 3// merge object 4dotProp.merge(obj, 'foo.bar', {c:3} ); 5//=> {foo: { bar:{ a:1, b:2, c:3} } } 6 7var arr = {foo: { bar: [1, 2] } }; 8 9// merge array 10dotProp.merge(arr, 'foo.bar', [3, 4] ); 11//=> {foo: { bar:[1, 2, 3, 4 ] }
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More