Skip to content

Commit

Permalink
Make exception for numbers in filename-case. Fixes #25 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and sindresorhus committed May 20, 2016
1 parent 57dac9c commit 198bb6a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
25 changes: 24 additions & 1 deletion rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ var pascalCase = function (str) {
return upperfirst(camelCase(str));
};

var numberRegex = /(\d+)/;
var PLACEHOLDER = '\uFFFF\uFFFF\uFFFF';
var PLACEHOLDER_REGEX = new RegExp(PLACEHOLDER, 'i');

function ignoreNumbers(fn) {
return function (string) {
var stack = [];
var execResult = numberRegex.exec(string);
while (execResult) {
stack.push(execResult[0]);
string = string.replace(execResult[0], PLACEHOLDER);
execResult = numberRegex.exec(string);
}

var withCase = fn(string);
while (stack.length > 0) {
withCase = withCase.replace(PLACEHOLDER_REGEX, stack.shift());
}

return withCase;
};
}

var cases = {
camelCase: {
fn: camelCase,
Expand All @@ -31,7 +54,7 @@ var cases = {
function fixFilename(chosenCase, filename) {
return filename
.split('.')
.map(chosenCase.fn)
.map(ignoreNumbers(chosenCase.fn))
.join('.');
}

Expand Down
14 changes: 14 additions & 0 deletions test/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ test(() => {
testCase('src/foo/Foo.Test.js', 'pascalCase'),
testCase('src/foo/FooBar.Test.js', 'pascalCase'),
testCase('src/foo/FooBar.TestUtils.js', 'pascalCase'),
testCase('spec/iss47Spec.js', 'camelCase'),
testCase('spec/iss47Spec100.js', 'camelCase'),
testCase('spec/i18n.js', 'camelCase'),
testCase('spec/iss47-spec.js', 'kebabCase'),
testCase('spec/iss-47-spec.js', 'kebabCase'),
testCase('spec/iss47-100spec.js', 'kebabCase'),
testCase('spec/i18n.js', 'kebabCase'),
testCase('spec/iss47_spec.js', 'snakeCase'),
testCase('spec/iss_47_spec.js', 'snakeCase'),
testCase('spec/iss47_100spec.js', 'snakeCase'),
testCase('spec/i18n.js', 'snakeCase'),
testCase('spec/Iss47Spec.js', 'pascalCase'),
testCase('spec/Iss47.100Spec.js', 'pascalCase'),
testCase('spec/I18n.js', 'pascalCase'),
testCase('<text>', 'camelCase'),
testCase('<text>', 'snakeCase'),
testCase('<text>', 'kebabCase'),
Expand Down

0 comments on commit 198bb6a

Please sign in to comment.