arkregex 0.0.2 → 0.0.3
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 +2 -0
- package/out/quantify.d.ts +16 -10
- package/out/quantify.js +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,6 +21,8 @@ const email = regex("^(?<name>\\w+)@(?<domain>\\w+\\.\\w+)$")
|
|
|
21
21
|
|
|
22
22
|
All you need to get started is `pnpm install arkregex` (or the equivalent for your package manager of choice) 🎉
|
|
23
23
|
|
|
24
|
+
Performs best with TS 5.9+
|
|
25
|
+
|
|
24
26
|
### Features
|
|
25
27
|
|
|
26
28
|
- **Types**: Infers string types for your existing regular expressions, including positional and named captures
|
package/out/quantify.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Scanner } from "@ark/util";
|
|
1
|
+
import type { parseNonNegativeInteger, Scanner } from "@ark/util";
|
|
2
2
|
import type { s, State } from "./state.ts";
|
|
3
3
|
export type parseBuiltinQuantifier<s extends State, quantifier extends QuantifyingChar, unscanned extends string> = s["root"] extends "" ? s.error<writeUnmatchedQuantifierError<quantifier>> : quantifyBuiltin<s, quantifier, unscanned extends Scanner.shift<"?", infer lazyUnscanned> ? lazyUnscanned : unscanned>;
|
|
4
4
|
type quantifyBuiltin<s extends State, quantifier extends QuantifyingChar, unscanned extends string> = quantifier extends "?" ? s.pushQuantifier<s, 0, 1, unscanned> : quantifier extends "+" ? s.pushQuantifier<s, 1, null, unscanned> : quantifier extends "*" ? s.pushQuantifier<s, 0, null, unscanned> : never;
|
|
@@ -11,20 +11,24 @@ declare namespace ParsedRange {
|
|
|
11
11
|
type from<r extends ParsedRange> = r;
|
|
12
12
|
}
|
|
13
13
|
type skipPossibleQuestionMark<unscanned extends string> = unscanned extends `?${infer next}` ? next : unscanned;
|
|
14
|
-
type parsePossibleRangeString<unscanned extends string> = unscanned extends (`${infer l extends number},${infer r extends number}}${infer next}`) ? ParsedRange.from<{
|
|
15
|
-
min: l
|
|
16
|
-
max: r
|
|
14
|
+
type parsePossibleRangeString<unscanned extends string> = unscanned extends (`${infer l extends `${number}`},${infer r extends `${number}`}}${infer next}`) ? ParsedRange.from<{
|
|
15
|
+
min: parseNonNegativeInteger<l>;
|
|
16
|
+
max: parseNonNegativeInteger<r>;
|
|
17
17
|
unscanned: skipPossibleQuestionMark<next>;
|
|
18
|
-
}> : unscanned extends `${infer l extends number},}${infer next}` ? ParsedRange.from<{
|
|
19
|
-
min: l
|
|
18
|
+
}> : unscanned extends `${infer l extends `${number}`},}${infer next}` ? ParsedRange.from<{
|
|
19
|
+
min: parseNonNegativeInteger<l>;
|
|
20
20
|
max: null;
|
|
21
21
|
unscanned: skipPossibleQuestionMark<next>;
|
|
22
|
-
}> : unscanned extends `${infer l extends number}}${infer next}` ? ParsedRange.from<{
|
|
23
|
-
min: l
|
|
24
|
-
max: l
|
|
22
|
+
}> : unscanned extends `${infer l extends `${number}`}}${infer next}` ? ParsedRange.from<{
|
|
23
|
+
min: parseNonNegativeInteger<l>;
|
|
24
|
+
max: parseNonNegativeInteger<l>;
|
|
25
25
|
unscanned: skipPossibleQuestionMark<next>;
|
|
26
26
|
}> : null;
|
|
27
|
-
|
|
27
|
+
type parseQuantifier<unscanned extends string, parsed extends ParsedRange> = unscanned extends `${infer range}${parsed["unscanned"]}` ? `{${range}` : never;
|
|
28
|
+
export type parsePossibleRange<s extends State, unscanned extends string, parsed extends ParsedRange | null = parsePossibleRangeString<unscanned>> = parsed extends ParsedRange ? s["root"] extends "" ? s.error<writeUnmatchedQuantifierError<parseQuantifier<unscanned, parsed>>> : [parsed["min"], parsed["max"]] extends ([
|
|
29
|
+
never,
|
|
30
|
+
unknown
|
|
31
|
+
] | [unknown, never]) ? s.error<writeUnnaturalNumberQuantifierError<parseQuantifier<unscanned, parsed>>> : s.pushQuantifier<s, parsed["min"], parsed["max"], parsed["unscanned"] extends Scanner.shift<"?", infer lazyUnscanned> ? lazyUnscanned : parsed["unscanned"]> : s.shiftQuantifiable<s, "{", unscanned>;
|
|
28
32
|
export type quantify<pattern extends string, min extends number, max extends number | null> = tryFastPath<pattern, min, max>;
|
|
29
33
|
type tryFastPath<pattern extends string, min extends number, max extends number | null> = max extends 0 ? "" : string extends pattern ? string : `${bigint}` extends pattern ? `${bigint}` : min extends 0 ? max extends 1 ? "" | pattern : max extends number ? loopFromZero<pattern, max, "", []> : // max is null, all we can do is append ${string}
|
|
30
34
|
"" | `${pattern}${string}` : loopUntilMin<pattern, min, max, "", []>;
|
|
@@ -34,4 +38,6 @@ type loopUntilMax<base extends string, min extends number, max extends number, a
|
|
|
34
38
|
export type QuantifyingChar = "*" | "+" | "?";
|
|
35
39
|
export declare const writeUnmatchedQuantifierError: <quantifier extends string>(quantifier: quantifier) => writeUnmatchedQuantifierError<quantifier>;
|
|
36
40
|
export type writeUnmatchedQuantifierError<quantifier extends string> = `Quantifier ${quantifier} requires a preceding token`;
|
|
41
|
+
export declare const writeUnnaturalNumberQuantifierError: <quantifier extends string>(quantifier: quantifier) => writeUnnaturalNumberQuantifierError<quantifier>;
|
|
42
|
+
export type writeUnnaturalNumberQuantifierError<quantifier extends string> = `Quantifier ${quantifier} must use natural numbers`;
|
|
37
43
|
export {};
|
package/out/quantify.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arkregex",
|
|
3
3
|
"description": "A drop-in replacement for new RegExp() with types",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"out"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ark/util": "0.
|
|
32
|
+
"@ark/util": "0.55.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|