Gathering detailed insights and metrics for @invrs/dot-prop-immutable
Gathering detailed insights and metrics for @invrs/dot-prop-immutable
npm install @invrs/dot-prop-immutable
Typescript
Module System
Node Version
NPM Version
69.2
Supply Chain
97.8
Quality
78.8
Maintenance
100
Vulnerability
98.6
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
3,564
Last Day
9
Last Week
10
Last Month
39
Last Year
226
1 Stars
338 Commits
4 Watching
2 Branches
1 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
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.
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: [] }
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'
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 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 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 a value by a dot path.
The target value must be an object, array, null, or undefined.
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 ] }
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
80 existing vulnerabilities detected
Details
Score
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