@package/value

0.0.16 • Public • Published

@package/value

work around for existential getting/setting values in nested Objects

Installation

    >$ npm install @package/value

Test

    @package/value>$ npm test

Build

if you want to convert @package/value into es5, you can simply run:

    @package/value>$ npm run build

API

value( item:Object, path:String ):Mixed

Returns the property value at the specified path in an Object.

Example:

    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( item:Object, path:String, value:Mixed ):Mixed

Assign a value to an item using the given path.

Returns the passed value.

Example:

    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] } } }

bless( namespace:String[, context:Object] ):Object

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.

Example:

    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.

Example:

    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

coerce( item:Mixed ):Mixed

Attempts to coerce primitive values "trapped" in Strings, into their real types.

Example:

    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"

empty( value:Mixed ):Boolean

Returns true if the passed value does not exist (see exist below), is an empty Array, Object, String or any other enumerable type.

Example:

    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

exists( value:Mixed ):Boolean

Returns false if the passed value is undefined , NaN or null, returns true otherwise.

Example:

    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

License

(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.

Analytics

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i @package/value

    Weekly Downloads

    2

    Version

    0.0.16

    License

    MIT

    Last publish

    Collaborators

    • wistla
    • muigui
    • constantology
    • jeffwad
    • ibrokethat
    • package