
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
set-object-path
Advanced tools
Functions for setting values deep insde nested objects.
I use this for generating callbacks that can modify data in a restricted scope of a larger object.
var test = require('tape')
var setPath = require('./')
test('use .call to pass root object', function (t) {
var o = {}
setPath.call(o, 'a.b.c', 2)
t.deepEqual(o, {a: {b: {c: 2}}})
t.end()
})
test('use array-paths instead of dot-separated strings', function (t) {
var o = {}
setPath.call(o, ['people', 'Mr. Jones'], "is back")
t.deepEqual(o, {
people: {
'Mr. Jones': 'is back'
}
})
t.end()
})
test('use .setter to pass path implicitly', function (t) {
var setName = setPath.setter('name')
var my = {}
setName.call(my, 'Stephen')
t.equal(my.name, 'Stephen')
t.end()
})
test('use .bind to pass root implicitly', function (t) {
var my = {}
var set = setPath.bind(my)
set('name', 'Stephen')
t.equal(my.name, 'Stephen')
t.end()
})
test('chain .bind and .setter for awesomeness', function (t) {
var my = {}
var setMyName = setPath.setter('name').bind(my)
setMyName('Stephen')
t.equal(my.name, 'Stephen')
// or do it the other way around
setPath.bind(my).setter('name')('Still Stephen')
t.equal(my.name, 'Still Stephen')
t.end()
})
test('use .prefix to limit the scope of allowed changes', function (t) {
var model = {}
var setApples = setPath.prefix('apples').bind(model)
var setOranges = setPath.prefix('oranges').bind(model)
setApples('count', 1)
setOranges('count', 2)
t.deepEqual(model, {
apples: { count: 1 },
oranges: { count: 2 },
})
t.end()
})
test('chain .bind, .prefix, .setter', function (t) {
var model = {}
var setAppleCount = setPath.bind(model).prefix('apples').setter('count')
var setOrangeCount = setPath.bind(model).prefix('oranges').setter('count')
setAppleCount(3)
setOrangeCount(4)
t.deepEqual(model, {
apples: { count: 3 },
oranges: { count: 4 },
})
t.end()
})
npm install set-object-path
MIT
FAQs
This package name is not currently in use, but was formerly occupied by a popular package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.
The npm package set-object-path receives a total of 4 weekly downloads. As such, set-object-path popularity was classified as not popular.
We found that set-object-path demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.