https://github.com/kilix/dafit
Transform data to fit into any model
https://github.com/kilix/dafit
async asynchronous data data-structures javascript js transform transform-data transformer
Last synced: 7 months ago
JSON representation
Transform data to fit into any model
- Host: GitHub
- URL: https://github.com/kilix/dafit
- Owner: Kilix
- License: mit
- Created: 2017-04-03T17:34:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-12T09:20:32.000Z (over 8 years ago)
- Last Synced: 2024-04-14T04:51:01.516Z (over 1 year ago)
- Topics: async, asynchronous, data, data-structures, javascript, js, transform, transform-data, transformer
- Language: JavaScript
- Size: 62.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Dafit
Transform data to fit your structures.
[](https://www.npmjs.com/package/dafit)
[](https://www.npmjs.com/package/dafit)
[](https://opensource.org/licenses/MIT)
[](https://circleci.com/gh/Kilix/dafit)
[](https://codecov.io/github/Kilix/dafit)
## Installation
```bash
$ npm install --save dafit
```
## Usage
Dafit enables you to create a dynamic schema you want your data to fit.
```javascript
import { Resolver } from 'dafit';
// import dafit from 'dafit';
// then user dafit.Resolver
// or
// const { Resolver } = require('dafit');
const defaultValue = null;
const resolveUser = new Resolver({
id: defaultValue, // if field id is present will return it if not will set it to defaultValue
firstname: user => user.first_name, // will set firstname as the return value of the function
lastname: user => user.last_name,
fullName: user => `${user.first_name} ${user.last_name.toUpperCase()}`
friends: user => getFriends(user.id), // will wait for any thenable (e.g Promise) to resolve
permissions: (user, context) => { // receives a context to enable more dynamic resolving
if (contact.withPermissions) return getPermissions(user)
return []
},
})
const user = {
id: 1,
first_name: 'John',
last_name: 'Cena',
age: 39
}
const result = resolveUser(user, { withPermissions: true });
// Promise<{
// id: 1,
// firstname: 'John',
// lastname: 'Cena',
// fullName: 'John CENA',
// friends: [...],
// permissions: ['WRITE_DASHBOARD']
// }>
```
You can also use a synchronuous resolver for some use case to avoid using Promises
```javascript
import { SyncResolver } from 'dafit';
// const { SyncResolver } = require('dafit');
const defaultValue = null;
const resolveUser = new SyncResolver({
id: defaultValue, // if field id is present will return it if not will set it to defaultValue
firstname: user => user.first_name, // will set firstname as the return value of the function
lastname: user => user.last_name,
fullName: user => `${user.first_name} ${user.last_name.toUpperCase()}`,
aPromise: () => new Promise((resolve) => resolve('Hello World!')), // the promise will not be resolved before return
})
const user = {
id: 1,
first_name: 'John',
last_name: 'Cena',
age: 39
}
const result = resolveUser(user, { withPermission: true });
// {
// id: 1,
// firstname: 'John',
// lastname: 'Cena',
// fullName: 'John CENA',
// aPromise: Promise<'Hello World!'>
// }
```
## Change Log
- Changes are reported on the [Github release page](https://github.com/Kilix/dafit/releases)
## Contributing
Read [How to contribute](https://github.com/Kilix/dafit/blob/master/CONTRIBUTING.md) to help us improve this library.
## Ideas for contribution
- [] Adding before / after hooks
- [] Adding support for default context
- [] Handling nested structures
- [] Adding support for Rx and such
- [] Adding plugins feature to add more flexibility *(e.g immutable-plugin to pass a withMutation object to the resolvers for improved performance)*