Installations
npm install @invrs/dot-prop-immutable
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.11.2
NPM Version
5.6.0
Score
69.2
Supply Chain
97.8
Quality
78.8
Maintenance
100
Vulnerability
98.6
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
invrs
Download Statistics
Total Downloads
3,564
Last Day
9
Last Week
10
Last Month
39
Last Year
226
GitHub Statistics
1 Stars
338 Commits
4 Watching
2 Branches
1 Contributors
Bundle Size
16.68 kB
Minified
6.16 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0
Package Id
@invrs/dot-prop-immutable@3.0.0
Unpacked Size
28.07 kB
Size
6.94 kB
File Count
5
NPM Version
5.6.0
Node Version
8.11.2
Total Downloads
Cumulative downloads
Total Downloads
3,564
Last day
0%
9
Compared to previous day
Last week
25%
10
Compared to previous week
Last month
1,200%
39
Compared to previous month
Last year
-21.5%
226
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
dot-prop-immutable
![npm version](https://badge.fury.io/js/dot-prop-immutable.svg)
Immutable version of dot-prop with some extensions.
npm 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:
get(object, path) --> value
set(object, path, value) --> object
delete(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.
Usage
1var dotProp = require("dot-prop-immutable") 2var state = { todos: [] }, 3 index = 0 4 5// Add todo: 6state = dotProp.set(state, "todos", list => [ 7 ...list, 8 { text: "cleanup", complete: false }, 9]) 10// or with destructuring assignment 11state = { 12 ...state, 13 todos: [ 14 ...state.todos, 15 { text: "cleanup", complete: false }, 16 ], 17} 18//=> { todos: [{text: 'cleanup', complete: false}] } 19 20// Complete todo: 21state = dotProp.set(state, `todos.${index}.complete`, true) 22// or with destructuring assignment 23state = { 24 ...state, 25 todos: [ 26 ...state.todos.slice(0, index), 27 { ...state.todos[index], complete: true }, 28 ...state.todos.slice(index + 1), 29 ], 30} 31//=> { todos: [{text: 'cleanup', complete: true}] } 32 33// Delete todo: 34state = dotProp.delete(state, `todos.${index}`) 35// or with destructuring assignment 36state = { 37 ...state, 38 todos: [ 39 ...state.todos.slice(0, index), 40 ...state.todos.slice(index + 1), 41 ], 42} 43//=> { todos: [] }
get
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( 9 { foo: { bar: "a" } }, 10 "foo.notDefined.deep", 11 "default value" 12) 13//=> default value 14 15dotProp.get( 16 { foo: { "dot.dot": "unicorn" } }, 17 "foo.dot\\.dot" 18) 19//=> 'unicorn'
or use a property array as a path.
1// Use an array as get path 2dotProp.get({ foo: { "dot.dot": "unicorn" } }, [ 3 "foo", 4 "dot.dot", 5]) 6//=> 'unicorn'
It is also possible to index into an array where the special index $end
refers to the last element of the array.
1var obj = { 2 foo: [ 3 { bar: "gold-unicorn" }, 4 "white-unicorn", 5 "silver-unicorn", 6 ], 7} 8 9// Index into array 10dotProp.get(obj, "foo.1") 11//=> 'white-unicorn' 12 13dotProp.get(obj, "foo.0.bar") 14//=> 'gold-unicorn' 15 16// Index into array with $end 17dotProp.get(obj, "foo.$end") 18//=> 'silver-unicorn' 19 20// If obj is an array 21dotProp.get( 22 [ 23 { bar: "gold-unicorn" }, 24 "white-unicorn", 25 "silver-unicorn", 26 ], 27 "0.bar" 28) 29//=> 'gold-unicorn'
set
Modify a nested property by a dot path
1// Setter 2var obj = { foo: { bar: "a" } } 3 4var obj1 = dotProp.set(obj, "foo.bar", "b") 5//obj1 => {foo: {bar: 'b'}} 6 7var 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
1var obj = { 2 foo: [ 3 { bar: "gold-unicorn" }, 4 "white-unicorn", 5 "silver-unicorn", 6 ], 7} 8 9// Index into array 10dotProp.set(obj, "foo.1", "platin-unicorn") 11//=> {foo: [{bar: 'gold-unicorn'}, 'platin-unicorn', 'silver-unicorn']} 12 13dotProp.set(obj, "foo.0.bar", "platin-unicorn") 14//=> {foo: [{bar: 'platin-unicorn'}, 'white-unicorn', 'silver-unicorn']} 15 16// Index into array with $end 17dotProp.set(obj, "foo.$end", "platin-unicorn") 18//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'platin-unicorn']}
delete
Delete a nested property/array by a dot path
1var obj = { 2 foo: [ 3 { bar: "gold-unicorn" }, 4 "white-unicorn", 5 "silver-unicorn", 6 ], 7} 8 9// delete 10dotProp.delete(obj, "foo.$end") 11//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn']} 12 13dotProp.delete(obj, "foo.0.bar") 14//=> {foo: [{}, 'white-unicorn', 'silver-unicorn']}
toggle
Toggle a boolean a value by a dot path.
1var obj = { foo: { bar: true } } 2 3// toggle 4dotProp.toggle(obj, "foo.bar") 5//=> {foo: { bar: false } }
merge
Merge a value by a dot path.
The target value must be an object, array, null, or undefined.
- If target is an object, Object.assign({}, target, param) is used.
- If target an array, target.concat(param) is used.
- If target is null or undefined, the value is simply set.
1var 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 ] }
License
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
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
80 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- 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-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-3gx7-xhv7-5mx3
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-w5p7-h5w8-2hfq
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-f9cm-qmx5-m98h
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- 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-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-p28h-cc7q-c4fg
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
Score
1.3
/10
Last Scanned on 2025-02-03
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