@duplojs/utils 1.4.36 → 1.4.38
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/dist/common/asserts.cjs +23 -0
- package/dist/common/asserts.d.ts +38 -0
- package/dist/common/asserts.mjs +20 -0
- package/dist/common/index.d.ts +2 -1
- package/dist/common/types/index.d.ts +1 -0
- package/dist/common/types/json.d.ts +4 -0
- package/dist/dataParser/parsers/nullable.d.ts +1 -1
- package/dist/dataParser/parsers/optional.d.ts +1 -1
- package/dist/either/right/asyncPipe.d.ts +2 -2
- package/dist/index.cjs +3 -0
- package/dist/index.mjs +1 -0
- package/dist/metadata.json +21 -0
- package/dist/string/index.cjs +2 -0
- package/dist/string/index.d.ts +2 -1
- package/dist/string/index.mjs +1 -0
- package/dist/string/to.cjs +10 -0
- package/dist/string/to.d.ts +23 -0
- package/dist/string/to.mjs +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errorKindNamespace = require('./errorKindNamespace.cjs');
|
|
4
|
+
var kind = require('./kind.cjs');
|
|
5
|
+
|
|
6
|
+
class AssertsError extends kind.kindHeritage("asserts-error", errorKindNamespace.createErrorKind("asserts-error"), Error) {
|
|
7
|
+
value;
|
|
8
|
+
constructor(value) {
|
|
9
|
+
super({}, ["Asserts Error."]);
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* {@include common/asserts/index.md}
|
|
15
|
+
*/
|
|
16
|
+
function asserts(input, predicate) {
|
|
17
|
+
if (!predicate(input)) {
|
|
18
|
+
throw new AssertsError(input);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.AssertsError = AssertsError;
|
|
23
|
+
exports.asserts = asserts;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
declare const AssertsError_base: new (params: {
|
|
2
|
+
"@DuplojsUtilsError/asserts-error"?: unknown;
|
|
3
|
+
}, parentParams: [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("./kind").Kind<import("./kind").KindDefinition<"asserts-error", unknown>, unknown> & import("./kind").Kind<import("./kind").KindDefinition<"@DuplojsUtilsError/asserts-error", unknown>, unknown>;
|
|
4
|
+
export declare class AssertsError extends AssertsError_base {
|
|
5
|
+
value: unknown;
|
|
6
|
+
constructor(value: unknown);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The asserts() function throws when a predicate fails and narrows the input type when it passes.
|
|
10
|
+
*
|
|
11
|
+
* **Supported call styles:**
|
|
12
|
+
* - Classic: `asserts(input, predicate)` → narrows the input type or throws
|
|
13
|
+
*
|
|
14
|
+
* It throws an `AssertsError` with the failing input value.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const input: string | number = "demo";
|
|
18
|
+
* asserts(input, isType("string"));
|
|
19
|
+
*
|
|
20
|
+
* const payload: { id?: number } = { id: 1 };
|
|
21
|
+
* asserts(payload.id, isType("undefined"));
|
|
22
|
+
*
|
|
23
|
+
* try {
|
|
24
|
+
* asserts(42 as string | number, isType("string"));
|
|
25
|
+
* } catch (error) {
|
|
26
|
+
* if (error instanceof AssertsError) {
|
|
27
|
+
* const failedValue = error.value;
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @see https://utils.duplojs.dev/en/v1/api/common/asserts
|
|
33
|
+
*
|
|
34
|
+
* @namespace C
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
export declare function asserts<GenericInput extends unknown, GenericPredicate extends GenericInput>(input: GenericInput, predicate: (input: GenericInput) => input is GenericPredicate): asserts input is GenericPredicate;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createErrorKind } from './errorKindNamespace.mjs';
|
|
2
|
+
import { kindHeritage } from './kind.mjs';
|
|
3
|
+
|
|
4
|
+
class AssertsError extends kindHeritage("asserts-error", createErrorKind("asserts-error"), Error) {
|
|
5
|
+
value;
|
|
6
|
+
constructor(value) {
|
|
7
|
+
super({}, ["Asserts Error."]);
|
|
8
|
+
this.value = value;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* {@include common/asserts/index.md}
|
|
13
|
+
*/
|
|
14
|
+
function asserts(input, predicate) {
|
|
15
|
+
if (!predicate(input)) {
|
|
16
|
+
throw new AssertsError(input);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { AssertsError, asserts };
|
package/dist/common/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
* What you will find in this namespace:
|
|
16
16
|
* - composition helpers (`pipe`, `innerPipe`, `asyncPipe`, `asyncInnerPipe`, `forward`)
|
|
17
|
-
* - predicates and guards (`when`, `whenNot`, `whenElse`, `and`, `or`, `isType`, `instanceOf`)
|
|
17
|
+
* - predicates and guards (`when`, `whenNot`, `whenElse`, `and`, `or`, `isType`, `asserts`, `instanceOf`)
|
|
18
18
|
* - control flow (`loop`, `asyncLoop`, `asyncRetry`, `sleep`, `memo`)
|
|
19
19
|
* - promise utilities (`externalPromise`, `promiseObject`)
|
|
20
20
|
* - string and value conversions (`toString`, `stringToMillisecond`, `stringToBytes`, `escapeRegExp`)
|
|
@@ -74,3 +74,4 @@ export * from "./hasSomeKinds";
|
|
|
74
74
|
export * from "./hasKinds";
|
|
75
75
|
export * from "./toCurriedPredicate";
|
|
76
76
|
export * from "./pipeCall";
|
|
77
|
+
export * from "./asserts";
|
|
@@ -51,7 +51,7 @@ export interface DataParserNullable<GenericDefinition extends DataParserDefiniti
|
|
|
51
51
|
* @namespace DP
|
|
52
52
|
*
|
|
53
53
|
*/
|
|
54
|
-
export declare function nullable<GenericDataParser extends DataParser, const GenericDefinition extends Partial<Omit<DataParserDefinitionNullable<Output<GenericDataParser
|
|
54
|
+
export declare function nullable<GenericDataParser extends DataParser, const GenericDefinition extends Partial<Omit<DataParserDefinitionNullable<Output<GenericDataParser>>, "inner">> = never>(inner: GenericDataParser, definition?: GenericDefinition): DataParserNullable<MergeDefinition<DataParserDefinitionNullable, NeverCoalescing<GenericDefinition, {}> & {
|
|
55
55
|
inner: GenericDataParser;
|
|
56
56
|
}>>;
|
|
57
57
|
export declare namespace nullable {
|
|
@@ -51,7 +51,7 @@ export interface DataParserOptional<GenericDefinition extends DataParserDefiniti
|
|
|
51
51
|
* @namespace DP
|
|
52
52
|
*
|
|
53
53
|
*/
|
|
54
|
-
export declare function optional<GenericDataParser extends DataParser, const GenericDefinition extends Partial<Omit<DataParserDefinitionOptional<Output<GenericDataParser
|
|
54
|
+
export declare function optional<GenericDataParser extends DataParser, const GenericDefinition extends Partial<Omit<DataParserDefinitionOptional<Output<GenericDataParser>>, "inner">> = never>(inner: GenericDataParser, definition?: GenericDefinition): DataParserOptional<MergeDefinition<DataParserDefinitionOptional, NeverCoalescing<GenericDefinition, {}> & {
|
|
55
55
|
inner: GenericDataParser;
|
|
56
56
|
}>>;
|
|
57
57
|
export declare namespace optional {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type EitherRight } from "./create";
|
|
2
2
|
import { type EitherLeft } from "../left";
|
|
3
3
|
import { type MaybeFutureEither } from "../future/maybeFutureEither";
|
|
4
|
-
import { type Future } from "../future";
|
|
4
|
+
import { type EitherFutureError, type Future } from "../future";
|
|
5
5
|
import { type EitherSuccess } from "./success";
|
|
6
6
|
import { type AnyValue, type Unwrap } from "../../common";
|
|
7
7
|
type Either = EitherRight | EitherLeft;
|
|
8
8
|
export type EitherRightAsyncPipeFunction<GenericInput extends AnyValue = AnyValue, GenericOutput extends MaybeFutureEither<AnyValue> = MaybeFutureEither<AnyValue>> = (input: Awaited<GenericInput> extends infer InferredInput ? InferredInput extends Either ? Unwrap<Exclude<InferredInput, EitherLeft>> : InferredInput : never) => GenericOutput;
|
|
9
|
-
export type EitherRightAsyncPipeResult<GenericPipeOutputs extends AnyValue, GenericLastPipeOutput extends AnyValue> = Extract<Awaited<GenericPipeOutputs>, EitherLeft> | (Awaited<GenericLastPipeOutput> extends infer InferredLastResult ? Exclude<InferredLastResult extends Either ? InferredLastResult : EitherSuccess<InferredLastResult>, EitherLeft> : never);
|
|
9
|
+
export type EitherRightAsyncPipeResult<GenericPipeOutputs extends AnyValue, GenericLastPipeOutput extends AnyValue> = (Extract<Awaited<GenericPipeOutputs>, EitherLeft> | (Awaited<GenericLastPipeOutput> extends infer InferredLastResult ? Exclude<InferredLastResult extends Either ? InferredLastResult : EitherSuccess<InferredLastResult>, EitherLeft> : never) | (Promise<any> extends Exclude<GenericPipeOutputs, Future<any>> ? EitherFutureError : never));
|
|
10
10
|
/**
|
|
11
11
|
* Asynchronous version of rightPipe. Automatically handles promises, Future, and Either, and short-circuits on the first Left.
|
|
12
12
|
*
|
package/dist/index.cjs
CHANGED
|
@@ -60,6 +60,7 @@ var hasSomeKinds = require('./common/hasSomeKinds.cjs');
|
|
|
60
60
|
var hasKinds = require('./common/hasKinds.cjs');
|
|
61
61
|
var toCurriedPredicate = require('./common/toCurriedPredicate.cjs');
|
|
62
62
|
var pipeCall = require('./common/pipeCall.cjs');
|
|
63
|
+
var asserts = require('./common/asserts.cjs');
|
|
63
64
|
|
|
64
65
|
|
|
65
66
|
|
|
@@ -147,3 +148,5 @@ exports.hasSomeKinds = hasSomeKinds.hasSomeKinds;
|
|
|
147
148
|
exports.hasKinds = hasKinds.hasKinds;
|
|
148
149
|
exports.toCurriedPredicate = toCurriedPredicate.toCurriedPredicate;
|
|
149
150
|
exports.pipeCall = pipeCall.pipeCall;
|
|
151
|
+
exports.AssertsError = asserts.AssertsError;
|
|
152
|
+
exports.asserts = asserts.asserts;
|
package/dist/index.mjs
CHANGED
|
@@ -82,3 +82,4 @@ export { hasSomeKinds } from './common/hasSomeKinds.mjs';
|
|
|
82
82
|
export { hasKinds } from './common/hasKinds.mjs';
|
|
83
83
|
export { toCurriedPredicate } from './common/toCurriedPredicate.mjs';
|
|
84
84
|
export { pipeCall } from './common/pipeCall.mjs';
|
|
85
|
+
export { AssertsError, asserts } from './common/asserts.mjs';
|
package/dist/metadata.json
CHANGED
|
@@ -1074,6 +1074,9 @@
|
|
|
1074
1074
|
{
|
|
1075
1075
|
"name": "isUnion.d.ts"
|
|
1076
1076
|
},
|
|
1077
|
+
{
|
|
1078
|
+
"name": "json.d.ts"
|
|
1079
|
+
},
|
|
1077
1080
|
{
|
|
1078
1081
|
"name": "lastUnionElement.d.ts"
|
|
1079
1082
|
},
|
|
@@ -1166,6 +1169,15 @@
|
|
|
1166
1169
|
{
|
|
1167
1170
|
"name": "and.mjs"
|
|
1168
1171
|
},
|
|
1172
|
+
{
|
|
1173
|
+
"name": "asserts.cjs"
|
|
1174
|
+
},
|
|
1175
|
+
{
|
|
1176
|
+
"name": "asserts.d.ts"
|
|
1177
|
+
},
|
|
1178
|
+
{
|
|
1179
|
+
"name": "asserts.mjs"
|
|
1180
|
+
},
|
|
1169
1181
|
{
|
|
1170
1182
|
"name": "asyncInnerPipe.cjs"
|
|
1171
1183
|
},
|
|
@@ -4898,6 +4910,15 @@
|
|
|
4898
4910
|
{
|
|
4899
4911
|
"name": "test.mjs"
|
|
4900
4912
|
},
|
|
4913
|
+
{
|
|
4914
|
+
"name": "to.cjs"
|
|
4915
|
+
},
|
|
4916
|
+
{
|
|
4917
|
+
"name": "to.d.ts"
|
|
4918
|
+
},
|
|
4919
|
+
{
|
|
4920
|
+
"name": "to.mjs"
|
|
4921
|
+
},
|
|
4901
4922
|
{
|
|
4902
4923
|
"name": "toLowerCase.cjs"
|
|
4903
4924
|
},
|
package/dist/string/index.cjs
CHANGED
|
@@ -29,6 +29,7 @@ var isIn = require('./isIn.cjs');
|
|
|
29
29
|
var length = require('./length.cjs');
|
|
30
30
|
var sort = require('./sort.cjs');
|
|
31
31
|
var sortCompare = require('./sortCompare.cjs');
|
|
32
|
+
var to = require('./to.cjs');
|
|
32
33
|
var _default = require('./at/default.cjs');
|
|
33
34
|
var first = require('./at/first.cjs');
|
|
34
35
|
var last = require('./at/last.cjs');
|
|
@@ -69,6 +70,7 @@ exports.isIn = isIn.isIn;
|
|
|
69
70
|
exports.length = length.length;
|
|
70
71
|
exports.sort = sort.sort;
|
|
71
72
|
exports.sortCompare = sortCompare.sortCompare;
|
|
73
|
+
exports.to = to.to;
|
|
72
74
|
exports.at = _default.at;
|
|
73
75
|
exports.first = first.first;
|
|
74
76
|
exports.last = last.last;
|
package/dist/string/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* - composition and slicing (`S.concat`, `S.slice`, `S.substring`, `S.split`)
|
|
19
19
|
* - indexing (`S.at`, `S.charAt`, `S.indexOf`, `S.lastIndexOf`)
|
|
20
20
|
* - padding and repeat (`S.padStart`, `S.padEnd`, `S.repeat`)
|
|
21
|
-
* - misc helpers (`S.normalize`, `S.isKeyof`, `S.isIn`, `S.length`, `S.sort`, `S.sortCompare`)
|
|
21
|
+
* - misc helpers (`S.normalize`, `S.isKeyof`, `S.isIn`, `S.length`, `S.sort`, `S.sortCompare`, `S.to`)
|
|
22
22
|
*
|
|
23
23
|
* @see https://utils.duplojs.dev/en/v1/api/string
|
|
24
24
|
*
|
|
@@ -57,3 +57,4 @@ export * from "./isIn";
|
|
|
57
57
|
export * from "./length";
|
|
58
58
|
export * from "./sort";
|
|
59
59
|
export * from "./sortCompare";
|
|
60
|
+
export * from "./to";
|
package/dist/string/index.mjs
CHANGED
|
@@ -27,6 +27,7 @@ export { isIn } from './isIn.mjs';
|
|
|
27
27
|
export { length } from './length.mjs';
|
|
28
28
|
export { sort } from './sort.mjs';
|
|
29
29
|
export { sortCompare } from './sortCompare.mjs';
|
|
30
|
+
export { to } from './to.mjs';
|
|
30
31
|
export { at } from './at/default.mjs';
|
|
31
32
|
export { first } from './at/first.mjs';
|
|
32
33
|
export { last } from './at/last.mjs';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type Primitive = (string | boolean | null | number | undefined | bigint);
|
|
2
|
+
/**
|
|
3
|
+
* Converts a primitive value to its string representation.
|
|
4
|
+
*
|
|
5
|
+
* **Supported call styles:**
|
|
6
|
+
* - Classic: `to(value)` -> returns a string
|
|
7
|
+
*
|
|
8
|
+
* It uses template literal conversion and preserves literal types when possible.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const fromNumber = S.to(42);
|
|
12
|
+
* const fromBoolean = S.to(false);
|
|
13
|
+
* const fromNull = S.to(null);
|
|
14
|
+
* const fromBigInt = S.to(10n);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @see https://utils.duplojs.dev/en/v1/api/string/to
|
|
18
|
+
*
|
|
19
|
+
* @namespace S
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
export declare function to<GenericValue extends Primitive>(value: GenericValue): `${GenericValue}`;
|
|
23
|
+
export {};
|