@bytebury/toolkit 1.9.0 → 2.1.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 +11 -2
- package/esm/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/build_message.d.ts.map +1 -1
- package/{script/deps/jsr.io/@std/internal/1.0.13 → esm/deps/jsr.io/@std/internal/1.0.14}/diff.d.ts.map +1 -1
- package/{script/deps/jsr.io/@std/internal/1.0.13 → esm/deps/jsr.io/@std/internal/1.0.14}/diff_str.d.ts.map +1 -1
- package/{script/deps/jsr.io/@std/internal/1.0.13 → esm/deps/jsr.io/@std/internal/1.0.14}/format.d.ts.map +1 -1
- package/esm/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/styles.d.ts.map +1 -1
- package/esm/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/types.d.ts.map +1 -1
- 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 +151 -26
- package/esm/src/core.d.ts.map +1 -1
- package/esm/src/core.js +226 -30
- package/esm/src/dates.d.ts +14 -0
- package/esm/src/dates.d.ts.map +1 -1
- package/esm/src/dates.js +18 -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 +70 -0
- package/esm/src/strings.d.ts.map +1 -1
- package/esm/src/strings.js +93 -0
- package/package.json +30 -1
- package/script/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/build_message.d.ts.map +1 -1
- package/{esm/deps/jsr.io/@std/internal/1.0.13 → script/deps/jsr.io/@std/internal/1.0.14}/diff.d.ts.map +1 -1
- package/{esm/deps/jsr.io/@std/internal/1.0.13 → script/deps/jsr.io/@std/internal/1.0.14}/diff_str.d.ts.map +1 -1
- package/{esm/deps/jsr.io/@std/internal/1.0.13 → script/deps/jsr.io/@std/internal/1.0.14}/format.d.ts.map +1 -1
- package/script/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/styles.d.ts.map +1 -1
- package/script/deps/jsr.io/@std/internal/{1.0.13 → 1.0.14}/types.d.ts.map +1 -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 +151 -26
- package/script/src/core.d.ts.map +1 -1
- package/script/src/core.js +240 -32
- package/script/src/dates.d.ts +14 -0
- package/script/src/dates.d.ts.map +1 -1
- package/script/src/dates.js +20 -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 +70 -0
- package/script/src/strings.d.ts.map +1 -1
- package/script/src/strings.js +99 -0
package/script/src/dates.js
CHANGED
|
@@ -27,6 +27,8 @@ exports.isWeekday = isWeekday;
|
|
|
27
27
|
exports.isInPast = isInPast;
|
|
28
28
|
exports.isInFuture = isInFuture;
|
|
29
29
|
exports.isToday = isToday;
|
|
30
|
+
exports.isYesterday = isYesterday;
|
|
31
|
+
exports.isTomorrow = isTomorrow;
|
|
30
32
|
const duration_js_1 = require("./duration.js");
|
|
31
33
|
/**
|
|
32
34
|
* Right now. This is an alias for `new Date()`.
|
|
@@ -236,3 +238,21 @@ function isInFuture(date) {
|
|
|
236
238
|
function isToday(date) {
|
|
237
239
|
return !isInFuture(date) && !isInPast(date);
|
|
238
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Determines if the date is yesterday.
|
|
243
|
+
*
|
|
244
|
+
* @remarks
|
|
245
|
+
* this is date specific. Time of day is ignored.
|
|
246
|
+
*/
|
|
247
|
+
function isYesterday(date) {
|
|
248
|
+
return date >= yesterday() && date < today();
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Determines if the date is tomorrow.
|
|
252
|
+
*
|
|
253
|
+
* @remarks
|
|
254
|
+
* this is date specific. Time of day is ignored.
|
|
255
|
+
*/
|
|
256
|
+
function isTomorrow(date) {
|
|
257
|
+
return date >= tomorrow() && date < addDays(tomorrow(), 1);
|
|
258
|
+
}
|
package/script/src/numbers.d.ts
CHANGED
|
@@ -139,4 +139,67 @@ export declare function sum(nums: number[]): number;
|
|
|
139
139
|
* ```
|
|
140
140
|
*/
|
|
141
141
|
export declare function average(nums: number[]): number;
|
|
142
|
+
/**
|
|
143
|
+
* Clamps a number between a minimum and maximum value.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* clamp(5, 0, 10); // 5
|
|
148
|
+
* clamp(-1, 0, 10); // 0
|
|
149
|
+
* clamp(15, 0, 10); // 10
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function clamp(num: number, min: number, max: number): number;
|
|
153
|
+
/**
|
|
154
|
+
* Returns the median value from a list of numbers.
|
|
155
|
+
*
|
|
156
|
+
* @remarks
|
|
157
|
+
* If you pass an empty list, this will return `0`.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* median([]); // 0
|
|
162
|
+
* median([1, 2, 3]); // 2
|
|
163
|
+
* median([1, 2, 3, 4]); // 2.5
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export declare function median(nums: number[]): number;
|
|
167
|
+
/**
|
|
168
|
+
* Rounds a number to the given number of decimal places.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* roundTo(1.235, 2); // 1.24
|
|
176
|
+
* roundTo(1.5, 0); // 2
|
|
177
|
+
* roundTo(1234.5, -2); // 1200
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function roundTo(num: number, decimals: number): number;
|
|
181
|
+
/**
|
|
182
|
+
* Gives a random number in the given range. The first parameter is inclusive
|
|
183
|
+
* and the second one is exclusive. Therefore, it will work with lists out of
|
|
184
|
+
* the box.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* random(0, 10); // 0 -> 9
|
|
189
|
+
* random(3, 7); // 3 -> 6
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
export declare function random(start: number, end: number): number;
|
|
193
|
+
/**
|
|
194
|
+
* Returns true if the given value is within the given range (inclusive on both ends).
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* inRange(5, 0, 10); // true
|
|
199
|
+
* inRange(0, 0, 10); // true
|
|
200
|
+
* inRange(10, 0, 10); // true
|
|
201
|
+
* inRange(11, 0, 10); // false
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export declare function inRange(value: number, min: number, max: number): boolean;
|
|
142
205
|
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/src/numbers.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE1C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MA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GAAG,MAAM,CAG9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAO7C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAG7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExE"}
|
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;;;;;;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AK,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
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
export declare function isWhitespace(text: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Alias for `isWhitespace`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isNoneOrWhitespace(text: string): boolean;
|
|
18
22
|
/**
|
|
19
23
|
* Determines if the given text contains any non-whitespace characters.
|
|
20
24
|
*
|
|
@@ -29,6 +33,10 @@ export declare function isWhitespace(text: string): boolean;
|
|
|
29
33
|
* ```
|
|
30
34
|
*/
|
|
31
35
|
export declare function isNotWhitespace(text: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Alias for `isNotWhitespace`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isNotNoneOrWhitespace(text: string): boolean;
|
|
32
40
|
/**
|
|
33
41
|
* Trims the whitespace from the beginning and the end. This is an
|
|
34
42
|
* alias for `.trim()`. Useful for when you're mapping over lists.
|
|
@@ -163,4 +171,66 @@ export declare function keepAlphanumeric(text: string): string;
|
|
|
163
171
|
* ```
|
|
164
172
|
*/
|
|
165
173
|
export declare function keepNumeric(text: string): string;
|
|
174
|
+
/**
|
|
175
|
+
* Converts the string to `camelCase` by removing punctuation, trimming extra
|
|
176
|
+
* spaces, and lowercasing the first word with subsequent words capitalized.
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* camel(null); // ""
|
|
184
|
+
* camel("Hello World"); // "helloWorld"
|
|
185
|
+
* camel("user_profile_page"); // "userProfilePage"
|
|
186
|
+
* camel("HELLO-WORLD"); // "helloWorld"
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export declare function camel(text: string): string;
|
|
190
|
+
/**
|
|
191
|
+
* Converts the string to `PascalCase` by removing punctuation, trimming extra
|
|
192
|
+
* spaces, and capitalizing every word.
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* pascal(null); // ""
|
|
200
|
+
* pascal("hello world"); // "HelloWorld"
|
|
201
|
+
* pascal("user_profile_page"); // "UserProfilePage"
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export declare function pascal(text: string): string;
|
|
205
|
+
/**
|
|
206
|
+
* Truncates a string to the given length. If truncation occurs, the suffix is
|
|
207
|
+
* appended and counted toward the total length.
|
|
208
|
+
*
|
|
209
|
+
* @remarks
|
|
210
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* truncate(null, 10); // ""
|
|
215
|
+
* truncate("Hello World", 5); // "He..."
|
|
216
|
+
* truncate("Hello World", 20); // "Hello World"
|
|
217
|
+
* truncate("Hello World", 8, "…"); // "Hello W…"
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
export declare function truncate(text: string, length: number, suffix?: string): string;
|
|
221
|
+
/**
|
|
222
|
+
* Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
|
|
223
|
+
* removed, and whitespace is replaced with hyphens.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* slugify(null); // ""
|
|
231
|
+
* slugify("Hello World!"); // "hello-world"
|
|
232
|
+
* slugify("Café à la Carte"); // "cafe-a-la-carte"
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export declare function slugify(text: string): string;
|
|
166
236
|
//# sourceMappingURL=strings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/src/strings.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM1C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAErD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAIrD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD"}
|
|
1
|
+
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src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;;;GAcG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAK1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM3C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAQ,GAAG,MAAM,CAI7E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE5C"}
|
package/script/src/strings.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isWhitespace = isWhitespace;
|
|
4
|
+
exports.isNoneOrWhitespace = isNoneOrWhitespace;
|
|
4
5
|
exports.isNotWhitespace = isNotWhitespace;
|
|
6
|
+
exports.isNotNoneOrWhitespace = isNotNoneOrWhitespace;
|
|
5
7
|
exports.trim = trim;
|
|
6
8
|
exports.title = title;
|
|
7
9
|
exports.lower = lower;
|
|
@@ -11,6 +13,10 @@ exports.snake = snake;
|
|
|
11
13
|
exports.keepAlphabetical = keepAlphabetical;
|
|
12
14
|
exports.keepAlphanumeric = keepAlphanumeric;
|
|
13
15
|
exports.keepNumeric = keepNumeric;
|
|
16
|
+
exports.camel = camel;
|
|
17
|
+
exports.pascal = pascal;
|
|
18
|
+
exports.truncate = truncate;
|
|
19
|
+
exports.slugify = slugify;
|
|
14
20
|
/**
|
|
15
21
|
* Determines if the given text is only comprised of whitespace.
|
|
16
22
|
*
|
|
@@ -30,6 +36,12 @@ exports.keepNumeric = keepNumeric;
|
|
|
30
36
|
function isWhitespace(text) {
|
|
31
37
|
return trim(text).length === 0;
|
|
32
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Alias for `isWhitespace`.
|
|
41
|
+
*/
|
|
42
|
+
function isNoneOrWhitespace(text) {
|
|
43
|
+
return isWhitespace(text);
|
|
44
|
+
}
|
|
33
45
|
/**
|
|
34
46
|
* Determines if the given text contains any non-whitespace characters.
|
|
35
47
|
*
|
|
@@ -46,6 +58,12 @@ function isWhitespace(text) {
|
|
|
46
58
|
function isNotWhitespace(text) {
|
|
47
59
|
return !isWhitespace(text);
|
|
48
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Alias for `isNotWhitespace`.
|
|
63
|
+
*/
|
|
64
|
+
function isNotNoneOrWhitespace(text) {
|
|
65
|
+
return isNotWhitespace(text);
|
|
66
|
+
}
|
|
49
67
|
/**
|
|
50
68
|
* Trims the whitespace from the beginning and the end. This is an
|
|
51
69
|
* alias for `.trim()`. Useful for when you're mapping over lists.
|
|
@@ -202,6 +220,87 @@ function keepAlphanumeric(text) {
|
|
|
202
220
|
function keepNumeric(text) {
|
|
203
221
|
return (text || "").replace(/[^\d]/g, "");
|
|
204
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Converts the string to `camelCase` by removing punctuation, trimming extra
|
|
225
|
+
* spaces, and lowercasing the first word with subsequent words capitalized.
|
|
226
|
+
*
|
|
227
|
+
* @remarks
|
|
228
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* camel(null); // ""
|
|
233
|
+
* camel("Hello World"); // "helloWorld"
|
|
234
|
+
* camel("user_profile_page"); // "userProfilePage"
|
|
235
|
+
* camel("HELLO-WORLD"); // "helloWorld"
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
function camel(text) {
|
|
239
|
+
const parts = kebab(text).split("-").filter(Boolean);
|
|
240
|
+
if (parts.length === 0)
|
|
241
|
+
return "";
|
|
242
|
+
return parts[0] +
|
|
243
|
+
parts.slice(1).map((p) => upper(p[0]) + p.slice(1)).join("");
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Converts the string to `PascalCase` by removing punctuation, trimming extra
|
|
247
|
+
* spaces, and capitalizing every word.
|
|
248
|
+
*
|
|
249
|
+
* @remarks
|
|
250
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* pascal(null); // ""
|
|
255
|
+
* pascal("hello world"); // "HelloWorld"
|
|
256
|
+
* pascal("user_profile_page"); // "UserProfilePage"
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
function pascal(text) {
|
|
260
|
+
return kebab(text)
|
|
261
|
+
.split("-")
|
|
262
|
+
.filter(Boolean)
|
|
263
|
+
.map((p) => upper(p[0]) + p.slice(1))
|
|
264
|
+
.join("");
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Truncates a string to the given length. If truncation occurs, the suffix is
|
|
268
|
+
* appended and counted toward the total length.
|
|
269
|
+
*
|
|
270
|
+
* @remarks
|
|
271
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* truncate(null, 10); // ""
|
|
276
|
+
* truncate("Hello World", 5); // "He..."
|
|
277
|
+
* truncate("Hello World", 20); // "Hello World"
|
|
278
|
+
* truncate("Hello World", 8, "…"); // "Hello W…"
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
function truncate(text, length, suffix = "...") {
|
|
282
|
+
const t = text || "";
|
|
283
|
+
if (t.length <= length)
|
|
284
|
+
return t;
|
|
285
|
+
return t.slice(0, Math.max(0, length - suffix.length)) + suffix;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
|
|
289
|
+
* removed, and whitespace is replaced with hyphens.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* slugify(null); // ""
|
|
297
|
+
* slugify("Hello World!"); // "hello-world"
|
|
298
|
+
* slugify("Café à la Carte"); // "cafe-a-la-carte"
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
function slugify(text) {
|
|
302
|
+
return kebab(text);
|
|
303
|
+
}
|
|
205
304
|
function removePunctuation(text) {
|
|
206
305
|
return text
|
|
207
306
|
.normalize("NFKD")
|