Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/objql
A query utility for JavaScript.
https://github.com/egoist/objql
Last synced: 22 days ago
JSON representation
A query utility for JavaScript.
- Host: GitHub
- URL: https://github.com/egoist/objql
- Owner: egoist
- License: mit
- Created: 2019-03-19T10:55:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T14:39:35.000Z (almost 3 years ago)
- Last Synced: 2024-10-10T12:49:07.810Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 335 KB
- Stars: 51
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ObjQL
[![NPM version](https://badgen.net/npm/v/objql)](https://npmjs.com/package/objql) [![NPM downloads](https://badgen.net/npm/dm/objql)](https://npmjs.com/package/objql) [![CircleCI](https://badgen.net/circleci/github/egoist/objql/master)](https://circleci.com/gh/egoist/objql/tree/master) [![donate](https://badgen.net/badge/support%20me/donate/ff69b4)](https://patreon.com/egoist) [![chat](https://badgen.net/badge/chat%20on/discord/7289DA)](https://chat.egoist.moe)
> Only query what you need.
## Install
```bash
yarn add objql
```## Usage
```js
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
```js
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]'
}
*/
```### Pass a String as Schema
```js
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](https://github.com/egoist), Released under the [MIT](./LICENSE) License.
Authored and maintained by EGOIST with help from contributors ([list](https://github.com/egoist/objql/contributors)).> [egoist.sh](https://egoist.sh) · GitHub [@EGOIST](https://github.com/egoist) · Twitter [@\_egoistlily](https://twitter.com/_egoistlily)