Skip to content

Commit e7e65f2

Browse files
authored
Added the hyphenate function (#28)
* Added the hyphenate function * Made minor changes * Added tests for _truncate
1 parent dbebf85 commit e7e65f2

6 files changed

Lines changed: 88 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Name
6969
- [snakeToCamelCase](./docs/pure/strings.md#snaketocamelcase)
7070
- [camelToSnakeCase](./docs/pure/strings.md#cameltosnakecase)
7171
- [capitalize](./docs/pure/strings.md#capitalize)
72+
- [hyphenate](./docs/pure/strings.md#hyphenate)
7273
- [truncate](./docs/pure/strings.md#truncate)
7374
- [noop](./docs/pure/general.md#noop)
7475
- [toLabelAndValue](./docs/pure/general.md#tolabelandvalue)

docs/pure/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { slugify } from "@bigbinary/neeto-cist";
9696
- [snakeToCamelCase](./strings.md#snaketocamelcase)
9797
- [camelToSnakeCase](./strings.md#cameltosnakecase)
9898
- [capitalize](./strings.md#capitalize)
99+
- [hyphenate](./strings.md#hyphenate)
99100
- [truncate](./strings.md#truncate)
100101

101102
</td>

docs/pure/strings.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
## slugify
44

5-
Curried: false
6-
Failsafe status: alternative available
5+
Curried: false Failsafe status: alternative available
76

87
Converts a given string to slug.
98

109
<details>
1110
<summary>(click for example)</summary>
1211

1312
### Arguments:
13+
1414
- `string`: The string to be converted.
1515

1616
### Usage:
@@ -27,8 +27,7 @@ slugify("my!@#$%^*(quiz"); // "myquiz"
2727

2828
## humanize
2929

30-
Curried: false
31-
Failsafe status: alternative available
30+
Curried: false Failsafe status: alternative available
3231

3332
Converts common developer friendly string formats (camelCase, snake_case,
3433
dash-case etc) to human readable string.
@@ -37,6 +36,7 @@ dash-case etc) to human readable string.
3736
<summary>(click for example)</summary>
3837

3938
### Arguments:
39+
4040
- `string`: The string to be converted.
4141

4242
### Usage:
@@ -52,15 +52,15 @@ humanize("HelloUSA"); // "Hello usa"
5252

5353
## snakeToCamelCase
5454

55-
Curried: false
56-
Failsafe status: alternative available
55+
Curried: false Failsafe status: alternative available
5756

5857
Converts snake_case string to camelCase.
5958

6059
<details>
6160
<summary>(click for example)</summary>
6261

6362
### Arguments:
63+
6464
- `string`: The string to be converted.
6565

6666
### Usage:
@@ -73,15 +73,15 @@ snakeToCamelCase("first_name"); // "firstName"
7373

7474
## camelToSnakeCase
7575

76-
Curried: false
77-
Failsafe status: alternative available
76+
Curried: false Failsafe status: alternative available
7877

7978
Converts camelCase string to snake_case.
8079

8180
<details>
8281
<summary>(click for example)</summary>
8382

8483
### Arguments:
84+
8585
- `string`: The string to be converted.
8686

8787
### Usage:
@@ -94,15 +94,15 @@ camelToSnakeCase("firstName"); // "first_name"
9494

9595
## capitalize
9696

97-
Curried: false
98-
Failsafe status: alternative available
97+
Curried: false Failsafe status: alternative available
9998

10099
Convert the first character of string to upper case.
101100

102101
<details>
103102
<summary>(click for example)</summary>
104103

105104
### Arguments:
105+
106106
- `string`: The string to be converted.
107107

108108
### Usage:
@@ -115,17 +115,42 @@ capitalize("oLIVER"); // "OLIVER"
115115

116116
</details>
117117

118+
## hyphenate
119+
120+
Curried: false Failsafe status: alternative available
121+
122+
The hyphenate function converts strings that contain underscores, spaces, and
123+
camelCase strings into hyphenated strings
124+
125+
<details>
126+
<summary>(click for example)</summary>
127+
128+
### Arguments:
129+
130+
- `string`: The string to be hyphenated.
131+
- `fallbackString`: value to be returned if string is empty
132+
133+
### Usage:
134+
135+
```js
136+
hyphenate("Hello World"); // "hello-world"
137+
hyphenate("hello_world"); // "hello-world"
138+
hyphenate("helloWorld"); // "hello-world"
139+
```
140+
141+
</details>
142+
118143
## truncate
119144

120-
Curried: false
121-
Failsafe status: alternative available
145+
Curried: false Failsafe status: alternative available
122146

123147
Truncate the string with `...` if it is longer than specified string length.
124148

125149
<details>
126150
<summary>(click for example)</summary>
127151

128152
### Arguments:
153+
129154
- `string`: The string to be truncated.
130155
- `length`: The maximum allowed length of the string.
131156

src/strings.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { concat, isNil, slice } from "ramda";
22

33
import { nullSafe } from "./general";
44

5-
export const slugify = (string) =>
5+
export const slugify = string =>
66
string
77
.toString()
88
.toLowerCase()
@@ -13,7 +13,7 @@ export const slugify = (string) =>
1313
.replace(/^-+/, "") // Trim - from start of text
1414
.replace(/-+$/, ""); // Trim - from end of text
1515

16-
export const humanize = (string) => {
16+
export const humanize = string => {
1717
string = string
1818
.replace(/[_-]+/g, " ")
1919
.replace(/\s{2,}/g, " ")
@@ -26,15 +26,29 @@ export const humanize = (string) => {
2626
return string;
2727
};
2828

29-
export const snakeToCamelCase = (string) =>
30-
string.replace(/(_\w)/g, (letter) => letter[1].toUpperCase());
29+
export const snakeToCamelCase = string =>
30+
string.replace(/(_\w)/g, letter => letter[1].toUpperCase());
3131

32-
export const camelToSnakeCase = (string) =>
33-
string.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
32+
export const camelToSnakeCase = string =>
33+
string.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
3434

35-
export const capitalize = (string) =>
35+
export const capitalize = string =>
3636
string.charAt(0).toUpperCase() + string.slice(1);
3737

38+
export const hyphenate = (string, fallbackString) => {
39+
if (typeof string === "number") return String(string);
40+
41+
if (string && typeof string === "string" && string.replace) {
42+
return string
43+
.replace(/[\s_]/g, "-")
44+
.replace(/([a-z])([A-Z])/g, "$1-$2")
45+
.replace(/-+/g, "-")
46+
.toLowerCase();
47+
}
48+
49+
return fallbackString;
50+
};
51+
3852
export const truncate = (string, length) =>
3953
string.length > length ? concat(slice(0, length, string), "...") : string;
4054

@@ -43,5 +57,9 @@ export const _humanize = /*#__PURE__*/ nullSafe(humanize);
4357
export const _snakeToCamelCase = /*#__PURE__*/ nullSafe(snakeToCamelCase);
4458
export const _camelToSnakeCase = /*#__PURE__*/ nullSafe(camelToSnakeCase);
4559
export const _capitalize = /*#__PURE__*/ nullSafe(capitalize);
60+
// eslint-disable-next-line @bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names
61+
export const _hyphenate = (string, length) =>
62+
isNil(string) ? string : hyphenate(string, length);
63+
// eslint-disable-next-line @bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names
4664
export const _truncate = (string, length) =>
4765
isNil(string) ? string : truncate(string, length);

tests/string.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
snakeToCamelCase,
55
slugify,
66
capitalize,
7+
hyphenate,
8+
_hyphenate,
79
truncate,
10+
_truncate,
811
} from "src/strings";
912

1013
describe("String operations", () => {
@@ -41,8 +44,25 @@ describe("String operations", () => {
4144
expect(capitalize("OLIVER")).toBe("OLIVER");
4245
});
4346

47+
test("hyphenate() method should hyphenate the string", () => {
48+
expect(hyphenate("Hello World")).toBe("hello-world");
49+
expect(hyphenate("hello_world")).toBe("hello-world");
50+
expect(hyphenate("helloWorld")).toBe("hello-world");
51+
});
52+
53+
test("_hyphenate() method should hyphenate the string", () => {
54+
expect(_hyphenate("Hello World")).toBe("hello-world");
55+
expect(_hyphenate("hello_world")).toBe("hello-world");
56+
expect(_hyphenate("helloWorld")).toBe("hello-world");
57+
});
58+
4459
test("truncate() method should truncate the string if it is longer than specified string length", () => {
4560
expect(truncate("Hello World", 5)).toBe("Hello...");
4661
expect(truncate("Hello World", 15)).toBe("Hello World");
4762
});
63+
64+
test("_truncate() method should truncate the string if it is longer than specified string length", () => {
65+
expect(_truncate("Hello World", 5)).toBe("Hello...");
66+
expect(_truncate("Hello World", 15)).toBe("Hello World");
67+
});
4868
});

typeTemplates/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ export function getRandomInt(a?: number, b?: number): number;
176176
export function humanize(string: string): string;
177177
export function _humanize(string: NilOr<string>): NilOr<string>;
178178

179+
export function hyphenate(string: string): string;
180+
export function _hyphenate(string: NilOr<string>): NilOr<string>;
181+
179182
export function isNot(a: any, b: any): boolean;
180183
export function isNot(a: any): (b: any) => boolean;
181184

@@ -333,7 +336,7 @@ export function transformObjectDeep(
333336
): object;
334337

335338
export function truncate(string: string, length: number): string;
336-
export function truncate(string: NilOr<string>, length: number): NilOr<string>;
339+
export function _truncate(string: NilOr<string>, length: number): NilOr<string>;
337340

338341
export function nullSafe<T extends Function>(
339342
func: T

0 commit comments

Comments
 (0)