Only query what you need.
yarn add objql
const objql = require('objql')
const data = {
foo: 1,
bar: '123',
baz: {
foo: new Date(),
bar: [1, 2, 3],
baz: 'hello',
},
wasted: 'blah blah..',
}
const schema = {
foo: true, // Select `foo` from `data`
bar: Number, // Select `bar` from `data` and coerce to `Number(data.foo)`
baz: {
foo: true,
bar: String,
baz: (v) => `${v} world`,
},
}
objql(data, schema)
/* =>
{
foo: 1,
bar: 123,
baz: {
foo: new Date(),
bar: ['1', '2', '3'],
baz: 'hello world'
}
}
*/
const data = {
email: 'kevin',
}
const schema = {
email: 'Email',
}
const types = {
Email(v) {
return v.includes('@') ? v : `${v}@gmail.com`
},
}
objql(data, schema, { types })
/* =>
{
email: '[email protected]'
}
*/
const data = {
age: '18',
}
const schema = JSON.stringify({
age: 'number',
})
const types = {
number: Number,
}
objql(data, schema, { types })
/* =>
{
age: 18
}
*/
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
objql © EGOIST, Released under the MIT License. Authored and maintained by EGOIST with help from contributors (list).
egoist.sh · GitHub @EGOIST · Twitter @_egoistlily