-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatches.js
60 lines (52 loc) · 1.26 KB
/
matches.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//@ts-check
/// <reference path="./types.d.ts" />
// import {Matcher} from './matcher.js';
import {matchAll} from './helpers.js';
//
// TODO: Align generic type of Matches and Matcher.matchAll
//
// <T extends MatcherMatch = MatcherExecArray | MatcherMatchArray>
//
export class Matches {
/** @param {string} text @param {Matcher} matcher */
constructor(text, matcher) {
this.text = text;
this.matcher = matcher;
}
get matches() {
/** @type {Readonly<ArrayLike<MatcherMatch>>} */
const value = Object.freeze(
Object.setPrototypeOf(
[
//@ts-ignore
...matchAll(this.text, this.matcher),
],
null,
),
);
Reflect.defineProperty(this, 'matches', {value, writable: false});
return value;
}
get length() {
return this.matches.length;
}
//@ts-ignore
/** @returns {IterableIterator<T>} */
[Symbol.iterator]() {}
}
{
const ArrayPrototypeIterator = Function.call.bind(Array.prototype[Symbol.iterator]);
Reflect.defineProperty(
Matches.prototype,
Symbol.iterator,
Reflect.getOwnPropertyDescriptor(
{
[Symbol.iterator]() {
//@ts-ignore
return ArrayPrototypeIterator(this.matches);
},
},
Symbol.iterator,
),
);
}