Skip to content

Commit ab44584

Browse files
authored
fix(underscoreToTitleCase!): fix function name and comments (#1)
* fix function name and comments * 0.5.0
1 parent 6fc21cb commit ab44584

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@utility/string",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"tasks": {
55
"coverage": "deno task test --coverage=./dist/coverage && deno coverage --include=src --lcov ./dist/coverage > ./dist/coverage/lcov.info",
66
"test": "deno test"

src/string.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,41 @@
22
* Converts provided underscore format string to camel case
33
*
44
* @param {string} value The value to convert
5-
* @return {boolean} Returns `true` if `value` is a valid iso date, else `false`.
5+
* @return {string} Returns the title cased string.
66
* @example
77
* ```ts
88
* underscoreToCamelCase("hello_world");
99
* // => Hello World
1010
* ```
1111
*/
12-
export function underscoreToCamelCase(value: string): string {
13-
value = value.charAt(0).toUpperCase() + value.slice(1);
14-
value = value.replace(/_(.)/g, (_match: string, letter) => {
12+
export function underscoreToTitleCase(
13+
value: string | null | undefined,
14+
): string {
15+
let strValue = _toString(value);
16+
strValue = strValue.charAt(0).toUpperCase() + strValue.slice(1);
17+
strValue = strValue.replace(/_(.)/g, (_match: string, letter) => {
1518
return ` ${letter.toUpperCase()}`;
1619
});
17-
return value.replace("_", " ");
20+
return strValue.replace("_", " ");
21+
}
22+
23+
/** Used as references for various `Number` constants. */
24+
const INFINITY = 1 / 0;
25+
26+
function _toString(value: any): string {
27+
if (value == null) {
28+
return "";
29+
}
30+
// Exit early for strings to avoid a performance hit in some environments.
31+
if (typeof value === "string") {
32+
return value;
33+
}
34+
if (Array.isArray(value)) {
35+
// Recursively convert values (susceptible to call stack limits).
36+
return `${
37+
value.map((other) => (other == null ? other : _toString(other)))
38+
}`;
39+
}
40+
const result = `${value}`;
41+
return result === "0" && 1 / value === -INFINITY ? "-0" : result;
1842
}

test/mod.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { assertEquals, describe, it } from "../test_deps.ts";
22

3-
import { underscoreToCamelCase } from "../mod.ts";
3+
import { underscoreToTitleCase } from "../mod.ts";
44

55
describe("string", function () {
6-
it("underscoreToCamelCase()", function () {
7-
assertEquals(underscoreToCamelCase("hello_world"), "Hello World");
8-
assertEquals(underscoreToCamelCase("_hello_world_"), " Hello World ");
6+
it("underscoreToTitleCase()", function () {
7+
assertEquals(underscoreToTitleCase("hello_world"), "Hello World");
8+
assertEquals(underscoreToTitleCase("_hello_world_"), " Hello World ");
9+
assertEquals(underscoreToTitleCase(null), "");
10+
assertEquals(underscoreToTitleCase(undefined), "");
911
});
1012
});

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.4.1",
2+
"version": "0.5.0",
33
"deno": true,
44
"node": true,
55
"jsr": true,

0 commit comments

Comments
 (0)