@pixpilot/string 2.2.0 → 2.4.0
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/_virtual/rolldown_runtime.cjs +25 -1
- package/dist/case.cjs +91 -1
- package/dist/case.js +78 -1
- package/dist/index.cjs +33 -1
- package/dist/index.js +6 -1
- package/dist/manipulation.cjs +206 -1
- package/dist/manipulation.js +195 -1
- package/dist/simple-hash.cjs +15 -0
- package/dist/simple-hash.d.cts +5 -0
- package/dist/simple-hash.d.ts +5 -0
- package/dist/simple-hash.js +13 -0
- package/dist/type-guards.cjs +21 -1
- package/dist/type-guards.js +20 -1
- package/dist/validation.cjs +78 -1
- package/dist/validation.js +74 -1
- package/package.json +1 -1
|
@@ -1 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
exports.__toESM = __toESM;
|
package/dist/case.cjs
CHANGED
|
@@ -1 +1,91 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let change_case = require("change-case");
|
|
3
|
+
change_case = require_rolldown_runtime.__toESM(change_case);
|
|
4
|
+
|
|
5
|
+
//#region src/case.ts
|
|
6
|
+
/**
|
|
7
|
+
* Convert a string to camel case (`fooBar`).
|
|
8
|
+
*/
|
|
9
|
+
function toCamelCase(str) {
|
|
10
|
+
return (0, change_case.camelCase)(str);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Convert a string to capital case (`Foo Bar`).
|
|
14
|
+
*/
|
|
15
|
+
function toCapitalCase(str) {
|
|
16
|
+
return (0, change_case.capitalCase)(str);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert a string to constant case (`CONSTANT_CASE`).
|
|
20
|
+
*/
|
|
21
|
+
function toConstantCase(str) {
|
|
22
|
+
return (0, change_case.constantCase)(str);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a string to dot case (`dot.case`).
|
|
26
|
+
*/
|
|
27
|
+
function toDotCase(str) {
|
|
28
|
+
return (0, change_case.dotCase)(str);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Convert a string to kebab case (`kebab-case`).
|
|
32
|
+
*/
|
|
33
|
+
function toKebabCase(str) {
|
|
34
|
+
return (0, change_case.kebabCase)(str);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Convert a string to no case (`no case`).
|
|
38
|
+
*/
|
|
39
|
+
function toNoCase(str) {
|
|
40
|
+
return (0, change_case.noCase)(str);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Convert a string to pascal case (`PascalCase`).
|
|
44
|
+
*/
|
|
45
|
+
function toPascalCase(str) {
|
|
46
|
+
return (0, change_case.pascalCase)(str);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Convert a string to pascal snake case (`Pascal_Snake_Case`).
|
|
50
|
+
*/
|
|
51
|
+
function toPascalSnakeCase(str) {
|
|
52
|
+
return (0, change_case.pascalSnakeCase)(str);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Convert a string to path case (`path/case`).
|
|
56
|
+
*/
|
|
57
|
+
function toPathCase(str) {
|
|
58
|
+
return (0, change_case.pathCase)(str);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert a string to sentence case (`Sentence case`).
|
|
62
|
+
*/
|
|
63
|
+
function toSentenceCase(str) {
|
|
64
|
+
return (0, change_case.sentenceCase)(str);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Convert a string to snake case (`snake_case`).
|
|
68
|
+
*/
|
|
69
|
+
function toSnakeCase(str) {
|
|
70
|
+
return (0, change_case.snakeCase)(str);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convert a string to train case (`Train-Case`).
|
|
74
|
+
*/
|
|
75
|
+
function toTrainCase(str) {
|
|
76
|
+
return (0, change_case.trainCase)(str);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.toCamelCase = toCamelCase;
|
|
81
|
+
exports.toCapitalCase = toCapitalCase;
|
|
82
|
+
exports.toConstantCase = toConstantCase;
|
|
83
|
+
exports.toDotCase = toDotCase;
|
|
84
|
+
exports.toKebabCase = toKebabCase;
|
|
85
|
+
exports.toNoCase = toNoCase;
|
|
86
|
+
exports.toPascalCase = toPascalCase;
|
|
87
|
+
exports.toPascalSnakeCase = toPascalSnakeCase;
|
|
88
|
+
exports.toPathCase = toPathCase;
|
|
89
|
+
exports.toSentenceCase = toSentenceCase;
|
|
90
|
+
exports.toSnakeCase = toSnakeCase;
|
|
91
|
+
exports.toTrainCase = toTrainCase;
|
package/dist/case.js
CHANGED
|
@@ -1 +1,78 @@
|
|
|
1
|
-
import{camelCase
|
|
1
|
+
import { camelCase, capitalCase, constantCase, dotCase, kebabCase, noCase, pascalCase, pascalSnakeCase, pathCase, sentenceCase, snakeCase, trainCase } from "change-case";
|
|
2
|
+
|
|
3
|
+
//#region src/case.ts
|
|
4
|
+
/**
|
|
5
|
+
* Convert a string to camel case (`fooBar`).
|
|
6
|
+
*/
|
|
7
|
+
function toCamelCase(str) {
|
|
8
|
+
return camelCase(str);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert a string to capital case (`Foo Bar`).
|
|
12
|
+
*/
|
|
13
|
+
function toCapitalCase(str) {
|
|
14
|
+
return capitalCase(str);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Convert a string to constant case (`CONSTANT_CASE`).
|
|
18
|
+
*/
|
|
19
|
+
function toConstantCase(str) {
|
|
20
|
+
return constantCase(str);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convert a string to dot case (`dot.case`).
|
|
24
|
+
*/
|
|
25
|
+
function toDotCase(str) {
|
|
26
|
+
return dotCase(str);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Convert a string to kebab case (`kebab-case`).
|
|
30
|
+
*/
|
|
31
|
+
function toKebabCase(str) {
|
|
32
|
+
return kebabCase(str);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Convert a string to no case (`no case`).
|
|
36
|
+
*/
|
|
37
|
+
function toNoCase(str) {
|
|
38
|
+
return noCase(str);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Convert a string to pascal case (`PascalCase`).
|
|
42
|
+
*/
|
|
43
|
+
function toPascalCase(str) {
|
|
44
|
+
return pascalCase(str);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Convert a string to pascal snake case (`Pascal_Snake_Case`).
|
|
48
|
+
*/
|
|
49
|
+
function toPascalSnakeCase(str) {
|
|
50
|
+
return pascalSnakeCase(str);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Convert a string to path case (`path/case`).
|
|
54
|
+
*/
|
|
55
|
+
function toPathCase(str) {
|
|
56
|
+
return pathCase(str);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Convert a string to sentence case (`Sentence case`).
|
|
60
|
+
*/
|
|
61
|
+
function toSentenceCase(str) {
|
|
62
|
+
return sentenceCase(str);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Convert a string to snake case (`snake_case`).
|
|
66
|
+
*/
|
|
67
|
+
function toSnakeCase(str) {
|
|
68
|
+
return snakeCase(str);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Convert a string to train case (`Train-Case`).
|
|
72
|
+
*/
|
|
73
|
+
function toTrainCase(str) {
|
|
74
|
+
return trainCase(str);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
export { toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase };
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,33 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_case = require('./case.cjs');
|
|
2
|
+
const require_manipulation = require('./manipulation.cjs');
|
|
3
|
+
const require_type_guards = require('./type-guards.cjs');
|
|
4
|
+
const require_validation = require('./validation.cjs');
|
|
5
|
+
|
|
6
|
+
exports.capitalize = require_manipulation.capitalize;
|
|
7
|
+
exports.capitalizeFirst = require_manipulation.capitalizeFirst;
|
|
8
|
+
exports.countOccurrences = require_manipulation.countOccurrences;
|
|
9
|
+
exports.isAlphanumeric = require_validation.isAlphanumeric;
|
|
10
|
+
exports.isEmail = require_validation.isEmail;
|
|
11
|
+
exports.isEmpty = require_validation.isEmpty;
|
|
12
|
+
exports.isString = require_type_guards.isString;
|
|
13
|
+
exports.isUrl = require_validation.isUrl;
|
|
14
|
+
exports.normalizeSpaces = require_manipulation.normalizeSpaces;
|
|
15
|
+
exports.padEnd = require_manipulation.padEnd;
|
|
16
|
+
exports.padStart = require_manipulation.padStart;
|
|
17
|
+
exports.removeWhitespace = require_manipulation.removeWhitespace;
|
|
18
|
+
exports.repeat = require_manipulation.repeat;
|
|
19
|
+
exports.reverse = require_manipulation.reverse;
|
|
20
|
+
exports.toCamelCase = require_case.toCamelCase;
|
|
21
|
+
exports.toCapitalCase = require_case.toCapitalCase;
|
|
22
|
+
exports.toConstantCase = require_case.toConstantCase;
|
|
23
|
+
exports.toDotCase = require_case.toDotCase;
|
|
24
|
+
exports.toKebabCase = require_case.toKebabCase;
|
|
25
|
+
exports.toNoCase = require_case.toNoCase;
|
|
26
|
+
exports.toPascalCase = require_case.toPascalCase;
|
|
27
|
+
exports.toPascalSnakeCase = require_case.toPascalSnakeCase;
|
|
28
|
+
exports.toPathCase = require_case.toPathCase;
|
|
29
|
+
exports.toSentenceCase = require_case.toSentenceCase;
|
|
30
|
+
exports.toSnakeCase = require_case.toSnakeCase;
|
|
31
|
+
exports.toTrainCase = require_case.toTrainCase;
|
|
32
|
+
exports.truncate = require_manipulation.truncate;
|
|
33
|
+
exports.words = require_manipulation.words;
|
package/dist/index.js
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase } from "./case.js";
|
|
2
|
+
import { capitalize, capitalizeFirst, countOccurrences, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, truncate, words } from "./manipulation.js";
|
|
3
|
+
import { isString } from "./type-guards.js";
|
|
4
|
+
import { isAlphanumeric, isEmail, isEmpty, isUrl } from "./validation.js";
|
|
5
|
+
|
|
6
|
+
export { capitalize, capitalizeFirst, countOccurrences, isAlphanumeric, isEmail, isEmpty, isString, isUrl, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase, truncate, words };
|
package/dist/manipulation.cjs
CHANGED
|
@@ -1 +1,206 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/manipulation.ts
|
|
3
|
+
/**
|
|
4
|
+
* Truncate a string to a specified length, adding an ellipsis if truncated.
|
|
5
|
+
*
|
|
6
|
+
* @param str - The string to truncate
|
|
7
|
+
* @param maxLength - The maximum length of the string
|
|
8
|
+
* @param ellipsis - The ellipsis to append (default: '...')
|
|
9
|
+
* @returns The truncated string
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* truncate('Hello World', 5); // 'Hello...'
|
|
14
|
+
* truncate('Hello', 10); // 'Hello'
|
|
15
|
+
* truncate('Hello World', 5, '…'); // 'Hello…'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function truncate(str, maxLength, ellipsis = "...") {
|
|
19
|
+
if (str.length <= maxLength) return str;
|
|
20
|
+
return str.slice(0, maxLength) + ellipsis;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Capitalize the first letter of a string.
|
|
24
|
+
*
|
|
25
|
+
* @param str - The string to capitalize
|
|
26
|
+
* @returns The capitalized string
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* capitalize('hello'); // 'Hello'
|
|
31
|
+
* capitalize('WORLD'); // 'WORLD'
|
|
32
|
+
* capitalize(''); // ''
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function capitalize(str) {
|
|
36
|
+
if (str.length === 0) return str;
|
|
37
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Capitalize the first letter of a string and lowercase the rest.
|
|
41
|
+
*
|
|
42
|
+
* @param str - The string to capitalize
|
|
43
|
+
* @returns The capitalized string
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* capitalizeFirst('hello'); // 'Hello'
|
|
48
|
+
* capitalizeFirst('WORLD'); // 'World'
|
|
49
|
+
* capitalizeFirst('hELLO wORLD'); // 'Hello world'
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function capitalizeFirst(str) {
|
|
53
|
+
if (str.length === 0) return str;
|
|
54
|
+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reverse a string.
|
|
58
|
+
*
|
|
59
|
+
* @param str - The string to reverse
|
|
60
|
+
* @returns The reversed string
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* reverse('hello'); // 'olleh'
|
|
65
|
+
* reverse('abc123'); // '321cba'
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
function reverse(str) {
|
|
69
|
+
return str.split("").reverse().join("");
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Remove all whitespace from a string.
|
|
73
|
+
*
|
|
74
|
+
* @param str - The string to process
|
|
75
|
+
* @returns The string without whitespace
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* removeWhitespace('hello world'); // 'helloworld'
|
|
80
|
+
* removeWhitespace(' a b c '); // 'abc'
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
function removeWhitespace(str) {
|
|
84
|
+
return str.replace(/\s+/gu, "");
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Replace multiple consecutive spaces with a single space.
|
|
88
|
+
*
|
|
89
|
+
* @param str - The string to process
|
|
90
|
+
* @returns The string with normalized spaces
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* normalizeSpaces('hello world'); // 'hello world'
|
|
95
|
+
* normalizeSpaces('a b c'); // 'a b c'
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
function normalizeSpaces(str) {
|
|
99
|
+
return str.replace(/\s+/gu, " ").trim();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Pad a string to a specified length from the start.
|
|
103
|
+
*
|
|
104
|
+
* @param str - The string to pad
|
|
105
|
+
* @param targetLength - The target length
|
|
106
|
+
* @param padString - The string to pad with (default: ' ')
|
|
107
|
+
* @returns The padded string
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* padStart('5', 3, '0'); // '005'
|
|
112
|
+
* padStart('hello', 10); // ' hello'
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
function padStart(str, targetLength, padString = " ") {
|
|
116
|
+
return str.padStart(targetLength, padString);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Pad a string to a specified length from the end.
|
|
120
|
+
*
|
|
121
|
+
* @param str - The string to pad
|
|
122
|
+
* @param targetLength - The target length
|
|
123
|
+
* @param padString - The string to pad with (default: ' ')
|
|
124
|
+
* @returns The padded string
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* padEnd('5', 3, '0'); // '500'
|
|
129
|
+
* padEnd('hello', 10); // 'hello '
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
function padEnd(str, targetLength, padString = " ") {
|
|
133
|
+
return str.padEnd(targetLength, padString);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Repeat a string a specified number of times.
|
|
137
|
+
*
|
|
138
|
+
* @param str - The string to repeat
|
|
139
|
+
* @param count - The number of times to repeat
|
|
140
|
+
* @returns The repeated string
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* repeat('ha', 3); // 'hahaha'
|
|
145
|
+
* repeat('*', 5); // '*****'
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
function repeat(str, count) {
|
|
149
|
+
return str.repeat(count);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Count the occurrences of a substring in a string.
|
|
153
|
+
*
|
|
154
|
+
* @param str - The string to search in
|
|
155
|
+
* @param searchValue - The substring to count
|
|
156
|
+
* @param caseSensitive - Whether the search is case-sensitive (default: true)
|
|
157
|
+
* @returns The number of occurrences
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* countOccurrences('hello world hello', 'hello'); // 2
|
|
162
|
+
* countOccurrences('Hello HELLO hello', 'hello', false); // 3
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
function countOccurrences(str, searchValue, caseSensitive = true) {
|
|
166
|
+
if (searchValue.length === 0) return 0;
|
|
167
|
+
const source = caseSensitive ? str : str.toLowerCase();
|
|
168
|
+
const search = caseSensitive ? searchValue : searchValue.toLowerCase();
|
|
169
|
+
let count = 0;
|
|
170
|
+
let position = 0;
|
|
171
|
+
while (true) {
|
|
172
|
+
const index = source.indexOf(search, position);
|
|
173
|
+
if (index === -1) break;
|
|
174
|
+
count++;
|
|
175
|
+
position = index + 1;
|
|
176
|
+
}
|
|
177
|
+
return count;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Extract words from a string.
|
|
181
|
+
*
|
|
182
|
+
* @param str - The string to extract words from
|
|
183
|
+
* @returns An array of words
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* words('hello world'); // ['hello', 'world']
|
|
188
|
+
* words('one-two_three'); // ['one', 'two', 'three']
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
function words(str) {
|
|
192
|
+
return str.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu) || [];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
exports.capitalize = capitalize;
|
|
197
|
+
exports.capitalizeFirst = capitalizeFirst;
|
|
198
|
+
exports.countOccurrences = countOccurrences;
|
|
199
|
+
exports.normalizeSpaces = normalizeSpaces;
|
|
200
|
+
exports.padEnd = padEnd;
|
|
201
|
+
exports.padStart = padStart;
|
|
202
|
+
exports.removeWhitespace = removeWhitespace;
|
|
203
|
+
exports.repeat = repeat;
|
|
204
|
+
exports.reverse = reverse;
|
|
205
|
+
exports.truncate = truncate;
|
|
206
|
+
exports.words = words;
|
package/dist/manipulation.js
CHANGED
|
@@ -1 +1,195 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/manipulation.ts
|
|
2
|
+
/**
|
|
3
|
+
* Truncate a string to a specified length, adding an ellipsis if truncated.
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to truncate
|
|
6
|
+
* @param maxLength - The maximum length of the string
|
|
7
|
+
* @param ellipsis - The ellipsis to append (default: '...')
|
|
8
|
+
* @returns The truncated string
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* truncate('Hello World', 5); // 'Hello...'
|
|
13
|
+
* truncate('Hello', 10); // 'Hello'
|
|
14
|
+
* truncate('Hello World', 5, '…'); // 'Hello…'
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function truncate(str, maxLength, ellipsis = "...") {
|
|
18
|
+
if (str.length <= maxLength) return str;
|
|
19
|
+
return str.slice(0, maxLength) + ellipsis;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Capitalize the first letter of a string.
|
|
23
|
+
*
|
|
24
|
+
* @param str - The string to capitalize
|
|
25
|
+
* @returns The capitalized string
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* capitalize('hello'); // 'Hello'
|
|
30
|
+
* capitalize('WORLD'); // 'WORLD'
|
|
31
|
+
* capitalize(''); // ''
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function capitalize(str) {
|
|
35
|
+
if (str.length === 0) return str;
|
|
36
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Capitalize the first letter of a string and lowercase the rest.
|
|
40
|
+
*
|
|
41
|
+
* @param str - The string to capitalize
|
|
42
|
+
* @returns The capitalized string
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* capitalizeFirst('hello'); // 'Hello'
|
|
47
|
+
* capitalizeFirst('WORLD'); // 'World'
|
|
48
|
+
* capitalizeFirst('hELLO wORLD'); // 'Hello world'
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function capitalizeFirst(str) {
|
|
52
|
+
if (str.length === 0) return str;
|
|
53
|
+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Reverse a string.
|
|
57
|
+
*
|
|
58
|
+
* @param str - The string to reverse
|
|
59
|
+
* @returns The reversed string
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* reverse('hello'); // 'olleh'
|
|
64
|
+
* reverse('abc123'); // '321cba'
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function reverse(str) {
|
|
68
|
+
return str.split("").reverse().join("");
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Remove all whitespace from a string.
|
|
72
|
+
*
|
|
73
|
+
* @param str - The string to process
|
|
74
|
+
* @returns The string without whitespace
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* removeWhitespace('hello world'); // 'helloworld'
|
|
79
|
+
* removeWhitespace(' a b c '); // 'abc'
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function removeWhitespace(str) {
|
|
83
|
+
return str.replace(/\s+/gu, "");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Replace multiple consecutive spaces with a single space.
|
|
87
|
+
*
|
|
88
|
+
* @param str - The string to process
|
|
89
|
+
* @returns The string with normalized spaces
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* normalizeSpaces('hello world'); // 'hello world'
|
|
94
|
+
* normalizeSpaces('a b c'); // 'a b c'
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
function normalizeSpaces(str) {
|
|
98
|
+
return str.replace(/\s+/gu, " ").trim();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Pad a string to a specified length from the start.
|
|
102
|
+
*
|
|
103
|
+
* @param str - The string to pad
|
|
104
|
+
* @param targetLength - The target length
|
|
105
|
+
* @param padString - The string to pad with (default: ' ')
|
|
106
|
+
* @returns The padded string
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* padStart('5', 3, '0'); // '005'
|
|
111
|
+
* padStart('hello', 10); // ' hello'
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
function padStart(str, targetLength, padString = " ") {
|
|
115
|
+
return str.padStart(targetLength, padString);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Pad a string to a specified length from the end.
|
|
119
|
+
*
|
|
120
|
+
* @param str - The string to pad
|
|
121
|
+
* @param targetLength - The target length
|
|
122
|
+
* @param padString - The string to pad with (default: ' ')
|
|
123
|
+
* @returns The padded string
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* padEnd('5', 3, '0'); // '500'
|
|
128
|
+
* padEnd('hello', 10); // 'hello '
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
function padEnd(str, targetLength, padString = " ") {
|
|
132
|
+
return str.padEnd(targetLength, padString);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Repeat a string a specified number of times.
|
|
136
|
+
*
|
|
137
|
+
* @param str - The string to repeat
|
|
138
|
+
* @param count - The number of times to repeat
|
|
139
|
+
* @returns The repeated string
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* repeat('ha', 3); // 'hahaha'
|
|
144
|
+
* repeat('*', 5); // '*****'
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
function repeat(str, count) {
|
|
148
|
+
return str.repeat(count);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Count the occurrences of a substring in a string.
|
|
152
|
+
*
|
|
153
|
+
* @param str - The string to search in
|
|
154
|
+
* @param searchValue - The substring to count
|
|
155
|
+
* @param caseSensitive - Whether the search is case-sensitive (default: true)
|
|
156
|
+
* @returns The number of occurrences
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* countOccurrences('hello world hello', 'hello'); // 2
|
|
161
|
+
* countOccurrences('Hello HELLO hello', 'hello', false); // 3
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
function countOccurrences(str, searchValue, caseSensitive = true) {
|
|
165
|
+
if (searchValue.length === 0) return 0;
|
|
166
|
+
const source = caseSensitive ? str : str.toLowerCase();
|
|
167
|
+
const search = caseSensitive ? searchValue : searchValue.toLowerCase();
|
|
168
|
+
let count = 0;
|
|
169
|
+
let position = 0;
|
|
170
|
+
while (true) {
|
|
171
|
+
const index = source.indexOf(search, position);
|
|
172
|
+
if (index === -1) break;
|
|
173
|
+
count++;
|
|
174
|
+
position = index + 1;
|
|
175
|
+
}
|
|
176
|
+
return count;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Extract words from a string.
|
|
180
|
+
*
|
|
181
|
+
* @param str - The string to extract words from
|
|
182
|
+
* @returns An array of words
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* words('hello world'); // ['hello', 'world']
|
|
187
|
+
* words('one-two_three'); // ['one', 'two', 'three']
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
function words(str) {
|
|
191
|
+
return str.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu) || [];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
export { capitalize, capitalizeFirst, countOccurrences, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, truncate, words };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/simple-hash.ts
|
|
3
|
+
const DEFAULT_RADIX = 36;
|
|
4
|
+
function simpleHash(str, radix = DEFAULT_RADIX) {
|
|
5
|
+
let hash = 0;
|
|
6
|
+
for (let i = 0; i < str.length; i++) {
|
|
7
|
+
hash = (hash << 5) - hash + str.charCodeAt(i);
|
|
8
|
+
hash = hash & hash;
|
|
9
|
+
}
|
|
10
|
+
return `${Math.abs(hash).toString(radix)}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
exports.DEFAULT_RADIX = DEFAULT_RADIX;
|
|
15
|
+
exports.simpleHash = simpleHash;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/simple-hash.ts
|
|
2
|
+
const DEFAULT_RADIX = 36;
|
|
3
|
+
function simpleHash(str, radix = DEFAULT_RADIX) {
|
|
4
|
+
let hash = 0;
|
|
5
|
+
for (let i = 0; i < str.length; i++) {
|
|
6
|
+
hash = (hash << 5) - hash + str.charCodeAt(i);
|
|
7
|
+
hash = hash & hash;
|
|
8
|
+
}
|
|
9
|
+
return `${Math.abs(hash).toString(radix)}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { DEFAULT_RADIX, simpleHash };
|
package/dist/type-guards.cjs
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/type-guards.ts
|
|
3
|
+
/**
|
|
4
|
+
* Type guard to check if a value is a string.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The value to check
|
|
7
|
+
* @returns True if the value is a string, false otherwise
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* isString('hello'); // true
|
|
12
|
+
* isString(123); // false
|
|
13
|
+
* isString(null); // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
function isString(value) {
|
|
17
|
+
return typeof value === "string";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.isString = isString;
|
package/dist/type-guards.js
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/type-guards.ts
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a value is a string.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The value to check
|
|
6
|
+
* @returns True if the value is a string, false otherwise
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* isString('hello'); // true
|
|
11
|
+
* isString(123); // false
|
|
12
|
+
* isString(null); // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function isString(value) {
|
|
16
|
+
return typeof value === "string";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { isString };
|
package/dist/validation.cjs
CHANGED
|
@@ -1 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/validation.ts
|
|
3
|
+
/**
|
|
4
|
+
* Check if a string is empty (zero length or only whitespace).
|
|
5
|
+
*
|
|
6
|
+
* @param str - The string to check
|
|
7
|
+
* @param trimWhitespace - If true, treats whitespace-only strings as empty (default: true)
|
|
8
|
+
* @returns True if the string is empty, false otherwise
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* isEmpty(''); // true
|
|
13
|
+
* isEmpty(' '); // true
|
|
14
|
+
* isEmpty(' ', false); // false
|
|
15
|
+
* isEmpty('hello'); // false
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function isEmpty(str, trimWhitespace = true) {
|
|
19
|
+
if (trimWhitespace) return str.trim().length === 0;
|
|
20
|
+
return str.length === 0;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Check if a string contains only alphanumeric characters.
|
|
24
|
+
*
|
|
25
|
+
* @param str - The string to check
|
|
26
|
+
* @returns True if the string contains only alphanumeric characters, false otherwise
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* isAlphanumeric('abc123'); // true
|
|
31
|
+
* isAlphanumeric('abc-123'); // false
|
|
32
|
+
* isAlphanumeric(''); // false
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function isAlphanumeric(str) {
|
|
36
|
+
return /^[\da-z]+$/iu.test(str);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check if a string is a valid email address.
|
|
40
|
+
*
|
|
41
|
+
* @param str - The string to check
|
|
42
|
+
* @returns True if the string is a valid email address, false otherwise
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* isEmail('test@example.com'); // true
|
|
47
|
+
* isEmail('invalid-email'); // false
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function isEmail(str) {
|
|
51
|
+
return /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(str);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Check if a string is a valid URL.
|
|
55
|
+
*
|
|
56
|
+
* @param str - The string to check
|
|
57
|
+
* @returns True if the string is a valid URL, false otherwise
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* isUrl('https://example.com'); // true
|
|
62
|
+
* isUrl('not a url'); // false
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
function isUrl(str) {
|
|
66
|
+
try {
|
|
67
|
+
const url = new URL(str);
|
|
68
|
+
return Boolean(url);
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
exports.isAlphanumeric = isAlphanumeric;
|
|
76
|
+
exports.isEmail = isEmail;
|
|
77
|
+
exports.isEmpty = isEmpty;
|
|
78
|
+
exports.isUrl = isUrl;
|
package/dist/validation.js
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/validation.ts
|
|
2
|
+
/**
|
|
3
|
+
* Check if a string is empty (zero length or only whitespace).
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to check
|
|
6
|
+
* @param trimWhitespace - If true, treats whitespace-only strings as empty (default: true)
|
|
7
|
+
* @returns True if the string is empty, false otherwise
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* isEmpty(''); // true
|
|
12
|
+
* isEmpty(' '); // true
|
|
13
|
+
* isEmpty(' ', false); // false
|
|
14
|
+
* isEmpty('hello'); // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function isEmpty(str, trimWhitespace = true) {
|
|
18
|
+
if (trimWhitespace) return str.trim().length === 0;
|
|
19
|
+
return str.length === 0;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Check if a string contains only alphanumeric characters.
|
|
23
|
+
*
|
|
24
|
+
* @param str - The string to check
|
|
25
|
+
* @returns True if the string contains only alphanumeric characters, false otherwise
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* isAlphanumeric('abc123'); // true
|
|
30
|
+
* isAlphanumeric('abc-123'); // false
|
|
31
|
+
* isAlphanumeric(''); // false
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function isAlphanumeric(str) {
|
|
35
|
+
return /^[\da-z]+$/iu.test(str);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check if a string is a valid email address.
|
|
39
|
+
*
|
|
40
|
+
* @param str - The string to check
|
|
41
|
+
* @returns True if the string is a valid email address, false otherwise
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* isEmail('test@example.com'); // true
|
|
46
|
+
* isEmail('invalid-email'); // false
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
function isEmail(str) {
|
|
50
|
+
return /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(str);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if a string is a valid URL.
|
|
54
|
+
*
|
|
55
|
+
* @param str - The string to check
|
|
56
|
+
* @returns True if the string is a valid URL, false otherwise
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* isUrl('https://example.com'); // true
|
|
61
|
+
* isUrl('not a url'); // false
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function isUrl(str) {
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(str);
|
|
67
|
+
return Boolean(url);
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { isAlphanumeric, isEmail, isEmpty, isUrl };
|
package/package.json
CHANGED