Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@package/value
Advanced tools
useful work around for existential getting/setting values in nested Objects
work around for existential getting/setting values in nested Objects
>$ npm install @package/value
@package/value>$ npm test
if you want to convert @package/value into es5, you can simply run:
@package/value>$ npm run build
Returns the property value at the specified path in an Object.
import {value} from '@package/value';
var data = { one : { two : { three : true, four : [1, 2, 3, 4] } } };
value( data, 'one' ); // returns => { two : { three : true, four : [1, 2, 3, 4] } }
value( data, 'one.two' ); // returns => { three : true, four : [1, 2, 3, 4] }
value( data, 'one.two.three' ); // returns => { three : true }
value( data, 'one.two.four' ); // returns => [1, 2, 3, 4]
value( data, 'one.two.four.2' ); // returns => 3
Assign a value
to an item
using the given path
.
Returns the passed value
.
import {assign} from '@package/value';
var data = {};
assign( data, 'one', {} ); // data == { one : {} }
value( data, 'one.two', {} ); // data == { one : { two : {} } }
value( data, 'one.two.three', true ); // data == { one : { two : { three : true } } }
value( data, 'one.two.four', [1, 2, 3, 4] ); // data == { one : { two : { three : true, four : [1, 2, 3, 4] } } }
Creates an Object representation of the passed namespace
String and returns it.
If a context
Object is given, the Object tree created will be added to the context
Object, otherwise it will be added to the global namespace.
NOTE: If any existing Objects with the same name already exist, they will NOT be replaced and any child Objects will be appended to them.
import {bless} from '@package/value';
// ENV == 'browser'
bless( 'foo.bar' ); // creates => global.foo.bar
// you can now do:
foo.bar.Something = function() {};
bless( 'foo.bar', value ); // creates => foo.bar
var bar = bless( 'foo.bar' );
bar === foo.bar // returns => true
IMPORTANT: When using bless
within a commonjs module: if you want your namespace Object to be assigned to the correct module.exports
, then you should always pass the module
— not module.exports
— instance as the context (ctx
) of your namespace.
import {bless} from '@package/value';
// ENV == 'commonjs'
// inside my_commonjs_module.js
bless( 'foo.bar', module ); // creates => module.exports.foo.bar
// you can now do:
module.exports.foo.bar.Something = function() {};
// if you want to include "exports" in your namespace, you can do so by placing a carat (^) at the start of the String
bless( 'exports.foo.bar', module ); // creates => module.exports.foo.bar
// otherwise, you will end up creating an extra exports Object, e.g:
bless( 'exports.foo.bar', module ); // creates => module.exports.exports.foo.bar
// alternatively, you can also do:
bless( 'foo.bar', module.exports ); // creates => module.exports.foo.bar
Attempts to coerce primitive values "trapped" in Strings, into their real types.
import {coerce} from '@package/value';
coerce( 'false' ); // returns false
coerce( 'null' ); // returns null
coerce( 'true' ); // returns true
coerce( 'undefined' ); // returns undefined
coerce( 'NaN' ); // returns NaN
coerce( '0001' ); // returns 1
coerce( '0012' ); // returns 12
coerce( '0123' ); // returns 123
coerce( '123.4' ); // returns 123.4
coerce( '123.45' ); // returns 123.45
coerce( '123.456' ); // returns 123.456
coerce( '123.456.789' ); // returns "123.456.789"
Returns true
if the passed value
does not exist (see exist
below), is an empty Array, Object, String or any other enumerable type.
import {empty} from '@package/value';
empty( undefined ); // returns => true
empty( null ); // returns => true
empty( '' ); // returns => true
empty( [] ); // returns => true
empty( {} ); // returns => true
empty( ' ' ); // returns => false
empty( [1] ); // returns => false
empty( { 0 : null } ); // returns => false
Returns false
if the passed value
is undefined
, NaN
or null
, returns true
otherwise.
import {exists} from '@package/value';
exists( undefined ); // returns => false
exists( NaN ); // returns => false
exists( null ); // returns => false
exists( 0 ); // returns => true
exists( false ); // returns => true
exists( {} ); // returns => true
(The MIT License)
Copyright (c) 2011 christos "constantology" constandinou http://muigui.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
useful work around for existential getting/setting values in nested Objects
We found that @package/value demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.