ts-string-lite 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/string.d.ts +23 -0
- package/dist/string.js +112 -0
- package/package.json +30 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './string';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./string"), exports);
|
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class StringUtils {
|
|
2
|
+
private readonly _html_escape_map;
|
|
3
|
+
slugify(input_text: string): string;
|
|
4
|
+
truncate(input_text: string, max_length: number, suffix?: string): string;
|
|
5
|
+
capitalize(input_text: string): string;
|
|
6
|
+
titleCase(input_text: string): string;
|
|
7
|
+
camelCase(input_text: string): string;
|
|
8
|
+
kebabCase(input_text: string): string;
|
|
9
|
+
snakeCase(input_text: string): string;
|
|
10
|
+
pascalCase(input_text: string): string;
|
|
11
|
+
escapeHtml(input_text: string): string;
|
|
12
|
+
unescapeHtml(input_text: string): string;
|
|
13
|
+
repeat(input_text: string, times: number): string;
|
|
14
|
+
reverse(input_text: string): string;
|
|
15
|
+
base64Encode(input_text: string): string;
|
|
16
|
+
base64Decode(encoded: string): string;
|
|
17
|
+
randomString(length: number): string;
|
|
18
|
+
trimStart(text: string, chars?: string): string;
|
|
19
|
+
trimEnd(text: string, chars?: string): string;
|
|
20
|
+
words(text: string, delimiter?: RegExp): string[];
|
|
21
|
+
countWords(text: string): number;
|
|
22
|
+
levenshtein(a: string, b: string): number;
|
|
23
|
+
}
|
package/dist/string.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringUtils = void 0;
|
|
4
|
+
class StringUtils {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._html_escape_map = {
|
|
7
|
+
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
slugify(input_text) {
|
|
11
|
+
if (!input_text)
|
|
12
|
+
return '';
|
|
13
|
+
return input_text.toLowerCase().trim()
|
|
14
|
+
.replace(/[^\w\s-]/g, '')
|
|
15
|
+
.replace(/[\s_-]+/g, '-')
|
|
16
|
+
.replace(/^-+|-+$/g, '');
|
|
17
|
+
}
|
|
18
|
+
truncate(input_text, max_length, suffix = '...') {
|
|
19
|
+
if (!input_text || input_text.length <= max_length)
|
|
20
|
+
return input_text;
|
|
21
|
+
return input_text.slice(0, max_length - suffix.length) + suffix;
|
|
22
|
+
}
|
|
23
|
+
capitalize(input_text) {
|
|
24
|
+
if (!input_text)
|
|
25
|
+
return '';
|
|
26
|
+
return input_text.charAt(0).toUpperCase() + input_text.slice(1).toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
titleCase(input_text) {
|
|
29
|
+
if (!input_text)
|
|
30
|
+
return '';
|
|
31
|
+
return input_text.replace(/\w\S*/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
|
|
32
|
+
}
|
|
33
|
+
camelCase(input_text) {
|
|
34
|
+
if (!input_text)
|
|
35
|
+
return '';
|
|
36
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index === 0 ? word.toLowerCase() : word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
37
|
+
}
|
|
38
|
+
kebabCase(input_text) {
|
|
39
|
+
if (!input_text)
|
|
40
|
+
return '';
|
|
41
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[\s_]+/g, '-').toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
snakeCase(input_text) {
|
|
44
|
+
if (!input_text)
|
|
45
|
+
return '';
|
|
46
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1_$2').replace(/[\s-]+/g, '_').toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
pascalCase(input_text) {
|
|
49
|
+
if (!input_text)
|
|
50
|
+
return '';
|
|
51
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
52
|
+
}
|
|
53
|
+
escapeHtml(input_text) {
|
|
54
|
+
if (!input_text)
|
|
55
|
+
return '';
|
|
56
|
+
return input_text.replace(/[&<>"']/g, (m) => this._html_escape_map[m] || m);
|
|
57
|
+
}
|
|
58
|
+
unescapeHtml(input_text) {
|
|
59
|
+
if (!input_text)
|
|
60
|
+
return '';
|
|
61
|
+
return input_text.replace(/&(amp|lt|gt|quot|#39);/g, (m) => this._html_escape_map[m] || m);
|
|
62
|
+
}
|
|
63
|
+
repeat(input_text, times) {
|
|
64
|
+
return input_text.repeat(Math.max(0, times));
|
|
65
|
+
}
|
|
66
|
+
reverse(input_text) {
|
|
67
|
+
return input_text.split('').reverse().join('');
|
|
68
|
+
}
|
|
69
|
+
base64Encode(input_text) {
|
|
70
|
+
return btoa(unescape(encodeURIComponent(input_text)));
|
|
71
|
+
}
|
|
72
|
+
base64Decode(encoded) {
|
|
73
|
+
try {
|
|
74
|
+
return decodeURIComponent(escape(atob(encoded)));
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return '';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
randomString(length) {
|
|
81
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
82
|
+
return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
|
|
83
|
+
}
|
|
84
|
+
trimStart(text, chars) {
|
|
85
|
+
return chars ? text.replace(new RegExp(`^[${chars}]+`, 'g'), '') : text.trimStart();
|
|
86
|
+
}
|
|
87
|
+
trimEnd(text, chars) {
|
|
88
|
+
return chars ? text.replace(new RegExp(`[${chars}]+$`, 'g'), '') : text.trimEnd();
|
|
89
|
+
}
|
|
90
|
+
words(text, delimiter = /\s+/) {
|
|
91
|
+
return text.split(delimiter).filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
countWords(text) {
|
|
94
|
+
return this.words(text).length;
|
|
95
|
+
}
|
|
96
|
+
levenshtein(a, b) {
|
|
97
|
+
if (!a || !b)
|
|
98
|
+
return Math.max(a?.length || 0, b?.length || 0);
|
|
99
|
+
const matrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(0));
|
|
100
|
+
for (let i = 0; i <= a.length; i++)
|
|
101
|
+
matrix[0][i] = i;
|
|
102
|
+
for (let j = 0; j <= b.length; j++)
|
|
103
|
+
matrix[j][0] = j;
|
|
104
|
+
for (let j = 1; j <= b.length; j++) {
|
|
105
|
+
for (let i = 1; i <= a.length; i++) {
|
|
106
|
+
matrix[j][i] = Math.min(matrix[j][i - 1] + 1, matrix[j - 1][i] + 1, matrix[j - 1][i - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return matrix[b.length][a.length];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.StringUtils = StringUtils;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-string-lite",
|
|
3
|
+
"version": "0.0.1-beta.1",
|
|
4
|
+
"description": "Lightweight, type-safe string utility library for web developers",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"utils",
|
|
20
|
+
"typescript",
|
|
21
|
+
"string",
|
|
22
|
+
"helpers",
|
|
23
|
+
"lite"
|
|
24
|
+
],
|
|
25
|
+
"author": "Gaurang Mody - G8X",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^6.0.2"
|
|
29
|
+
}
|
|
30
|
+
}
|