Skip to content

Commit

Permalink
feat: string.camelToHyphen method
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 25, 2022
1 parent 1bf039a commit b8ea4ab
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ npm install ext
- `String`
- [`random`](docs/string/random.md)
- `String.prototype`
- [`campelToHyphen`](docs/string_/camel-to-hyphen.md)
- [`includes`](docs/string_/includes.md)
- `Thenable.prototype`
- [`finally`](docs/thenable_/finally.md)
9 changes: 0 additions & 9 deletions _es5-ext/string/#/camel-to-hyphen.js

This file was deleted.

7 changes: 0 additions & 7 deletions _es5-ext/test/string/#/camel-to-hyphen.js

This file was deleted.

9 changes: 9 additions & 0 deletions docs/string_/camel-to-hyphen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `string.camelToHyphen()` _(ext/string\_/camel-to-hyphen)_

Convert camelCase string to hyphen separated, e.g. `oneTwoThree` into `one-to-three`. Useful when converting names from js property convention into filename convention.

```javascript
const camelToHyphen = require("ext/string_/camelToHyphen");

camelToHyphen.call("razDwaTrzy"); // raz-dwa-trzy
```
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
"globalThis": true
}
},
{
"files": "string_/camel-to-hyphen.js",
"rules": {
"id-length": "off"
}
},
{
"files": "test/**/*.js",
"env": {
Expand Down
49 changes: 49 additions & 0 deletions string_/camel-to-hyphen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";

var ensureString = require("type/string/ensure")
, objHasOwnProperty = Object.prototype.hasOwnProperty;

var capitalLetters = {
A: true,
B: true,
C: true,
D: true,
E: true,
F: true,
G: true,
H: true,
I: true,
J: true,
K: true,
L: true,
M: true,
N: true,
O: true,
P: true,
Q: true,
R: true,
S: true,
T: true,
U: true,
V: true,
W: true,
X: true,
Y: true,
Z: true
};

module.exports = function () {
var input = ensureString(this);
if (!input) return input;
var outputLetters = [];
for (var index = 0, letter; (letter = input[index]); ++index) {
if (objHasOwnProperty.call(capitalLetters, letter)) {
if (index) outputLetters.push("-");
outputLetters.push(letter.toLowerCase());
} else {
outputLetters.push(letter);
}
}

return outputLetters.join("");
};
13 changes: 13 additions & 0 deletions test/string_/camel-to-hyphen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

var assert = require("chai").assert;

var camelToHyphen = require("../../string_/camel-to-hyphen");

describe("string_/camel-to-hyphen", function () {
it("should convert camel notation to hyphen", function () {
assert.equal(camelToHyphen.call("razDwaTRzy4yFoo45My"), "raz-dwa-t-rzy4y-foo45-my");
assert.equal(camelToHyphen.call("razDwaTRzy4yFoo45My-"), "raz-dwa-t-rzy4y-foo45-my-");
assert.equal(camelToHyphen.call("razDwaTRzy4yFoo45My--"), "raz-dwa-t-rzy4y-foo45-my--");
});
});

0 comments on commit b8ea4ab

Please sign in to comment.