Nanotechnology Promises Much
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

var-style

0.0.1 • Public • Published

var-style var-style

Transform variable style between camel case and snake case.

In node world, all our node code are camel case style. But sometimes, our rest api params or other api params are snake case style. So we need a transformer.

Usage

var transformer = require('var-style');
 
//transform input as key
transformer.toSnake('userName').should.equal('user_name');
transformer.toCamel('user_name').should.equal('userName');
 
//tranform input as object
transformer.camelToSnake({testData: [1, 2, 3]}).should.eql({test_data: [1, 2, 3]});
transformer.snakeToCamel({test_data: {second_level: 1}}).should.eql({testData: {secondLevel: 1}});
 
//You can use your own tranformer function 
function customToCamel(key, reg) {
  return key.replace(/_([a-z])/g, function (all, a) {
    return a.toUpperCase();
  });
};
transformer.snakeToCamel({test_data: {second_level: 1}}, customToCamel).should.eql({testData: {secondLevel: 1}});
 
function customToSnake(key, reg) {
  return key.replace(/[A-Z][a-z]/g, function (a) {
    return '_' + a.toLowerCase();
  });
}
transformer.camelToSnake({testData: {secondLevel: 1}}, customToSnake).should.eql({test_data: {second_level: 1}});

License

MIT

Keywords

none

install

npm i var-style

Downloadsweekly downloads

1

version

0.0.1

license

none

last publish

collaborators

  • avatar
Report a vulnerability