Skip to content
A query utility for JavaScript.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.editorconfig
.gitattributes
.gitignore
.prettierrc
LICENSE
README.md
bili.config.ts
circle.yml
jest.config.js
package.json
tsconfig.json
yarn.lock

README.md

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

You can’t perform that action at this time.