Skip to content

Commit ac27661

Browse files
Merge pull request #2 from jhonsnow456/feat/text
feat(text): add capitalize, stripAnsi, isAlpha, isNumeric, isAlphanumeric, escapeHtml, unescapeHtml
2 parents f129d5e + 0c1eb49 commit ac27661

16 files changed

Lines changed: 350 additions & 7 deletions

text/capitalize.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
// This module is browser compatible.
3+
4+
/**
5+
* Capitalizes the first character of a string and lowercases the rest.
6+
*
7+
* @param input The string to capitalize
8+
* @returns The capitalized string
9+
*
10+
* @example Usage
11+
* ```ts
12+
* import { capitalize } from "@std/text/capitalize";
13+
* import { assertEquals } from "@std/assert";
14+
*
15+
* assertEquals(capitalize("hello"), "Hello");
16+
* assertEquals(capitalize("hELLO"), "Hello");
17+
* ```
18+
*/
19+
export function capitalize(input: string): string {
20+
if (input.length === 0) return input;
21+
return input[0]!.toUpperCase() + input.slice(1).toLowerCase();
22+
}

text/capitalize_test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
3+
import { assertEquals } from "@std/assert";
4+
import { capitalize } from "./capitalize.ts";
5+
6+
Deno.test("capitalize() capitalizes a lowercase string", () => {
7+
assertEquals(capitalize("hello"), "Hello");
8+
});
9+
10+
Deno.test("capitalize() lowercases the rest of the string", () => {
11+
assertEquals(capitalize("hELLO"), "Hello");
12+
});
13+
14+
Deno.test("capitalize() handles empty string", () => {
15+
assertEquals(capitalize(""), "");
16+
});
17+
18+
Deno.test("capitalize() handles single character", () => {
19+
assertEquals(capitalize("a"), "A");
20+
});

