forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
33 lines (27 loc) · 983 Bytes
/
Copy pathstring.js
File metadata and controls
33 lines (27 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* String utilities for Nova Rewards
*/
/** Capitalize the first letter of a string */
const capitalize = (str) =>
typeof str === 'string' && str.length > 0
? str.charAt(0).toUpperCase() + str.slice(1)
: str;
/** Convert camelCase to snake_case */
const toSnakeCase = (str) =>
str.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`);
/** Truncate a string to maxLen, appending ellipsis if needed */
const truncate = (str, maxLen = 50) =>
str.length <= maxLen ? str : `${str.slice(0, maxLen)}...`;
/** Slugify a string (lowercase, hyphens) */
const slugify = (str) =>
str
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
/** Mask all but the last n characters of a string */
const mask = (str, visibleChars = 4, maskChar = '*') =>
str.length <= visibleChars
? str
: maskChar.repeat(str.length - visibleChars) + str.slice(-visibleChars);
module.exports = { capitalize, toSnakeCase, truncate, slugify, mask };