@sealcode/sealgen 0.15.33 → 0.15.34

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.
@@ -14,3 +14,4 @@ export * from "./make-slug.js";
14
14
  export * from "./render-attributes.js";
15
15
  export * from "./format-external-url.js";
16
16
  export * from "./base64.js";
17
+ export * from "./with-fallback.js";
@@ -0,0 +1,2 @@
1
+ export declare function withFallback<T1, T2 extends NonNullable<unknown>>(value1: T1, value2: T2): NonNullable<T1> | T2;
2
+ export declare function withFallback<T1, T2, T3 extends NonNullable<unknown>>(value1: T1, value2: T2, value3?: T3): NonNullable<T1> | NonNullable<T2> | T3;
@@ -13,6 +13,7 @@ export * from "./make-slug.js";
13
13
  export * from "./render-attributes.js";
14
14
  export * from "./format-external-url.js";
15
15
  export * from "./base64.js";
16
+ export * from "./with-fallback.js";
16
17
  export {
17
18
  UrlWithNewParams,
18
19
  assertThrowsAsync,
@@ -0,0 +1,21 @@
1
+ function withFallback(value1, value2, value3) {
2
+ if (value3 !== void 0) {
3
+ if (value1 !== null && value1 !== void 0 && value2 !== null && value2 !== void 0) {
4
+ return value2;
5
+ } else {
6
+ return value3;
7
+ }
8
+ } else {
9
+ if (value1 !== null && value1 !== void 0) {
10
+ return value1;
11
+ } else if (value2 !== void 0 && value2 !== null) {
12
+ return value2;
13
+ } else {
14
+ throw new Error("No suitable fallback: ${arguments}");
15
+ }
16
+ }
17
+ }
18
+ export {
19
+ withFallback
20
+ };
21
+ //# sourceMappingURL=with-fallback.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.15.33",
3
+ "version": "0.15.34",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -23,3 +23,4 @@ export * from "./make-slug.js";
23
23
  export * from "./render-attributes.js";
24
24
  export * from "./format-external-url.js";
25
25
  export * from "./base64.js";
26
+ export * from "./with-fallback.js";
@@ -0,0 +1,47 @@
1
+ // use this to more concisely define values with one or more fallbacks to use when they are null.
2
+
3
+ // If called with two args, the second one has to be non-null
4
+ // if called with three args, the third one has to be non-null
5
+ //
6
+ // in two-arg mode, if the first argument is not null, then it's returned and fallback is not used.
7
+ // if the first argument is null, then the second arg is returned
8
+ //
9
+ // in three-arg mode, the first value is never returned. If the first arg is not-null, then the second arg is returned (if it's not null). Otherwise the third arg is returned
10
+
11
+ export function withFallback<T1, T2 extends NonNullable<unknown>>(
12
+ value1: T1,
13
+ value2: T2
14
+ ): NonNullable<T1> | T2;
15
+ export function withFallback<T1, T2, T3 extends NonNullable<unknown>>(
16
+ value1: T1,
17
+ value2: T2,
18
+ value3?: T3
19
+ ): NonNullable<T1> | NonNullable<T2> | T3;
20
+ export function withFallback<T1, T2, T3 extends NonNullable<unknown>>(
21
+ value1: T1,
22
+ value2: T2,
23
+ value3?: T3
24
+ ): NonNullable<T1> | NonNullable<T2> | T3 {
25
+ if (value3 !== undefined) {
26
+ // three arg mode
27
+ if (
28
+ value1 !== null &&
29
+ value1 !== undefined &&
30
+ value2 !== null &&
31
+ value2 !== undefined
32
+ ) {
33
+ return value2;
34
+ } else {
35
+ return value3;
36
+ }
37
+ } else {
38
+ // two arg mode
39
+ if (value1 !== null && value1 !== undefined) {
40
+ return value1;
41
+ } else if (value2 !== undefined && value2 !== null) {
42
+ return value2;
43
+ } else {
44
+ throw new Error("No suitable fallback: ${arguments}");
45
+ }
46
+ }
47
+ }