0.2.1 • Published 7 years ago

body-shaper v0.2.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

body-shaper

Put bodies in good shape. The goal is to make objects with expected structure safe to navigate. Useful for normalizing (shaping) raw request payload.

Installation

npm install body-shaper

Examples

const shaper = require('body-shaper');
    
const shape = shaper({
    username: String,
    dateOfBirth: Date,
    isPublic: Boolean,
    experiences: [
        {
            company: String,
            years: {
                from: Number,
                to: Number
            }
        }
    ]
});


> shape({})
{
    username: '',
    dateOfBirth: null,
    idPublic: null,
    experiences: []
}


> shape({
>     username: '    Aleks ',
>     isPublic: 'yes',
>     dateOfBirth: '1987-12-01',
>     experiences: [
>         {
>             company: 'Slow Motion Software'
>         }
>     ]
> }
{
    username: 'Aleks',
    isPublic: true,
    dateOfBirth: new Date('1987-12-01'),
    experiences: [
        {
            company: 'Slow Motion Software',
            years: {
                from: null,
                to: null
            }
        }
    ]
}

Extending

Feel free to use your own shapers.

const shape = shaper({
    tags: (string) => 
        String(string || '').split(',')
            .map(tag => tag.trim())
            .filter(tag => tag)
})

> shape({
>     tags: 'hello, world,     ,, ,yay',
> })
{
    tags: ['hello', 'world', 'yay']
}
0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago