@bytebury/toolkit 1.8.0 → 2.0.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/README.md +2 -2
- package/esm/mod.d.ts +1 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +1 -0
- package/esm/src/core.d.ts +141 -24
- package/esm/src/core.d.ts.map +1 -1
- package/esm/src/core.js +213 -29
- package/esm/src/dates.d.ts +22 -0
- package/esm/src/dates.d.ts.map +1 -1
- package/esm/src/dates.js +30 -0
- package/esm/src/numbers.d.ts +63 -0
- package/esm/src/numbers.d.ts.map +1 -1
- package/esm/src/numbers.js +80 -0
- package/esm/src/objects.d.ts +90 -0
- package/esm/src/objects.d.ts.map +1 -0
- package/esm/src/objects.js +129 -0
- package/esm/src/objects_test.d.ts.map +1 -0
- package/esm/src/strings.d.ts +62 -0
- package/esm/src/strings.d.ts.map +1 -1
- package/esm/src/strings.js +81 -0
- package/package.json +30 -1
- package/script/mod.d.ts +1 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +1 -0
- package/script/src/core.d.ts +141 -24
- package/script/src/core.d.ts.map +1 -1
- package/script/src/core.js +225 -31
- package/script/src/dates.d.ts +22 -0
- package/script/src/dates.d.ts.map +1 -1
- package/script/src/dates.js +34 -0
- package/script/src/numbers.d.ts +63 -0
- package/script/src/numbers.d.ts.map +1 -1
- package/script/src/numbers.js +85 -0
- package/script/src/objects.d.ts +90 -0
- package/script/src/objects.d.ts.map +1 -0
- package/script/src/objects.js +137 -0
- package/script/src/objects_test.d.ts.map +1 -0
- package/script/src/strings.d.ts +62 -0
- package/script/src/strings.d.ts.map +1 -1
- package/script/src/strings.js +85 -0
package/script/src/numbers.js
CHANGED
|
@@ -8,6 +8,11 @@ exports.min = min;
|
|
|
8
8
|
exports.round = round;
|
|
9
9
|
exports.sum = sum;
|
|
10
10
|
exports.average = average;
|
|
11
|
+
exports.clamp = clamp;
|
|
12
|
+
exports.median = median;
|
|
13
|
+
exports.roundTo = roundTo;
|
|
14
|
+
exports.random = random;
|
|
15
|
+
exports.inRange = inRange;
|
|
11
16
|
const core_js_1 = require("./core.js");
|
|
12
17
|
/**
|
|
13
18
|
* Determines if a number is even.
|
|
@@ -187,3 +192,83 @@ function average(nums) {
|
|
|
187
192
|
return 0;
|
|
188
193
|
return sum(nums) / nums.length;
|
|
189
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Clamps a number between a minimum and maximum value.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* clamp(5, 0, 10); // 5
|
|
201
|
+
* clamp(-1, 0, 10); // 0
|
|
202
|
+
* clamp(15, 0, 10); // 10
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
function clamp(num, min, max) {
|
|
206
|
+
return Math.min(Math.max(num, min), max);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Returns the median value from a list of numbers.
|
|
210
|
+
*
|
|
211
|
+
* @remarks
|
|
212
|
+
* If you pass an empty list, this will return `0`.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* median([]); // 0
|
|
217
|
+
* median([1, 2, 3]); // 2
|
|
218
|
+
* median([1, 2, 3, 4]); // 2.5
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
function median(nums) {
|
|
222
|
+
if ((0, core_js_1.isEmpty)(nums))
|
|
223
|
+
return 0;
|
|
224
|
+
const sorted = [...nums].sort((a, b) => a - b);
|
|
225
|
+
const mid = Math.floor(sorted.length / 2);
|
|
226
|
+
return sorted.length % 2 === 0
|
|
227
|
+
? (sorted[mid - 1] + sorted[mid]) / 2
|
|
228
|
+
: sorted[mid];
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Rounds a number to the given number of decimal places.
|
|
232
|
+
*
|
|
233
|
+
* @remarks
|
|
234
|
+
* This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* roundTo(1.235, 2); // 1.24
|
|
239
|
+
* roundTo(1.5, 0); // 2
|
|
240
|
+
* roundTo(1234.5, -2); // 1200
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
function roundTo(num, decimals) {
|
|
244
|
+
const factor = Math.pow(10, decimals);
|
|
245
|
+
return Math.round((num || 0) * factor) / factor;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Gives a random number in the given range. The first parameter is inclusive
|
|
249
|
+
* and the second one is exclusive. Therefore, it will work with lists out of
|
|
250
|
+
* the box.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* random(0, 10); // 0 -> 9
|
|
255
|
+
* random(3, 7); // 3 -> 6
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
function random(start, end) {
|
|
259
|
+
return Math.floor(Math.random() * (end - start)) + start;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Returns true if the given value is within the given range (inclusive on both ends).
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* inRange(5, 0, 10); // true
|
|
267
|
+
* inRange(0, 0, 10); // true
|
|
268
|
+
* inRange(10, 0, 10); // true
|
|
269
|
+
* inRange(11, 0, 10); // false
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
function inRange(value, min, max) {
|
|
273
|
+
return value >= min && value <= max;
|
|
274
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { None } from "./utility_types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a strongly-typed array of an object's own enumerable keys.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty array.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* keys({ a: 1, b: 2 }); // ["a", "b"]
|
|
11
|
+
* keys({}); // []
|
|
12
|
+
* keys(null); // []
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function keys<T extends Record<string, unknown>>(obj: T | None): (keyof T)[];
|
|
16
|
+
/**
|
|
17
|
+
* Returns a strongly-typed array of an object's own enumerable `[key, value]` pairs.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty array.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
|
|
25
|
+
* entries({}); // []
|
|
26
|
+
* entries(null); // []
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function entries<T extends Record<string, unknown>>(obj: T | None): [keyof T, T[keyof T]][];
|
|
30
|
+
/**
|
|
31
|
+
* Returns a new object containing only the specified keys. Keys that are not
|
|
32
|
+
* present on the source object are silently ignored.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
|
|
40
|
+
* pick({ a: 1, b: 2 }, []); // {}
|
|
41
|
+
* pick(null, ["a"]); // {}
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T | None, keys: K[]): Pick<T, K>;
|
|
45
|
+
/**
|
|
46
|
+
* Returns a new object with the specified keys excluded.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* omit({ a: 1, b: 2, c: 3 }, ["a"]); // { b: 2, c: 3 }
|
|
54
|
+
* omit({ a: 1, b: 2 }, []); // { a: 1, b: 2 }
|
|
55
|
+
* omit(null, ["a"]); // {}
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T | None, keys: K[]): Omit<T, K>;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a new object with the same keys, where each value is transformed by `fn`.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* mapValues({ a: 1, b: 2 }, (v) => v * 2); // { a: 2, b: 4 }
|
|
68
|
+
* mapValues({ a: 1, b: 2 }, (v, k) => `${k}=${v}`); // { a: "a=1", b: "b=2" }
|
|
69
|
+
* mapValues(null, (v) => v); // {}
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function mapValues<T extends Record<string, unknown>, V>(obj: T | None, fn: (value: T[keyof T], key: keyof T) => V): {
|
|
73
|
+
[K in keyof T]: V;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Returns a new object with each key transformed by `fn`. Values are unchanged.
|
|
77
|
+
* If `fn` produces duplicate keys, later entries overwrite earlier ones.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* mapKeys({ a: 1, b: 2 }, (k) => k.toUpperCase()); // { A: 1, B: 2 }
|
|
85
|
+
* mapKeys({ first: "Bob", last: "Lee" }, (k) => `user_${k}`); // { user_first: "Bob", user_last: "Lee" }
|
|
86
|
+
* mapKeys(null, (k) => k); // {}
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function mapKeys<T extends Record<string, unknown>>(obj: T | None, fn: (key: keyof T, value: T[keyof T]) => string): Record<string, T[keyof T]>;
|
|
90
|
+
//# sourceMappingURL=objects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.d.ts","sourceRoot":"","sources":["../../src/src/objects.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/C;;;;;;;;;;;;GAYG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpD,GAAG,EAAE,CAAC,GAAG,IAAI,GACZ,CAAC,MAAM,CAAC,CAAC,EAAE,CAGb;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvD,GAAG,EAAE,CAAC,GAAG,IAAI,GACZ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAGzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACvE,GAAG,EAAE,CAAC,GAAG,IAAI,EACb,IAAI,EAAE,CAAC,EAAE,GACR,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAOZ;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACvE,GAAG,EAAE,CAAC,GAAG,IAAI,EACb,IAAI,EAAE,CAAC,EAAE,GACR,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAQZ;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,EAC5D,GAAG,EAAE,CAAC,GAAG,IAAI,EACb,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,GACzC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC;CAAE,CAOvB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvD,GAAG,EAAE,CAAC,GAAG,IAAI,EACb,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,MAAM,GAC9C,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAO5B"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keys = keys;
|
|
4
|
+
exports.entries = entries;
|
|
5
|
+
exports.pick = pick;
|
|
6
|
+
exports.omit = omit;
|
|
7
|
+
exports.mapValues = mapValues;
|
|
8
|
+
exports.mapKeys = mapKeys;
|
|
9
|
+
/**
|
|
10
|
+
* Returns a strongly-typed array of an object's own enumerable keys.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty array.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* keys({ a: 1, b: 2 }); // ["a", "b"]
|
|
18
|
+
* keys({}); // []
|
|
19
|
+
* keys(null); // []
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function keys(obj) {
|
|
23
|
+
if (obj == null)
|
|
24
|
+
return [];
|
|
25
|
+
return Object.keys(obj);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns a strongly-typed array of an object's own enumerable `[key, value]` pairs.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty array.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
|
|
36
|
+
* entries({}); // []
|
|
37
|
+
* entries(null); // []
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function entries(obj) {
|
|
41
|
+
if (obj == null)
|
|
42
|
+
return [];
|
|
43
|
+
return Object.entries(obj);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns a new object containing only the specified keys. Keys that are not
|
|
47
|
+
* present on the source object are silently ignored.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
|
|
55
|
+
* pick({ a: 1, b: 2 }, []); // {}
|
|
56
|
+
* pick(null, ["a"]); // {}
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function pick(obj, keys) {
|
|
60
|
+
if (obj == null)
|
|
61
|
+
return {};
|
|
62
|
+
const result = {};
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
if (key in obj)
|
|
65
|
+
result[key] = obj[key];
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a new object with the specified keys excluded.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* omit({ a: 1, b: 2, c: 3 }, ["a"]); // { b: 2, c: 3 }
|
|
78
|
+
* omit({ a: 1, b: 2 }, []); // { a: 1, b: 2 }
|
|
79
|
+
* omit(null, ["a"]); // {}
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function omit(obj, keys) {
|
|
83
|
+
if (obj == null)
|
|
84
|
+
return {};
|
|
85
|
+
const excluded = new Set(keys);
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const key of Object.keys(obj)) {
|
|
88
|
+
if (!excluded.has(key))
|
|
89
|
+
result[key] = obj[key];
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns a new object with the same keys, where each value is transformed by `fn`.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* mapValues({ a: 1, b: 2 }, (v) => v * 2); // { a: 2, b: 4 }
|
|
102
|
+
* mapValues({ a: 1, b: 2 }, (v, k) => `${k}=${v}`); // { a: "a=1", b: "b=2" }
|
|
103
|
+
* mapValues(null, (v) => v); // {}
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function mapValues(obj, fn) {
|
|
107
|
+
if (obj == null)
|
|
108
|
+
return {};
|
|
109
|
+
const result = {};
|
|
110
|
+
for (const key of Object.keys(obj)) {
|
|
111
|
+
result[key] = fn(obj[key], key);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns a new object with each key transformed by `fn`. Values are unchanged.
|
|
117
|
+
* If `fn` produces duplicate keys, later entries overwrite earlier ones.
|
|
118
|
+
*
|
|
119
|
+
* @remarks
|
|
120
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an empty object.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* mapKeys({ a: 1, b: 2 }, (k) => k.toUpperCase()); // { A: 1, B: 2 }
|
|
125
|
+
* mapKeys({ first: "Bob", last: "Lee" }, (k) => `user_${k}`); // { user_first: "Bob", user_last: "Lee" }
|
|
126
|
+
* mapKeys(null, (k) => k); // {}
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function mapKeys(obj, fn) {
|
|
130
|
+
if (obj == null)
|
|
131
|
+
return {};
|
|
132
|
+
const result = {};
|
|
133
|
+
for (const key of Object.keys(obj)) {
|
|
134
|
+
result[fn(key, obj[key])] = obj[key];
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects_test.d.ts","sourceRoot":"","sources":["../../src/src/objects_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}
|
package/script/src/strings.d.ts
CHANGED
|
@@ -163,4 +163,66 @@ export declare function keepAlphanumeric(text: string): string;
|
|
|
163
163
|
* ```
|
|
164
164
|
*/
|
|
165
165
|
export declare function keepNumeric(text: string): string;
|
|
166
|
+
/**
|
|
167
|
+
* Converts the string to `camelCase` by removing punctuation, trimming extra
|
|
168
|
+
* spaces, and lowercasing the first word with subsequent words capitalized.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* camel(null); // ""
|
|
176
|
+
* camel("Hello World"); // "helloWorld"
|
|
177
|
+
* camel("user_profile_page"); // "userProfilePage"
|
|
178
|
+
* camel("HELLO-WORLD"); // "helloWorld"
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export declare function camel(text: string): string;
|
|
182
|
+
/**
|
|
183
|
+
* Converts the string to `PascalCase` by removing punctuation, trimming extra
|
|
184
|
+
* spaces, and capitalizing every word.
|
|
185
|
+
*
|
|
186
|
+
* @remarks
|
|
187
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* pascal(null); // ""
|
|
192
|
+
* pascal("hello world"); // "HelloWorld"
|
|
193
|
+
* pascal("user_profile_page"); // "UserProfilePage"
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare function pascal(text: string): string;
|
|
197
|
+
/**
|
|
198
|
+
* Truncates a string to the given length. If truncation occurs, the suffix is
|
|
199
|
+
* appended and counted toward the total length.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* truncate(null, 10); // ""
|
|
207
|
+
* truncate("Hello World", 5); // "He..."
|
|
208
|
+
* truncate("Hello World", 20); // "Hello World"
|
|
209
|
+
* truncate("Hello World", 8, "…"); // "Hello W…"
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export declare function truncate(text: string, length: number, suffix?: string): string;
|
|
213
|
+
/**
|
|
214
|
+
* Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
|
|
215
|
+
* removed, and whitespace is replaced with hyphens.
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* slugify(null); // ""
|
|
223
|
+
* slugify("Hello World!"); // "hello-world"
|
|
224
|
+
* slugify("Café à la Carte"); // "cafe-a-la-carte"
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export declare function slugify(text: string): string;
|
|
166
228
|
//# sourceMappingURL=strings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/src/strings.ts"],"names":[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M,CAI7E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE5C"}
|
package/script/src/strings.js
CHANGED
|
@@ -11,6 +11,10 @@ exports.snake = snake;
|
|
|
11
11
|
exports.keepAlphabetical = keepAlphabetical;
|
|
12
12
|
exports.keepAlphanumeric = keepAlphanumeric;
|
|
13
13
|
exports.keepNumeric = keepNumeric;
|
|
14
|
+
exports.camel = camel;
|
|
15
|
+
exports.pascal = pascal;
|
|
16
|
+
exports.truncate = truncate;
|
|
17
|
+
exports.slugify = slugify;
|
|
14
18
|
/**
|
|
15
19
|
* Determines if the given text is only comprised of whitespace.
|
|
16
20
|
*
|
|
@@ -202,6 +206,87 @@ function keepAlphanumeric(text) {
|
|
|
202
206
|
function keepNumeric(text) {
|
|
203
207
|
return (text || "").replace(/[^\d]/g, "");
|
|
204
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Converts the string to `camelCase` by removing punctuation, trimming extra
|
|
211
|
+
* spaces, and lowercasing the first word with subsequent words capitalized.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* camel(null); // ""
|
|
219
|
+
* camel("Hello World"); // "helloWorld"
|
|
220
|
+
* camel("user_profile_page"); // "userProfilePage"
|
|
221
|
+
* camel("HELLO-WORLD"); // "helloWorld"
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
function camel(text) {
|
|
225
|
+
const parts = kebab(text).split("-").filter(Boolean);
|
|
226
|
+
if (parts.length === 0)
|
|
227
|
+
return "";
|
|
228
|
+
return parts[0] +
|
|
229
|
+
parts.slice(1).map((p) => upper(p[0]) + p.slice(1)).join("");
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Converts the string to `PascalCase` by removing punctuation, trimming extra
|
|
233
|
+
* spaces, and capitalizing every word.
|
|
234
|
+
*
|
|
235
|
+
* @remarks
|
|
236
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* pascal(null); // ""
|
|
241
|
+
* pascal("hello world"); // "HelloWorld"
|
|
242
|
+
* pascal("user_profile_page"); // "UserProfilePage"
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
function pascal(text) {
|
|
246
|
+
return kebab(text)
|
|
247
|
+
.split("-")
|
|
248
|
+
.filter(Boolean)
|
|
249
|
+
.map((p) => upper(p[0]) + p.slice(1))
|
|
250
|
+
.join("");
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Truncates a string to the given length. If truncation occurs, the suffix is
|
|
254
|
+
* appended and counted toward the total length.
|
|
255
|
+
*
|
|
256
|
+
* @remarks
|
|
257
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* truncate(null, 10); // ""
|
|
262
|
+
* truncate("Hello World", 5); // "He..."
|
|
263
|
+
* truncate("Hello World", 20); // "Hello World"
|
|
264
|
+
* truncate("Hello World", 8, "…"); // "Hello W…"
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
function truncate(text, length, suffix = "...") {
|
|
268
|
+
const t = text || "";
|
|
269
|
+
if (t.length <= length)
|
|
270
|
+
return t;
|
|
271
|
+
return t.slice(0, Math.max(0, length - suffix.length)) + suffix;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
|
|
275
|
+
* removed, and whitespace is replaced with hyphens.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* slugify(null); // ""
|
|
283
|
+
* slugify("Hello World!"); // "hello-world"
|
|
284
|
+
* slugify("Café à la Carte"); // "cafe-a-la-carte"
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
function slugify(text) {
|
|
288
|
+
return kebab(text);
|
|
289
|
+
}
|
|
205
290
|
function removePunctuation(text) {
|
|
206
291
|
return text
|
|
207
292
|
.normalize("NFKD")
|