@duplojs/utils 1.4.42 → 1.4.43
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/isType.d.ts +2 -2
- package/dist/common/pipeCall.cjs +3 -0
- package/dist/common/pipeCall.d.ts +9 -0
- package/dist/common/pipeCall.mjs +3 -0
- package/dist/common/toCurriedPredicate.cjs +3 -0
- package/dist/common/toCurriedPredicate.d.ts +9 -0
- package/dist/common/toCurriedPredicate.mjs +3 -0
- package/dist/common/types/index.d.ts +1 -0
- package/dist/dataParser/base.cjs +34 -0
- package/dist/dataParser/base.d.ts +49 -7
- package/dist/dataParser/base.mjs +34 -1
- package/dist/dataParser/baseExtended.d.ts +36 -4
- package/dist/dataParser/index.cjs +1 -0
- package/dist/dataParser/index.mjs +1 -1
- package/dist/metadata.json +18 -0
- package/dist/pattern/match/builder.cjs +10 -0
- package/dist/pattern/match/builder.d.ts +2 -0
- package/dist/pattern/match/builder.mjs +10 -0
- package/dist/pattern/when.d.ts +4 -2
- package/dist/string/extract.cjs +22 -0
- package/dist/string/extract.d.ts +45 -0
- package/dist/string/extract.mjs +20 -0
- package/dist/string/extractAll.cjs +20 -0
- package/dist/string/extractAll.d.ts +38 -0
- package/dist/string/extractAll.mjs +18 -0
- package/dist/string/index.cjs +4 -0
- package/dist/string/index.d.ts +3 -1
- package/dist/string/index.mjs +2 -0
- package/dist/string/match.d.ts +2 -0
- package/dist/string/matchAll.d.ts +2 -0
- package/package.json +1 -1
package/dist/common/isType.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type IsEqual, type AnyFunction } from "./types";
|
|
1
|
+
import { type IsEqual, type AnyFunction, type BreakGenericLink } from "./types";
|
|
2
2
|
interface Type {
|
|
3
3
|
string: [string, never];
|
|
4
4
|
number: [number, never];
|
|
@@ -50,6 +50,6 @@ type EligibleType<GenericInput extends unknown> = {
|
|
|
50
50
|
* @see https://utils.duplojs.dev/en/v1/api/common/isType
|
|
51
51
|
*
|
|
52
52
|
*/
|
|
53
|
-
export declare function isType<GenericInput extends unknown, GenericType extends EligibleType<GenericInput>>(type: GenericType): (input: GenericInput) => input is ComputeResult<GenericInput
|
|
53
|
+
export declare function isType<GenericInput extends unknown, GenericType extends EligibleType<GenericInput>>(type: GenericType): (input: GenericInput) => input is ComputeResult<BreakGenericLink<GenericInput>, Type[GenericType]>;
|
|
54
54
|
export declare function isType<GenericInput extends unknown, GenericType extends EligibleType<GenericInput>>(input: GenericInput, type: GenericType): input is ComputeResult<GenericInput, Type[GenericType]>;
|
|
55
55
|
export {};
|
package/dist/common/pipeCall.cjs
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pipeCall() function returns the same function it receives. It is used to adapt a function to a pipe-friendly shape without changing behavior.
|
|
3
|
+
*
|
|
4
|
+
* **Supported call styles:**
|
|
5
|
+
* - Direct: `pipeCall(theFunction)` -> returns the same function
|
|
6
|
+
*
|
|
7
|
+
* The input function is not mutated.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
1
10
|
export declare function pipeCall<GenericInput extends unknown, GenericOutput extends unknown>(theFunction: (input: NoInfer<GenericInput>) => GenericOutput): (input: GenericInput) => NoInfer<GenericOutput>;
|
package/dist/common/pipeCall.mjs
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toCurriedPredicate() function returns the same predicate it receives. It exists to force type inference when a single-argument predicate is passed to a pipe and inference can go wrong.
|
|
3
|
+
*
|
|
4
|
+
* **Supported call styles:**
|
|
5
|
+
* - Direct: `toCurriedPredicate(thePredicate)` -> returns the same predicate
|
|
6
|
+
*
|
|
7
|
+
* The input predicate is not mutated.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
1
10
|
export declare function toCurriedPredicate<GenericInput extends unknown, GenericPredicate extends GenericInput>(thePredicate: (input: GenericInput) => input is GenericPredicate): (input: GenericInput) => input is GenericPredicate;
|
package/dist/dataParser/base.cjs
CHANGED
|
@@ -5,7 +5,9 @@ var kind = require('./kind.cjs');
|
|
|
5
5
|
var simpleClone = require('../common/simpleClone.cjs');
|
|
6
6
|
var wrapValue = require('../common/wrapValue.cjs');
|
|
7
7
|
var override = require('../common/override.cjs');
|
|
8
|
+
var kind$1 = require('../common/kind.cjs');
|
|
8
9
|
var pipe = require('../common/pipe.cjs');
|
|
10
|
+
var errorKindNamespace = require('../common/errorKindNamespace.cjs');
|
|
9
11
|
var error$1 = require('../either/left/error.cjs');
|
|
10
12
|
var success = require('../either/right/success.cjs');
|
|
11
13
|
|
|
@@ -27,6 +29,13 @@ const DPE = error.createError();
|
|
|
27
29
|
const EE = error$1.error(null);
|
|
28
30
|
const ES = success.success(null);
|
|
29
31
|
const KWV = wrapValue.keyWrappedValue;
|
|
32
|
+
class DataParserThrowError extends kind$1.kindHeritage("dataParserThrowError", errorKindNamespace.createErrorKind("dataParserThrowError"), Error) {
|
|
33
|
+
value;
|
|
34
|
+
constructor(value) {
|
|
35
|
+
super({}, ["Parse Error."]);
|
|
36
|
+
this.value = value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
30
39
|
function dataParserInit(kind, definition, exec, specificOverrideHandler) {
|
|
31
40
|
const formattedExec = typeof exec === "object"
|
|
32
41
|
? exec
|
|
@@ -130,11 +139,36 @@ function dataParserInit(kind, definition, exec, specificOverrideHandler) {
|
|
|
130
139
|
}), exec, specificOverrideHandler),
|
|
131
140
|
clone: () => dataParserInit(kind, simpleClone.simpleClone(definition), exec, specificOverrideHandler),
|
|
132
141
|
contract: () => self,
|
|
142
|
+
parseOrThrow(data) {
|
|
143
|
+
const error = {
|
|
144
|
+
...DPE,
|
|
145
|
+
issues: [],
|
|
146
|
+
currentPath: [],
|
|
147
|
+
};
|
|
148
|
+
const result = middleExec(data, error);
|
|
149
|
+
if (result === SDPE) {
|
|
150
|
+
throw new DataParserThrowError(error);
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
},
|
|
154
|
+
async asyncParseOrThrow(data) {
|
|
155
|
+
const error = {
|
|
156
|
+
...DPE,
|
|
157
|
+
issues: [],
|
|
158
|
+
currentPath: [],
|
|
159
|
+
};
|
|
160
|
+
const result = await middleAsyncExec(data, error);
|
|
161
|
+
if (result === SDPE) {
|
|
162
|
+
throw new DataParserThrowError(error);
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
},
|
|
133
166
|
}, (value) => dataParserKind.setTo(value, null), kind.setTo, (value) => dataParserInit.overrideHandler.apply(value), (value) => specificOverrideHandler.apply(value));
|
|
134
167
|
return self;
|
|
135
168
|
}
|
|
136
169
|
dataParserInit.overrideHandler = override.createOverride("@duplojs/utils/data-parser/base");
|
|
137
170
|
|
|
171
|
+
exports.DataParserThrowError = DataParserThrowError;
|
|
138
172
|
exports.SymbolDataParserError = SymbolDataParserError;
|
|
139
173
|
exports.SymbolDataParserErrorLabel = SymbolDataParserErrorLabel;
|
|
140
174
|
exports.checkerKind = checkerKind;
|
|
@@ -76,13 +76,6 @@ export interface DataParser<GenericDefinition extends DataParserDefinition = Dat
|
|
|
76
76
|
*
|
|
77
77
|
* It executes the async parser path, applies all registered checkers, and never mutates the input.
|
|
78
78
|
*
|
|
79
|
-
* ```ts
|
|
80
|
-
* // TODO: add asyncParse examples.
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* @remarks
|
|
84
|
-
* - TODO: complete this documentation and examples.
|
|
85
|
-
*
|
|
86
79
|
* @namespace DP
|
|
87
80
|
*
|
|
88
81
|
*/
|
|
@@ -169,11 +162,60 @@ export interface DataParser<GenericDefinition extends DataParserDefinition = Dat
|
|
|
169
162
|
contract<GenericValue extends unknown>(...args: IsEqual<Output<this>, GenericValue> extends true ? [] : [] & {
|
|
170
163
|
[SymbolContractError]: "Contract error.";
|
|
171
164
|
}): Contract<GenericValue>;
|
|
165
|
+
/**
|
|
166
|
+
* The parseOrThrow() method runs a data parser synchronously and returns the parsed value or throws a DataParserThrowError.
|
|
167
|
+
*
|
|
168
|
+
* **Supported call styles:**
|
|
169
|
+
* - Method: `dataParser.parseOrThrow(input)` -> returns the parsed value
|
|
170
|
+
*
|
|
171
|
+
* It executes the parser, applies all registered checkers, and never mutates the input.
|
|
172
|
+
*
|
|
173
|
+
* ```ts
|
|
174
|
+
* const stringSchema = DP.string({
|
|
175
|
+
* checkers: [DP.checkerStringMin(3)],
|
|
176
|
+
* });
|
|
177
|
+
*
|
|
178
|
+
* const value = stringSchema.parseOrThrow("DuploJS");
|
|
179
|
+
* // value: string
|
|
180
|
+
*
|
|
181
|
+
* try {
|
|
182
|
+
* stringSchema.parseOrThrow("ok");
|
|
183
|
+
* } catch (error) {
|
|
184
|
+
* if (error instanceof DP.DataParserThrowError) {
|
|
185
|
+
* // parseError: DP.DataParserError
|
|
186
|
+
* }
|
|
187
|
+
* }
|
|
188
|
+
*
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @namespace DP
|
|
192
|
+
*
|
|
193
|
+
*/
|
|
194
|
+
parseOrThrow(data: unknown): GenericOutput;
|
|
195
|
+
/**
|
|
196
|
+
* The asyncParseOrThrow() method runs a data parser asynchronously and resolves to the parsed value or rejects with a DataParserThrowError.
|
|
197
|
+
*
|
|
198
|
+
* **Supported call styles:**
|
|
199
|
+
* - Method: `dataParser.asyncParseOrThrow(input)` -> returns a promise
|
|
200
|
+
*
|
|
201
|
+
* It executes the async parser path, applies all registered checkers, and never mutates the input.
|
|
202
|
+
*
|
|
203
|
+
* @namespace DP
|
|
204
|
+
*
|
|
205
|
+
*/
|
|
206
|
+
asyncParseOrThrow(data: unknown): Promise<GenericOutput>;
|
|
172
207
|
}
|
|
173
208
|
interface DataParserInitExecParams<GenericDataParser extends DataParser> {
|
|
174
209
|
sync(...args: [...Parameters<GenericDataParser["exec"]>, self: GenericDataParser]): (GetKindValue<typeof dataParserKind, GenericDataParser>["output"] | SymbolDataParserErrorIssue | SymbolDataParserErrorPromiseIssue);
|
|
175
210
|
async(...args: [...Parameters<GenericDataParser["exec"]>, self: GenericDataParser]): Promise<GetKindValue<typeof dataParserKind, GenericDataParser>["output"] | SymbolDataParserErrorIssue | SymbolDataParserErrorPromiseIssue>;
|
|
176
211
|
}
|
|
212
|
+
declare const DataParserThrowError_base: new (params: {
|
|
213
|
+
"@DuplojsUtilsError/dataParserThrowError"?: unknown;
|
|
214
|
+
}, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & Kind<import("../common").KindDefinition<"dataParserThrowError", unknown>, unknown> & Kind<import("../common").KindDefinition<"@DuplojsUtilsError/dataParserThrowError", unknown>, unknown>;
|
|
215
|
+
export declare class DataParserThrowError extends DataParserThrowError_base {
|
|
216
|
+
value: unknown;
|
|
217
|
+
constructor(value: unknown);
|
|
218
|
+
}
|
|
177
219
|
export declare function dataParserInit<GenericDataParser extends DataParser>(kind: Exclude<GetKindHandler<GenericDataParser>, typeof dataParserKind>, definition: GenericDataParser["definition"], exec: (DataParserInitExecParams<GenericDataParser> | DataParserInitExecParams<GenericDataParser>["sync"]), specificOverrideHandler: OverrideHandler<GenericDataParser>): GenericDataParser;
|
|
178
220
|
export declare namespace dataParserInit {
|
|
179
221
|
var overrideHandler: OverrideHandler<DataParser<DataParserDefinition<DataParserChecker<DataParserCheckerDefinition, unknown>>, unknown, unknown>>;
|
package/dist/dataParser/base.mjs
CHANGED
|
@@ -3,7 +3,9 @@ import { createDataParserKind } from './kind.mjs';
|
|
|
3
3
|
import { simpleClone } from '../common/simpleClone.mjs';
|
|
4
4
|
import { keyWrappedValue } from '../common/wrapValue.mjs';
|
|
5
5
|
import { createOverride } from '../common/override.mjs';
|
|
6
|
+
import { kindHeritage } from '../common/kind.mjs';
|
|
6
7
|
import { pipe } from '../common/pipe.mjs';
|
|
8
|
+
import { createErrorKind } from '../common/errorKindNamespace.mjs';
|
|
7
9
|
import { error } from '../either/left/error.mjs';
|
|
8
10
|
import { success } from '../either/right/success.mjs';
|
|
9
11
|
|
|
@@ -25,6 +27,13 @@ const DPE = createError();
|
|
|
25
27
|
const EE = error(null);
|
|
26
28
|
const ES = success(null);
|
|
27
29
|
const KWV = keyWrappedValue;
|
|
30
|
+
class DataParserThrowError extends kindHeritage("dataParserThrowError", createErrorKind("dataParserThrowError"), Error) {
|
|
31
|
+
value;
|
|
32
|
+
constructor(value) {
|
|
33
|
+
super({}, ["Parse Error."]);
|
|
34
|
+
this.value = value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
28
37
|
function dataParserInit(kind, definition, exec, specificOverrideHandler) {
|
|
29
38
|
const formattedExec = typeof exec === "object"
|
|
30
39
|
? exec
|
|
@@ -128,9 +137,33 @@ function dataParserInit(kind, definition, exec, specificOverrideHandler) {
|
|
|
128
137
|
}), exec, specificOverrideHandler),
|
|
129
138
|
clone: () => dataParserInit(kind, simpleClone(definition), exec, specificOverrideHandler),
|
|
130
139
|
contract: () => self,
|
|
140
|
+
parseOrThrow(data) {
|
|
141
|
+
const error = {
|
|
142
|
+
...DPE,
|
|
143
|
+
issues: [],
|
|
144
|
+
currentPath: [],
|
|
145
|
+
};
|
|
146
|
+
const result = middleExec(data, error);
|
|
147
|
+
if (result === SDPE) {
|
|
148
|
+
throw new DataParserThrowError(error);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
},
|
|
152
|
+
async asyncParseOrThrow(data) {
|
|
153
|
+
const error = {
|
|
154
|
+
...DPE,
|
|
155
|
+
issues: [],
|
|
156
|
+
currentPath: [],
|
|
157
|
+
};
|
|
158
|
+
const result = await middleAsyncExec(data, error);
|
|
159
|
+
if (result === SDPE) {
|
|
160
|
+
throw new DataParserThrowError(error);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
},
|
|
131
164
|
}, (value) => dataParserKind.setTo(value, null), kind.setTo, (value) => dataParserInit.overrideHandler.apply(value), (value) => specificOverrideHandler.apply(value));
|
|
132
165
|
return self;
|
|
133
166
|
}
|
|
134
167
|
dataParserInit.overrideHandler = createOverride("@duplojs/utils/data-parser/base");
|
|
135
168
|
|
|
136
|
-
export { SymbolDataParserError, SymbolDataParserErrorLabel, checkerKind, dataParserCheckerInit, dataParserInit, dataParserKind };
|
|
169
|
+
export { DataParserThrowError, SymbolDataParserError, SymbolDataParserErrorLabel, checkerKind, dataParserCheckerInit, dataParserInit, dataParserKind };
|
|
@@ -54,17 +54,49 @@ export interface DataParserExtended<GenericDefinition extends DataParserDefiniti
|
|
|
54
54
|
*
|
|
55
55
|
* It executes the async parser path, applies all registered checkers, and keeps the extended API available on the parser instance.
|
|
56
56
|
*
|
|
57
|
+
* @namespace DPE
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
asyncParse(data: unknown): Promise<DEither.EitherSuccess<GenericOutput> | DEither.EitherError<DataParserError>>;
|
|
61
|
+
/**
|
|
62
|
+
* The parseOrThrow() method runs an extended data parser synchronously and returns the parsed value or throws a DataParserThrowError.
|
|
63
|
+
*
|
|
64
|
+
* **Supported call styles:**
|
|
65
|
+
* - Method: `dataParser.parseOrThrow(input)` -> returns the parsed value
|
|
66
|
+
*
|
|
67
|
+
* It executes the parser, applies all registered checkers, and keeps the extended API available on the parser instance.
|
|
68
|
+
*
|
|
57
69
|
* ```ts
|
|
58
|
-
*
|
|
70
|
+
* const stringSchema = DPE.string().min(3);
|
|
71
|
+
*
|
|
72
|
+
* const value = stringSchema.parseOrThrow("DuploJS");
|
|
73
|
+
* // value: string
|
|
74
|
+
*
|
|
75
|
+
* try {
|
|
76
|
+
* stringSchema.parseOrThrow("ok");
|
|
77
|
+
* } catch (error) {
|
|
78
|
+
* if (error instanceof DP.DataParserThrowError) {
|
|
79
|
+
* // DP.DataParserError
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
59
82
|
* ```
|
|
60
83
|
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
84
|
+
* @namespace DPE
|
|
85
|
+
*
|
|
86
|
+
*/
|
|
87
|
+
parseOrThrow(data: unknown): GenericOutput;
|
|
88
|
+
/**
|
|
89
|
+
* The asyncParseOrThrow() method runs an extended data parser asynchronously and resolves to the parsed value or rejects with a DataParserThrowError.
|
|
90
|
+
*
|
|
91
|
+
* **Supported call styles:**
|
|
92
|
+
* - Method: `dataParser.asyncParseOrThrow(input)` -> returns a promise
|
|
93
|
+
*
|
|
94
|
+
* It executes the async parser path, applies all registered checkers, and keeps the extended API available on the parser instance.
|
|
63
95
|
*
|
|
64
96
|
* @namespace DPE
|
|
65
97
|
*
|
|
66
98
|
*/
|
|
67
|
-
|
|
99
|
+
asyncParseOrThrow(data: unknown): Promise<GenericOutput>;
|
|
68
100
|
/**
|
|
69
101
|
* The addChecker() method returns a new extended data parser with one or more additional checkers appended.
|
|
70
102
|
*
|
|
@@ -53,6 +53,7 @@ var recover = require('./parsers/recover.cjs');
|
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
|
|
56
|
+
exports.DataParserThrowError = base.DataParserThrowError;
|
|
56
57
|
exports.SymbolDataParserError = base.SymbolDataParserError;
|
|
57
58
|
exports.SymbolDataParserErrorLabel = base.SymbolDataParserErrorLabel;
|
|
58
59
|
exports.checkerKind = base.checkerKind;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { SymbolDataParserError, SymbolDataParserErrorLabel, checkerKind, dataParserCheckerInit, dataParserInit, dataParserKind } from './base.mjs';
|
|
1
|
+
export { DataParserThrowError, SymbolDataParserError, SymbolDataParserErrorLabel, checkerKind, dataParserCheckerInit, dataParserInit, dataParserKind } from './base.mjs';
|
|
2
2
|
export { dataParserExtendedInit, extendedKind } from './baseExtended.mjs';
|
|
3
3
|
export { SymbolDataParserErrorIssue, SymbolDataParserErrorIssueLabel, SymbolDataParserErrorPromiseIssue, SymbolDataParserErrorPromiseIssueLabel, addIssue, addPromiseIssue, createError, errorIssueKind, errorKind, errorPromiseIssueKind, popErrorPath, setErrorPath } from './error.mjs';
|
|
4
4
|
import * as index from './extended/index.mjs';
|
package/dist/metadata.json
CHANGED
|
@@ -4771,6 +4771,24 @@
|
|
|
4771
4771
|
{
|
|
4772
4772
|
"name": "endsWith.mjs"
|
|
4773
4773
|
},
|
|
4774
|
+
{
|
|
4775
|
+
"name": "extract.cjs"
|
|
4776
|
+
},
|
|
4777
|
+
{
|
|
4778
|
+
"name": "extract.d.ts"
|
|
4779
|
+
},
|
|
4780
|
+
{
|
|
4781
|
+
"name": "extract.mjs"
|
|
4782
|
+
},
|
|
4783
|
+
{
|
|
4784
|
+
"name": "extractAll.cjs"
|
|
4785
|
+
},
|
|
4786
|
+
{
|
|
4787
|
+
"name": "extractAll.d.ts"
|
|
4788
|
+
},
|
|
4789
|
+
{
|
|
4790
|
+
"name": "extractAll.mjs"
|
|
4791
|
+
},
|
|
4774
4792
|
{
|
|
4775
4793
|
"name": "includes.cjs"
|
|
4776
4794
|
},
|
|
@@ -33,6 +33,16 @@ matchBuilder.set("when", ({ args: [predicate, theFunction], accumulator, next, }
|
|
|
33
33
|
},
|
|
34
34
|
],
|
|
35
35
|
}));
|
|
36
|
+
matchBuilder.set("whenNot", ({ args: [predicate, theFunction], accumulator, next, }) => next({
|
|
37
|
+
...accumulator,
|
|
38
|
+
matchers: [
|
|
39
|
+
...accumulator.matchers,
|
|
40
|
+
{
|
|
41
|
+
isMatch: (value) => !predicate(value),
|
|
42
|
+
theFunction,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
}));
|
|
36
46
|
matchBuilder.set("exhaustive", ({ accumulator: { input, matchers, }, }) => {
|
|
37
47
|
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
38
48
|
for (let index = 0; index < matchers.length; index++) {
|
|
@@ -13,6 +13,8 @@ export interface MatchBuilder<GenericValue extends unknown = never, GenericResul
|
|
|
13
13
|
with<const GenericPattern extends Pattern<GenericValue>, GenericOutput extends unknown>(pattern: FixDeepFunctionInfer<Pattern<GenericValue>, GenericPattern>, theFunction: (value: ComplexMatchedValue<GenericValue, PatternValue<GenericPattern>>) => GenericOutput): MatchBuilder<ComplexUnMatchedValue<GenericValue, PatternValue<GenericPattern>>, GenericOutput | GenericResult>;
|
|
14
14
|
when<GenericPredicatedInput extends GenericValue, GenericOutput extends unknown>(predicate: (input: GenericValue) => input is GenericPredicatedInput, theFunction: (predicatedInput: GenericPredicatedInput) => GenericOutput): MatchBuilder<Exclude<GenericValue, GenericPredicatedInput>, GenericOutput | GenericResult>;
|
|
15
15
|
when<GenericOutput extends unknown>(predicate: (input: GenericValue) => boolean, theFunction: (predicatedInput: GenericValue) => GenericOutput): MatchBuilder<GenericValue, GenericOutput | GenericResult>;
|
|
16
|
+
whenNot<GenericPredicatedInput extends GenericValue, GenericOutput extends unknown>(predicate: (input: GenericValue) => input is GenericPredicatedInput, theFunction: (predicatedInput: Exclude<GenericValue, GenericPredicatedInput>) => GenericOutput): MatchBuilder<Extract<GenericValue, GenericPredicatedInput>, GenericOutput | GenericResult>;
|
|
17
|
+
whenNot<GenericOutput extends unknown>(predicate: (input: GenericValue) => boolean, theFunction: (predicatedInput: GenericValue) => GenericOutput): MatchBuilder<GenericValue, GenericOutput | GenericResult>;
|
|
16
18
|
exhaustive: IsEqual<GenericValue, never> extends true ? () => GenericResult : {
|
|
17
19
|
[SymbolErrorMatchExhaustive]: "Pattern are not exhaustive.";
|
|
18
20
|
restValue: GenericValue;
|
|
@@ -31,6 +31,16 @@ matchBuilder.set("when", ({ args: [predicate, theFunction], accumulator, next, }
|
|
|
31
31
|
},
|
|
32
32
|
],
|
|
33
33
|
}));
|
|
34
|
+
matchBuilder.set("whenNot", ({ args: [predicate, theFunction], accumulator, next, }) => next({
|
|
35
|
+
...accumulator,
|
|
36
|
+
matchers: [
|
|
37
|
+
...accumulator.matchers,
|
|
38
|
+
{
|
|
39
|
+
isMatch: (value) => !predicate(value),
|
|
40
|
+
theFunction,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
}));
|
|
34
44
|
matchBuilder.set("exhaustive", ({ accumulator: { input, matchers, }, }) => {
|
|
35
45
|
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
36
46
|
for (let index = 0; index < matchers.length; index++) {
|
package/dist/pattern/when.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type AnyValue, type BreakGenericLink, type IsEqual, type EscapeVoid } from "../common";
|
|
2
2
|
import { type PatternResult } from "./result";
|
|
3
|
+
type ComputePredicateInput<GenericValue extends unknown> = Exclude<IsEqual<GenericValue, unknown> extends true ? AnyValue : GenericValue, PatternResult>;
|
|
3
4
|
/**
|
|
4
5
|
* Applies a handler when a predicate matches.
|
|
5
6
|
*
|
|
@@ -43,7 +44,8 @@ import { type PatternResult } from "./result";
|
|
|
43
44
|
* @namespace P
|
|
44
45
|
*
|
|
45
46
|
*/
|
|
46
|
-
export declare function when<GenericInput extends unknown, GenericInputValue extends
|
|
47
|
-
export declare function when<GenericInput extends unknown, GenericInputValue extends Exclude<IsEqual<GenericInput, unknown> extends true ? AnyValue : GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericOutput extends AnyValue | EscapeVoid>(predicate: (input: GenericInputValue) => boolean, theFunction: (predicatedInput: GenericInputValue) => GenericOutput): (input: (GenericInput | GenericInputPatternResult | GenericInputValue)) => (GenericInputPatternResult | GenericInputValue | PatternResult<GenericOutput>);
|
|
47
|
+
export declare function when<GenericInput extends unknown, GenericInputValue extends ComputePredicateInput<GenericInput>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericPredicatedInput extends GenericInputValue, GenericOutput extends AnyValue | EscapeVoid>(predicate: (((input: GenericInputValue) => input is GenericPredicatedInput) & (IsEqual<ComputePredicateInput<GenericInput>, GenericInputValue> extends true ? unknown : (input: ComputePredicateInput<NoInfer<GenericInput>>) => input is GenericPredicatedInput)), theFunction: (predicatedInput: GenericPredicatedInput) => GenericOutput): (input: (GenericInput | GenericInputPatternResult | GenericInputValue)) => (GenericInputPatternResult | Exclude<BreakGenericLink<GenericInput>, GenericPredicatedInput | PatternResult> | PatternResult<GenericOutput>);
|
|
48
|
+
export declare function when<GenericInput extends unknown, GenericInputValue extends Exclude<IsEqual<GenericInput, unknown> extends true ? AnyValue : GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericOutput extends AnyValue | EscapeVoid>(predicate: (((input: GenericInputValue) => boolean) & (IsEqual<ComputePredicateInput<GenericInput>, GenericInputValue> extends true ? unknown : (input: ComputePredicateInput<NoInfer<GenericInput>>) => boolean)), theFunction: (predicatedInput: GenericInputValue) => GenericOutput): (input: (GenericInput | GenericInputPatternResult | GenericInputValue)) => (GenericInputPatternResult | GenericInputValue | PatternResult<GenericOutput>);
|
|
48
49
|
export declare function when<GenericInput extends unknown, GenericInputValue extends Exclude<IsEqual<GenericInput, unknown> extends true ? AnyValue : GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericPredicatedInput extends GenericInputValue, GenericOutput extends AnyValue | EscapeVoid>(input: (GenericInput | GenericInputPatternResult | GenericInputValue), predicate: (input: GenericInputValue) => input is GenericPredicatedInput, theFunction: (predicatedInput: GenericPredicatedInput) => GenericOutput): (GenericInputPatternResult | Exclude<BreakGenericLink<GenericInput>, GenericPredicatedInput | PatternResult> | PatternResult<GenericOutput>);
|
|
49
50
|
export declare function when<GenericInput extends unknown, GenericInputValue extends Exclude<IsEqual<GenericInput, unknown> extends true ? AnyValue : GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericOutput extends AnyValue | EscapeVoid>(input: (GenericInput | GenericInputPatternResult | GenericInputValue), predicate: (input: GenericInputValue) => boolean, theFunction: (predicatedInput: GenericInputValue) => GenericOutput): (GenericInputPatternResult | GenericInputValue | PatternResult<GenericOutput>);
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function extract(...args) {
|
|
4
|
+
if (args.length === 1) {
|
|
5
|
+
const [pattern] = args;
|
|
6
|
+
return (input) => extract(input, pattern);
|
|
7
|
+
}
|
|
8
|
+
const [input, pattern] = args;
|
|
9
|
+
const result = input.match(pattern);
|
|
10
|
+
if (!result) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
matchedValue: result[0],
|
|
15
|
+
groups: result.slice(1),
|
|
16
|
+
namedGroups: result.groups ? { ...result.groups } : undefined,
|
|
17
|
+
offset: result.index ?? 0,
|
|
18
|
+
self: result.input ?? input,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.extract = extract;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface ExtractResult {
|
|
2
|
+
matchedValue: string;
|
|
3
|
+
groups: string[];
|
|
4
|
+
namedGroups?: Record<string, string>;
|
|
5
|
+
offset: number;
|
|
6
|
+
self: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Extracts details about the first match of a pattern in a string.
|
|
10
|
+
*
|
|
11
|
+
* **Supported call styles:**
|
|
12
|
+
* - Classic: `extract(input, pattern)` → returns an ExtractResult or `undefined`
|
|
13
|
+
* - Curried: `extract(pattern)` → returns a function waiting for the input
|
|
14
|
+
*
|
|
15
|
+
* The input string is not mutated.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* S.extract("id=order-42", /(?<name>\w+)-(\d+)/);
|
|
19
|
+
* // {
|
|
20
|
+
* // matchedValue: "order-42",
|
|
21
|
+
* // groups: ["order", "42"],
|
|
22
|
+
* // namedGroups: { name: "order" },
|
|
23
|
+
* // offset: 3,
|
|
24
|
+
* // self: "id=order-42",
|
|
25
|
+
* // }
|
|
26
|
+
*
|
|
27
|
+
* S.extract("hello", /\d+/); // undefined
|
|
28
|
+
*
|
|
29
|
+
* pipe(
|
|
30
|
+
* "user-7",
|
|
31
|
+
* S.extract(/(\w+)-(\d+)/),
|
|
32
|
+
* ); // ExtractResult | undefined
|
|
33
|
+
*
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* - Uses the same semantics as [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
|
38
|
+
*
|
|
39
|
+
* @see https://utils.duplojs.dev/en/v1/api/string/extract
|
|
40
|
+
*
|
|
41
|
+
* @namespace S
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
export declare function extract<GenericInput extends string>(pattern: string | RegExp): (input: GenericInput) => ExtractResult | undefined;
|
|
45
|
+
export declare function extract<GenericInput extends string>(input: GenericInput, pattern: string | RegExp): ExtractResult | undefined;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function extract(...args) {
|
|
2
|
+
if (args.length === 1) {
|
|
3
|
+
const [pattern] = args;
|
|
4
|
+
return (input) => extract(input, pattern);
|
|
5
|
+
}
|
|
6
|
+
const [input, pattern] = args;
|
|
7
|
+
const result = input.match(pattern);
|
|
8
|
+
if (!result) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
matchedValue: result[0],
|
|
13
|
+
groups: result.slice(1),
|
|
14
|
+
namedGroups: result.groups ? { ...result.groups } : undefined,
|
|
15
|
+
offset: result.index ?? 0,
|
|
16
|
+
self: result.input ?? input,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { extract };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var map = require('../generator/map.cjs');
|
|
4
|
+
|
|
5
|
+
function extractAll(...args) {
|
|
6
|
+
if (args.length === 1) {
|
|
7
|
+
const [pattern] = args;
|
|
8
|
+
return (input) => extractAll(input, pattern);
|
|
9
|
+
}
|
|
10
|
+
const [input, pattern] = args;
|
|
11
|
+
return map.map(input.matchAll(pattern), (value) => ({
|
|
12
|
+
matchedValue: value[0],
|
|
13
|
+
groups: value.slice(1),
|
|
14
|
+
namedGroups: value.groups ? { ...value.groups } : undefined,
|
|
15
|
+
offset: value.index ?? 0,
|
|
16
|
+
self: value.input ?? input,
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.extractAll = extractAll;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ExtractResult } from "./extract";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts details about all matches of a pattern in a string.
|
|
4
|
+
*
|
|
5
|
+
* **Supported call styles:**
|
|
6
|
+
* - Classic: `extractAll(input, pattern)` → returns a generator of ExtractResult
|
|
7
|
+
* - Curried: `extractAll(pattern)` → returns a function waiting for the input
|
|
8
|
+
*
|
|
9
|
+
* The input string is not mutated.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* pipe(
|
|
13
|
+
* "id=1; id=2",
|
|
14
|
+
* S.extractAll(/id=(\d+)/g),
|
|
15
|
+
* A.from,
|
|
16
|
+
* ); // [{ ... }, { ... }]
|
|
17
|
+
*
|
|
18
|
+
* S.extractAll("hello", /\d+/g); // Generator
|
|
19
|
+
*
|
|
20
|
+
* pipe(
|
|
21
|
+
* "a1b2",
|
|
22
|
+
* S.extractAll(/(\d)/g),
|
|
23
|
+
* A.from,
|
|
24
|
+
* A.map((value) => value.matchedValue),
|
|
25
|
+
* ); // ["1", "2"]
|
|
26
|
+
*
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* - Uses the same semantics as [`String.prototype.matchAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll)
|
|
31
|
+
*
|
|
32
|
+
* @see https://utils.duplojs.dev/en/v1/api/string/extractAll
|
|
33
|
+
*
|
|
34
|
+
* @namespace S
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractAll<GenericInput extends string>(pattern: RegExp): (input: GenericInput) => Generator<ExtractResult>;
|
|
38
|
+
export declare function extractAll<GenericInput extends string>(input: GenericInput, pattern: RegExp): Generator<ExtractResult>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { map } from '../generator/map.mjs';
|
|
2
|
+
|
|
3
|
+
function extractAll(...args) {
|
|
4
|
+
if (args.length === 1) {
|
|
5
|
+
const [pattern] = args;
|
|
6
|
+
return (input) => extractAll(input, pattern);
|
|
7
|
+
}
|
|
8
|
+
const [input, pattern] = args;
|
|
9
|
+
return map(input.matchAll(pattern), (value) => ({
|
|
10
|
+
matchedValue: value[0],
|
|
11
|
+
groups: value.slice(1),
|
|
12
|
+
namedGroups: value.groups ? { ...value.groups } : undefined,
|
|
13
|
+
offset: value.index ?? 0,
|
|
14
|
+
self: value.input ?? input,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { extractAll };
|
package/dist/string/index.cjs
CHANGED
|
@@ -30,6 +30,8 @@ var length = require('./length.cjs');
|
|
|
30
30
|
var sort = require('./sort.cjs');
|
|
31
31
|
var sortCompare = require('./sortCompare.cjs');
|
|
32
32
|
var to = require('./to.cjs');
|
|
33
|
+
var extract = require('./extract.cjs');
|
|
34
|
+
var extractAll = require('./extractAll.cjs');
|
|
33
35
|
var _default = require('./at/default.cjs');
|
|
34
36
|
var first = require('./at/first.cjs');
|
|
35
37
|
var last = require('./at/last.cjs');
|
|
@@ -71,6 +73,8 @@ exports.length = length.length;
|
|
|
71
73
|
exports.sort = sort.sort;
|
|
72
74
|
exports.sortCompare = sortCompare.sortCompare;
|
|
73
75
|
exports.to = to.to;
|
|
76
|
+
exports.extract = extract.extract;
|
|
77
|
+
exports.extractAll = extractAll.extractAll;
|
|
74
78
|
exports.at = _default.at;
|
|
75
79
|
exports.first = first.first;
|
|
76
80
|
exports.last = last.last;
|
package/dist/string/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
* What you will find in this namespace:
|
|
16
16
|
* - casing and trimming (`S.toUpperCase`, `S.toLowerCase`, `S.capitalize`, `S.uncapitalize`, `S.trim`)
|
|
17
|
-
* - search and match (`S.includes`, `S.startsWith`, `S.endsWith`, `S.search`, `S.match`, `S.matchAll`, `S.test`)
|
|
17
|
+
* - search and match (`S.includes`, `S.startsWith`, `S.endsWith`, `S.search`, `S.match`, `S.matchAll`, `S.extract`, `S.extractAll`, `S.test`)
|
|
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`)
|
|
@@ -58,3 +58,5 @@ export * from "./length";
|
|
|
58
58
|
export * from "./sort";
|
|
59
59
|
export * from "./sortCompare";
|
|
60
60
|
export * from "./to";
|
|
61
|
+
export * from "./extract";
|
|
62
|
+
export * from "./extractAll";
|
package/dist/string/index.mjs
CHANGED
|
@@ -28,6 +28,8 @@ export { length } from './length.mjs';
|
|
|
28
28
|
export { sort } from './sort.mjs';
|
|
29
29
|
export { sortCompare } from './sortCompare.mjs';
|
|
30
30
|
export { to } from './to.mjs';
|
|
31
|
+
export { extract } from './extract.mjs';
|
|
32
|
+
export { extractAll } from './extractAll.mjs';
|
|
31
33
|
export { at } from './at/default.mjs';
|
|
32
34
|
export { first } from './at/first.mjs';
|
|
33
35
|
export { last } from './at/last.mjs';
|
package/dist/string/match.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Finds the first match of a pattern in a string.
|
|
3
3
|
*
|
|
4
|
+
* @deprecated Use `extract` instead.
|
|
5
|
+
*
|
|
4
6
|
* **Supported call styles:**
|
|
5
7
|
* - Classic: `match(input, pattern)` → returns a match array or `undefined`
|
|
6
8
|
* - Curried: `match(pattern)` → returns a function waiting for the input
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Finds all matches of a pattern in a string.
|
|
3
3
|
*
|
|
4
|
+
* @deprecated Use `extractAll` instead.
|
|
5
|
+
*
|
|
4
6
|
* **Supported call styles:**
|
|
5
7
|
* - Classic: `matchAll(input, pattern)` → returns an iterator of matches
|
|
6
8
|
* - Curried: `matchAll(pattern)` → returns a function waiting for the input
|