Giter Site home page Giter Site logo

caio-ribeiro-pereira / field-normalizer Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 2.0 890 KB

A tiny lib for object key's normalization, transform camelCase { fullName } objects to snakeCase { full_name } or other patterns.

Home Page: https://www.npmjs.com/package/field-normalizer

License: MIT License

JavaScript 100.00%
javascript javascript-library nodejs nodejs-modules object object-transform object-normalization object-keys pure-javascript

field-normalizer's Introduction

Field Normalizer

Donate via Paypal Build Status Codacy Badge Coverage Status npm GitHub npm GitHub stars GitHub forks

A tiny lib for object field's normalization.

About

This is a tiny lib (~14kb size unpacked) compatible with Node.js v11+, useful to format the name of all keys in a object.
For example, imagine you have a frontend in JavaScript which uses the camelCase ({ fullName, contactInfo }) pattern for object's naming convention and you have a ruby on rails's backend api which uses the snakeCase convention ({ full_name, contact_info }), sometimes the data flow can be a messy if you don't keep these objects normalized between these frontend and backend, and that's why this tiny lib was born.

How to install

npm install --save field-normalizer

How to use

Object example:

const FNZ = require('field-normalizer');

const exampleObj = {
  fullName: 'John Connor',
  contactEmail: '[email protected]',
  phoneNumber: '+05533334444',
};

Transforming all object fields to lower case:

FNZ.toLowerCase(exampleObj);
// { fullname, contactemail, phonenumber }

Transforming all object fields to upper case:

FNZ.toUpperCase(exampleObj);
// { FULLNAME, CONTACTEMAIL, PHONENUMBER }

Transforming all object fields to camel case:

FNZ.toCamelCase(exampleObj);
// { fullName, contactEmail, phoneNumber }

Transforming all object fields to pascal case:

FNZ.toPascalCase(exampleObj);
// { FullName, ContactEmail, PhoneNumber }

Transforming all object fields to constant case:

FNZ.toConstantCase(exampleObj);
// { FULL_NAME, CONTACT_EMAIL, PHONE_NUMBER }

Transforming all object fields to snake case:

FNZ.toSnakeCase(exampleObj);
// { full_name, contact_email, phone_number }

Transforming all object fields to header case:

FNZ.toHeaderCase(exampleObj);
// { Full-Name, Contact-Email, Phone-Number }

Transforming all object fields to param case:

FNZ.toParamCase(exampleObj);
// { full-name, contact-email, phone-number }

Constant of all public functions:

console.log(FNZ.FUNCTIONS)

Factory transform() for function dynamic invokation:

FNZ.transform(exampleObj, FNZ.FUNCTIONS.camelCase);

Author

Caio Ribeiro Pereira [email protected]
Twitter: https://twitter.com/crp_underground
About me: https://crpwebdev.github.io

field-normalizer's People

Contributors

caio-ribeiro-pereira avatar kleber-gueriero avatar sunxiaoshen-booker avatar yklydxtt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

field-normalizer's Issues

Unhandled objects can bug whole code flow

When using it to normalize something like an API request response, sometimes the incoming object won't attend to the "valid object"'s of the lib.
In this case, just returning the original object would make everything work just as expected, while the current behaviour breaks the application.

e.g.:
If I call FieldNormalizer.toCamelCase(error.errors), where error has a format like this:

{
  "errors": {
    "user_id": ["cant be blank", "already in use"],
    "amount": ["cant be blank"]
  }
}

Doesn't handle arrays

When an object A has an attribute B as an array, it doesn't normalize the items inside this array.
e.g:

a = { b: [ { c_minus: 1} ] }
FieldNormalizer.toCamelCase(a)
=> { b: [ { c_minus: 1} ] }

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.