Permalink
Browse files

Require Node.js 6

  • Loading branch information...
sindresorhus committed Mar 28, 2018
1 parent d9960c7 commit 8696b619769fe52c0167e0567a99788cc2969ca7
Showing with 32 additions and 29 deletions.
  1. +0 −1 .travis.yml
  2. +24 −22 index.js
  3. +5 −3 package.json
  4. +1 −1 readme.md
  5. +2 −2 test.js
View
@@ -2,4 +2,3 @@ language: node_js
node_js:
- '8'
- '6'
- '4'
View
@@ -1,21 +1,21 @@
'use strict';
function preserveCamelCase(str) {
const preserveCamelCase = input => {
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;
for (let i = 0; i < str.length; i++) {
const c = str[i];
for (let i = 0; i < input.length; i++) {
const c = input[i];
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;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
i++;
} 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;
isLastCharUpper = false;
isLastCharLower = true;
@@ -26,44 +26,46 @@ function preserveCamelCase(str) {
}
}
return str;
}
return input;
};
module.exports = function (str, opts) {
opts = Object.assign({
module.exports = (input, options) => {
options = Object.assign({
pascalCase: false
}, opts);
}, options);
const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
if (Array.isArray(str)) {
str = str.map(x => x.trim())
if (Array.isArray(input)) {
input = input.map(x => x.trim())
.filter(x => x.length)
.join('-');
} else {
str = str.trim();
input = input.trim();
}
if (str.length === 0) {
if (input.length === 0) {
return '';
}
if (str.length === 1) {
return opts.pascalCase ? str.toUpperCase() : str.toLowerCase();
if (input.length === 1) {
return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
}
if (/^[a-z0-9]+$/.test(str)) {
return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str;
if (/^[a-z\d]+$/.test(input)) {
return postProcess(input);
}
const hasUpperCase = str !== str.toLowerCase();
const hasUpperCase = input !== input.toLowerCase();
if (hasUpperCase) {
str = preserveCamelCase(str);
input = preserveCamelCase(input);
}
str = str
input = input
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase());
return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str;
return postProcess(input);
};
View
@@ -1,7 +1,7 @@
{
"name": "camelcase",
"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",
"repository": "sindresorhus/camelcase",
"author": {
@@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
@@ -30,7 +30,9 @@
"separator",
"string",
"text",
"convert"
"convert",
"pascalcase",
"pascal-case"
],
"devDependencies": {
"ava": "*",
View
@@ -1,6 +1,6 @@
# 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
View
@@ -1,7 +1,7 @@
import test from 'ava';
import m from '.';
test('camelCase default - with no options object', t => {
test('camelCase', t => {
t.is(m('foo'), 'foo');
t.is(m('foo-bar'), 'fooBar');
t.is(m('foo-bar-baz'), 'fooBarBaz');
@@ -49,7 +49,7 @@ test('camelCase default - with no options object', t => {
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-bar', {pascalCase: true}), 'FooBar');
t.is(m('foo-bar-baz', {pascalCase: true}), 'FooBarBaz');

0 comments on commit 8696b61

Please sign in to comment.