Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
97 changes: 97 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});