text/deno.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
"version": "1.0.19",
44
"exports": {
55
".": "./mod.ts",
6+
"./capitalize": "./capitalize.ts",
67
"./closest-string": "./closest_string.ts",
78
"./compare-similarity": "./compare_similarity.ts",
8-
"./unstable-dedent": "./unstable_dedent.ts",
9+
"./escape-html": "./escape_html.ts",
10+
"./is-alpha": "./is_alpha.ts",
11+
"./is-alphanumeric": "./is_alphanumeric.ts",
12+
"./is-numeric": "./is_numeric.ts",
913
"./levenshtein-distance": "./levenshtein_distance.ts",
14+
"./strip-ansi": "./strip_ansi.ts",
15+
"./to-camel-case": "./to_camel_case.ts",
16+
"./to-kebab-case": "./to_kebab_case.ts",
17+
"./to-pascal-case": "./to_pascal_case.ts",
18+
"./to-snake-case": "./to_snake_case.ts",
19+
"./unescape-html": "./unescape_html.ts",
20+
"./unstable-dedent": "./unstable_dedent.ts",
1021
"./unstable-longest-common-prefix": "./unstable_longest_common_prefix.ts",
1122
"./unstable-reverse": "./unstable_reverse.ts",
1223
"./unstable-slugify": "./unstable_slugify.ts",
1324
"./unstable-trim-by": "./unstable_trim_by.ts",
14-
"./to-camel-case": "./to_camel_case.ts",
1525
"./unstable-to-constant-case": "./unstable_to_constant_case.ts",
16-
"./to-kebab-case": "./to_kebab_case.ts",
17-
"./to-pascal-case": "./to_pascal_case.ts",
1826
"./unstable-to-sentence-case": "./unstable_to_sentence_case.ts",
19-
"./to-snake-case": "./to_snake_case.ts",
2027
"./unstable-to-title-case": "./unstable_to_title_case.ts",
2128
"./unstable-truncate": "./unstable_truncate.ts",
2229
"./word-similarity-sort": "./word_similarity_sort.ts"

text/escape_html.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
// This module is browser compatible.
3+
4+
const HTML_ESCAPE_MAP: Record<string, string> = {
5+
"&": "&amp;",
6+
"<": "&lt;",
7+
">": "&gt;",
8+
'"': "&quot;",
9+
"'": "&#39;",
10+
};
11+
12+
const HTML_ESCAPE_REGEXP = /[&<>"']/g;
13+
14+
/**
15+
* Escapes HTML special characters in a string.
16+
*
17+
* @param input The string to escape
18+
* @returns The escaped string
19+
*
20+
* @example Usage
21+
* ```ts
22+
* import { escapeHtml } from "@std/text/escape-html";
23+
* import { assertEquals } from "@std/assert";
24+
*
25+
* assertEquals(escapeHtml("<script>alert('xss')</script>"), "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;");
26+
* ```
27+
*/
28+
export function escapeHtml(input: string): string {
29+
return input.replace(
30+
HTML_ESCAPE_REGEXP,
31+
(char) => HTML_ESCAPE_MAP[char]!,
32+
);
33+
}

text/escape_html_test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
3+
import { assertEquals } from "@std/assert";
4+
import { escapeHtml } from "./escape_html.ts";
5+
6+
Deno.test("escapeHtml() escapes HTML special characters", () => {
7+
assertEquals(
8+
escapeHtml("<script>alert('xss')</script>"),
9+
"&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;",
10+
);
11+
});
12+
13+
Deno.test("escapeHtml() escapes ampersands first", () => {
14+
assertEquals(escapeHtml("a & b"), "a &amp; b");
15+
});
16+
17+
Deno.test("escapeHtml() handles strings without special chars", () => {
18+
assertEquals(escapeHtml("hello"), "hello");
19+
});
20+
21+
Deno.test("escapeHtml() handles empty string", () => {
22+
assertEquals(escapeHtml(""), "");
23+
});

text/is_alpha.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
// This module is browser compatible.
3+
4+
/**
5+
* Checks if a string contains only alphabetic characters.
6+
*
7+
* @param input The string to check
8+
* @returns `true` if the string contains only alphabetic characters
9+
*
10+
* @example Usage
11+
* ```ts
12+
* import { isAlpha } from "@std/text/is-alpha";
13+
* import { assertEquals } from "@std/assert";
14+
*
15+
* assertEquals(isAlpha("hello"), true);
16+
* assertEquals(isAlpha("hello123"), false);
17+
* ```
18+
*/
19+
export function isAlpha(input: string): boolean {
20+
return /^[A-Za-z]+$/.test(input);
21+
}

text/is_alpha_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
3+
import { assertEquals } from "@std/assert";
4+
import { isAlpha } from "./is_alpha.ts";
5+
6+
Deno.test("isAlpha() returns true for alphabetic strings", () => {
7+
assertEquals(isAlpha("hello"), true);
8+
assertEquals(isAlpha("HELLO"), true);
9+
});
10+
11+
Deno.test("isAlpha() returns false for strings with numbers", () => {
12+
assertEquals(isAlpha("hello123"), false);
13+
});
14+
15+
Deno.test("isAlpha() returns false for strings with special characters", () => {
16+
assertEquals(isAlpha("hello world"), false);
17+
assertEquals(isAlpha("hello!"), false);
18+
});
19+
20+
Deno.test("isAlpha() returns false for empty string", () => {
21+
assertEquals(isAlpha(""), false);
22+
});

text/is_alphanumeric.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
// This module is browser compatible.
3+
4+
/**
5+
* Checks if a string contains only alphabetic and numeric characters.
6+
*
7+
* @param input The string to check
8+
* @returns `true` if the string contains only alphanumeric characters
9+
*
10+
* @example Usage
11+
* ```ts
12+
* import { isAlphanumeric } from "@std/text/is-alphanumeric";
13+
* import { assertEquals } from "@std/assert";
14+
*
15+
* assertEquals(isAlphanumeric("hello123"), true);
16+
* assertEquals(isAlphanumeric("hello 123"), false);
17+
* ```
18+
*/
19+
export function isAlphanumeric(input: string): boolean {
20+
return /^[A-Za-z0-9]+$/.test(input);
21+
}

text/is_alphanumeric_test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
3+
import { assertEquals } from "@std/assert";
4+
import { isAlphanumeric } from "./is_alphanumeric.ts";
5+
6+
Deno.test("isAlphanumeric() returns true for alphanumeric strings", () => {
7+
assertEquals(isAlphanumeric("hello123"), true);
8+
assertEquals(isAlphanumeric("abc"), true);
9+
assertEquals(isAlphanumeric("123"), true);
10+
});
11+
12+
Deno.test("isAlphanumeric() returns false for strings with special characters", () => {
13+
assertEquals(isAlphanumeric("hello 123"), false);
14+
assertEquals(isAlphanumeric("hello!"), false);
15+
});
16+
17+
Deno.test("isAlphanumeric() returns false for empty string", () => {
18+
assertEquals(isAlphanumeric(""), false);
19+
});

text/is_numeric.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
// This module is browser compatible.
3+
4+
/**
5+
* Checks if a string contains only numeric characters.
6+
*
7+
* @param input The string to check
8+
* @returns `true` if the string contains only numeric characters
9+
*
10+
* @example Usage
11+
* ```ts
12+
* import { isNumeric } from "@std/text/is-numeric";
13+
* import { assertEquals } from "@std/assert";
14+
*
15+
* assertEquals(isNumeric("123"), true);
16+
* assertEquals(isNumeric("123abc"), false);
17+
* ```
18+
*/
19+
export function isNumeric(input: string): boolean {
20+
return /^[0-9]+$/.test(input);
21+
}

0 commit comments

Comments
 (0)