Skip to content

Commit e6d4469

Browse files
authored
Add String Helpers code snippets.
1 parent 93822e6 commit e6d4469

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ServiceNow JavaScript Helpers
2+
3+
A set of lightweight JavaScript helper functions for use in ServiceNow Script Includes, Business Rules, and Client Scripts.
4+
No external dependencies, just pure JavaScript.
5+
6+
---
7+
8+
## ✨ String Helpers (`stringHelpers.js`)
9+
10+
- `toSlug("Hello World!")``"hello-world"`
11+
- `fromSlugToWords("hello-world")``"hello world"`
12+
- `fromSlugToCompact("hello-world")``"helloworld"`
13+
- `toUnderscoreCase("hello world")``"hello_world"`
14+
- `capitalizeWords("hello world")``"Hello World"`
15+
- `toUpperCase("hello")``"HELLO"`
16+
- `toLowerCase("HELLO")``"hello"`
17+
- `trimText(" hello ")``"hello"`
18+
19+
---
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Converts string to lowercase, removes symbols, replaces spaces with dashes
2+
function toSlug(text) {
3+
let str = text.toLowerCase();
4+
str = str.replace(/[^a-z0-9_\s-]/g, "");
5+
str = str.replace(/[\s-]+/g, " ");
6+
str = str.replace(/[\s_]/g, "-");
7+
return str;
8+
};
9+
10+
// Converts dashed/underscored text back to words with spaces
11+
function fromSlugToWords(text) {
12+
return text.replace(/[-_]/g, " ");
13+
};
14+
15+
// Converts dashed/underscored text to continuous word
16+
function fromSlugToCompact(text) {
17+
return text.replace(/[-_]/g, "");
18+
};
19+
20+
// Replaces spaces with underscores
21+
function toUnderscoreCase(text) {
22+
return text.replace(/\s/g, "_");
23+
};
24+
25+
// Capitalizes the first letter of each word
26+
function capitalizeWords(text) {
27+
return text.toLowerCase().replace(/(^\w{1})|(\s+\w{1})/g, (letter) => letter.toUpperCase());
28+
};
29+
30+
// Converts entire string to uppercase
31+
function toUpperCase(text) {
32+
return text ? text.toUpperCase() : text;
33+
};
34+
35+
// Converts entire string to lowercase
36+
function toLowerCase(text) {
37+
return text ? text.toLowerCase() : text;
38+
};
39+
40+
// Removes spaces from start and end
41+
function trimText(text) {
42+
return text.trim();
43+
};

0 commit comments

Comments
 (0)