Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
Installations
npm install object-path-immutable
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=0.10.0
Node Version
14.9.0
NPM Version
7.5.2
Score
98.7
Supply Chain
99.6
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
mariocasciaro
Download Statistics
Total Downloads
15,184,141
Last Day
6,326
Last Week
32,595
Last Month
148,286
Last Year
2,632,778
GitHub Statistics
414 Stars
117 Commits
50 Forks
9 Watching
9 Branches
24 Contributors
Bundle Size
6.27 kB
Minified
2.19 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.1.2
Package Id
object-path-immutable@4.1.2
Size
11.42 kB
NPM Version
7.5.2
Node Version
14.9.0
Publised On
16 Sept 2021
Total Downloads
Cumulative downloads
Total Downloads
15,184,141
Last day
-31.4%
6,326
Compared to previous day
Last week
-34.4%
32,595
Compared to previous week
Last month
18.5%
148,286
Compared to previous month
Last year
28.2%
2,632,778
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
object-path-immutable
Tiny JS library to modify deep object properties without modifying the original object (immutability).
Works great with React (especially when using setState()
) and Redux (inside a reducer).
This can be seen as a simpler and more intuitive alternative to the React Immutability Helpers and Immutable.js.
Changelog
Install
npm install object-path-immutable --save
Quick usage
The following, sets a property without modifying the original object.
It will minimize the number of clones down the line. The resulting object is just a plain JS object literal,
so be warned that it will not be protected against property mutations (like Immutable.js
)
1const obj = { 2 a: { 3 b: 'c', 4 c: ['d', 'f'] 5 } 6} 7 8const newObj = immutable.set(obj, 'a.b', 'f') 9// { 10// a: { 11// b: 'f', 12// c: ['d', 'f'] 13// } 14// } 15 16// obj !== newObj 17// obj.a !== newObj.a 18// obj.a.b !== newObj.a.b 19 20// However: 21// obj.a.c === newObj.a.c
Wrap mode
You can also chain the api's and call value()
at the end to retrieve the resulting object.
1const newObj = immutable.wrap(obj).set('a.b', 'f').del('a.c.0').value()
API
1// Premises 2 3const obj = { 4 a: { 5 b: 'c', 6 c: ['d', 'f'] 7 } 8} 9 10import * as immutable from 'object-path-immutable'
set (initialObject, path, value)
Changes an object property.
- Path can be either a string or an array.
1const newObj1 = immutable.set(obj, 'a.b', 'f') 2const newObj2 = immutable.set(obj, ['a', 'b'], 'f') 3 4// { 5// a: { 6// b: 'f', 7// c: ['d', 'f'] 8// } 9// } 10 11// Note that if the path is specified as a string, numbers are automatically interpreted as array indexes. 12 13const newObj = immutable.set(obj, 'a.c.1', 'fooo') 14// { 15// a: { 16// b: 'f', 17// c: ['d', 'fooo'] 18// } 19// }
update (initialObject, path, updater)
Updates an object property.
1const obj = { 2 a: { 3 b: 1 4 } 5} 6 7const newObj = immutable.update(obj, ['a', 'b'], v => v + 1) 8 9// { 10// a: { 11// b: 2, 12// } 13// }
push (initialObject, path, value)
Push into a deep array (it will create intermediate objects/arrays if necessary).
1const newObj = immutable.push(obj, 'a.d', 'f') 2// { 3// a: { 4// b: 'f', 5// c: ['d', 'f'], 6// d: ['f'] 7// } 8// }
del (initialObject, path)
Deletes a property.
1const newObj = immutable.del(obj, 'a.c') 2// { 3// a: { 4// b: 'f' 5// } 6// }
Can also delete a deep array item using splice
1const newObj = immutable.del(obj, 'a.c.0') 2// { 3// a: { 4// b: 'f', 5// c: ['f'] 6// } 7// }
assign (initialObject, path, payload)
Shallow copy properties.
1const newObj = immutable.assign(obj, 'a', { b: 'f', g: 'h' }) 2// { 3// a: { 4// b: 'f', 5// c: ['d, 'f'], 6// g: 'h' 7// } 8// }
insert (initialObject, path, payload, position)
Insert property at the specific array index.
1const newObj = immutable.insert(obj, 'a.c', 'k', 1) 2// var obj = { 3// a: { 4// b: 'c', 5// c: ['d, 'k' 'f'], 6// } 7// }
merge (initialObject, path, value)
Deep merge properties.
1const newObj = immutable.merge(obj, 'a.c', {b: 'd'})
Getters (not available in wrap mode)
get (object, path, defaultValue)
Retrieve a deep object property. Imported from object-path for convenience.
Equivalent library with side effects
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Warn: no linked content found
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
Found 6/25 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Reason
21 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
2.3
/10
Last Scanned on 2025-01-27
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 MoreOther packages similar to object-path-immutable
dot-prop-immutable
Immutable version of dot-prop with some extensions
@thi.ng/paths
Immutable, optimized and optionally typed path-based object property / array accessors with structural sharing
typed-object-path-immutable
A well-typed immutable object helper library
object-path-immutable-yolo
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.