ts-string-lite 2.0.0 → 2.0.16
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/README.md +49 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/string.d.ts +23 -0
- package/dist/string.js +108 -0
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# ts-string-lite
|
|
2
|
+
|
|
3
|
+
Lightweight, type-safe string utility library for web developers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ts-string-lite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Injectable } from '@angular/core';
|
|
15
|
+
import { StringUtils } from 'ts-string-lite';
|
|
16
|
+
|
|
17
|
+
@Injectable({ providedIn: 'root' })
|
|
18
|
+
export class MyService {
|
|
19
|
+
constructor(private str: StringUtils) { }
|
|
20
|
+
|
|
21
|
+
myMethod(): void {
|
|
22
|
+
this.str.slugify('Hello World'); // 'hello-world'
|
|
23
|
+
this.str.camelCase('hello world'); // 'helloWorld'
|
|
24
|
+
this.str.kebabCase('HelloWorld'); // 'hello-world'
|
|
25
|
+
this.str.pascalCase('hello world'); // 'HelloWorld'
|
|
26
|
+
this.str.truncate('Hello World', 10); // 'Hello W...'
|
|
27
|
+
this.str.capitalize('hello'); // 'Hello'
|
|
28
|
+
this.str.titleCase('hello world'); // 'Hello World'
|
|
29
|
+
this.str.escapeHtml('<script>'); // '<script>'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Methods
|
|
35
|
+
|
|
36
|
+
| Method | Description | Example |
|
|
37
|
+
|--------|-------------|---------|
|
|
38
|
+
| `slugify` | Convert string to URL-friendly slug | `'Hello World'` → `'hello-world'` |
|
|
39
|
+
| `camelCase` | Convert to camelCase | `'hello world'` → `'helloWorld'` |
|
|
40
|
+
| `kebabCase` | Convert to kebab-case | `'HelloWorld'` → `'hello-world'` |
|
|
41
|
+
| `pascalCase` | Convert to PascalCase | `'hello world'` → `'HelloWorld'` |
|
|
42
|
+
| `truncate` | Truncate string to length | `'Hello World', 10` → `'Hello W...'` |
|
|
43
|
+
| `capitalize` | Capitalize first letter | `'hello'` → `'Hello'` |
|
|
44
|
+
| `titleCase` | Convert to Title Case | `'hello world'` → `'Hello World'` |
|
|
45
|
+
| `escapeHtml` | Escape HTML characters | `'<script>'` → `'<script>'` |
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './string';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './string';
|
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,108 @@
|
|
|
1
|
+
export class StringUtils {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._html_escape_map = {
|
|
4
|
+
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
slugify(input_text) {
|
|
8
|
+
if (!input_text)
|
|
9
|
+
return '';
|
|
10
|
+
return input_text.toLowerCase().trim()
|
|
11
|
+
.replace(/[^\w\s-]/g, '')
|
|
12
|
+
.replace(/[\s_-]+/g, '-')
|
|
13
|
+
.replace(/^-+|-+$/g, '');
|
|
14
|
+
}
|
|
15
|
+
truncate(input_text, max_length, suffix = '...') {
|
|
16
|
+
if (!input_text || input_text.length <= max_length)
|
|
17
|
+
return input_text;
|
|
18
|
+
return input_text.slice(0, max_length - suffix.length) + suffix;
|
|
19
|
+
}
|
|
20
|
+
capitalize(input_text) {
|
|
21
|
+
if (!input_text)
|
|
22
|
+
return '';
|
|
23
|
+
return input_text.charAt(0).toUpperCase() + input_text.slice(1).toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
titleCase(input_text) {
|
|
26
|
+
if (!input_text)
|
|
27
|
+
return '';
|
|
28
|
+
return input_text.replace(/\w\S*/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
|
|
29
|
+
}
|
|
30
|
+
camelCase(input_text) {
|
|
31
|
+
if (!input_text)
|
|
32
|
+
return '';
|
|
33
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index === 0 ? word.toLowerCase() : word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
34
|
+
}
|
|
35
|
+
kebabCase(input_text) {
|
|
36
|
+
if (!input_text)
|
|
37
|
+
return '';
|
|
38
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[\s_]+/g, '-').toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
snakeCase(input_text) {
|
|
41
|
+
if (!input_text)
|
|
42
|
+
return '';
|
|
43
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1_$2').replace(/[\s-]+/g, '_').toLowerCase();
|
|
44
|
+
}
|
|
45
|
+
pascalCase(input_text) {
|
|
46
|
+
if (!input_text)
|
|
47
|
+
return '';
|
|
48
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
49
|
+
}
|
|
50
|
+
escapeHtml(input_text) {
|
|
51
|
+
if (!input_text)
|
|
52
|
+
return '';
|
|
53
|
+
return input_text.replace(/[&<>"']/g, (m) => this._html_escape_map[m] || m);
|
|
54
|
+
}
|
|
55
|
+
unescapeHtml(input_text) {
|
|
56
|
+
if (!input_text)
|
|
57
|
+
return '';
|
|
58
|
+
return input_text.replace(/&(amp|lt|gt|quot|#39);/g, (m) => this._html_escape_map[m] || m);
|
|
59
|
+
}
|
|
60
|
+
repeat(input_text, times) {
|
|
61
|
+
return input_text.repeat(Math.max(0, times));
|
|
62
|
+
}
|
|
63
|
+
reverse(input_text) {
|
|
64
|
+
return input_text.split('').reverse().join('');
|
|
65
|
+
}
|
|
66
|
+
base64Encode(input_text) {
|
|
67
|
+
return btoa(unescape(encodeURIComponent(input_text)));
|
|
68
|
+
}
|
|
69
|
+
base64Decode(encoded) {
|
|
70
|
+
try {
|
|
71
|
+
return decodeURIComponent(escape(atob(encoded)));
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
randomString(length) {
|
|
78
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
79
|
+
return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
|
|
80
|
+
}
|
|
81
|
+
trimStart(text, chars) {
|
|
82
|
+
return chars ? text.replace(new RegExp(`^[${chars}]+`, 'g'), '') : text.trimStart();
|
|
83
|
+
}
|
|
84
|
+
trimEnd(text, chars) {
|
|
85
|
+
return chars ? text.replace(new RegExp(`[${chars}]+$`, 'g'), '') : text.trimEnd();
|
|
86
|
+
}
|
|
87
|
+
words(text, delimiter = /\s+/) {
|
|
88
|
+
return text.split(delimiter).filter(Boolean);
|
|
89
|
+
}
|
|
90
|
+
countWords(text) {
|
|
91
|
+
return this.words(text).length;
|
|
92
|
+
}
|
|
93
|
+
levenshtein(a, b) {
|
|
94
|
+
if (!a || !b)
|
|
95
|
+
return Math.max(a?.length || 0, b?.length || 0);
|
|
96
|
+
const matrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(0));
|
|
97
|
+
for (let i = 0; i <= a.length; i++)
|
|
98
|
+
matrix[0][i] = i;
|
|
99
|
+
for (let j = 0; j <= b.length; j++)
|
|
100
|
+
matrix[j][0] = j;
|
|
101
|
+
for (let j = 1; j <= b.length; j++) {
|
|
102
|
+
for (let i = 1; i <= a.length; i++) {
|
|
103
|
+
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));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return matrix[b.length][a.length];
|
|
107
|
+
}
|
|
108
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-string-lite",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.16",
|
|
4
|
+
"deprecated": "Previous versions are deprecated. Please upgrade to 2.0.15 or later.",
|
|
4
5
|
"description": "Lightweight, type-safe string utility library for web developers",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"module": "./dist/index.js",
|
|
@@ -26,5 +27,9 @@
|
|
|
26
27
|
"lite"
|
|
27
28
|
],
|
|
28
29
|
"author": "Gaurang Mody - G8X",
|
|
29
|
-
"license": "MIT"
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/gbmrocks/ts-utils-lite.git"
|
|
34
|
+
}
|
|
30
35
|
}
|