Get, set, or delete a property from a nested object using a dot path
Installations
npm install dot-prop
Score
99.3
Supply Chain
99.5
Quality
76.9
Maintenance
100
Vulnerability
99.6
License
Developer
sindresorhus
Developer Guide
Module System
ESM
Min. Node Version
>=18
Typescript Support
Yes
Node Version
20.12.2
NPM Version
10.6.0
Statistics
817 Stars
115 Commits
79 Forks
14 Watching
1 Branches
23 Contributors
Updated on 01 Nov 2024
Bundle Size
3.16 kB
Minified
1.14 kB
Minified + Gzipped
Languages
JavaScript (95.73%)
TypeScript (4.27%)
Total Downloads
Cumulative downloads
Total Downloads
5,031,641,124
Last day
-8.2%
4,001,132
Compared to previous day
Last week
2%
23,977,987
Compared to previous week
Last month
17%
97,321,737
Compared to previous month
Last year
2.9%
952,798,927
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
5
dot-prop
Get, set, or delete a property from a nested object using a dot path
Install
1npm install dot-prop
Usage
1import {getProperty, setProperty, hasProperty, deleteProperty} from 'dot-prop'; 2 3// Getter 4getProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); 5//=> 'unicorn' 6 7getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep'); 8//=> undefined 9 10getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'); 11//=> 'default value' 12 13getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'); 14//=> 'unicorn' 15 16getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar'); 17//=> 'unicorn' 18 19// Setter 20const object = {foo: {bar: 'a'}}; 21setProperty(object, 'foo.bar', 'b'); 22console.log(object); 23//=> {foo: {bar: 'b'}} 24 25const foo = setProperty({}, 'foo.bar', 'c'); 26console.log(foo); 27//=> {foo: {bar: 'c'}} 28 29setProperty(object, 'foo.baz', 'x'); 30console.log(object); 31//=> {foo: {bar: 'b', baz: 'x'}} 32 33setProperty(object, 'foo.biz[0]', 'a'); 34console.log(object); 35//=> {foo: {bar: 'b', baz: 'x', biz: ['a']}} 36 37// Has 38hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); 39//=> true 40 41// Deleter 42const object = {foo: {bar: 'a'}}; 43deleteProperty(object, 'foo.bar'); 44console.log(object); 45//=> {foo: {}} 46 47object.foo.bar = {x: 'y', y: 'x'}; 48deleteProperty(object, 'foo.bar.x'); 49console.log(object); 50//=> {foo: {bar: {y: 'x'}}}
API
getProperty(object, path, defaultValue?)
Get the value of the property at the given path.
Returns the value if any.
setProperty(object, path, value)
Set the property at the given path to the given value.
Returns the object.
hasProperty(object, path)
Check whether the property at the given path exists.
Returns a boolean.
deleteProperty(object, path)
Delete the property at the given path.
Returns a boolean of whether the property existed before being deleted.
escapePath(path)
Escape special characters in a path. Useful for sanitizing user input.
1import {getProperty, escapePath} from 'dot-prop'; 2 3const object = { 4 foo: { 5 bar: '👸🏻 You found me Mario!', 6 }, 7 'foo.bar' : '🍄 The princess is in another castle!', 8}; 9const escapedPath = escapePath('foo.bar'); 10 11console.log(getProperty(object, escapedPath)); 12//=> '🍄 The princess is in another castle!'
deepKeys(object)
Returns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
1import {getProperty, deepKeys} from 'dot-prop'; 2 3const user = { 4 name: { 5 first: 'Richie', 6 last: 'Bendall', 7 }, 8 activeTasks: [], 9 currentProject: null 10}; 11 12for (const property of deepKeys(user)) { 13 console.log(`${property}: ${getProperty(user, property)}`); 14 //=> name.first: Richie 15 //=> name.last: Bendall 16 //=> activeTasks: [] 17 //=> currentProject: null 18}
Sparse arrays are supported. In general, avoid using sparse arrays.
object
Type: object | array
Object or array to get, set, or delete the path
value.
You are allowed to pass in undefined
as the object to the get
and has
functions.
path
Type: string
Path of the property in the object, using .
to separate each nested key.
Use \\.
if you have a .
in the key.
The following path components are invalid and results in undefined
being returned: __proto__
, prototype
, constructor
.
value
Type: unknown
Value to set at path
.
defaultValue
Type: unknown
Default value.
Stable Version
The latest stable version of the package.
Stable Version
9.0.0
HIGH
2
7.3/10
Summary
dot-prop Prototype Pollution vulnerability
Affected Versions
>= 5.0.0, < 5.1.1
Patched Versions
5.1.1
7.3/10
Summary
dot-prop Prototype Pollution vulnerability
Affected Versions
< 4.2.1
Patched Versions
4.2.1
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 16/30 approved changesets -- score normalized to 5
Reason
0 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
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/dot-prop/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/dot-prop/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:21
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
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 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
4.5
/10
Last Scanned on 2024-11-18
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