-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds leading dash flag feature and corresponding tests #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,16 @@ | |
var KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g; | ||
var REVERSE_REGEX = /-[a-z\u00E0-\u00F6\u00F8-\u00FE]/g; | ||
|
||
module.exports = exports = function kebabCase(str) { | ||
return str.replace(KEBAB_REGEX, function (match) { | ||
return '-' + match.toLowerCase(); | ||
module.exports = exports = function kebabCase(str, leadingDashFlag) { | ||
let kebab = str.replace(KEBAB_REGEX, function (match) { | ||
return '-' + match | ||
}); | ||
let firstChar = kebab.charAt(1); | ||
return ( | ||
leadingDashFlag === false && | ||
firstChar.toUpperCase() === firstChar | ||
? kebab.slice(1).toLowerCase() : kebab.toLowerCase() | ||
); | ||
Comment on lines
+9
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you are mixing tabs and spaces... What about simply writing the feature like this instead: module.exports = exports = function kebabCase(str, stripLeadingDash) {
const kebab = str.replace(KEBAB_REGEX, function (match) {
return '-' + match.toLowerCase();
});
return stripLeadingDash && kebab[0] === '-' ? kebab.slice(1) : kebab;
}; To me that is more readable and more clearly does what it says. What do you think? |
||
}; | ||
|
||
exports.reverse = function (str) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,13 @@ test("the reverse", (t) => { | |
const str = "Hallå, Mr. Kebab Überstein! How you doin'?-"; | ||
t.equal(kebabCase.reverse(kebabCase(str)), str); | ||
}); | ||
|
||
test("string with leading dash", (t) => { | ||
const str = "KebabCase"; | ||
t.equal(kebabCase(str, false), "kebab-case"); | ||
}) | ||
|
||
test("string reverse without leading dash", (t) => { | ||
const str = "-kebab-case"; | ||
t.equal(reverse(str), "KebabCase"); | ||
}) | ||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't do what it says... According to the test's title the variable should be: const str = "kebab-case"; But that will give the result: t.equal(reverse(str), "kebabCase"); Which is not the reverse of what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's also a missing newline at the end of the file... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"leadingDashFlag" should be named something better. Perhaps "stripLeadingDash"?