objql
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

ObjQL

NPM version NPM downloads CircleCI donate chat

Only query what you need.

Install

yarn add objql

Usage

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'
  }
}
*/

Custom Type

const data = {
  email: 'kevin',
}

const schema = {
  email: 'Email',
}

const types = {
  Email(v) {
    return v.includes('@') ? v : `${v}@gmail.com`
  },
}

objql(data, schema, { types })
/* =>
{
  email: 'kevin@gmail.com'
}
*/

Pass a String as Schema

const data = {
  age: '18',
}

const schema = JSON.stringify({
  age: 'number',
})

const types = {
  number: Number,
}

objql(data, schema, { types })
/* =>
{
  age: 18
}
*/

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

objql © EGOIST, Released under the MIT License.
Authored and maintained by EGOIST with help from contributors (list).

egoist.sh · GitHub @EGOIST · Twitter @_egoistlily

/objql/

    Package Sidebar

    Install

    npm i objql

    Weekly Downloads

    1

    Version

    1.2.1

    License

    MIT

    Unpacked Size

    8.54 kB

    Total Files

    7

    Last publish

    Collaborators

    • egoist