Coder Social home page Coder Social logo

strato / object-case-converter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ridi/object-case-converter

0.0 0.0 0.0 112 KB

Convert keys in JavaScript object or Array (camelCase, snake_case, etc.)

License: MIT License

JavaScript 4.52% TypeScript 95.48%

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

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.