Skip to content

Commit

Permalink
ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 8, 2016
1 parent 164a7e6 commit 2296030
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 72 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
sudo: false
language: node_js
node_js:
- '5'
- '6'
- '4'
- '0.12'
- '0.10'
28 changes: 11 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

function preserveCamelCase(str) {
var isLastCharLower = false;
var isLastCharUpper = false;
var isLastLastCharUpper = false;
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;

for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
for (let i = 0; i < str.length; i++) {
const c = str.charAt(i);

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

since it's all modern, not worrying about IE8, you can use str[i] instead of charAt.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 8, 2016

Author Owner

There's a difference though. Didn't look closely whether it matters here.

let x = '';

x.charAt(0);
//=> ''

x[0];
//=> undefined

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

Nice distinction! Since you're in a loop you won't hit that case.
Speaking of loops it could be a for-of :D

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 8, 2016

Author Owner

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

Ah!


if (isLastCharLower && (/[a-zA-Z]/).test(c) && c.toUpperCase() === c) {
str = str.substr(0, i) + '-' + str.substr(i);

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

You can use str.slice instead of str.substr. It's closer to spec since String#slice is in the spec proper and String#substr is in an appendix.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 8, 2016

Author Owner

Yeah, I was too lazy to change it out. Waiting for autofixing here: sindresorhus/eslint-plugin-unicorn#67

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

Ohh fancy!

Expand All @@ -20,21 +20,17 @@ function preserveCamelCase(str) {
isLastCharUpper = false;
isLastCharLower = true;
} else {
isLastCharLower = (c.toLowerCase() === c);
isLastCharLower = c.toLowerCase() === c;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = (c.toUpperCase() === c);
isLastCharUpper = c.toUpperCase() === c;
}
}

return str;
}

module.exports = function () {

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

arrow function?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 8, 2016

Author Owner

Can't... Need the arguments, and rest arguments are Node.js 6...

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

Ah, I always forget about that.

var str = [].map.call(arguments, function (str) {
return str.trim();
}).filter(function (str) {
return str.length;
}).join('-');
let str = [].map.call(arguments, x => x.trim()).filter(x => x.length).join('-');

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 8, 2016

Maybe Array.from?


if (str.length === 0) {
return '';
Expand All @@ -47,9 +43,7 @@ module.exports = function () {
str = preserveCamelCase(str);

return str
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, function (m, p1) {
return p1.toUpperCase();
});
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase());
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "http://sindresorhus.com"
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -35,5 +35,8 @@
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ camelCase('__foo__', '--bar');

## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
90 changes: 45 additions & 45 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import test from 'ava';
import fn from './';
import m from './';

test('camelCase', t => {
t.is(fn('foo'), 'foo');
t.is(fn('foo-bar'), 'fooBar');
t.is(fn('foo-bar-baz'), 'fooBarBaz');
t.is(fn('foo--bar'), 'fooBar');
t.is(fn('--foo-bar'), 'fooBar');
t.is(fn('--foo--bar'), 'fooBar');
t.is(fn('FOO-BAR'), 'fooBar');
t.is(fn('FOÈ-BAR'), 'foèBar');
t.is(fn('-foo-bar-'), 'fooBar');
t.is(fn('--foo--bar--'), 'fooBar');
t.is(fn('foo.bar'), 'fooBar');
t.is(fn('foo..bar'), 'fooBar');
t.is(fn('..foo..bar..'), 'fooBar');
t.is(fn('foo_bar'), 'fooBar');
t.is(fn('__foo__bar__'), 'fooBar');
t.is(fn('__foo__bar__'), 'fooBar');
t.is(fn('foo bar'), 'fooBar');
t.is(fn(' foo bar '), 'fooBar');
t.is(fn('-'), '-');
t.is(fn(' - '), '-');
t.is(fn('fooBar'), 'fooBar');
t.is(fn('fooBar-baz'), 'fooBarBaz');
t.is(fn('foìBar-baz'), 'foìBarBaz');
t.is(fn('fooBarBaz-bazzy'), 'fooBarBazBazzy');
t.is(fn('FBBazzy'), 'fbBazzy');
t.is(fn('F'), 'f');
t.is(fn('FooBar'), 'fooBar');
t.is(fn('Foo'), 'foo');
t.is(fn('FOO'), 'foo');
t.is(fn('foo', 'bar'), 'fooBar');
t.is(fn('foo', '-bar'), 'fooBar');
t.is(fn('foo', '-bar', 'baz'), 'fooBarBaz');
t.is(fn('', ''), '');
t.is(fn('--'), '');
t.is(fn(''), '');
t.is(fn('--__--_--_'), '');
t.is(fn('---_', '--', '', '-_- '), '');
t.is(fn('foo bar?'), 'fooBar?');
t.is(fn('foo bar!'), 'fooBar!');
t.is(fn('foo bar$'), 'fooBar$');
t.is(fn('foo-bar#'), 'fooBar#');
t.is(fn('XMLHttpRequest'), 'xmlHttpRequest');
t.is(fn('AjaxXMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(fn('Ajax-XMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(m('foo'), 'foo');
t.is(m('foo-bar'), 'fooBar');
t.is(m('foo-bar-baz'), 'fooBarBaz');
t.is(m('foo--bar'), 'fooBar');
t.is(m('--foo-bar'), 'fooBar');
t.is(m('--foo--bar'), 'fooBar');
t.is(m('FOO-BAR'), 'fooBar');
t.is(m('FOÈ-BAR'), 'foèBar');
t.is(m('-foo-bar-'), 'fooBar');
t.is(m('--foo--bar--'), 'fooBar');
t.is(m('foo.bar'), 'fooBar');
t.is(m('foo..bar'), 'fooBar');
t.is(m('..foo..bar..'), 'fooBar');
t.is(m('foo_bar'), 'fooBar');
t.is(m('__foo__bar__'), 'fooBar');
t.is(m('__foo__bar__'), 'fooBar');
t.is(m('foo bar'), 'fooBar');
t.is(m(' foo bar '), 'fooBar');
t.is(m('-'), '-');
t.is(m(' - '), '-');
t.is(m('fooBar'), 'fooBar');
t.is(m('fooBar-baz'), 'fooBarBaz');
t.is(m('foìBar-baz'), 'foìBarBaz');
t.is(m('fooBarBaz-bazzy'), 'fooBarBazBazzy');
t.is(m('FBBazzy'), 'fbBazzy');
t.is(m('F'), 'f');
t.is(m('FooBar'), 'fooBar');
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('', ''), '');
t.is(m('--'), '');
t.is(m(''), '');
t.is(m('--__--_--_'), '');
t.is(m('---_', '--', '', '-_- '), '');
t.is(m('foo bar?'), 'fooBar?');
t.is(m('foo bar!'), 'fooBar!');
t.is(m('foo bar$'), 'fooBar$');
t.is(m('foo-bar#'), 'fooBar#');
t.is(m('XMLHttpRequest'), 'xmlHttpRequest');
t.is(m('AjaxXMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(m('Ajax-XMLHttpRequest'), 'ajaxXmlHttpRequest');
});

0 comments on commit 2296030

Please sign in to comment.