Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 28, 2018
1 parent d9960c7 commit 8696b61
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js: node_js:
- '8' - '8'
- '6' - '6'
- '4'
46 changes: 24 additions & 22 deletions index.js
@@ -1,21 +1,21 @@
'use strict'; 'use strict';


function preserveCamelCase(str) { const preserveCamelCase = input => {
let isLastCharLower = false; let isLastCharLower = false;
let isLastCharUpper = false; let isLastCharUpper = false;
let isLastLastCharUpper = false; let isLastLastCharUpper = false;


for (let i = 0; i < str.length; i++) { for (let i = 0; i < input.length; i++) {
const c = str[i]; const c = input[i];


if (isLastCharLower && /[a-zA-Z]/.test(c) && c.toUpperCase() === c) { if (isLastCharLower && /[a-zA-Z]/.test(c) && c.toUpperCase() === c) {
str = str.substr(0, i) + '-' + str.substr(i); input = input.slice(0, i) + '-' + input.slice(i);
isLastCharLower = false; isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper; isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true; isLastCharUpper = true;
i++; i++;
} else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(c) && c.toLowerCase() === c) { } else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(c) && c.toLowerCase() === c) {
str = str.substr(0, i - 1) + '-' + str.substr(i - 1); input = input.slice(0, i - 1) + '-' + input.slice(i - 1);
isLastLastCharUpper = isLastCharUpper; isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = false; isLastCharUpper = false;
isLastCharLower = true; isLastCharLower = true;
Expand All @@ -26,44 +26,46 @@ function preserveCamelCase(str) {
} }
} }


return str; return input;
} };


module.exports = function (str, opts) { module.exports = (input, options) => {
opts = Object.assign({ options = Object.assign({
pascalCase: false pascalCase: false
}, opts); }, options);

const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;


if (Array.isArray(str)) { if (Array.isArray(input)) {
str = str.map(x => x.trim()) input = input.map(x => x.trim())
.filter(x => x.length) .filter(x => x.length)
.join('-'); .join('-');
} else { } else {
str = str.trim(); input = input.trim();
} }


if (str.length === 0) { if (input.length === 0) {
return ''; return '';
} }


if (str.length === 1) { if (input.length === 1) {
return opts.pascalCase ? str.toUpperCase() : str.toLowerCase(); return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
} }


if (/^[a-z0-9]+$/.test(str)) { if (/^[a-z\d]+$/.test(input)) {
return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str; return postProcess(input);
} }


const hasUpperCase = str !== str.toLowerCase(); const hasUpperCase = input !== input.toLowerCase();


if (hasUpperCase) { if (hasUpperCase) {
str = preserveCamelCase(str); input = preserveCamelCase(input);
} }


str = str input = input
.replace(/^[_.\- ]+/, '') .replace(/^[_.\- ]+/, '')
.toLowerCase() .toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase()); .replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase());


return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str; return postProcess(input);
}; };
8 changes: 5 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{ {
"name": "camelcase", "name": "camelcase",
"version": "4.1.0", "version": "4.1.0",
"description": "Convert a dash/dot/underscore/space separated string to camelCase: foo-bar → fooBar", "description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar``fooBar`",
"license": "MIT", "license": "MIT",
"repository": "sindresorhus/camelcase", "repository": "sindresorhus/camelcase",
"author": { "author": {
Expand All @@ -10,7 +10,7 @@
"url": "sindresorhus.com" "url": "sindresorhus.com"
}, },
"engines": { "engines": {
"node": ">=4" "node": ">=6"
}, },
"scripts": { "scripts": {
"test": "xo && ava" "test": "xo && ava"
Expand All @@ -30,7 +30,9 @@
"separator", "separator",
"string", "string",
"text", "text",
"convert" "convert",
"pascalcase",
"pascal-case"
], ],
"devDependencies": { "devDependencies": {
"ava": "*", "ava": "*",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
@@ -1,6 +1,6 @@
# camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase) # camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase)


> Convert a dash/dot/underscore/space separated string to camelCase: `foo-bar``fooBar` or PascalCase: `foo-bar``FooBar` > Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar``fooBar`


## Install ## Install
Expand Down
4 changes: 2 additions & 2 deletions test.js
@@ -1,7 +1,7 @@
import test from 'ava'; import test from 'ava';
import m from '.'; import m from '.';


test('camelCase default - with no options object', t => { test('camelCase', t => {
t.is(m('foo'), 'foo'); t.is(m('foo'), 'foo');
t.is(m('foo-bar'), 'fooBar'); t.is(m('foo-bar'), 'fooBar');
t.is(m('foo-bar-baz'), 'fooBarBaz'); t.is(m('foo-bar-baz'), 'fooBarBaz');
Expand Down Expand Up @@ -49,7 +49,7 @@ test('camelCase default - with no options object', t => {
t.is(m([]), ''); t.is(m([]), '');
}); });


test('camelCase with pascalCase option set to true', t => { test('camelCase with pascalCase option', t => {
t.is(m('foo', {pascalCase: true}), 'Foo'); t.is(m('foo', {pascalCase: true}), 'Foo');
t.is(m('foo-bar', {pascalCase: true}), 'FooBar'); t.is(m('foo-bar', {pascalCase: true}), 'FooBar');
t.is(m('foo-bar-baz', {pascalCase: true}), 'FooBarBaz'); t.is(m('foo-bar-baz', {pascalCase: true}), 'FooBarBaz');
Expand Down

0 comments on commit 8696b61

Please sign in to comment.