regex-partial-match 1.3.0 → 2.0.0
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 +120 -267
- package/lib/core/corePartialMatchRegExp.d.ts +24 -0
- package/lib/core/corePartialMatchRegExp.js +22 -0
- package/lib/core/index.d.ts +4 -0
- package/lib/core/index.js +2 -0
- package/lib/extend/index.d.ts +6 -7
- package/lib/extend/index.js +1 -1
- package/lib/features/index.d.ts +20 -0
- package/lib/features/index.js +23 -0
- package/lib/hitEnd/index.d.ts +45 -0
- package/lib/hitEnd/index.js +57 -0
- package/lib/hitEnd/matchHitEnd.d.ts +4 -0
- package/lib/hitEnd/matchHitEnd.js +51 -0
- package/lib/{partialMatchRegExp/isComplete → hitEnd}/partRole.d.ts +1 -1
- package/lib/hitEnd/partRole.js +17 -0
- package/lib/hitEnd/truncationProbe.d.ts +8 -0
- package/lib/hitEnd/truncationProbe.js +187 -0
- package/lib/hitEnd/truncationProbeCache.d.ts +14 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/modules/backreferences/asNativeAtom.d.ts +2 -0
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/asNativeAtom.js +2 -2
- package/lib/modules/backreferences/asPreScanPart.d.ts +2 -0
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/asPreScanPart.js +2 -2
- package/lib/modules/backreferences/backreferenceRecorder.d.ts +2 -0
- package/lib/modules/backreferences/backreferenceRecorder.js +38 -0
- package/lib/modules/backreferences/compileDynamic.d.ts +4 -0
- package/lib/modules/backreferences/compileDynamic.js +53 -0
- package/lib/modules/backreferences/compiledBackreferences.d.ts +9 -0
- package/lib/modules/backreferences/compiledBackreferences.js +9 -0
- package/lib/modules/backreferences/constants.d.ts +2 -0
- package/lib/modules/backreferences/constants.js +2 -0
- package/lib/modules/backreferences/execDynamic.d.ts +2 -0
- package/lib/modules/backreferences/execDynamic.js +43 -0
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/flagsAtBackreference.d.ts +1 -1
- package/lib/{partialMatchRegExp → modules/backreferences}/groupName.js +1 -1
- package/lib/modules/backreferences/index.d.ts +10 -0
- package/lib/modules/backreferences/index.js +20 -0
- package/lib/modules/backreferences/longestBakedPrefixEndingInput.js +46 -0
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/renderParts.d.ts +1 -1
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/renderParts.js +1 -1
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/resolvedFromScan.d.ts +1 -1
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/resolvedFromScan.js +2 -2
- package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/startsWithUnderFlags.js +1 -1
- package/lib/modules/carets/appendMultilineCaret.d.ts +7 -0
- package/lib/modules/carets/appendMultilineCaret.js +181 -0
- package/lib/modules/carets/caretRecorder.d.ts +2 -0
- package/lib/modules/carets/caretRecorder.js +164 -0
- package/lib/modules/carets/caretSyntax.d.ts +3 -0
- package/lib/modules/carets/caretSyntax.js +11 -0
- package/lib/modules/carets/index.d.ts +9 -0
- package/lib/modules/carets/index.js +18 -0
- package/lib/modules/carets/lineTerminator.d.ts +1 -0
- package/lib/modules/carets/lineTerminator.js +31 -0
- package/lib/modules/carets/needsCaretRules.d.ts +1 -0
- package/lib/modules/carets/needsCaretRules.js +15 -0
- package/lib/modules/carets/quantifierBounds.d.ts +3 -0
- package/lib/modules/carets/quantifierBounds.js +18 -0
- package/lib/modules/index.d.ts +3 -0
- package/lib/modules/index.js +2 -0
- package/lib/partialMatchRegExp/atomSyntax.d.ts +14 -1
- package/lib/partialMatchRegExp/atomSyntax.js +24 -1
- package/lib/partialMatchRegExp/backreferenceExpansion.d.ts +1 -6
- package/lib/partialMatchRegExp/backreferenceHook.d.ts +11 -0
- package/lib/partialMatchRegExp/backreferenceHook.js +1 -0
- package/lib/partialMatchRegExp/caretHook.d.ts +17 -0
- package/lib/partialMatchRegExp/caretHook.js +5 -0
- package/lib/partialMatchRegExp/compilePartial/compileWith.d.ts +3 -0
- package/lib/partialMatchRegExp/compilePartial/compileWith.js +36 -0
- package/lib/partialMatchRegExp/compilePartial/compiled.d.ts +17 -17
- package/lib/partialMatchRegExp/compilePartial/compiled.js +8 -21
- package/lib/partialMatchRegExp/compilePartial/constants.d.ts +4 -0
- package/lib/partialMatchRegExp/compilePartial/constants.js +4 -0
- package/lib/partialMatchRegExp/compilePartial/groupShape.d.ts +4 -0
- package/lib/partialMatchRegExp/compilePartial/groupShape.js +9 -0
- package/lib/partialMatchRegExp/compilePartial/toStatic.d.ts +2 -2
- package/lib/partialMatchRegExp/compilePartial/toStatic.js +2 -2
- package/lib/partialMatchRegExp/constants.d.ts +5 -0
- package/lib/partialMatchRegExp/constants.js +5 -0
- package/lib/partialMatchRegExp/createPartialMatchRegExp.d.ts +3 -0
- package/lib/partialMatchRegExp/createPartialMatchRegExp.js +21 -0
- package/lib/partialMatchRegExp/execFrom.d.ts +1 -0
- package/lib/partialMatchRegExp/execFrom.js +4 -0
- package/lib/partialMatchRegExp/index.d.ts +49 -3
- package/lib/partialMatchRegExp/index.js +31 -3
- package/lib/partialMatchRegExp/moduleHooks.d.ts +14 -0
- package/lib/partialMatchRegExp/moduleHooks.js +15 -0
- package/lib/partialMatchRegExp/opaqueModule.d.ts +9 -0
- package/lib/partialMatchRegExp/opaqueModule.js +1 -0
- package/lib/partialMatchRegExp/part.d.ts +2 -11
- package/lib/partialMatchRegExp/partialMatchInternals.d.ts +2 -1
- package/lib/partialMatchRegExp/partialMatchInternals.js +3 -1
- package/lib/partialMatchRegExp/quantifier.d.ts +6 -0
- package/lib/partialMatchRegExp/quantifier.js +9 -0
- package/lib/partialMatchRegExp/rawLookaroundInfo.d.ts +11 -0
- package/lib/partialMatchRegExp/rawLookaroundInfo.js +1 -0
- package/lib/partialMatchRegExp/regexFeatures.d.ts +1 -5
- package/lib/partialMatchRegExp/regexFeatures.js +2 -8
- package/lib/partialMatchRegExp/scope.d.ts +12 -0
- package/lib/partialMatchRegExp/scope.js +45 -0
- package/lib/partialMatchRegExp/walk.d.ts +13 -5
- package/lib/partialMatchRegExp/walk.js +211 -165
- package/lib/partialMatchRegExp/withModules.d.ts +13 -0
- package/lib/partialMatchRegExp/withModules.js +27 -0
- package/package.json +39 -56
- package/lib/partialMatchRegExp/compilePartial/asNativeAtom.d.ts +0 -2
- package/lib/partialMatchRegExp/compilePartial/asOptionalAtom.d.ts +0 -1
- package/lib/partialMatchRegExp/compilePartial/asOptionalAtom.js +0 -4
- package/lib/partialMatchRegExp/compilePartial/asPreScanPart.d.ts +0 -2
- package/lib/partialMatchRegExp/compilePartial/index.d.ts +0 -2
- package/lib/partialMatchRegExp/compilePartial/index.js +0 -79
- package/lib/partialMatchRegExp/compilePartial/longestBakedPrefixEndingInput.js +0 -50
- package/lib/partialMatchRegExp/compilePartial/reclassifyLegacyEscapes.d.ts +0 -2
- package/lib/partialMatchRegExp/compilePartial/reclassifyLegacyEscapes.js +0 -17
- package/lib/partialMatchRegExp/compilePartial/spliceOriginalSource.d.ts +0 -2
- package/lib/partialMatchRegExp/compilePartial/spliceOriginalSource.js +0 -12
- package/lib/partialMatchRegExp/isComplete/index.d.ts +0 -38
- package/lib/partialMatchRegExp/isComplete/index.js +0 -41
- package/lib/partialMatchRegExp/isComplete/isMatchComplete.d.ts +0 -3
- package/lib/partialMatchRegExp/isComplete/isMatchComplete.js +0 -14
- package/lib/partialMatchRegExp/isComplete/partRole.js +0 -28
- package/lib/partialMatchRegExp/isComplete/truncationProbe.d.ts +0 -8
- package/lib/partialMatchRegExp/isComplete/truncationProbe.js +0 -105
- package/lib/partialMatchRegExp/isComplete/truncationProbeCache.d.ts +0 -4
- package/lib/partialMatchRegExp/partialMatchRegExp.d.ts +0 -55
- package/lib/partialMatchRegExp/partialMatchRegExp.js +0 -114
- package/lib/partialMatchRegExp/preferLongerCaptures.d.ts +0 -1
- package/lib/partialMatchRegExp/preferLongerCaptures.js +0 -30
- /package/lib/{partialMatchRegExp/isComplete → hitEnd}/truncationProbeCache.js +0 -0
- /package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/caseFoldFlags.d.ts +0 -0
- /package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/caseFoldFlags.js +0 -0
- /package/lib/{partialMatchRegExp → modules/backreferences}/escapeAtom.d.ts +0 -0
- /package/lib/{partialMatchRegExp → modules/backreferences}/escapeAtom.js +0 -0
- /package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/flagsAtBackreference.js +0 -0
- /package/lib/{partialMatchRegExp → modules/backreferences}/groupName.d.ts +0 -0
- /package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/longestBakedPrefixEndingInput.d.ts +0 -0
- /package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/startsWithUnderFlags.d.ts +0 -0
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { CompiledPartial } from "./compilePartial/compiled.ts";
|
|
2
|
-
import { compiledPartial, truncationProbeCache } from "./partialMatchInternals.ts";
|
|
3
|
-
import type { TruncationProbeCache } from "./isComplete/truncationProbeCache.ts";
|
|
4
|
-
import type { RegexFeature } from "./regexFeatures.ts";
|
|
5
|
-
export type { RegexFeature };
|
|
6
|
-
/**
|
|
7
|
-
* A `RegExp` subclass that supports partial (prefix) matching.
|
|
8
|
-
*
|
|
9
|
-
* An instance behaves like a normal `RegExp` but also matches any input string
|
|
10
|
-
* that is a valid prefix of the original pattern — i.e. strings that could still
|
|
11
|
-
* lead to a full match with more input. This enables validation of incomplete
|
|
12
|
-
* input strings.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const pattern = /^hello world/;
|
|
17
|
-
* const partial = new PartialMatchRegExp(pattern);
|
|
18
|
-
*
|
|
19
|
-
* partial.test('h'); // true - could match
|
|
20
|
-
* partial.test('hello'); // true - could match
|
|
21
|
-
* partial.test('hello world'); // true - full match
|
|
22
|
-
* partial.test('goodbye'); // false - cannot match
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* - The transformed pattern always matches an empty string at the end of input;
|
|
27
|
-
* use a start anchor (`^`) to prevent false positives from empty string matches
|
|
28
|
-
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
29
|
-
*
|
|
30
|
-
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
31
|
-
*/
|
|
32
|
-
declare class PartialMatchRegExp extends RegExp {
|
|
33
|
-
[compiledPartial]: CompiledPartial;
|
|
34
|
-
[truncationProbeCache]: TruncationProbeCache;
|
|
35
|
-
constructor(pattern: RegExp | string, flags?: string);
|
|
36
|
-
/**
|
|
37
|
-
* The syntactic constructs the original pattern uses, recorded as a side
|
|
38
|
-
* effect of the single walk that builds the partial-match regex.
|
|
39
|
-
*
|
|
40
|
-
* The set is built on first read and cached, so patterns that are only ever
|
|
41
|
-
* matched against never pay for it. It iterates in `RegexFeature` declaration
|
|
42
|
-
* order, not the order the constructs appear in the pattern.
|
|
43
|
-
*
|
|
44
|
-
* @returns The features the original pattern contains
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```typescript
|
|
48
|
-
* new PartialMatchRegExp(/^[a-z]+/).features.has("startAnchor"); // true
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
get features(): ReadonlySet<RegexFeature>;
|
|
52
|
-
exec(input: string): RegExpExecArray | null;
|
|
53
|
-
private _execDynamic;
|
|
54
|
-
}
|
|
55
|
-
export default PartialMatchRegExp;
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import compilePartial from "./compilePartial/index.js";
|
|
2
|
-
import renderParts from "./compilePartial/renderParts.js";
|
|
3
|
-
import { backreferenceExpansion } from "./backreferenceExpansion.js";
|
|
4
|
-
import { compiledPartial, truncationProbeCache } from "./partialMatchInternals.js";
|
|
5
|
-
import { preferLongerCaptures } from "./preferLongerCaptures.js";
|
|
6
|
-
/**
|
|
7
|
-
* A `RegExp` subclass that supports partial (prefix) matching.
|
|
8
|
-
*
|
|
9
|
-
* An instance behaves like a normal `RegExp` but also matches any input string
|
|
10
|
-
* that is a valid prefix of the original pattern — i.e. strings that could still
|
|
11
|
-
* lead to a full match with more input. This enables validation of incomplete
|
|
12
|
-
* input strings.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const pattern = /^hello world/;
|
|
17
|
-
* const partial = new PartialMatchRegExp(pattern);
|
|
18
|
-
*
|
|
19
|
-
* partial.test('h'); // true - could match
|
|
20
|
-
* partial.test('hello'); // true - could match
|
|
21
|
-
* partial.test('hello world'); // true - full match
|
|
22
|
-
* partial.test('goodbye'); // false - cannot match
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* - The transformed pattern always matches an empty string at the end of input;
|
|
27
|
-
* use a start anchor (`^`) to prevent false positives from empty string matches
|
|
28
|
-
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
29
|
-
*
|
|
30
|
-
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
31
|
-
*/
|
|
32
|
-
class PartialMatchRegExp extends RegExp {
|
|
33
|
-
constructor(pattern, flags) {
|
|
34
|
-
super(pattern, flags);
|
|
35
|
-
this[compiledPartial] = compilePartial(this);
|
|
36
|
-
this[truncationProbeCache] = { probe: undefined };
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* The syntactic constructs the original pattern uses, recorded as a side
|
|
40
|
-
* effect of the single walk that builds the partial-match regex.
|
|
41
|
-
*
|
|
42
|
-
* The set is built on first read and cached, so patterns that are only ever
|
|
43
|
-
* matched against never pay for it. It iterates in `RegexFeature` declaration
|
|
44
|
-
* order, not the order the constructs appear in the pattern.
|
|
45
|
-
*
|
|
46
|
-
* @returns The features the original pattern contains
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* new PartialMatchRegExp(/^[a-z]+/).features.has("startAnchor"); // true
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
get features() {
|
|
54
|
-
return this[compiledPartial].features;
|
|
55
|
-
}
|
|
56
|
-
exec(input) {
|
|
57
|
-
const compiled = this[compiledPartial];
|
|
58
|
-
if (compiled.kind === "dynamic")
|
|
59
|
-
return this._execDynamic(compiled.dynamic, input);
|
|
60
|
-
const { regex } = compiled;
|
|
61
|
-
const match = execFrom(regex, input, this.lastIndex);
|
|
62
|
-
this.lastIndex = regex.lastIndex;
|
|
63
|
-
return match;
|
|
64
|
-
}
|
|
65
|
-
_execDynamic(dynamic, input) {
|
|
66
|
-
var _a, _b;
|
|
67
|
-
const { originalCaptureScan, preScan, expand, expansionFitsCaptures } = dynamic;
|
|
68
|
-
const honoursLastIndex = this.global || this.sticky;
|
|
69
|
-
const start = honoursLastIndex ? this.lastIndex : 0;
|
|
70
|
-
const originalMatch = super.exec(input);
|
|
71
|
-
if (isAtOrBefore(originalMatch, start))
|
|
72
|
-
return originalMatch;
|
|
73
|
-
let preScanMatch = null;
|
|
74
|
-
if (originalMatch) {
|
|
75
|
-
preScanMatch = execFrom(preScan, input, start);
|
|
76
|
-
const noEarlierPartialPossible = preScanMatch === null || preScanMatch.index >= originalMatch.index;
|
|
77
|
-
if (noEarlierPartialPossible)
|
|
78
|
-
return originalMatch;
|
|
79
|
-
}
|
|
80
|
-
const capture = (_b = (_a = execFrom(originalCaptureScan, input, start)) !== null && _a !== void 0 ? _a : preScanMatch) !== null && _b !== void 0 ? _b : execFrom(preScan, input, start);
|
|
81
|
-
if (capture === null)
|
|
82
|
-
return originalMatch;
|
|
83
|
-
let expandedFrom = capture;
|
|
84
|
-
let expandedParts = expand(expandedFrom);
|
|
85
|
-
let expanded = new RegExp(renderParts(expandedParts), this.flags);
|
|
86
|
-
let match = execFrom(expanded, input, start);
|
|
87
|
-
if (match !== null && !expansionFitsCaptures(expandedFrom, match, input)) {
|
|
88
|
-
expandedFrom = match;
|
|
89
|
-
expandedParts = expand(expandedFrom);
|
|
90
|
-
expanded = new RegExp(renderParts(expandedParts), this.flags);
|
|
91
|
-
match = execFrom(expanded, input, start);
|
|
92
|
-
if (match !== null && !expansionFitsCaptures(expandedFrom, match, input))
|
|
93
|
-
match = null;
|
|
94
|
-
}
|
|
95
|
-
if (match === null || isAtOrBefore(originalMatch, match.index))
|
|
96
|
-
return originalMatch;
|
|
97
|
-
preferLongerCaptures(match, capture);
|
|
98
|
-
if (honoursLastIndex)
|
|
99
|
-
this.lastIndex = expanded.lastIndex;
|
|
100
|
-
match[backreferenceExpansion] = {
|
|
101
|
-
parts: expandedParts,
|
|
102
|
-
probe: undefined
|
|
103
|
-
};
|
|
104
|
-
return match;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function execFrom(regex, input, start) {
|
|
108
|
-
regex.lastIndex = start;
|
|
109
|
-
return regex.exec(input);
|
|
110
|
-
}
|
|
111
|
-
function isAtOrBefore(match, index) {
|
|
112
|
-
return match !== null && match.index <= index;
|
|
113
|
-
}
|
|
114
|
-
export default PartialMatchRegExp;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function preferLongerCaptures(match: RegExpExecArray, scanned: RegExpExecArray): void;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
function pickLonger(scanned, matched) {
|
|
2
|
-
return scanned !== undefined &&
|
|
3
|
-
matched !== undefined &&
|
|
4
|
-
scanned.length > matched.length
|
|
5
|
-
? scanned
|
|
6
|
-
: undefined;
|
|
7
|
-
}
|
|
8
|
-
export function preferLongerCaptures(match, scanned) {
|
|
9
|
-
var _a, _b, _c, _d;
|
|
10
|
-
for (let index = 1; index < match.length; index++) {
|
|
11
|
-
const longer = pickLonger(scanned[index], match[index]);
|
|
12
|
-
if (longer === undefined)
|
|
13
|
-
continue;
|
|
14
|
-
match[index] = longer;
|
|
15
|
-
if (match.indices && ((_a = scanned.indices) === null || _a === void 0 ? void 0 : _a[index])) {
|
|
16
|
-
match.indices[index] = scanned.indices[index];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
if (match.groups && scanned.groups) {
|
|
20
|
-
for (const name of Object.keys(match.groups)) {
|
|
21
|
-
const longer = pickLonger(scanned.groups[name], match.groups[name]);
|
|
22
|
-
if (longer === undefined)
|
|
23
|
-
continue;
|
|
24
|
-
match.groups[name] = longer;
|
|
25
|
-
if ((_b = match.indices) === null || _b === void 0 ? void 0 : _b.groups) {
|
|
26
|
-
match.indices.groups[name] = (_d = (_c = scanned.indices) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d[name];
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
File without changes
|
/package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/caseFoldFlags.d.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/flagsAtBackreference.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/lib/{partialMatchRegExp/compilePartial → modules/backreferences}/startsWithUnderFlags.d.ts
RENAMED
|
File without changes
|