<commit_before># Goku
> Transform & serialize to simple JS objects

Goku takes simple descriptors and transform + serializes complex JS objects into simple JSON objects.

Features:
- recursion
- es6 support (class name based serialization)
- field aliases
- serialization groups

## Sample

```javascript
// descriptor in user-descriptor.json
{
    // aliases
    number: { as: 'id' },
    // always serialized
    firstName: true,
    lastName: true,
    // not serialized by default
    email: [ 'details' ],
    createdAt: [ 'details' ],
    children: [ 'details' ]
};

const user = new User({
    number: 1,
    firstName: 'Jane',
    lastName: 'Doe',
    email: '<EMAIL>',
    createdAt: new Date(),
    children: [ child1, child2 ]
});

goku.serialize(user);
// --› {
//      id: 1,
//      firstName: 'Jane',
//      lastName: 'Doe'
//  }

goku.serialize(user, ['details']);
// --› {
//      id: 1,
//      firstName: 'Jane',
//      lastName: 'Doe',
//      email: '<EMAIL>',
//      createdAt: [ Date ],
//      children: [
//          {...},
//          {...}
//      ]
//  }
```

## Usage

### Installation

```
$ npm install goku
```

Then

```
// ES6
import Goku from 'goku';
// ES5
var Goku = require('goku');
```

### Defining and registering your descriptors

```json
// in user-descriptor.json
{
    "firstName": true,
    "lastName": true,
    "email": [ "details" ]
}
```

```javascript
import Goku from 'goku';
const goku = new Goku();
goku.registerDescriptor('User', require('user-descriptor.json'));
goku.serialize(user);
```

### Using in an ES6 project

```javascript
require('babel/register')({
    stage: 1,
    ignore: /node_modules\/(?!goku)/
})
```

## Recipes

### Rest APIs with Express + Goku

```
// initialize goku
const goku = new Goku();
goku.registerDescriptor('User', ...);
goku.registerDescriptor('Invoice', ...);

// middlewares for serialization

function groups(...data) {
    return function(req, res, next) {
        req.groups = data;
        next();
    };
}

server.use(function(req, res, next) {
    res.goku = function(data) {
        return res.json(goku.serialize(data, req.groups));
    }
    next();
});

function list(req, res, next) {
    const users = ...;
    res.goku(users);
}
<commit_msg>JS readme<commit_after># Goku
> Transform & serialize to simple JS objects

Goku takes simple descriptors and transform + serializes complex JS objects into simple JSON objects.

Features:
- recursion
- es6 support (class name based serialization)
- field aliases
- serialization groups

## Sample

```javascript
// descriptor in user-descriptor.json
{
    // aliases
    number: { as: 'id' },
    // always serialized
    firstName: true,
    lastName: true,
    // not serialized by default
    email: [ 'details' ],
    createdAt: [ 'details' ],
    children: [ 'details' ]
};

const user = new User({
    number: 1,
    firstName: 'Jane',
    lastName: 'Doe',
    email: '<EMAIL>',
    createdAt: new Date(),
    children: [ child1, child2 ]
});

goku.serialize(user);
// --› {
//      id: 1,
//      firstName: 'Jane',
//      lastName: 'Doe'
//  }

goku.serialize(user, ['details']);
// --› {
//      id: 1,
//      firstName: 'Jane',
//      lastName: 'Doe',
//      email: '<EMAIL>',
//      createdAt: [ Date ],
//      children: [
//          {...},
//          {...}
//      ]
//  }
```

## Usage

### Installation

```
$ npm install goku
```

Then

```
// ES6
import Goku from 'goku';
// ES5
var Goku = require('goku');
```

### Defining and registering your descriptors

```json
// in user-descriptor.json
{
    "firstName": true,
    "lastName": true,
    "email": [ "details" ]
}
```

```javascript
import Goku from 'goku';
const goku = new Goku();
goku.registerDescriptor('User', require('user-descriptor.json'));
goku.serialize(user);
```

### Using in an ES6 project

```javascript
require('babel/register')({
    stage: 1,
    ignore: /node_modules\/(?!goku)/
})
```

## Recipes

### Rest APIs with Express + Goku

```javascript
// initialize goku
const goku = new Goku();
goku.registerDescriptor('User', ...);
goku.registerDescriptor('Invoice', ...);

// middlewares for serialization

function groups(...data) {
    return function(req, res, next) {
        req.groups = data;
        next();
    };
}

server.use(function(req, res, next) {
    res.goku = function(data) {
        return res.json(goku.serialize(data, req.groups));
    }
    next();
});

function list(req, res, next) {
    const users = ...;
    res.goku(users);
}

{'fraction_non_alphanumeric': 0.18302976454908929, 'fraction_numerical': 0.00444247001332741, 'mean_word_length': 2.637318255250404, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1284534', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1499, 'n_words': 388}