0.0.4 • Published 6 years ago

object-keys-parser v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Object Keys Parser

A small lib to translate object keys.

Why?

We can not predict the response of each api with which we work, sometimes we must deal with responses in different languages, or we simply need to normalize some objects keys to fit the application better.

Install

npm install object-keys-parser --save

Usage

Import ObjectParser:

import ObjectParser from 'object-keys-parser'

Create a dictionary:

const adventureDictionary = {
    human: 'finn',
    dog: 'jake',
    vampire: 'marceline'
}

Then instantiate a ObjectParser using the dictionary:

const adventureParser = new ObjectParser(adventureDictionary)

Now you can normalize the object keys or revert the keys to normal:

const items = {
    human: 'sword',
    dog: null,
    vampire: 'axe'
}

adventureParser.parse(items);
/* output:
{
    finn: 'sword',
    jake: null,
    marceline: 'axe'
}
*/
const items = {
    finn: 'sword',
    jake: null,
    marceline: 'axe'
}

adventureParser.revert(items);
/* output:
{
    human: 'sword',
    dog: null,
    vampire: 'axe'
}
*/

It also translate keys recursively:

const friends = {
    human: {
        dog: null
    }
}

adventureParser.parse(friends);
/* output:
{
    finn: {
        jake: null
    }
}
*/