Skip to content

JSON key/values transformer, to make it fit on your project

License

Notifications You must be signed in to change notification settings

kination/tucson

Repository files navigation

Build Status

TUCSON - TUne/Convert Your jSON

Tucson (/ˈtuːsɒn, tuːˈsɒn/) is a package to convert JSON key/values to make it fit on your project.

Motivation

We usually want to keep unified code format, such as naming rule, spacing, etc. in project. But when code convention between client side(front-end) and server side(back-end) are different, this would cause some exception on it.

For example, I developed client side with javascript and server side with python. But python usually uses snake_case for parameter name, while javascirpt has been developed, in my case, with camelCase. In this situation, some of the javascript needs to be using snake_case, to receive and handle parameters from server.

So this is why I thought about this. By wrapping response data with this and setup option fit to convention in client side, I could keep codes in unified form.

Thanks for reviewers

Usage

This is still in beta status, so API format/name could be changed by version updates.

Installation

// NPM
$ npm install tucson

// YARN
$ yarn add tucson

Example

let { Tucson, Config } = require('tucson')

const dummyUnderscoreJson = {
  "group_id": 139,
  "user": "inylove82@gmail.com",
  "phone_number": "000-111-2222",
  "salary": "Unknown",
  "is_valid": 0,
  "date": "2014-06-01 12:00"
}

let formedResult = {}
let cctucson = new Tucson(new Config('camelCase'))
cctucson.formed(dummyUnderscoreJson, {
  makeDate: ['date'],
  makeBool: ['is_valid']
}, formedResult)
console.log(formedResult)

...result is...

{
    "groupId":139,
    "user":"inylove82@gmail.com",
    "phoneNumber":"000-111-2222",
    "salary":"Unknown",
    "isValid": false,
    "date":"2014-06-01T03:00:00.000Z"
}

Options


Config

It has single parameter(will be updated more...)

Key Name Type Available Value Description
KeyConvertType (optional) string 'camelCase', 'snake_case' Converting key data to defined type

Tucson::formed

Definition of formed is:

Tucson.formed(original-object, option, reformed-object)

Second parameter is JSON format option to select converting rule. More options will be updated.

Key Name Type Description
exclude string[key of original json] Exclude selected keys in reformed object
makeDate string[key of original json] Make value of selected keys as format of Moment.Date
makeBool string[key of original json] Make value of selected keys as boolean.

Contribution

Any type of contribution/feedback(Issue, PR, or Star) is Welcome! Feel free to join.