@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/README.md
CHANGED
|
@@ -30,8 +30,10 @@ deno add jsr:@bytebury/toolkit
|
|
|
30
30
|
## Sample Usage
|
|
31
31
|
|
|
32
32
|
```ts
|
|
33
|
+
import { average, isNoneOrWhitespace, title } from "@bytebury/toolkit";
|
|
34
|
+
|
|
33
35
|
function sayHelloTo(name: string): void {
|
|
34
|
-
if (
|
|
36
|
+
if (isNoneOrWhitespace(name)) {
|
|
35
37
|
console.log("Hello, Guest!");
|
|
36
38
|
} else {
|
|
37
39
|
console.log(`Hello, ${title(name)}!`);
|
|
@@ -42,8 +44,15 @@ function getAverageAge(): number {
|
|
|
42
44
|
const people = [
|
|
43
45
|
{ name: "Tom", age: 2 },
|
|
44
46
|
{ name: "Carly", age: 8 },
|
|
45
|
-
{ name: "Jenny", age: 5 }
|
|
47
|
+
{ name: "Jenny", age: 5 },
|
|
46
48
|
];
|
|
47
49
|
return average(people.map(({ age }) => age)); // 5
|
|
48
50
|
}
|
|
49
51
|
```
|
|
52
|
+
|
|
53
|
+
## Naming Notes
|
|
54
|
+
|
|
55
|
+
Helpers such as `isWhitespace` and `isEmpty` are already `null` and `undefined`
|
|
56
|
+
safe. The more explicit `isNoneOrWhitespace`, `isNoneOrEmpty`,
|
|
57
|
+
`isNotNoneOrWhitespace`, and `isNotNoneOrEmpty` aliases are available when that
|
|
58
|
+
behavior should be visible at the call site.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build_message.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"build_message.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/build_message.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEvD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,QAAQ;AAClB;;;GAGG;AACH,UAAU,UAAQ,GACjB,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAWvB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CASrD;AAED,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,YAAY,CAC1B,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAC7C,OAAO,GAAE,mBAAwB,EACjC,YAAY,CAAC,EAAE,CACb,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAC7C,UAAU,EAAE,OAAO,EACnB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,KAC1B,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,GACrC,MAAM,EAAE,CA8BV"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/diff.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEvD,2DAA2D;AAC3D,MAAM,WAAW,aAAa;IAC5B,qCAAqC;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,2BAA2B;IAC3B,EAAE,EAAE,MAAM,CAAC;CACZ;AAMD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAanD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,aAAa,CAWvE;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,CAAC,EAAE,CAAC,EAAE,EACN,CAAC,EAAE,CAAC,EAAE,EACN,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,WAAW,EACnB,kBAAkB,EAAE,MAAM,GACzB,KAAK,CAAC;IACP,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,CAAC,CAAC;CACV,CAAC,CAiCD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,QAAQ,CACtB,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,MAAM,EAAE,WAAW,EACnB,kBAAkB,EAAE,MAAM,EAC1B,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,aAAa,EACrB,IAAI,CAAC,EAAE,aAAa,GACnB,aAAa,CAsBf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,CAwEvD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diff_str.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"diff_str.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/diff_str.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAqB,UAAU,EAAE,MAAM,YAAY,CAAC;AAGhE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAY/C;AAKD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,UAAQ,GAAG,MAAM,EAAE,CAiBnE;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,EACxB,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,EAAE,GAC3B,UAAU,CAAC,MAAM,CAAC,EAAE,CAetB;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,CAkDlE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/format.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,SAAS,GAAG,CACtB,CAAC,EAAE,OAAO,EACV,OAAO,EAAE;IACP,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,KACE,MAAM,CAAC;AAEZ;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,CAmBzC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/styles.ts"],"names":[],"mappings":"AAqCA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAExC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAExC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE3C;AAWD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEpD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/internal/1.0.14/types.ts"],"names":[],"mappings":"AAGA,kDAAkD;AAClD,MAAM,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAEvE;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI;IAChC,IAAI,EAAE,QAAQ,GAAG,YAAY,CAAC;IAC9B,KAAK,EAAE,CAAC,CAAC;CACV,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;IACjC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC;IAC1B,KAAK,EAAE,CAAC,CAAC;IACT,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;CAC3B,CAAC"}
|
package/esm/mod.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./src/core.js";
|
|
|
2
2
|
export * from "./src/dates.js";
|
|
3
3
|
export * from "./src/duration.js";
|
|
4
4
|
export * from "./src/numbers.js";
|
|
5
|
+
export * from "./src/objects.js";
|
|
5
6
|
export * from "./src/strings.js";
|
|
6
7
|
export * from "./src/utility_types.js";
|
|
7
8
|
//# sourceMappingURL=mod.d.ts.map
|
package/esm/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC"}
|
package/esm/mod.js
CHANGED
package/esm/src/core.d.ts
CHANGED
|
@@ -163,6 +163,10 @@ export declare function reverse<T>(thing: Set<T>): Set<T>;
|
|
|
163
163
|
* ```
|
|
164
164
|
*/
|
|
165
165
|
export declare function isEmpty(thing: unknown): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Alias for `isEmpty`.
|
|
168
|
+
*/
|
|
169
|
+
export declare function isNoneOrEmpty(thing: unknown): boolean;
|
|
166
170
|
/**
|
|
167
171
|
* Determines if the given thing is not empty.
|
|
168
172
|
*
|
|
@@ -180,6 +184,10 @@ export declare function isEmpty(thing: unknown): boolean;
|
|
|
180
184
|
* ```
|
|
181
185
|
*/
|
|
182
186
|
export declare function isNotEmpty(thing: unknown): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Alias for `isNotEmpty`.
|
|
189
|
+
*/
|
|
190
|
+
export declare function isNotNoneOrEmpty(thing: unknown): boolean;
|
|
183
191
|
/**
|
|
184
192
|
* Returns the distinct values from a list.
|
|
185
193
|
*
|
|
@@ -220,18 +228,6 @@ export declare function distinct<T>(list: T[]): T[];
|
|
|
220
228
|
* ```
|
|
221
229
|
*/
|
|
222
230
|
export declare function sample<T>(list: T[]): T | undefined;
|
|
223
|
-
/**
|
|
224
|
-
* Gives a random number in the given range. The first parameter is inclusive
|
|
225
|
-
* and the second one is exclusive. Therefore, it will work with lists out of
|
|
226
|
-
* the box.
|
|
227
|
-
*
|
|
228
|
-
* @example
|
|
229
|
-
* ```ts
|
|
230
|
-
* rand(0, 10); // 0 -> 9
|
|
231
|
-
* rand(3, 7); // 3 -> 6
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
export declare function random(start: number, end: number): number;
|
|
235
231
|
/**
|
|
236
232
|
* Determines if the given value is truthy.
|
|
237
233
|
*
|
|
@@ -265,7 +261,7 @@ export declare function falsy(thing: unknown): boolean;
|
|
|
265
261
|
* isSome({}); // true
|
|
266
262
|
* ```
|
|
267
263
|
*/
|
|
268
|
-
export declare function isSome(thing:
|
|
264
|
+
export declare function isSome<T>(thing: T | null | undefined): thing is T;
|
|
269
265
|
/**
|
|
270
266
|
* Returns true if the given value is null or undefined.
|
|
271
267
|
*
|
|
@@ -277,7 +273,23 @@ export declare function isSome(thing: unknown): boolean;
|
|
|
277
273
|
* isNone(0); // false
|
|
278
274
|
* ```
|
|
279
275
|
*/
|
|
280
|
-
export declare function isNone(thing: unknown):
|
|
276
|
+
export declare function isNone(thing: unknown): thing is null | undefined;
|
|
277
|
+
/**
|
|
278
|
+
* Determines if the given value is an array. Acts as a type guard, narrowing
|
|
279
|
+
* the value to `unknown[]` in branches where the result is `true`.
|
|
280
|
+
*
|
|
281
|
+
* @remarks alias for `Array.isArray`.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* isArray([]); // true
|
|
286
|
+
* isArray([1, 2, 3]); // true
|
|
287
|
+
* isArray("hello"); // false
|
|
288
|
+
* isArray(null); // false
|
|
289
|
+
* isArray({}); // false
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
export declare function isArray(thing: unknown): thing is unknown[];
|
|
281
293
|
/**
|
|
282
294
|
* Returns a function that does nothing.
|
|
283
295
|
*
|
|
@@ -301,18 +313,6 @@ export declare function noop(): void;
|
|
|
301
313
|
* ```
|
|
302
314
|
*/
|
|
303
315
|
export declare function todo(message?: string): void;
|
|
304
|
-
/**
|
|
305
|
-
* Returns true if the given value is within the given range.
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* ```ts
|
|
309
|
-
* inRange(5, 0, 10); // true
|
|
310
|
-
* inRange(0, 0, 10); // true
|
|
311
|
-
* inRange(10, 0, 10); // true
|
|
312
|
-
* inRange(11, 0, 10); // false
|
|
313
|
-
* ```
|
|
314
|
-
*/
|
|
315
|
-
export declare function inRange(value: number, min: number, max: number): boolean;
|
|
316
316
|
/**
|
|
317
317
|
* Splits an array into chunks of a fixed size.
|
|
318
318
|
*
|
|
@@ -409,4 +409,129 @@ export declare function includes(searchIn: string | string[] | null | undefined,
|
|
|
409
409
|
* ```
|
|
410
410
|
*/
|
|
411
411
|
export declare function includesAny(searchIn: string | string[] | null | undefined, searchFor: string | string[] | null | undefined): boolean;
|
|
412
|
+
/**
|
|
413
|
+
* Returns a new array with elements removed and/or inserted, without mutating the original.
|
|
414
|
+
*
|
|
415
|
+
* @param list - The source array
|
|
416
|
+
* @param start - Index at which to start changing the array
|
|
417
|
+
* @param deleteCount - Number of elements to remove
|
|
418
|
+
* @param items - Elements to insert at the start index
|
|
419
|
+
* @returns A new array with the modifications applied
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* splice([1, 2, 3, 4], 1, 2) // [1, 4]
|
|
424
|
+
* splice([1, 2, 3], 1, 0, [9, 10]) // [1, 9, 10, 2, 3]
|
|
425
|
+
* splice([1, 2, 3, 4], 2) // [1, 2] — omitting deleteCount removes from start to end
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
export declare function splice<T>(list: T[], start: number, deleteCount?: number, items?: T[]): T[];
|
|
429
|
+
/**
|
|
430
|
+
* Groups items into a `Map` keyed by the value returned from `keyFn`.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* groupBy([1, 2, 3, 4], (n) => n % 2 === 0 ? "even" : "odd");
|
|
435
|
+
* // Map { "odd" => [1, 3], "even" => [2, 4] }
|
|
436
|
+
*
|
|
437
|
+
* const people = [{ role: "admin", name: "A" }, { role: "user", name: "B" }];
|
|
438
|
+
* groupBy(people, (p) => p.role);
|
|
439
|
+
* // Map { "admin" => [{...}], "user" => [{...}] }
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
export declare function groupBy<T, K>(list: T[], keyFn: (item: T) => K): Map<K, T[]>;
|
|
443
|
+
/**
|
|
444
|
+
* Splits a list into two: the first contains items where the predicate is true,
|
|
445
|
+
* the second contains items where it is false.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* partition([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
export declare function partition<T>(list: T[], predicate: (item: T) => boolean): [T[], T[]];
|
|
453
|
+
/**
|
|
454
|
+
* Returns a new array sorted by the value returned from `keyFn`.
|
|
455
|
+
*
|
|
456
|
+
* @remarks
|
|
457
|
+
* Does not mutate the input list.
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* ```ts
|
|
461
|
+
* sortBy([{ age: 30 }, { age: 20 }], (p) => p.age); // [{ age: 20 }, { age: 30 }]
|
|
462
|
+
* sortBy(["banana", "apple", "cherry"], (s) => s); // ["apple", "banana", "cherry"]
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
export declare function sortBy<T>(list: T[], keyFn: (item: T) => number | string): T[];
|
|
466
|
+
/**
|
|
467
|
+
* Removes `null` and `undefined` values from a list.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```ts
|
|
471
|
+
* compact([1, null, 2, undefined, 3]); // [1, 2, 3]
|
|
472
|
+
* compact(["a", "", null, "b"]); // ["a", "", "b"]
|
|
473
|
+
* ```
|
|
474
|
+
*/
|
|
475
|
+
export declare function compact<T>(list: (T | null | undefined)[]): T[];
|
|
476
|
+
/**
|
|
477
|
+
* Generates a list of numbers in the given range. The start is inclusive,
|
|
478
|
+
* the end is exclusive.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```ts
|
|
482
|
+
* range(0, 5); // [0, 1, 2, 3, 4]
|
|
483
|
+
* range(2, 8, 2); // [2, 4, 6]
|
|
484
|
+
* range(5, 0, -1); // [5, 4, 3, 2, 1]
|
|
485
|
+
* ```
|
|
486
|
+
*/
|
|
487
|
+
export declare function range(start: number, end: number, step?: number): number[];
|
|
488
|
+
/**
|
|
489
|
+
* Pairs up elements from two lists. Stops at the shorter list.
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* zip([1, 2, 3], ["a", "b", "c"]); // [[1, "a"], [2, "b"], [3, "c"]]
|
|
494
|
+
* zip([1, 2, 3], ["a"]); // [[1, "a"]]
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
export declare function zip<A, B>(a: A[], b: B[]): [A, B][];
|
|
498
|
+
/**
|
|
499
|
+
* Returns the first `n` elements of a list. Does not mutate the input.
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```ts
|
|
503
|
+
* take([1, 2, 3, 4], 2); // [1, 2]
|
|
504
|
+
* take([1, 2], 5); // [1, 2]
|
|
505
|
+
* take([1, 2, 3], 0); // []
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
export declare function take<T>(list: T[], n: number): T[];
|
|
509
|
+
/**
|
|
510
|
+
* Returns a list with the first `n` elements removed. Does not mutate the input.
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```ts
|
|
514
|
+
* drop([1, 2, 3, 4], 2); // [3, 4]
|
|
515
|
+
* drop([1, 2], 5); // []
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
export declare function drop<T>(list: T[], n: number): T[];
|
|
519
|
+
/**
|
|
520
|
+
* Returns a new list with the elements at indices `i` and `j` swapped.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* swap([1, 2, 3, 4], 0, 3); // [4, 2, 3, 1]
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
527
|
+
export declare function swap<T>(list: T[], i: number, j: number): T[];
|
|
528
|
+
/**
|
|
529
|
+
* Returns a new list with an element moved from one index to another.
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```ts
|
|
533
|
+
* move([1, 2, 3, 4], 0, 2); // [2, 3, 1, 4]
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
export declare function move<T>(list: T[], from: number, to: number): T[];
|
|
412
537
|
//# sourceMappingURL=core.d.ts.map
|
package/esm/src/core.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/src/core.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAElC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAEjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,KA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,GAClC,CAAC,EAAE,CAQL;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAE9D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,MAAU,GAAG,MAAM,EAAE,CAS5E;AAED;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAKlD;AAED;;;;;;;;;GASG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAEjD;AAED;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAEjD;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAI5D;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC,EAAE,CAKhE"}
|
package/esm/src/core.js
CHANGED
|
@@ -126,7 +126,7 @@ export function reverse(thing) {
|
|
|
126
126
|
return thing.split("").reverse().join("");
|
|
127
127
|
if (thing instanceof Set)
|
|
128
128
|
return new Set([...thing].reverse());
|
|
129
|
-
return thing.reverse();
|
|
129
|
+
return [...thing].reverse();
|
|
130
130
|
}
|
|
131
131
|
/**
|
|
132
132
|
* Determines if the given thing is empty.
|
|
@@ -160,6 +160,12 @@ export function isEmpty(thing) {
|
|
|
160
160
|
return Object.keys(thing).length === 0;
|
|
161
161
|
return false;
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Alias for `isEmpty`.
|
|
165
|
+
*/
|
|
166
|
+
export function isNoneOrEmpty(thing) {
|
|
167
|
+
return isEmpty(thing);
|
|
168
|
+
}
|
|
163
169
|
/**
|
|
164
170
|
* Determines if the given thing is not empty.
|
|
165
171
|
*
|
|
@@ -179,6 +185,12 @@ export function isEmpty(thing) {
|
|
|
179
185
|
export function isNotEmpty(thing) {
|
|
180
186
|
return !isEmpty(thing);
|
|
181
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Alias for `isNotEmpty`.
|
|
190
|
+
*/
|
|
191
|
+
export function isNotNoneOrEmpty(thing) {
|
|
192
|
+
return isNotEmpty(thing);
|
|
193
|
+
}
|
|
182
194
|
/**
|
|
183
195
|
* Returns the distinct values from a list.
|
|
184
196
|
*
|
|
@@ -223,21 +235,7 @@ export function distinct(list) {
|
|
|
223
235
|
* ```
|
|
224
236
|
*/
|
|
225
237
|
export function sample(list) {
|
|
226
|
-
return list[random(
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Gives a random number in the given range. The first parameter is inclusive
|
|
230
|
-
* and the second one is exclusive. Therefore, it will work with lists out of
|
|
231
|
-
* the box.
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* ```ts
|
|
235
|
-
* rand(0, 10); // 0 -> 9
|
|
236
|
-
* rand(3, 7); // 3 -> 6
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
export function random(start, end) {
|
|
240
|
-
return Math.floor(Math.random() * (end - start)) + start;
|
|
238
|
+
return list[Math.floor(Math.random() * list.length)];
|
|
241
239
|
}
|
|
242
240
|
/**
|
|
243
241
|
* Determines if the given value is truthy.
|
|
@@ -293,6 +291,24 @@ export function isSome(thing) {
|
|
|
293
291
|
export function isNone(thing) {
|
|
294
292
|
return thing === null || thing === undefined;
|
|
295
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Determines if the given value is an array. Acts as a type guard, narrowing
|
|
296
|
+
* the value to `unknown[]` in branches where the result is `true`.
|
|
297
|
+
*
|
|
298
|
+
* @remarks alias for `Array.isArray`.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* isArray([]); // true
|
|
303
|
+
* isArray([1, 2, 3]); // true
|
|
304
|
+
* isArray("hello"); // false
|
|
305
|
+
* isArray(null); // false
|
|
306
|
+
* isArray({}); // false
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
export function isArray(thing) {
|
|
310
|
+
return Array.isArray(thing);
|
|
311
|
+
}
|
|
296
312
|
/**
|
|
297
313
|
* Returns a function that does nothing.
|
|
298
314
|
*
|
|
@@ -321,20 +337,6 @@ export function noop() {
|
|
|
321
337
|
export function todo(message) {
|
|
322
338
|
noop();
|
|
323
339
|
}
|
|
324
|
-
/**
|
|
325
|
-
* Returns true if the given value is within the given range.
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* ```ts
|
|
329
|
-
* inRange(5, 0, 10); // true
|
|
330
|
-
* inRange(0, 0, 10); // true
|
|
331
|
-
* inRange(10, 0, 10); // true
|
|
332
|
-
* inRange(11, 0, 10); // false
|
|
333
|
-
* ```
|
|
334
|
-
*/
|
|
335
|
-
export function inRange(value, min, max) {
|
|
336
|
-
return value >= min && value <= max;
|
|
337
|
-
}
|
|
338
340
|
/**
|
|
339
341
|
* Splits an array into chunks of a fixed size.
|
|
340
342
|
*
|
|
@@ -466,3 +468,197 @@ export function includesAny(searchIn, searchFor) {
|
|
|
466
468
|
const terms = Array.isArray(searchFor) ? searchFor : [searchFor];
|
|
467
469
|
return terms.some((term) => searchIn.includes(term));
|
|
468
470
|
}
|
|
471
|
+
/**
|
|
472
|
+
* Returns a new array with elements removed and/or inserted, without mutating the original.
|
|
473
|
+
*
|
|
474
|
+
* @param list - The source array
|
|
475
|
+
* @param start - Index at which to start changing the array
|
|
476
|
+
* @param deleteCount - Number of elements to remove
|
|
477
|
+
* @param items - Elements to insert at the start index
|
|
478
|
+
* @returns A new array with the modifications applied
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```ts
|
|
482
|
+
* splice([1, 2, 3, 4], 1, 2) // [1, 4]
|
|
483
|
+
* splice([1, 2, 3], 1, 0, [9, 10]) // [1, 9, 10, 2, 3]
|
|
484
|
+
* splice([1, 2, 3, 4], 2) // [1, 2] — omitting deleteCount removes from start to end
|
|
485
|
+
* ```
|
|
486
|
+
*/
|
|
487
|
+
export function splice(list, start, deleteCount, items = []) {
|
|
488
|
+
const copy = [...list];
|
|
489
|
+
copy.splice(start, deleteCount ?? list.length, ...items);
|
|
490
|
+
return copy;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Groups items into a `Map` keyed by the value returned from `keyFn`.
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```ts
|
|
497
|
+
* groupBy([1, 2, 3, 4], (n) => n % 2 === 0 ? "even" : "odd");
|
|
498
|
+
* // Map { "odd" => [1, 3], "even" => [2, 4] }
|
|
499
|
+
*
|
|
500
|
+
* const people = [{ role: "admin", name: "A" }, { role: "user", name: "B" }];
|
|
501
|
+
* groupBy(people, (p) => p.role);
|
|
502
|
+
* // Map { "admin" => [{...}], "user" => [{...}] }
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
export function groupBy(list, keyFn) {
|
|
506
|
+
const result = new Map();
|
|
507
|
+
for (const item of list) {
|
|
508
|
+
const key = keyFn(item);
|
|
509
|
+
const existing = result.get(key);
|
|
510
|
+
if (existing)
|
|
511
|
+
existing.push(item);
|
|
512
|
+
else
|
|
513
|
+
result.set(key, [item]);
|
|
514
|
+
}
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Splits a list into two: the first contains items where the predicate is true,
|
|
519
|
+
* the second contains items where it is false.
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* partition([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
|
|
524
|
+
* ```
|
|
525
|
+
*/
|
|
526
|
+
export function partition(list, predicate) {
|
|
527
|
+
const pass = [];
|
|
528
|
+
const fail = [];
|
|
529
|
+
for (const item of list) {
|
|
530
|
+
if (predicate(item))
|
|
531
|
+
pass.push(item);
|
|
532
|
+
else
|
|
533
|
+
fail.push(item);
|
|
534
|
+
}
|
|
535
|
+
return [pass, fail];
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Returns a new array sorted by the value returned from `keyFn`.
|
|
539
|
+
*
|
|
540
|
+
* @remarks
|
|
541
|
+
* Does not mutate the input list.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```ts
|
|
545
|
+
* sortBy([{ age: 30 }, { age: 20 }], (p) => p.age); // [{ age: 20 }, { age: 30 }]
|
|
546
|
+
* sortBy(["banana", "apple", "cherry"], (s) => s); // ["apple", "banana", "cherry"]
|
|
547
|
+
* ```
|
|
548
|
+
*/
|
|
549
|
+
export function sortBy(list, keyFn) {
|
|
550
|
+
return [...list].sort((a, b) => {
|
|
551
|
+
const aKey = keyFn(a);
|
|
552
|
+
const bKey = keyFn(b);
|
|
553
|
+
if (aKey < bKey)
|
|
554
|
+
return -1;
|
|
555
|
+
if (aKey > bKey)
|
|
556
|
+
return 1;
|
|
557
|
+
return 0;
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Removes `null` and `undefined` values from a list.
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```ts
|
|
565
|
+
* compact([1, null, 2, undefined, 3]); // [1, 2, 3]
|
|
566
|
+
* compact(["a", "", null, "b"]); // ["a", "", "b"]
|
|
567
|
+
* ```
|
|
568
|
+
*/
|
|
569
|
+
export function compact(list) {
|
|
570
|
+
return list.filter(isSome);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Generates a list of numbers in the given range. The start is inclusive,
|
|
574
|
+
* the end is exclusive.
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* ```ts
|
|
578
|
+
* range(0, 5); // [0, 1, 2, 3, 4]
|
|
579
|
+
* range(2, 8, 2); // [2, 4, 6]
|
|
580
|
+
* range(5, 0, -1); // [5, 4, 3, 2, 1]
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
583
|
+
export function range(start, end, step = 1) {
|
|
584
|
+
if (step === 0)
|
|
585
|
+
throw new Error("range step must not be 0");
|
|
586
|
+
const result = [];
|
|
587
|
+
if (step > 0) {
|
|
588
|
+
for (let i = start; i < end; i += step)
|
|
589
|
+
result.push(i);
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
for (let i = start; i > end; i += step)
|
|
593
|
+
result.push(i);
|
|
594
|
+
}
|
|
595
|
+
return result;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Pairs up elements from two lists. Stops at the shorter list.
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* ```ts
|
|
602
|
+
* zip([1, 2, 3], ["a", "b", "c"]); // [[1, "a"], [2, "b"], [3, "c"]]
|
|
603
|
+
* zip([1, 2, 3], ["a"]); // [[1, "a"]]
|
|
604
|
+
* ```
|
|
605
|
+
*/
|
|
606
|
+
export function zip(a, b) {
|
|
607
|
+
const length = Math.min(a.length, b.length);
|
|
608
|
+
const result = [];
|
|
609
|
+
for (let i = 0; i < length; i++)
|
|
610
|
+
result.push([a[i], b[i]]);
|
|
611
|
+
return result;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Returns the first `n` elements of a list. Does not mutate the input.
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* ```ts
|
|
618
|
+
* take([1, 2, 3, 4], 2); // [1, 2]
|
|
619
|
+
* take([1, 2], 5); // [1, 2]
|
|
620
|
+
* take([1, 2, 3], 0); // []
|
|
621
|
+
* ```
|
|
622
|
+
*/
|
|
623
|
+
export function take(list, n) {
|
|
624
|
+
return list.slice(0, Math.max(0, n));
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Returns a list with the first `n` elements removed. Does not mutate the input.
|
|
628
|
+
*
|
|
629
|
+
* @example
|
|
630
|
+
* ```ts
|
|
631
|
+
* drop([1, 2, 3, 4], 2); // [3, 4]
|
|
632
|
+
* drop([1, 2], 5); // []
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
export function drop(list, n) {
|
|
636
|
+
return list.slice(Math.max(0, n));
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Returns a new list with the elements at indices `i` and `j` swapped.
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
642
|
+
* ```ts
|
|
643
|
+
* swap([1, 2, 3, 4], 0, 3); // [4, 2, 3, 1]
|
|
644
|
+
* ```
|
|
645
|
+
*/
|
|
646
|
+
export function swap(list, i, j) {
|
|
647
|
+
const copy = [...list];
|
|
648
|
+
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
649
|
+
return copy;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Returns a new list with an element moved from one index to another.
|
|
653
|
+
*
|
|
654
|
+
* @example
|
|
655
|
+
* ```ts
|
|
656
|
+
* move([1, 2, 3, 4], 0, 2); // [2, 3, 1, 4]
|
|
657
|
+
* ```
|
|
658
|
+
*/
|
|
659
|
+
export function move(list, from, to) {
|
|
660
|
+
const copy = [...list];
|
|
661
|
+
const [item] = copy.splice(from, 1);
|
|
662
|
+
copy.splice(to, 0, item);
|
|
663
|
+
return copy;
|
|
664
|
+
}
|
package/esm/src/dates.d.ts
CHANGED
|
@@ -118,4 +118,18 @@ export declare function isInFuture(date: Date): boolean;
|
|
|
118
118
|
* Determines if the date is today.
|
|
119
119
|
*/
|
|
120
120
|
export declare function isToday(date: Date): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Determines if the date is yesterday.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* this is date specific. Time of day is ignored.
|
|
126
|
+
*/
|
|
127
|
+
export declare function isYesterday(date: Date): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Determines if the date is tomorrow.
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* this is date specific. Time of day is ignored.
|
|
133
|
+
*/
|
|
134
|
+
export declare function isTomorrow(date: Date): boolean;
|
|
121
135
|
//# sourceMappingURL=dates.d.ts.map
|
package/esm/src/dates.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../../src/src/dates.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,GAAG,IAAI,IAAI,CAE1B;AAED;;GAEG;AACH,wBAAgB,KAAK,IAAI,IAAI,CAI5B;AAED;;GAEG;AACH,wBAAgB,QAAQ,IAAI,IAAI,CAI/B;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,IAAI,CAEhC;AAED;;GAEG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAItD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAE3D;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAW1D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE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