arkregex 0.0.2 → 0.0.4
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 +17 -11
- package/out/quantify.js +1 -0
- package/out/regex.d.ts +5 -4
- package/out/state.d.ts +2 -2
- 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,22 +11,26 @@ 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
|
-
type tryFastPath<pattern extends string, min extends number, max extends number | null> = max extends 0 ? "" : string extends pattern ? string :
|
|
33
|
+
type tryFastPath<pattern extends string, min extends number, max extends number | null> = max extends 0 ? "" : string extends pattern ? string : 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, "", []>;
|
|
31
35
|
type loopFromZero<base extends string, max extends number, acc extends string, repetitions extends 1[]> = repetitions["length"] extends max ? acc : loopFromZero<base, max, acc | `${acc}${base}`, [...repetitions, 1]>;
|
|
32
36
|
type loopUntilMin<base extends string, min extends number, max extends number | null, acc extends string, repetitions extends 1[]> = repetitions["length"] extends min ? max extends number ? loopUntilMax<base, min, max, acc, repetitions> : repetitions["length"] extends 0 ? acc | `${acc}${base}${string}` : `${acc}${string}` : loopUntilMin<base, min, max, `${acc}${base}`, [...repetitions, 1]>;
|
|
@@ -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/out/regex.d.ts
CHANGED
|
@@ -16,12 +16,13 @@ export interface Regex<out pattern extends string = string, out ctx extends Rege
|
|
|
16
16
|
infer: pattern;
|
|
17
17
|
inferCaptures: ctx["captures"] extends IndexedCaptures ? ctx["captures"] : [];
|
|
18
18
|
inferNamedCaptures: ctx["names"] extends NamedCaptures ? ctx["names"] : {};
|
|
19
|
-
|
|
20
|
-
test(s: string): s is pattern;
|
|
21
|
-
exec(s: string): RegexExecArray<[
|
|
19
|
+
inferExecArray: RegexExecArray<[
|
|
22
20
|
pattern,
|
|
23
21
|
...this["inferCaptures"]
|
|
24
|
-
], this["inferNamedCaptures"], this["flags"]
|
|
22
|
+
], this["inferNamedCaptures"], this["flags"]>;
|
|
23
|
+
flags: ctx["flags"] extends Flags ? ctx["flags"] : "";
|
|
24
|
+
test(s: string): s is pattern;
|
|
25
|
+
exec(s: string): this["inferExecArray"] | null;
|
|
25
26
|
exec(s: string): never;
|
|
26
27
|
}
|
|
27
28
|
export interface RegexParser {
|
package/out/state.d.ts
CHANGED
|
@@ -352,8 +352,8 @@ export type finalizeTree<tree, ctx extends FinalizationContext> = tree extends s
|
|
|
352
352
|
}> : tree extends SequenceTree ? SequenceTree.finalize<tree, ctx> : tree extends UnionTree ? UnionTree.finalize<tree, ctx> : tree extends GroupTree ? GroupTree.finalize<tree, ctx> : tree extends QuantifierTree ? QuantifierTree.finalize<tree, ctx> : tree extends ReferenceNode ? ReferenceNode.finalize<tree, ctx> : never;
|
|
353
353
|
type applyAnchors<pattern extends string> = pattern extends `${StartAnchorMarker}${infer startStripped}` ? startStripped extends `${infer bothStripped}${EndAnchorMarker}` ? bothStripped : appendNonRedundant<startStripped, string> : pattern extends `${infer endStripped}${EndAnchorMarker}` ? prependNonRedundant<endStripped, string> : prependNonRedundant<appendNonRedundant<pattern, string>, string>;
|
|
354
354
|
type anchorsAway<pattern extends string> = pattern extends `${StartAnchorMarker}${infer startStripped}` ? startStripped extends `${infer bothStripped}${EndAnchorMarker}` ? bothStripped : startStripped : pattern extends `${infer endStripped}${EndAnchorMarker}` ? endStripped : pattern;
|
|
355
|
-
type appendNonRedundant<base extends string, suffix extends string> = string extends base ? string extends suffix ? string : `${base}${suffix}` : `${
|
|
356
|
-
type prependNonRedundant<base extends string, prefix extends string> = string extends base ? string extends prefix ? string : `${prefix}${base}` : `${
|
|
355
|
+
type appendNonRedundant<base extends string, suffix extends string> = string extends base ? string extends suffix ? string : `${base}${suffix}` : `${base}${suffix}`;
|
|
356
|
+
type prependNonRedundant<base extends string, prefix extends string> = string extends base ? string extends prefix ? string : `${prefix}${base}` : `${prefix}${base}`;
|
|
357
357
|
export declare const writeMidAnchorError: <anchor extends Anchor>(anchor: anchor) => writeMidAnchorError<anchor>;
|
|
358
358
|
type writeMidAnchorError<anchor extends Anchor> = `Anchor ${anchor} may not appear mid-pattern`;
|
|
359
359
|
export {};
|
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.4",
|
|
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.56.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|