Git Product home page Git Product logo

object-case-converter's Introduction

object-case-converter

npm Build Status Greenkeeper badge

Convert keys in an javascript Object to the specific style(camelCase, snake_case, etc.)

Installation

$ npm install --save @ridi/object-case-converter

Usage

ES6

import { camelize, decamelize } from '@ridi/object-case-converter';

const result1 = camelize(null);
// result1 = null

const result2 = camelize({
    id: '1',
    nick_name: 'nick1',
    contacts: [{ contact_type: 'phone', value: '000-000-000' }, { contact_type: 'email', value: '[email protected]' }],
    news_letter: { all_email: false, marketing_email: false },
});
// result2 = {
//    id: '1',
//    nickName: 'nick1',
//    contacts: [{ contact_type: 'phone', value: '000-000-000' }, { contact_type: 'email', value: '[email protected]' }],
//    newsLetter: { all_email: false, marketing_email: false },
//}

const result3 = decamelize([
    { id: '1', nickName: 'nick1', contacts: [{ contactType: 'phone', value: '000-000-000' }, { contactType: 'email', value: '[email protected]' }] },
    { id: '2', nickName: 'nick2', contacts: [] },
    { id: '3', nickName: 'nick3', contacts: [{ contactType: 'address', value: 'xxx' }] },
], true);
// result3 = [
//    { id: '1', nick_name: 'nick1', contacts: [{ contact_type: 'phone', value: '000-000-000' }, { contact_type: 'email', value: '[email protected]' }] },
//    { id: '2', nick_name: 'nick2', contacts: [] },
//    { id: '3', nick_name: 'nick3', contacts: [{ contact_type: 'address', value: 'xxx' }] },
//]

ES5

var converter = require('@ridi/object-case-converter');

converter.camelize(...);
converter.decamelize(...);

Methods

camelize(collection, options = {})

Convert keys in an object or collection to camelCase

  • collection (Array|Object) - object or array to be converted

  • [options] (Object)

      {
          recursive?: true | string[]| { excludes: string[] }
          excludes?: string[] | RegExp | ((key: string) => boolean)
      }
    
    • recursive - convert as recursively
    • excludes - excludes from conversion

decamelize(collection, options = { force: false })

Convert keys that are camelCase only in an object or collection to snake_case

  • collection (Array|Object) - object or array to be converted

  • [options] (Object)

      {
          recursive?: true | string[]| { excludes: string[] }
          exception?: { [key: string]: string | ((key?: string) => string) }
          force?: true
      }
    
    • recursive - convert as recursively
    • exception - convert to specified value
    • force - convert all keys to snake_case

Options

  • recursive: false

    • true -- always convert recursively
    • { excludes: string[] } -- convert when key is not in recursive.excludes array

    When used with the excludes option, excludes keys are also excluded from recursive conversions.

  • excludes: undefined

    • string[] -- excludes from conversion if key is in the array
    • RegExp -- excludes matched keys
    • (key: string) => boolean -- excludes when function are return true
  • exception: {}

    {
      [key: string]: string | ((key?: string) => string)
    }
    
    • string -- key is converted to specific string
    • (key?: string) => string -- converted to return value
  • force: false

    If true, convert without checking that the key is camelCase

Development

$ git clone [email protected]:ridi/object-case-converter.git
$ cd object-case-converter
$ npm install

Build

Transpile TypeScript

$ npm run build

Test

Tests using Jest

$ npm test

object-case-converter's People

Contributors

freekering avatar greenkeeper[bot] avatar gyujincho avatar kyungmi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

object-case-converter's Issues

lodash-es causing issue in ts-node

As node does not support es modules without the --experimental-modules this package is currently not suited to a node app, whilst it works perfectly with babel/webpack.

ts-node does not support --experimental-modules so therefore there is not a way to make this object-case-converter function without modification:

node_modules/lodash-es/lodash.js:10
export { default as add } from './add.js';
^^^^^^

SyntaxError: Unexpected token export

By modifying the lodash imports to:

import camelCase from 'lodash/camelCase';
import  snakeCase from 'lodash/snakeCase';

it should work perfectly with es5 and therefore ts-node.

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.