@zthun/helpful-fn 2.4.0 → 3.2.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 +14 -0
- package/dist/anchor/anchor.d.mts +63 -0
- package/dist/assert/assert.d.mts +53 -0
- package/dist/count-buckets/count-buckets.d.mts +57 -0
- package/dist/{types/create-error/create-error.d.ts → create-error/create-error.d.mts} +15 -1
- package/dist/create-guid/create-guid.d.mts +23 -0
- package/dist/{types/first-defined/first-defined.d.ts → first-defined/first-defined.d.mts} +15 -0
- package/dist/index.cjs +215 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +215 -0
- package/dist/index.js.map +1 -0
- package/dist/{types/join-defined/join-defined.d.ts → join-defined/join-defined.d.mts} +48 -2
- package/dist/obligation/obligation.d.mts +62 -0
- package/dist/{types/orientation/orientation.d.ts → orientation/orientation.d.mts} +1 -1
- package/dist/peel/peel.d.mts +50 -0
- package/dist/set-first/set-first.d.mts +32 -0
- package/dist/{types/sleep/sleep.d.ts → sleep/sleep.d.mts} +3 -1
- package/dist/try-fallback/try-fallback.d.mts +46 -0
- package/package.json +14 -8
- package/dist/lib/index.esm.js +0 -2
- package/dist/lib/index.esm.js.map +0 -1
- package/dist/lib/index.js +0 -2
- package/dist/lib/index.js.map +0 -1
- package/dist/types/anchor/anchor.d.ts +0 -42
- package/dist/types/count-buckets/count-buckets.d.ts +0 -31
- package/dist/types/create-guid/create-guid.d.ts +0 -7
- package/dist/types/index.d.ts +0 -9
- package/dist/types/set-first/set-first.d.ts +0 -14
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Helpful Functions
|
|
2
|
+
|
|
3
|
+
Provides a basic set of javascript functions and classes that are not a part of the javascript engine or lodash.
|
|
4
|
+
|
|
5
|
+
Helpful Functions can be used in node as well as browsers.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
# NPM
|
|
11
|
+
npm install @zthun/helpful-fn
|
|
12
|
+
# Yarn
|
|
13
|
+
yarn add @zthun/helpful-fn
|
|
14
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a targeted point along a y axis.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum ZVerticalAnchor {
|
|
5
|
+
/**
|
|
6
|
+
* Top boundary.
|
|
7
|
+
*
|
|
8
|
+
* In vertical device space, this would equate to y = 0.
|
|
9
|
+
*/
|
|
10
|
+
Top = "top",
|
|
11
|
+
/**
|
|
12
|
+
* Centerpoint between the top and bottom.
|
|
13
|
+
*/
|
|
14
|
+
Middle = "middle",
|
|
15
|
+
/**
|
|
16
|
+
* Bottom boundary.
|
|
17
|
+
*
|
|
18
|
+
* In vertical device space, this would equate to y = Infinity.
|
|
19
|
+
*/
|
|
20
|
+
Bottom = "bottom"
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Represents a targeted point along an x axis.
|
|
24
|
+
*/
|
|
25
|
+
export declare enum ZHorizontalAnchor {
|
|
26
|
+
/**
|
|
27
|
+
* Left boundary.
|
|
28
|
+
*
|
|
29
|
+
* In horizontal device space, this would equate to x = 0.
|
|
30
|
+
*/
|
|
31
|
+
Left = "left",
|
|
32
|
+
/**
|
|
33
|
+
* Centerpoint between the left and right boundary.
|
|
34
|
+
*/
|
|
35
|
+
Center = "center",
|
|
36
|
+
/**
|
|
37
|
+
* Right boundary.
|
|
38
|
+
*
|
|
39
|
+
* In horizontal device space, this would equate to x = Infinity.
|
|
40
|
+
*/
|
|
41
|
+
Right = "right"
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Represents an anchor point in 2d space.
|
|
45
|
+
*
|
|
46
|
+
* An anchor array is defined by [vertical, horizontal]
|
|
47
|
+
* and is read as such.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* const topCenter = [ZVerticalAnchor.Top, ZHorizontalAnchor.Center];
|
|
53
|
+
* const bottomRight = [ZVerticalAnchor.Bottom, ZHorizontalAnchor.Right];
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @see {@link ZVerticalAnchor} For more information.
|
|
57
|
+
* @see {@link ZHorizontalAnchor} For more information.
|
|
58
|
+
*/
|
|
59
|
+
export type ZAnchor = [ZVerticalAnchor, ZHorizontalAnchor];
|
|
60
|
+
/**
|
|
61
|
+
* Represents a special type of anchor that excludes the center points.
|
|
62
|
+
*/
|
|
63
|
+
export type ZSideAnchor = ZVerticalAnchor.Top | ZVerticalAnchor.Bottom | ZHorizontalAnchor.Left | ZHorizontalAnchor.Right;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an object that can be used to build a list of assertions.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { ZAssert, createError } from '@zthun/helpful-fn';
|
|
8
|
+
*
|
|
9
|
+
* const user = readUser();
|
|
10
|
+
*
|
|
11
|
+
* ZAssert
|
|
12
|
+
* .claim(user.name != null, 'User name is required')
|
|
13
|
+
* .claim(user.email != null, 'User email is required')
|
|
14
|
+
* .assert((m) => createError(m));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class ZAssert {
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a new instance of a ZAssert with one claim.
|
|
20
|
+
*
|
|
21
|
+
* @param claim -
|
|
22
|
+
* The claim to make.
|
|
23
|
+
* @param msg -
|
|
24
|
+
* The message to throw if the claim is false.
|
|
25
|
+
*
|
|
26
|
+
* @returns
|
|
27
|
+
* A new ZAssert object with an initial claim.
|
|
28
|
+
*/
|
|
29
|
+
static claim(claim: boolean, msg: any): ZAssert;
|
|
30
|
+
private _messages;
|
|
31
|
+
/**
|
|
32
|
+
* Initializes a new instance of this object.
|
|
33
|
+
*/
|
|
34
|
+
private constructor();
|
|
35
|
+
/**
|
|
36
|
+
* Adds a claim.
|
|
37
|
+
*
|
|
38
|
+
* @param claim -
|
|
39
|
+
* The claim predicate.
|
|
40
|
+
* @param msg -
|
|
41
|
+
* The message to add if the claim fails.
|
|
42
|
+
*
|
|
43
|
+
* @returns This object.
|
|
44
|
+
*/
|
|
45
|
+
claim(claim: boolean, msg: any): this;
|
|
46
|
+
/**
|
|
47
|
+
* Runs the assertion.
|
|
48
|
+
*
|
|
49
|
+
* @param fail -
|
|
50
|
+
* The factory that is responsible for returning the specified error to throw.
|
|
51
|
+
*/
|
|
52
|
+
assert<E extends Error>(fail: (message: any | any[]) => E): void;
|
|
53
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the total number of buckets you need to
|
|
3
|
+
* store a number of items where each bucket can hold a
|
|
4
|
+
* maximum weight of items.
|
|
5
|
+
*
|
|
6
|
+
* You can use this function to calculate groupings of
|
|
7
|
+
* items based on total counts and sizes. A good example
|
|
8
|
+
* usage would be to calculate the total number of pages
|
|
9
|
+
* on a paginated list of items given a page size and item
|
|
10
|
+
* count.
|
|
11
|
+
*
|
|
12
|
+
* @param weight -
|
|
13
|
+
* The maximum weight a bucket can store. If this value receives
|
|
14
|
+
* Infinity, then it will result in 1 or min buckets being returned,
|
|
15
|
+
* whichever is larger. If this receives NaN, then this method will
|
|
16
|
+
* result in NaN. Finally, if this receives a negative value, then
|
|
17
|
+
* the result will be max.
|
|
18
|
+
* @param items -
|
|
19
|
+
* The total number of items you need to store where each item
|
|
20
|
+
* counts as 1 towards the weight. If the total number of items
|
|
21
|
+
* is Infinity, then this method will result in max buckets.
|
|
22
|
+
* If this receives NaN, then this method will result in NaN. Finally,
|
|
23
|
+
* if you pass 0, or a negative number of items, then result will be min.
|
|
24
|
+
* @param min -
|
|
25
|
+
* The bounded minimum value. If the total number of buckets
|
|
26
|
+
* evaluates to less than this value, then this value is returned.
|
|
27
|
+
* The default is 0.
|
|
28
|
+
* @param max -
|
|
29
|
+
* The bounded maximum value. If the total number of buckets
|
|
30
|
+
* evaluates to more than this value, then this value is returned.
|
|
31
|
+
* The default is Infinity.
|
|
32
|
+
*
|
|
33
|
+
* @returns
|
|
34
|
+
* The number of buckets you need to store the total number
|
|
35
|
+
* of items given that a single bucket can hold a max weight of items.
|
|
36
|
+
* If either weight or items is NaN, then NaN will be returned regardless
|
|
37
|
+
* of the opposite value. Passing a negative number is the same as
|
|
38
|
+
* passing 0.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* // The following will return 5 for numberOfPages since you need 5 buckets
|
|
44
|
+
* // to store 101 number of items where each bucket can hold a max of 25
|
|
45
|
+
* // items. The 5th page would be a page of 1 item, since 1 is the remainder.
|
|
46
|
+
* const numberOfPages = countBuckets(25, 101);
|
|
47
|
+
*
|
|
48
|
+
* // In this case, the numberOfPages would be 1 here since our minimum buckets
|
|
49
|
+
* // is 1. By default, this would be 0.
|
|
50
|
+
* const numberOfPages = countBuckets(10, 0, 1);
|
|
51
|
+
*
|
|
52
|
+
* // In this case, we have more items that can be held in the number of buckets
|
|
53
|
+
* // available, so only 4 is returned instead of the 5.
|
|
54
|
+
* const numberOfPages = countBuckets(25, 101, undefined, 4);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function countBuckets(weight: number, items: number, min?: number, max?: number): number;
|
|
@@ -9,9 +9,23 @@
|
|
|
9
9
|
* The list of acceptable object keys to look into problem. These will be recursively
|
|
10
10
|
* evaluated to strip out the real error message. Note that this is in order of
|
|
11
11
|
* priority. If schema[0] and schema[1] are both keys on problem, then schema[0] takes
|
|
12
|
-
* a higher precedence than schema[1].
|
|
12
|
+
* a higher precedence than schema[1]. By default, the schema will look for properties
|
|
13
|
+
* named, message, error, exception, and data, in that order.
|
|
13
14
|
*
|
|
14
15
|
* @returns
|
|
15
16
|
* An error object that is the best evaluation of what the problem actually is.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // All of these result an Error with the message, 'Something went wrong'
|
|
22
|
+
* const errorWithStringProblem = createError('Something went wrong');
|
|
23
|
+
* const errorWithObjectProblemInSchema = createError({ error: 'Something went wrong'});
|
|
24
|
+
* const errorWithCustomSchema = createError({ issue: 'Something went wrong'}, ['issue']);
|
|
25
|
+
* const errorRecursive = createError({ error: { message: 'Something went wrong' }});
|
|
26
|
+
*
|
|
27
|
+
* // This would result in '[Object object]' as there is no way to figure out how to deconstruct this problem.
|
|
28
|
+
* const errorCannotBeFound = createError({ wut: 'Something went wrong' });
|
|
29
|
+
* ```
|
|
16
30
|
*/
|
|
17
31
|
export declare function createError(problem: any, schema?: string[]): Error;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a globally unique identifier.
|
|
3
|
+
*
|
|
4
|
+
* The identifier holds to v4 of the UUID specification. A summary
|
|
5
|
+
* of the specification can be found at
|
|
6
|
+
* {@link https://commons.apache.org/sandbox/commons-id/uuid.html | Apache Commons UUID Documentation}.
|
|
7
|
+
*
|
|
8
|
+
* The official documentation of the UUID specification is under
|
|
9
|
+
* {@link https://www.rfc-editor.org/info/rfc4122 | RFC 4122}
|
|
10
|
+
*
|
|
11
|
+
* @returns
|
|
12
|
+
* A new generated globally unique identifier based on random bytes.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* // Will get a value similar to 53e33fb6-d05a-4fa9-8dc0-b78e4feaa702
|
|
18
|
+
* const guidA = createGuid();
|
|
19
|
+
* // Will most likely not ever be equal to guidA
|
|
20
|
+
* const guidB = createGuid();
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const createGuid: () => string;
|
|
@@ -7,10 +7,25 @@
|
|
|
7
7
|
* The first value to check.
|
|
8
8
|
* @param remaining -
|
|
9
9
|
* The remaining values beyond the first to check.
|
|
10
|
+
* @param T -
|
|
11
|
+
* The type of data that will be enumerated.
|
|
10
12
|
*
|
|
11
13
|
* @returns
|
|
12
14
|
* The first value if it is not null or undefined. If first is undefined or null, then the first item
|
|
13
15
|
* in remaining such that remaining[i] is not null or undefined is returned. If first and all values of
|
|
14
16
|
* remaining are null or undefined, then fallback is returned.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // 'Defined'
|
|
22
|
+
* const shouldBeDefined = firstDefined('Fallback', null, undefined, 'Defined');
|
|
23
|
+
* // 'Fallback'
|
|
24
|
+
* const shouldBeFallback = firstDefined('Fallback', null, undefined);
|
|
25
|
+
* const shouldAlsoBeFallback = firstDefined('Fallback', undefined);
|
|
26
|
+
* // 'First'
|
|
27
|
+
* const shouldBeFirst = firstDefined('Fallback', 'First', null, 'Third');
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
15
30
|
*/
|
|
16
31
|
export declare function firstDefined<T = any>(fallback: T, first: T | null | undefined, ...remaining: (T | null | undefined)[]): T;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const uuid = require("uuid");
|
|
4
|
+
var ZVerticalAnchor = /* @__PURE__ */ ((ZVerticalAnchor2) => {
|
|
5
|
+
ZVerticalAnchor2["Top"] = "top";
|
|
6
|
+
ZVerticalAnchor2["Middle"] = "middle";
|
|
7
|
+
ZVerticalAnchor2["Bottom"] = "bottom";
|
|
8
|
+
return ZVerticalAnchor2;
|
|
9
|
+
})(ZVerticalAnchor || {});
|
|
10
|
+
var ZHorizontalAnchor = /* @__PURE__ */ ((ZHorizontalAnchor2) => {
|
|
11
|
+
ZHorizontalAnchor2["Left"] = "left";
|
|
12
|
+
ZHorizontalAnchor2["Center"] = "center";
|
|
13
|
+
ZHorizontalAnchor2["Right"] = "right";
|
|
14
|
+
return ZHorizontalAnchor2;
|
|
15
|
+
})(ZHorizontalAnchor || {});
|
|
16
|
+
class ZAssert {
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of this object.
|
|
19
|
+
*/
|
|
20
|
+
constructor() {
|
|
21
|
+
this._messages = [];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Initializes a new instance of a ZAssert with one claim.
|
|
25
|
+
*
|
|
26
|
+
* @param claim -
|
|
27
|
+
* The claim to make.
|
|
28
|
+
* @param msg -
|
|
29
|
+
* The message to throw if the claim is false.
|
|
30
|
+
*
|
|
31
|
+
* @returns
|
|
32
|
+
* A new ZAssert object with an initial claim.
|
|
33
|
+
*/
|
|
34
|
+
static claim(claim, msg) {
|
|
35
|
+
return new ZAssert().claim(claim, msg);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Adds a claim.
|
|
39
|
+
*
|
|
40
|
+
* @param claim -
|
|
41
|
+
* The claim predicate.
|
|
42
|
+
* @param msg -
|
|
43
|
+
* The message to add if the claim fails.
|
|
44
|
+
*
|
|
45
|
+
* @returns This object.
|
|
46
|
+
*/
|
|
47
|
+
claim(claim, msg) {
|
|
48
|
+
if (!claim) {
|
|
49
|
+
this._messages.push(msg);
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Runs the assertion.
|
|
55
|
+
*
|
|
56
|
+
* @param fail -
|
|
57
|
+
* The factory that is responsible for returning the specified error to throw.
|
|
58
|
+
*/
|
|
59
|
+
assert(fail) {
|
|
60
|
+
if (this._messages.length === 1) {
|
|
61
|
+
throw fail(this._messages[0]);
|
|
62
|
+
}
|
|
63
|
+
if (this._messages.length) {
|
|
64
|
+
throw fail(this._messages);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function countBuckets(weight, items, min = 0, max = Infinity) {
|
|
69
|
+
weight = Math.max(0, weight);
|
|
70
|
+
items = Math.max(0, items);
|
|
71
|
+
if (Number.isNaN(weight) || Number.isNaN(items)) {
|
|
72
|
+
return NaN;
|
|
73
|
+
}
|
|
74
|
+
if (items === 0) {
|
|
75
|
+
return min;
|
|
76
|
+
}
|
|
77
|
+
const boxes = weight === Infinity ? 1 : Math.ceil(items / weight);
|
|
78
|
+
return Math.min(max, Math.max(min, boxes));
|
|
79
|
+
}
|
|
80
|
+
function createError(problem, schema = ["message", "error", "exception", "data"]) {
|
|
81
|
+
if (problem instanceof Error) {
|
|
82
|
+
return problem;
|
|
83
|
+
}
|
|
84
|
+
if (problem == null) {
|
|
85
|
+
return new Error();
|
|
86
|
+
}
|
|
87
|
+
for (let i = 0; i < schema.length; ++i) {
|
|
88
|
+
const key = schema[i];
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(problem, key)) {
|
|
90
|
+
return createError(problem[key]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return new Error(String(problem));
|
|
94
|
+
}
|
|
95
|
+
const createGuid = uuid.v4;
|
|
96
|
+
function firstDefined(fallback, first, ...remaining) {
|
|
97
|
+
if (first != null) {
|
|
98
|
+
return first;
|
|
99
|
+
}
|
|
100
|
+
for (let i = 0; i < remaining.length; ++i) {
|
|
101
|
+
const val = remaining[i];
|
|
102
|
+
if (val != null) {
|
|
103
|
+
return val;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return fallback;
|
|
107
|
+
}
|
|
108
|
+
function joinDefined(delimiter, ...items) {
|
|
109
|
+
return items.map((item) => item instanceof Array ? item[1] ? item[0] : null : item).filter((item) => item != null).join(delimiter);
|
|
110
|
+
}
|
|
111
|
+
const spaceJoinDefined = joinDefined.bind(null, " ");
|
|
112
|
+
const cssJoinDefined = spaceJoinDefined;
|
|
113
|
+
const commaJoinDefined = joinDefined.bind(null, ",");
|
|
114
|
+
const semiColonJoinDefined = joinDefined.bind(null, ";");
|
|
115
|
+
const pipeJoinDefined = joinDefined.bind(null, "|");
|
|
116
|
+
async function required(val) {
|
|
117
|
+
if (val == null) {
|
|
118
|
+
throw new Error("A required value was not provided");
|
|
119
|
+
}
|
|
120
|
+
const _val = await val;
|
|
121
|
+
if (_val == null) {
|
|
122
|
+
throw new Error("A required value was not provided");
|
|
123
|
+
}
|
|
124
|
+
return _val;
|
|
125
|
+
}
|
|
126
|
+
async function optional(val, fallback = null) {
|
|
127
|
+
if (val == null) {
|
|
128
|
+
return fallback;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const _val = await val;
|
|
132
|
+
return _val == null ? fallback : _val;
|
|
133
|
+
} catch {
|
|
134
|
+
return fallback;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
var ZOrientation = /* @__PURE__ */ ((ZOrientation2) => {
|
|
138
|
+
ZOrientation2["Horizontal"] = "horizontal";
|
|
139
|
+
ZOrientation2["Vertical"] = "vertical";
|
|
140
|
+
return ZOrientation2;
|
|
141
|
+
})(ZOrientation || {});
|
|
142
|
+
function peel(str, candidates) {
|
|
143
|
+
for (const check of candidates) {
|
|
144
|
+
if (str.startsWith(check)) {
|
|
145
|
+
return [str.substring(0, check.length), str.substring(check.length)];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return [null, str];
|
|
149
|
+
}
|
|
150
|
+
function peelBetween(str, open, close) {
|
|
151
|
+
if (!str.startsWith(open)) {
|
|
152
|
+
return [null, str];
|
|
153
|
+
}
|
|
154
|
+
let stack = 1;
|
|
155
|
+
const _str = str.substring(open.length);
|
|
156
|
+
for (let i = 0; i < _str.length; ++i) {
|
|
157
|
+
if (_str.startsWith(open, i)) {
|
|
158
|
+
stack += 1;
|
|
159
|
+
i += open.length - 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (_str.startsWith(close, i)) {
|
|
163
|
+
stack -= 1;
|
|
164
|
+
if (stack === 0) {
|
|
165
|
+
return [_str.substring(0, i), _str.substring(i + close.length)];
|
|
166
|
+
}
|
|
167
|
+
i += close.length - 1;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return [null, str];
|
|
171
|
+
}
|
|
172
|
+
function setFirst(setValue, fallback, value) {
|
|
173
|
+
const _value = (value == null ? void 0 : value.length) ? value[0] : fallback;
|
|
174
|
+
setValue(_value);
|
|
175
|
+
}
|
|
176
|
+
function sleep(ms = 0, val = void 0) {
|
|
177
|
+
return new Promise((resolve) => setTimeout(() => resolve(val), ms));
|
|
178
|
+
}
|
|
179
|
+
function tryFallback(candidate, fallback) {
|
|
180
|
+
try {
|
|
181
|
+
return candidate();
|
|
182
|
+
} catch {
|
|
183
|
+
return fallback;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function tryFallbackAsync(candidate, fallback) {
|
|
187
|
+
try {
|
|
188
|
+
return await candidate();
|
|
189
|
+
} catch {
|
|
190
|
+
return fallback;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.ZAssert = ZAssert;
|
|
194
|
+
exports.ZHorizontalAnchor = ZHorizontalAnchor;
|
|
195
|
+
exports.ZOrientation = ZOrientation;
|
|
196
|
+
exports.ZVerticalAnchor = ZVerticalAnchor;
|
|
197
|
+
exports.commaJoinDefined = commaJoinDefined;
|
|
198
|
+
exports.countBuckets = countBuckets;
|
|
199
|
+
exports.createError = createError;
|
|
200
|
+
exports.createGuid = createGuid;
|
|
201
|
+
exports.cssJoinDefined = cssJoinDefined;
|
|
202
|
+
exports.firstDefined = firstDefined;
|
|
203
|
+
exports.joinDefined = joinDefined;
|
|
204
|
+
exports.optional = optional;
|
|
205
|
+
exports.peel = peel;
|
|
206
|
+
exports.peelBetween = peelBetween;
|
|
207
|
+
exports.pipeJoinDefined = pipeJoinDefined;
|
|
208
|
+
exports.required = required;
|
|
209
|
+
exports.semiColonJoinDefined = semiColonJoinDefined;
|
|
210
|
+
exports.setFirst = setFirst;
|
|
211
|
+
exports.sleep = sleep;
|
|
212
|
+
exports.spaceJoinDefined = spaceJoinDefined;
|
|
213
|
+
exports.tryFallback = tryFallback;
|
|
214
|
+
exports.tryFallbackAsync = tryFallbackAsync;
|
|
215
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/anchor/anchor.mts","../src/assert/assert.mts","../src/count-buckets/count-buckets.mts","../src/create-error/create-error.mts","../src/create-guid/create-guid.mts","../src/first-defined/first-defined.mts","../src/join-defined/join-defined.mts","../src/obligation/obligation.mts","../src/orientation/orientation.mts","../src/peel/peel.mts","../src/set-first/set-first.mts","../src/sleep/sleep.mts","../src/try-fallback/try-fallback.mts"],"sourcesContent":["/**\n * Represents a targeted point along a y axis.\n */\nexport enum ZVerticalAnchor {\n /**\n * Top boundary.\n *\n * In vertical device space, this would equate to y = 0.\n */\n Top = 'top',\n /**\n * Centerpoint between the top and bottom.\n */\n Middle = 'middle',\n /**\n * Bottom boundary.\n *\n * In vertical device space, this would equate to y = Infinity.\n */\n Bottom = 'bottom'\n}\n\n/**\n * Represents a targeted point along an x axis.\n */\nexport enum ZHorizontalAnchor {\n /**\n * Left boundary.\n *\n * In horizontal device space, this would equate to x = 0.\n */\n Left = 'left',\n /**\n * Centerpoint between the left and right boundary.\n */\n Center = 'center',\n /**\n * Right boundary.\n *\n * In horizontal device space, this would equate to x = Infinity.\n */\n Right = 'right'\n}\n\n/**\n * Represents an anchor point in 2d space.\n *\n * An anchor array is defined by [vertical, horizontal]\n * and is read as such.\n *\n * @example\n *\n * ```ts\n * const topCenter = [ZVerticalAnchor.Top, ZHorizontalAnchor.Center];\n * const bottomRight = [ZVerticalAnchor.Bottom, ZHorizontalAnchor.Right];\n * ```\n *\n * @see {@link ZVerticalAnchor} For more information.\n * @see {@link ZHorizontalAnchor} For more information.\n */\nexport type ZAnchor = [ZVerticalAnchor, ZHorizontalAnchor];\n\n/**\n * Represents a special type of anchor that excludes the center points.\n */\nexport type ZSideAnchor =\n | ZVerticalAnchor.Top\n | ZVerticalAnchor.Bottom\n | ZHorizontalAnchor.Left\n | ZHorizontalAnchor.Right;\n","/**\n * Represents an object that can be used to build a list of assertions.\n *\n * @example\n *\n * ```ts\n * import { ZAssert, createError } from '@zthun/helpful-fn';\n *\n * const user = readUser();\n *\n * ZAssert\n * .claim(user.name != null, 'User name is required')\n * .claim(user.email != null, 'User email is required')\n * .assert((m) => createError(m));\n * ```\n */\nexport class ZAssert {\n /**\n * Initializes a new instance of a ZAssert with one claim.\n *\n * @param claim -\n * The claim to make.\n * @param msg -\n * The message to throw if the claim is false.\n *\n * @returns\n * A new ZAssert object with an initial claim.\n */\n public static claim(claim: boolean, msg: any): ZAssert {\n return new ZAssert().claim(claim, msg);\n }\n\n private _messages: any[] = [];\n\n /**\n * Initializes a new instance of this object.\n */\n private constructor() {}\n\n /**\n * Adds a claim.\n *\n * @param claim -\n * The claim predicate.\n * @param msg -\n * The message to add if the claim fails.\n *\n * @returns This object.\n */\n public claim(claim: boolean, msg: any): this {\n if (!claim) {\n this._messages.push(msg);\n }\n return this;\n }\n\n /**\n * Runs the assertion.\n *\n * @param fail -\n * The factory that is responsible for returning the specified error to throw.\n */\n public assert<E extends Error>(fail: (message: any | any[]) => E): void {\n if (this._messages.length === 1) {\n throw fail(this._messages[0]);\n }\n\n if (this._messages.length) {\n throw fail(this._messages);\n }\n }\n}\n","/**\n * Calculates the total number of buckets you need to\n * store a number of items where each bucket can hold a\n * maximum weight of items.\n *\n * You can use this function to calculate groupings of\n * items based on total counts and sizes. A good example\n * usage would be to calculate the total number of pages\n * on a paginated list of items given a page size and item\n * count.\n *\n * @param weight -\n * The maximum weight a bucket can store. If this value receives\n * Infinity, then it will result in 1 or min buckets being returned,\n * whichever is larger. If this receives NaN, then this method will\n * result in NaN. Finally, if this receives a negative value, then\n * the result will be max.\n * @param items -\n * The total number of items you need to store where each item\n * counts as 1 towards the weight. If the total number of items\n * is Infinity, then this method will result in max buckets.\n * If this receives NaN, then this method will result in NaN. Finally,\n * if you pass 0, or a negative number of items, then result will be min.\n * @param min -\n * The bounded minimum value. If the total number of buckets\n * evaluates to less than this value, then this value is returned.\n * The default is 0.\n * @param max -\n * The bounded maximum value. If the total number of buckets\n * evaluates to more than this value, then this value is returned.\n * The default is Infinity.\n *\n * @returns\n * The number of buckets you need to store the total number\n * of items given that a single bucket can hold a max weight of items.\n * If either weight or items is NaN, then NaN will be returned regardless\n * of the opposite value. Passing a negative number is the same as\n * passing 0.\n *\n * @example\n *\n * ```ts\n * // The following will return 5 for numberOfPages since you need 5 buckets\n * // to store 101 number of items where each bucket can hold a max of 25\n * // items. The 5th page would be a page of 1 item, since 1 is the remainder.\n * const numberOfPages = countBuckets(25, 101);\n *\n * // In this case, the numberOfPages would be 1 here since our minimum buckets\n * // is 1. By default, this would be 0.\n * const numberOfPages = countBuckets(10, 0, 1);\n *\n * // In this case, we have more items that can be held in the number of buckets\n * // available, so only 4 is returned instead of the 5.\n * const numberOfPages = countBuckets(25, 101, undefined, 4);\n * ```\n */\nexport function countBuckets(weight: number, items: number, min = 0, max = Infinity) {\n weight = Math.max(0, weight);\n items = Math.max(0, items);\n\n if (Number.isNaN(weight) || Number.isNaN(items)) {\n return NaN;\n }\n\n if (items === 0) {\n return min;\n }\n\n const boxes = weight === Infinity ? 1 : Math.ceil(items / weight);\n return Math.min(max, Math.max(min, boxes));\n}\n","/**\n * A helper method to construct a JavaScript error object given a list of acceptable schema keys.\n *\n * @param problem -\n * A generic representation of the problem that occurred. This can be an Error object,\n * another object representing some kind of error, or some message representing the error.\n * If this is null or undefined, then a generic error is returned.\n * @param schema -\n * The list of acceptable object keys to look into problem. These will be recursively\n * evaluated to strip out the real error message. Note that this is in order of\n * priority. If schema[0] and schema[1] are both keys on problem, then schema[0] takes\n * a higher precedence than schema[1]. By default, the schema will look for properties\n * named, message, error, exception, and data, in that order.\n *\n * @returns\n * An error object that is the best evaluation of what the problem actually is.\n *\n * @example\n *\n * ```ts\n * // All of these result an Error with the message, 'Something went wrong'\n * const errorWithStringProblem = createError('Something went wrong');\n * const errorWithObjectProblemInSchema = createError({ error: 'Something went wrong'});\n * const errorWithCustomSchema = createError({ issue: 'Something went wrong'}, ['issue']);\n * const errorRecursive = createError({ error: { message: 'Something went wrong' }});\n *\n * // This would result in '[Object object]' as there is no way to figure out how to deconstruct this problem.\n * const errorCannotBeFound = createError({ wut: 'Something went wrong' });\n * ```\n */\nexport function createError(problem: any, schema = ['message', 'error', 'exception', 'data']): Error {\n if (problem instanceof Error) {\n return problem;\n }\n\n if (problem == null) {\n return new Error();\n }\n\n for (let i = 0; i < schema.length; ++i) {\n const key = schema[i];\n\n if (Object.prototype.hasOwnProperty.call(problem, key)) {\n return createError(problem[key]);\n }\n }\n\n return new Error(String(problem));\n}\n","import { v4 } from 'uuid';\n\n/**\n * Creates a globally unique identifier.\n *\n * The identifier holds to v4 of the UUID specification. A summary\n * of the specification can be found at\n * {@link https://commons.apache.org/sandbox/commons-id/uuid.html | Apache Commons UUID Documentation}.\n *\n * The official documentation of the UUID specification is under\n * {@link https://www.rfc-editor.org/info/rfc4122 | RFC 4122}\n *\n * @returns\n * A new generated globally unique identifier based on random bytes.\n *\n * @example\n *\n * ```ts\n * // Will get a value similar to 53e33fb6-d05a-4fa9-8dc0-b78e4feaa702\n * const guidA = createGuid();\n * // Will most likely not ever be equal to guidA\n * const guidB = createGuid();\n * ```\n */\nexport const createGuid: () => string = v4;\n","/**\n * Returns the first value in args such that args is not null or undefined.\n *\n * @param fallback -\n * The fallback value in the case that all values in args are null/undefined.\n * @param first -\n * The first value to check.\n * @param remaining -\n * The remaining values beyond the first to check.\n * @param T -\n * The type of data that will be enumerated.\n *\n * @returns\n * The first value if it is not null or undefined. If first is undefined or null, then the first item\n * in remaining such that remaining[i] is not null or undefined is returned. If first and all values of\n * remaining are null or undefined, then fallback is returned.\n *\n * @example\n *\n * ```ts\n * // 'Defined'\n * const shouldBeDefined = firstDefined('Fallback', null, undefined, 'Defined');\n * // 'Fallback'\n * const shouldBeFallback = firstDefined('Fallback', null, undefined);\n * const shouldAlsoBeFallback = firstDefined('Fallback', undefined);\n * // 'First'\n * const shouldBeFirst = firstDefined('Fallback', 'First', null, 'Third');\n * ```\n *\n */\nexport function firstDefined<T = any>(\n fallback: T,\n first: T | null | undefined,\n ...remaining: (T | null | undefined)[]\n): T {\n if (first != null) {\n return first;\n }\n\n for (let i = 0; i < remaining.length; ++i) {\n const val = remaining[i];\n\n if (val != null) {\n return val;\n }\n }\n\n return fallback;\n}\n","/**\n * A set of parameters that can be used for a string join.\n *\n * Join lists can be a regular object, null, undefined, or a tuple where the\n * first item is the object to join, and the second item is a boolean that specifies\n * whether to include it if the value is truthy.\n *\n * @param T -\n * The type of data to join.\n */\nexport type JoinListInputParameter<T> = T | [T, boolean] | null | undefined;\n\n/**\n * Similar to {@link Array.join}, but filters out null and undefined items.\n *\n * @param delimiter -\n * The delimiter that separates the items.\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by delimiter.\n *\n * @example\n *\n * ```ts\n * // 'a b d'\n * const joinBySpace = joinDefined(' ', 'a', 'b', ['c', false], null, undefined, ['d', true]);\n * // (Empty String)\n * const joinEmpty = joinDefined('?');\n * ```\n */\nexport function joinDefined<T>(delimiter: string, ...items: JoinListInputParameter<T>[]) {\n return items\n .map((item) => (item instanceof Array ? (item[1] ? item[0] : null) : item))\n .filter((item) => item != null)\n .join(delimiter);\n}\n\n/**\n * Alias to joinList with a space delimiter.\n *\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by space delimiter.\n *\n * @see\n * {@link joinDefined} for more information and examples.\n */\nexport const spaceJoinDefined: <T>(...items: JoinListInputParameter<T>[]) => string = joinDefined.bind(null, ' ');\n\n/**\n * Alias of spaceJoinList.\n *\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by a space delimiter.\n *\n * @see\n * {@link joinDefined} for more information and examples.\n */\nexport const cssJoinDefined: <T>(...items: JoinListInputParameter<T>[]) => string = spaceJoinDefined;\n\n/**\n * Alias of joinList with a comma delimiter.\n *\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by a comma delimiter.\n *\n * @see\n * {@link joinDefined} for more information and examples.\n */\nexport const commaJoinDefined: <T>(...items: JoinListInputParameter<T>[]) => string = joinDefined.bind(null, ',');\n\n/**\n * Alias of joinList with a semi-colon delimiter.\n *\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by a semi-colon delimiter.\n *\n * @see\n * {@link joinDefined} for more information and examples.\n */\nexport const semiColonJoinDefined: <T>(...items: JoinListInputParameter<T>[]) => string = joinDefined.bind(null, ';');\n\n/**\n * Alias of joinList with a pipe delimiter.\n *\n * @param items -\n * The items to join.\n * @param T -\n * The type of data to join.\n *\n * @returns\n * A string that joins the items that are defined, separated by a pipe delimiter.\n *\n * @see\n * {@link joinDefined} for more information and examples.\n */\nexport const pipeJoinDefined: <T>(...items: JoinListInputParameter<T>[]) => string = joinDefined.bind(null, '|');\n","/**\n * A specific set of possible values that need to be checked for requirements.\n *\n * @param T -\n * The type of value that is being checked.\n */\nexport type ZObligatedValue<T> = T | null | undefined | Promise<T | null | undefined>;\n\n/**\n * Requires a value to be non null.\n *\n * @param val -\n * The value to require. This can be a promise\n * that can return null or undefined in addition to the\n * value.\n * @param T -\n * The type of value that is being checked.\n *\n * @returns\n * This method returns val if it is not null\n * or undefined, otherwise, an error is thrown.\n *\n * @throws\n * An error if the value is null or undefined.\n */\nexport async function required<T>(val: ZObligatedValue<T>): Promise<T> {\n if (val == null) {\n throw new Error('A required value was not provided');\n }\n\n const _val = await val;\n\n if (_val == null) {\n throw new Error('A required value was not provided');\n }\n\n return _val;\n}\n\n/**\n * Allows a value to be null or undefined and has a fallback.\n *\n * @param val -\n * The value to check. This can be a promise\n * that can return null or undefined in addition to the\n * value.\n * @param fallback -\n * The fallback value in the case that val is\n * null or undefined.\n * @param T -\n * The type of value that is being checked.\n *\n * @returns\n * This method returns val if it is not null\n * or undefined, otherwise, the fallback is returned.\n * If val is a promise and rejects, then fallback is\n * also returned.\n */\nexport function optional<T>(val: ZObligatedValue<T>, fallback: T): Promise<T>;\n\n/**\n * Allows a value to be null or undefined and has a fallback.\n *\n * @param val -\n * The value to check. This can be a promise\n * that can return null or undefined in addition to the\n * value.\n * @param T -\n * The type of value that is being checked.\n *\n * @returns\n * This method returns val if it is not null\n * or undefined, otherwise, null is returned.\n * If val is a promise and rejects, then null is\n * also returned.\n */\nexport function optional<T>(val: ZObligatedValue<T>): Promise<T | null>;\n\n/**\n * Allows a value to be null or undefined and has a fallback.\n *\n * @param val -\n * The value to check. This can be a promise\n * that can return null or undefined in addition to the\n * value.\n * @param fallback -\n * The fallback value in the case that val is\n * null or undefined.\n * @param T -\n * The type of value that is being checked.\n *\n * @returns\n * This method returns val if it is not null\n * or undefined, otherwise, the fallback is returned.\n * If val is a promise and rejects, then fallback is\n * also returned.\n */\nexport async function optional<T>(val: ZObligatedValue<T>, fallback: T | null = null): Promise<T | null> {\n if (val == null) {\n return fallback;\n }\n\n try {\n const _val = await val;\n return _val == null ? fallback : _val;\n } catch {\n return fallback;\n }\n}\n","/**\n * Represents a direction orientation.\n */\nexport enum ZOrientation {\n /**\n * Horizontal orientation.\n *\n * This type of orientation is a row\n * style orientation or flex-row orientation.\n */\n Horizontal = 'horizontal',\n\n /**\n * Vertical orientation.\n *\n * This type of orientation is a column\n * style orientation or block orientation.\n */\n Vertical = 'vertical'\n}\n","/**\n * Peels a candidate string from the start of a string and splits it into two segments.\n *\n * @param str -\n * The string to peel from.\n * @param candidates -\n * The candidate list of values to peel from str.\n *\n * @returns\n * A tuple where the first item is the peeled string and\n * the second item is the remainder of the string. If the string\n * does not start with any given candidates, then the first\n * item will be null and the second item will be str.\n */\nexport function peel<T extends string = string>(str: string, candidates: T[]): [T | null, string] {\n for (const check of candidates) {\n if (str.startsWith(check)) {\n return [str.substring(0, check.length) as T, str.substring(check.length)];\n }\n }\n\n return [null, str];\n}\n\n/**\n * Peels the values between an opening string set and a closing string set.\n *\n * The actual value will handle issues with internal opening and closing brackets\n *\n * @example\n *\n * ```ts\n * const [peeled, rest] = peelBetween('[a, b, [c1, c2],[d]] other', '[' ']');\n *\n * // Outputs 'a, b, [c1, c2], [d]'\n * console.log(peeled);\n *\n * // Outputs ' other' - white space is included'\n * console.log(rest);\n * ```\n *\n * @param str -\n * The string to peel between.\n * @param open -\n * The opening string. This will test\n * at the start of str.\n * @param close -\n * The closing string. This can be anywhere\n * in the str. If this is equal to open, then\n * the open string is removed from the start of the\n * string and the rest of the string would be returned.\n *\n * @returns\n * A tuple where the first argument is string that was found between\n * the opening and closing bracket. Returns null if no string could\n * be found between an open and close pair. The second argument will\n * be the remaining text of the string, including whitespace.\n */\nexport function peelBetween(str: string, open: string, close: string): [string | null, string] {\n if (!str.startsWith(open)) {\n return [null, str];\n }\n\n let stack = 1;\n\n const _str = str.substring(open.length);\n\n for (let i = 0; i < _str.length; ++i) {\n if (_str.startsWith(open, i)) {\n stack += 1;\n i += open.length - 1;\n continue;\n }\n\n if (_str.startsWith(close, i)) {\n stack -= 1;\n\n if (stack === 0) {\n return [_str.substring(0, i), _str.substring(i + close.length)];\n }\n\n i += close.length - 1;\n }\n }\n\n return [null, str];\n}\n","/**\n * Represents a setter function for when you want to set a single value,\n * but you have an array instead.\n *\n * This is useful when you want to support lists of items, but you need\n * backwards compatibility for setting a single item. This is primarily meant\n * to be use with bind to construct a new method setter that takes an array\n * and forwards it to a setter method which will accept a single value of the\n * first item in the target value.\n *\n * @param setValue -\n * The setter function that takes a single value. This will receive\n * the first item of the value list.\n * @param fallback -\n * The fallback value in the case that there are no values.\n * @param value -\n * The value list to retrieve the first item from.\n * @param T -\n * The type of data that the array holds.\n *\n * @example\n *\n * ```ts\n * // An example of a react component that needs a value of an array, but you only care about a single selection.\n * const [value, setValue] = useState<TypesOfPie>();\n * const _values = useMemo(() => value ? [value] : [], [value]);\n * const _setValues = setFirst.bind(null, setValue, undefined);\n *\n * return <ComponentWithMultiSelectSupport values={_values} onValuesChange={_setValues} />\n * ```\n */\nexport function setFirst<T>(setValue: (val: T) => any, fallback: T, value: T[] | null | undefined) {\n const _value = value?.length ? value[0] : fallback;\n setValue(_value);\n}\n","/**\n * Halts the current thread to invoke an event loop.\n *\n * @param ms -\n * The total number of milliseconds to sleep.\n *\n * @returns\n * A promise that resolves after ms milliseconds.\n */\nexport function sleep(ms?: number): Promise<void>;\n\n/**\n * Halts the current thread to invoke an event loop.\n *\n * @param ms -\n * The total number of milliseconds to sleep.\n * @param val -\n * The value to resolve with.\n * @param T -\n * The type of value that will be resolved.\n *\n * @returns\n * A promise that resolves with val after ms milliseconds.\n */\nexport function sleep<T>(ms: number, val: T): Promise<T>;\n\n/**\n * Halts the current thread to invoke an event loop.\n *\n * @param ms -\n * The total number of milliseconds to sleep.\n * @param T -\n * The type of value that will be resolved.\n *\n * @returns\n * A promise that resolves with val after ms milliseconds.\n */\nexport function sleep<T>(ms = 0, val: T | undefined = undefined): Promise<T | undefined> {\n return new Promise<T | undefined>((resolve) => setTimeout(() => resolve(val), ms));\n}\n","/**\n * Invokes the candidate function and returns undefined if an error is thrown from it.\n *\n * @param candidate -\n * The candidate function to run.\n *\n * @returns\n * The result from candidate. Returns undefined if candidate throws an Error.\n */\nexport function tryFallback<T>(candidate: () => T): T | undefined;\n\n/**\n * Invokes the candidate function and returns fallback if an error is thrown from it.\n *\n * @param candidate -\n * The candidate function to run.\n * @param fallback -\n * The fallback value to return if candidate throws an error.\n *\n * @returns\n * The result from candidate. Returns fallback if candidate throws an Error.\n */\nexport function tryFallback<T>(candidate: () => T, fallback: T): T;\n\n/**\n * Invokes the candidate function and returns fallback if an error is thrown from it.\n *\n * @param candidate -\n * The candidate function to run.\n * @param fallback -\n * The fallback value to return if candidate throws an error.\n *\n * @returns\n * The result from candidate. Returns fallback if candidate throws an Error.\n * If no fallback value is provided, then undefined is returned.\n */\nexport function tryFallback<T>(candidate: () => T, fallback?: T): T | undefined {\n try {\n return candidate();\n } catch {\n return fallback;\n }\n}\n\n/**\n * Invokes the candidate function and returns undefined if an rejected promise is returned from it.\n *\n * @param candidate -\n * The candidate function to run.\n *\n * @returns\n * A promise that resolves with the result from candidate. Returns undefined\n * if candidate returns a rejected promise.\n */\nexport function tryFallbackAsync<T>(candidate: () => Promise<T>): Promise<T | undefined>;\n\n/**\n * Invokes the candidate function and returns undefined if an rejected promise is returned from it.\n *\n * @param candidate -\n * The candidate function to run.\n * @param fallback -\n * The fallback value that will be returned if candidate returns a rejected promise.\n *\n * @returns\n * A promise that resolves with the result from candidate. Returns fallback\n * if candidate returns a rejected promise.\n */\nexport function tryFallbackAsync<T>(candidate: () => Promise<T>, fallback: T): Promise<T>;\n\n/**\n * Invokes the candidate function and returns undefined if an rejected promise is returned from it.\n *\n * @param candidate -\n * The candidate function to run.\n * @param fallback -\n * The optional fallback value to return if the candidate throws an Error.\n *\n * @returns\n * A promise that resolves with the result from candidate or fallback\n * if candidate returns a rejected promise. Resolves with undefined\n * if no fallback is provided.\n */\nexport async function tryFallbackAsync<T>(candidate: () => Promise<T>, fallback?: T): Promise<T | undefined> {\n try {\n return await candidate();\n } catch {\n return fallback;\n }\n}\n"],"names":["ZVerticalAnchor","ZHorizontalAnchor","v4","ZOrientation"],"mappings":";;;AAGY,IAAA,oCAAAA,qBAAL;AAMLA,mBAAA,KAAM,IAAA;AAINA,mBAAA,QAAS,IAAA;AAMTA,mBAAA,QAAS,IAAA;AAhBCA,SAAAA;AAAA,GAAA,mBAAA,CAAA,CAAA;AAsBA,IAAA,sCAAAC,uBAAL;AAMLA,qBAAA,MAAO,IAAA;AAIPA,qBAAA,QAAS,IAAA;AAMTA,qBAAA,OAAQ,IAAA;AAhBEA,SAAAA;AAAA,GAAA,qBAAA,CAAA,CAAA;ACTL,MAAM,QAAQ;AAAA;AAAA;AAAA;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,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC;AACnF;ACHgB,SAAA,YAAe,WAAoB,UAA6B;AAC1E,MAAA;AACF,WAAO,UAAU;AAAA,EAAA,QACX;AACC,WAAA;AAAA,EACT;AACF;AAyCsB,eAAA,iBAAoB,WAA6B,UAAsC;AACvG,MAAA;AACF,WAAO,MAAM,UAAU;AAAA,EAAA,QACjB;AACC,WAAA;AAAA,EACT;AACF;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './anchor/anchor.mjs';
|
|
2
|
+
export * from './assert/assert.mjs';
|
|
3
|
+
export * from './count-buckets/count-buckets.mjs';
|
|
4
|
+
export * from './create-error/create-error.mjs';
|
|
5
|
+
export * from './create-guid/create-guid.mjs';
|
|
6
|
+
export * from './first-defined/first-defined.mjs';
|
|
7
|
+
export * from './join-defined/join-defined.mjs';
|
|
8
|
+
export * from './obligation/obligation.mjs';
|
|
9
|
+
export * from './orientation/orientation.mjs';
|
|
10
|
+
export * from './peel/peel.mjs';
|
|
11
|
+
export * from './set-first/set-first.mjs';
|
|
12
|
+
export * from './sleep/sleep.mjs';
|
|
13
|
+
export * from './try-fallback/try-fallback.mjs';
|