@blackglory/match 0.3.6 → 0.4.1
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 +86 -36
- package/lib/index.d.ts +17 -17
- package/lib/index.js +17 -37
- package/lib/index.js.map +1 -1
- package/lib/match-element.d.ts +3 -4
- package/lib/match-element.js +9 -11
- package/lib/match-element.js.map +1 -1
- package/lib/match.d.ts +3 -4
- package/lib/match.js +9 -11
- package/lib/match.js.map +1 -1
- package/lib/matchers/any-of.d.ts +1 -1
- package/lib/matchers/any-of.js +1 -5
- package/lib/matchers/any-of.js.map +1 -1
- package/lib/matchers/child-nodes.d.ts +1 -1
- package/lib/matchers/child-nodes.js +10 -12
- package/lib/matchers/child-nodes.js.map +1 -1
- package/lib/matchers/children.d.ts +1 -1
- package/lib/matchers/children.js +10 -12
- package/lib/matchers/children.js.map +1 -1
- package/lib/matchers/css.d.ts +1 -1
- package/lib/matchers/css.js +5 -9
- package/lib/matchers/css.js.map +1 -1
- package/lib/matchers/element.d.ts +1 -1
- package/lib/matchers/element.js +11 -15
- package/lib/matchers/element.js.map +1 -1
- package/lib/matchers/multiple.d.ts +1 -1
- package/lib/matchers/multiple.js +14 -17
- package/lib/matchers/multiple.js.map +1 -1
- package/lib/matchers/node.d.ts +1 -1
- package/lib/matchers/node.js +9 -13
- package/lib/matchers/node.js.map +1 -1
- package/lib/matchers/optional.d.ts +1 -1
- package/lib/matchers/optional.js +3 -7
- package/lib/matchers/optional.js.map +1 -1
- package/lib/matchers/repeat.d.ts +1 -1
- package/lib/matchers/repeat.js +6 -10
- package/lib/matchers/repeat.js.map +1 -1
- package/lib/matchers/tap.d.ts +1 -1
- package/lib/matchers/tap.js +1 -5
- package/lib/matchers/tap.js.map +1 -1
- package/lib/matchers/text-content-equals.d.ts +1 -1
- package/lib/matchers/text-content-equals.js +3 -7
- package/lib/matchers/text-content-equals.js.map +1 -1
- package/lib/matchers/text-content-includes.d.ts +1 -1
- package/lib/matchers/text-content-includes.js +3 -7
- package/lib/matchers/text-content-includes.js.map +1 -1
- package/lib/matchers/text-content-matches.d.ts +1 -1
- package/lib/matchers/text-content-matches.js +3 -7
- package/lib/matchers/text-content-matches.js.map +1 -1
- package/lib/matchers/text-node.d.ts +1 -1
- package/lib/matchers/text-node.js +10 -14
- package/lib/matchers/text-node.js.map +1 -1
- package/lib/matchers/xpath.d.ts +1 -1
- package/lib/matchers/xpath.js +8 -12
- package/lib/matchers/xpath.js.map +1 -1
- package/lib/types.d.ts +7 -6
- package/lib/types.js +1 -2
- package/lib/utils/match-multiple.d.ts +4 -1
- package/lib/utils/match-multiple.js +6 -7
- package/lib/utils/match-multiple.js.map +1 -1
- package/lib/utils/match-one-by-one.d.ts +1 -1
- package/lib/utils/match-one-by-one.js +10 -10
- package/lib/utils/match-one-by-one.js.map +1 -1
- package/lib/utils/merge-in-place.d.ts +2 -5
- package/lib/utils/merge-in-place.js +5 -9
- package/lib/utils/merge-in-place.js.map +1 -1
- package/lib/utils/next-element-sibling.js +2 -6
- package/lib/utils/next-element-sibling.js.map +1 -1
- package/lib/utils/next-sibling.js +2 -6
- package/lib/utils/next-sibling.js.map +1 -1
- package/package.json +30 -29
- package/src/index.ts +17 -17
- package/src/match-element.ts +9 -5
- package/src/match.ts +9 -5
- package/src/matchers/any-of.ts +1 -1
- package/src/matchers/child-nodes.ts +6 -5
- package/src/matchers/children.ts +6 -5
- package/src/matchers/css.ts +4 -4
- package/src/matchers/element.ts +42 -17
- package/src/matchers/multiple.ts +3 -3
- package/src/matchers/node.ts +35 -13
- package/src/matchers/optional.ts +5 -3
- package/src/matchers/repeat.ts +3 -3
- package/src/matchers/tap.ts +1 -1
- package/src/matchers/text-content-equals.ts +2 -2
- package/src/matchers/text-content-includes.ts +2 -2
- package/src/matchers/text-content-matches.ts +2 -2
- package/src/matchers/text-node.ts +13 -9
- package/src/matchers/xpath.ts +10 -10
- package/src/types.ts +7 -4
- package/src/utils/match-multiple.ts +1 -1
- package/src/utils/match-one-by-one.ts +3 -3
- package/src/utils/merge-in-place.ts +3 -3
- package/lib/utils/concat.d.ts +0 -1
- package/lib/utils/concat.js +0 -14
- package/lib/utils/concat.js.map +0 -1
- package/src/utils/concat.ts +0 -9
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { INestedMatcher, ITerminalMatcher, IReadonlyContext } from '@src/types'
|
|
2
|
-
import { isArray, isString } from '@blackglory/
|
|
3
|
-
import { concat } from '
|
|
1
|
+
import { INestedMatcher, ITerminalMatcher, IReadonlyContext } from '@src/types.js'
|
|
2
|
+
import { isArray, isString } from '@blackglory/prelude'
|
|
3
|
+
import { concat } from 'extra-tags'
|
|
4
4
|
import { isntTextNode } from 'extra-dom'
|
|
5
|
-
import { mergeInPlace } from '@utils/merge-in-place'
|
|
5
|
+
import { mergeInPlace } from '@utils/merge-in-place.js'
|
|
6
6
|
|
|
7
7
|
export function textNode(
|
|
8
8
|
strings: TemplateStringsArray
|
|
@@ -21,16 +21,20 @@ export function textNode(...args:
|
|
|
21
21
|
| [...matchers: Array<ITerminalMatcher<Node>>]
|
|
22
22
|
) {
|
|
23
23
|
if (isArray(args[0])) {
|
|
24
|
-
const [strings, ...values] =
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
const [strings, ...values] = args as [
|
|
25
|
+
strings: TemplateStringsArray
|
|
26
|
+
, ...values: string[]
|
|
27
|
+
]
|
|
28
|
+
const name = concat(strings, ...values)
|
|
27
29
|
|
|
28
30
|
return (...matchers: Array<ITerminalMatcher<Node>>) => textNode(name, ...matchers)
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
if (isString(args[0])) {
|
|
32
|
-
const [name, ...matchers] =
|
|
33
|
-
|
|
34
|
+
const [name, ...matchers] = args as [
|
|
35
|
+
name: string
|
|
36
|
+
, ...matchers: Array<ITerminalMatcher<Node>>
|
|
37
|
+
]
|
|
34
38
|
|
|
35
39
|
return function (this: IReadonlyContext, node: Node) {
|
|
36
40
|
const result = textNode(...matchers).call(this, node)
|
package/src/matchers/xpath.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { ITerminalMatcher, IReadonlyContext } from '@src/types'
|
|
2
|
-
import { isString } from '@blackglory/
|
|
3
|
-
import { concat } from '
|
|
4
|
-
import { assert } from '@blackglory/
|
|
1
|
+
import { ITerminalMatcher, IReadonlyContext } from '@src/types.js'
|
|
2
|
+
import { isntNull, isString } from '@blackglory/prelude'
|
|
3
|
+
import { concat } from 'extra-tags'
|
|
4
|
+
import { assert } from '@blackglory/prelude'
|
|
5
5
|
|
|
6
6
|
const UNORDERED_NODE_ITERATOR_TYPE =
|
|
7
7
|
'XPathResult' in globalThis
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
? XPathResult.UNORDERED_NODE_ITERATOR_TYPE
|
|
9
|
+
: 4
|
|
10
10
|
|
|
11
11
|
export function xpath(
|
|
12
12
|
strings: TemplateStringsArray
|
|
@@ -21,7 +21,7 @@ export function xpath(...args:
|
|
|
21
21
|
): ITerminalMatcher<Node> {
|
|
22
22
|
if (isString(args[0])) {
|
|
23
23
|
const [expression] = args
|
|
24
|
-
assert(expression.startsWith('
|
|
24
|
+
assert(expression.startsWith('/'), 'XPath expressions must start with "/"')
|
|
25
25
|
|
|
26
26
|
return function (
|
|
27
27
|
this: Pick<IReadonlyContext, 'document'>
|
|
@@ -31,7 +31,7 @@ export function xpath(...args:
|
|
|
31
31
|
}
|
|
32
32
|
} else {
|
|
33
33
|
const [strings, ...values] = args
|
|
34
|
-
const expression = concat(strings, values)
|
|
34
|
+
const expression = concat(strings, ...values)
|
|
35
35
|
|
|
36
36
|
return xpath(expression)
|
|
37
37
|
}
|
|
@@ -46,8 +46,8 @@ function xpathMatches(document: Document, expression: string, node: Node): boole
|
|
|
46
46
|
, null
|
|
47
47
|
)
|
|
48
48
|
|
|
49
|
-
let value
|
|
50
|
-
while (
|
|
49
|
+
let value: Node | null
|
|
50
|
+
while (value = iterator.iterateNext(), isntNull(value)) {
|
|
51
51
|
if (value === node) return true
|
|
52
52
|
}
|
|
53
53
|
return false
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { Arrayable } from '@blackglory/prelude'
|
|
2
|
+
|
|
1
3
|
export interface IContext {
|
|
2
4
|
readonly document: Document
|
|
3
5
|
readonly next: (node: Node, distance?: number) => Node | undefined
|
|
4
6
|
readonly collection: {
|
|
5
|
-
[name: string]: Node
|
|
7
|
+
[name: string]: Arrayable<Node>
|
|
6
8
|
}
|
|
7
9
|
}
|
|
8
10
|
|
|
@@ -10,7 +12,7 @@ export interface IReadonlyContext {
|
|
|
10
12
|
readonly document: Document
|
|
11
13
|
readonly next: (node: Node, distance?: number) => Node | undefined
|
|
12
14
|
readonly collection: {
|
|
13
|
-
readonly [name: string]: Node
|
|
15
|
+
readonly [name: string]: Arrayable<Node>
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -19,8 +21,9 @@ export type IMatcher<T extends Node> =
|
|
|
19
21
|
| INestedMatcher<T>
|
|
20
22
|
| ISkipMatcher<T>
|
|
21
23
|
| (
|
|
22
|
-
<T extends Node>(
|
|
23
|
-
|
|
24
|
+
<T extends Node>(
|
|
25
|
+
this: IReadonlyContext, node: T
|
|
26
|
+
) => boolean | number | Iterable<number>
|
|
24
27
|
)
|
|
25
28
|
|
|
26
29
|
export type INestedMatcher<T extends Node> = (
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { countup } from 'extra-generator'
|
|
2
|
-
import { INestedMatcher, ITerminalMatcher, IReadonlyContext } from '@src/types'
|
|
2
|
+
import { INestedMatcher, ITerminalMatcher, IReadonlyContext } from '@src/types.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @returns {number} 返回值为成功匹配的元素个数, 当此值等于ubound时, 代表匹配成功.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { IMatcher, IReadonlyContext } from '@src/types'
|
|
2
|
-
import { isBoolean, isNumber, isIterable } from '@blackglory/
|
|
1
|
+
import { IMatcher, IReadonlyContext } from '@src/types.js'
|
|
2
|
+
import { isBoolean, isNumber, isIterable, isEmptyArray } from '@blackglory/prelude'
|
|
3
3
|
|
|
4
4
|
export function matchOneByOne<T extends Node>(
|
|
5
5
|
context: IReadonlyContext
|
|
6
6
|
, source: T | null
|
|
7
7
|
, ...matchers: Array<IMatcher<T>>
|
|
8
8
|
): boolean {
|
|
9
|
-
if (matchers
|
|
9
|
+
if (isEmptyArray(matchers)) return true
|
|
10
10
|
if (!source) return false
|
|
11
11
|
|
|
12
12
|
const [matcher, ...otherMatchers] = matchers
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isArray } from '@blackglory/
|
|
1
|
+
import { Arrayable, isArray } from '@blackglory/prelude'
|
|
2
2
|
|
|
3
3
|
export function mergeInPlace<T>(
|
|
4
|
-
target:
|
|
5
|
-
, source:
|
|
4
|
+
target: Record<string, Arrayable<T>>
|
|
5
|
+
, source: Record<string, Arrayable<T>>
|
|
6
6
|
): void {
|
|
7
7
|
for (const [key, value] of Object.entries(source)) {
|
|
8
8
|
if (target[key]) {
|
package/lib/utils/concat.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function concat(strings: TemplateStringsArray, values: string[]): string[];
|
package/lib/utils/concat.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.concat = void 0;
|
|
4
|
-
function concat(strings, values) {
|
|
5
|
-
const result = [];
|
|
6
|
-
for (let i = 0, len = values.length; i < len; i++) {
|
|
7
|
-
result.push(strings[i]);
|
|
8
|
-
result.push(values[i]);
|
|
9
|
-
}
|
|
10
|
-
result.push(strings[strings.length - 1]);
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
exports.concat = concat;
|
|
14
|
-
//# sourceMappingURL=concat.js.map
|
package/lib/utils/concat.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"concat.js","sourceRoot":"","sources":["../../src/utils/concat.ts"],"names":[],"mappings":";;;AAAA,SAAgB,MAAM,CAAC,OAA6B,EAAE,MAAgB;IACpE,MAAM,MAAM,GAAG,EAAE,CAAA;IACjB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QACjD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;QACvB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;KACvB;IACD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;IACxC,OAAO,MAAM,CAAA;AACf,CAAC;AARD,wBAQC"}
|
package/src/utils/concat.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export function concat(strings: TemplateStringsArray, values: string[]): string[] {
|
|
2
|
-
const result = []
|
|
3
|
-
for (let i = 0, len = values.length; i < len; i++) {
|
|
4
|
-
result.push(strings[i])
|
|
5
|
-
result.push(values[i])
|
|
6
|
-
}
|
|
7
|
-
result.push(strings[strings.length - 1])
|
|
8
|
-
return result
|
|
9
|
-
}
|