From 5dfb07ccb5178686f6c9f28e00a39cf9c944838a Mon Sep 17 00:00:00 2001 From: Leonard Martin Date: Wed, 3 Sep 2014 17:11:13 +0100 Subject: [PATCH] First commit --- README.md | 23 +++++++++++++++++++++++ index.js | 21 +++++++++++++++++++++ package.json | 20 ++++++++++++++++++++ test/test.js | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/README.md b/README.md index 139ee12..04c2cd6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ camelify ======== + +Turns hyphen-separated object keys or strings into camel-cased versions. + +Examples +-------- + +`npm install camelify` + +```javascript +camelify('one-two'); +// => 'oneTwo' + +camelify({ + 'first-name': 'John', + 'last-name': 'Smith' +}); +// => { firstName: 'John', 'lastName': 'Smith' } +``` + +Tests +----- + +`npm test` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..74defd6 --- /dev/null +++ b/index.js @@ -0,0 +1,21 @@ +function camelifyString(str) { + return str.replace(/\-([a-z]{1})/g, function (a, s) { return s.toUpperCase(); }, 'g'); +} + +module.exports = function camelify(obj) { + if (typeof obj === 'string') { + return camelifyString(obj); + } + if (typeof obj !== 'object') { + throw new TypeError('argument must be an object'); + } + var output = {}; + for (var i in obj) { + if (typeof i === 'string') { + output[camelifyString(i)] = obj[i]; + } else { + output[i] = obj[i]; + } + } + return output; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ab3b037 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "camelify", + "version": "0.0.1", + "description": "Simple util for mapping an object with hyphen-separated keys to camelCased keys", + "main": "index.js", + "author": "Lenny Martin ", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/lennym/camelify" + }, + "scripts": { + "test": "./node_modules/.bin/mocha test/test.js" + }, + "dependencies": {}, + "devDependencies": { + "chai": "^1.9.1", + "mocha": "^1.21.4" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..228b15a --- /dev/null +++ b/test/test.js @@ -0,0 +1,38 @@ +var camelify = require('../index'); + +require('chai').should(); + +describe('camelify', function () { + + it('maps hyphen-separated object keys to camelCase', function () { + var input = { + 'foo-bar': 1, + 'baz': 2 + }; + camelify(input).should.eql({ + fooBar: 1, + baz: 2 + }); + }); + + it('can handle multiple hyphen separations', function () { + var input = { + 'foo-bar-baz': 1 + }; + camelify(input).should.eql({ + fooBarBaz: 1 + }); + }); + + it('returns a camelified string if passed a string', function () { + camelify('hyphen-separated').should.equal('hyphenSeparated'); + }); + + it('throws on non-object input', function () { + var fn = function () { + camelify(1); + }; + fn.should.throw(); + }); + +}); \ No newline at end of file