0.0.1 • Published 10 years ago
shallow-pick v0.0.1
shallow-pick
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'}
0.0.1
10 years ago