shallow-pick

0.0.1 • Public • Published

shallow-pick

browser support

A utility function similar to pick() in underscore or lo-dash.

function pick(source /*...keys*/) {/*...*/}

Creates an object constructed of all specified properties of source leaving source itself untouched. Returns shallow partial clone of source

Makes only a shallow copy of the source feilds, consider using it with node-extend or node-deep-extend if a deep copy desired.

Examples

Picking only existing properties:

var source = {a: 1, b: 2, c: 3};
 
var destination = pick(source, 'a', 'c', 'd');
// destination --> {a: 1, c: 3}
// source --> {a: 1, b: 2, c: 3}

Sanitizing input parameters with pick and extend:

function sanitizeInput(opts) {
    return extend({
        a: 'default value',
        b: 'default value'
    }, pick(opts, 'a', 'b'));
}
 
var input = sanitizeInput({a: 'something', evil: 'injection'});
// input --> {a: 'something', b: 'default value'}

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i shallow-pick

    Weekly Downloads

    2

    Version

    0.0.1

    License

    MIT

    Last publish

    Collaborators

    • maslennikov