Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Owner

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"?

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are mixing tabs and spaces...
And you are doing unnecessary calls to toLowerCase() at the end.

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) {
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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 kebabCase outputs with leadingDashFlag=false.
That's my concern about this feature. So that needs to be documented in the readme along with the feature/flag itself.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a missing newline at the end of the file...