Empowered but easy to use lodash Pick alternative with configurable and extensive schema and
Supick creates an object/array composed of the deeply picked object properties/array of picked object properties.
npm i supick
const pick = require("supick");
const person = {
name: "John Doe",
car: {
model: "BMW X5",
year: 2019,
color: "black",
},
hobby: "music"
};
pick(person, "name, car: {model, year}"); // ==> { name: "John Doe", { car: { model: "BMW X5", year: 2019 } } }
- Schema could be string, array of strings(lodash pick style), object(mongodb projection style) or customizer function
- You may use trailing whitespaces, newlines and carret returns - they are ignored.
- You may enclose schema in brackets: '{a, b, c, d: {d_a, d_b}}'
- Use commas to separate prop names on each level. Trailing comma is ignored.
- Use colon ":" and curly brackets ("{", "}") to specify inner properties.
- You may use supick both for objects and arrays, for example: schema "a: 1, b: 2" works this way:
pick({ a: 1, b: 2, c: 3 }, 'a, b')
=>{ a: 1, b: 2 }
pick([{ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }], 'a, b')
=>[{ a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }]
- Customizer function is very flexible way to add some extra logic. Customizer function must return truthy or falsy value to determine if property should be picked. Function receives 4 arguments:
key
(property name),value
(property value),path
(parent properties concatenated with dot delimiter),depth
(depth of current nested property).
- String:
'{ a, b, c: { d, e }, f: { g: { h, i }, j } }'
. - Array of strings(lodash-style):
['a', 'b', 'c: { d, e }', 'f: { g: { h, i }, j }']
. - Object(mongodb projection-style):
{ a: true, b: true, c: { d: true, e: true }, f: { g: { h: true, i: true }, j: true } }
// "true" could be replaced to 1 - Customizer function:
function (key, value, path, depth) {
if (['a', 'b', 'c', 'e'].includes(key)) return true; // pick all properties with names a, b, c, e
if (path.startsWith('f')) return true; // pick f property and all its nested properties
if (depth === 0) return true; // pick all top level properties
return false;
}
const person = {
"name" : "John Doe",
"age": 35,
"family" : {
"wife" : {
"name": "Susan",
"age": 32
},
"children": [
{
"name": "Alex",
"age": 5,
"hobby": "dancing"
},
{
"name": "Alice",
"age": 3,
"hobby": "music"
}
]
},
"cars": [
{
"model": "BMW X5",
"year": 2015
},
{
"model": "Tesla Model S",
"year": 2018
}
]
};
const schema = `
{
name,
family: {
wife,
children: {
name,
age
}
},
cars: { model }
}`;
pick(obj, schema);
will result in this =>
{
"name" : "John Doe",
"family" : {
"wife" : { "name": "Susan", "age": 32 },
"children": [
{ "name": "Alex", "age": 5 },
{ "name": "Alice", "age": 3 }
]
},
"cars": [
{ "model": "BMW X5" },
{ "model": "Tesla Model S" }
]
};
More examples in test file index.test.js