diff --git a/README.md b/README.md index 5767258..a583a6c 100644 --- a/README.md +++ b/README.md @@ -30,22 +30,27 @@ var newObject = changeCaseObject.camelCase(myObject); All methods are available under the `changeCaseObject` object after the module has been required. ### .camelCase -Conerts all object keys into camel case. +Converts all object keys into camel case. `hello_world -> helloWorld` ### .snakeCase -Conerts all object keys into snake case. +Converts all object keys into snake case. `helloWorld -> hello_world` ### .paramCase -Conerts all object keys into param case. +Converts all object keys into param case. `helloWorld -> hello-world` +### .constantCase +Converts all object keys into constant case. +`helloWorld -> HELLO_WORLD` + Shorthand methods are also available: ``` .camelCase -> .camel .snakeCase -> .snake .paramCase -> .param +.constantCase -> .constant ``` ## Code Guideline diff --git a/index.js b/index.js index 53b57ad..cbcc8b2 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var camelCase = require('camel-case'); var snakeCase = require('snake-case'); var paramCase = require('param-case'); +var constantCase = require('constant-case').constantCase; var changeKeys = function changeKeys(transformer, obj) { var objectKeys; @@ -56,4 +57,12 @@ changeCaseObject.param = changeCaseObject.paramCase = function paramCaseObject(o return changeKeys(paramCase, obj); }; +changeCaseObject.constant = changeCaseObject.constantCase = function constantCaseObject(obj) { + if (typeof obj === 'string') { + return constantCase(obj); + } + + return changeKeys(constantCase, obj); +}; + module.exports = changeCaseObject; diff --git a/package.json b/package.json index afc2176..b969008 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "homepage": "https://github.com/BinaryThumb/change-case-object#readme", "dependencies": { "camel-case": "^1.2.0", + "constant-case": "^3.0.3", "param-case": "^1.1.1", "snake-case": "^1.1.1" }, diff --git a/test/index.js b/test/index.js index 002f377..8f50830 100644 --- a/test/index.js +++ b/test/index.js @@ -293,4 +293,101 @@ describe('change-case-object', function () { expect(changeCaseObject.snakeCase(Infinity)).to.equal(Infinity); expect(changeCaseObject.snakeCase(NaN)).to.a('number'); }); + + it('constantCase', function () { + var initialObj = { + 'helloWorld': 'test', + }; + + var fixtureObj = { + 'HELLO_WORLD': 'test', + }; + + expect(changeCaseObject.constantCase(initialObj)).to.deep.equal(fixtureObj); + }); + + it('constantCase (special characters)', function () { + var initialObj = { + 'helloWorld': 'n#mä!%ads$§a', + }; + + var fixtureObj = { + 'HELLO_WORLD': 'n#mä!%ads$§a', + }; + expect(changeCaseObject.constantCase(initialObj)).to.deep.equal(fixtureObj); + }); + + it('constantCase (Array of Object)', function () { + var initialArrObj = [ + { 'helloWorld': 'test' }, + { isActive: true }, + { 'is-Active': true }, + ]; + + var fixtureArrObj = [ + { 'HELLO_WORLD': 'test' }, + { IS_ACTIVE: true }, + { IS_ACTIVE: true }, + ]; + + expect(changeCaseObject.constantCase(initialArrObj)).to.deep.equal(fixtureArrObj); + }); + + it('constantCase (Array of string)', function () { + var initialArrStr = ['isActive', 'isBlocked']; + + var fixtureArrStr = ['IS_ACTIVE', 'IS_BLOCKED']; + + expect(changeCaseObject.constantCase(initialArrStr)).to.deep.equal(fixtureArrStr); + }); + + it('constantCase (Primitive - string)', function () { + var initialPrimitive = 'isActive'; + + var fixturePrimitive = 'IS_ACTIVE'; + + expect(changeCaseObject.constantCase(initialPrimitive)).to.deep.equal(fixturePrimitive); + }); + + it('constantCase (Primitive - number)', function () { + var initialPrimitive = 42; + + var fixturePrimitive = 42; + + expect(changeCaseObject.constantCase(initialPrimitive)).to.deep.equal(fixturePrimitive); + }); + + it('constantCase (Primitive - bool)', function () { + var initialPrimitive = true; + + var fixturePrimitive = true; + + expect(changeCaseObject.constantCase(initialPrimitive)).to.deep.equal(fixturePrimitive); + }); + + it('constantCase (deep)', function () { + var initialObj = { + 'helloWorld': { + 'helloThere': 'name', + 'myName': 'someone', + }, + }; + + var fixtureObj = { + 'HELLO_WORLD': { + 'HELLO_THERE': 'name', + 'MY_NAME': 'someone', + }, + }; + + expect(changeCaseObject.constantCase(initialObj)).to.deep.equal(fixtureObj); + }); + + it('constantCase (edge cases)', function () { + expect(changeCaseObject.constantCase(null)).to.equal(null); + expect(changeCaseObject.constantCase({})).to.a('object'); + expect(changeCaseObject.constantCase([])).to.a('array'); + expect(changeCaseObject.constantCase(Infinity)).to.equal(Infinity); + expect(changeCaseObject.constantCase(NaN)).to.a('number'); + }); });