@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.
Files changed (97) hide show
  1. package/README.md +86 -36
  2. package/lib/index.d.ts +17 -17
  3. package/lib/index.js +17 -37
  4. package/lib/index.js.map +1 -1
  5. package/lib/match-element.d.ts +3 -4
  6. package/lib/match-element.js +9 -11
  7. package/lib/match-element.js.map +1 -1
  8. package/lib/match.d.ts +3 -4
  9. package/lib/match.js +9 -11
  10. package/lib/match.js.map +1 -1
  11. package/lib/matchers/any-of.d.ts +1 -1
  12. package/lib/matchers/any-of.js +1 -5
  13. package/lib/matchers/any-of.js.map +1 -1
  14. package/lib/matchers/child-nodes.d.ts +1 -1
  15. package/lib/matchers/child-nodes.js +10 -12
  16. package/lib/matchers/child-nodes.js.map +1 -1
  17. package/lib/matchers/children.d.ts +1 -1
  18. package/lib/matchers/children.js +10 -12
  19. package/lib/matchers/children.js.map +1 -1
  20. package/lib/matchers/css.d.ts +1 -1
  21. package/lib/matchers/css.js +5 -9
  22. package/lib/matchers/css.js.map +1 -1
  23. package/lib/matchers/element.d.ts +1 -1
  24. package/lib/matchers/element.js +11 -15
  25. package/lib/matchers/element.js.map +1 -1
  26. package/lib/matchers/multiple.d.ts +1 -1
  27. package/lib/matchers/multiple.js +14 -17
  28. package/lib/matchers/multiple.js.map +1 -1
  29. package/lib/matchers/node.d.ts +1 -1
  30. package/lib/matchers/node.js +9 -13
  31. package/lib/matchers/node.js.map +1 -1
  32. package/lib/matchers/optional.d.ts +1 -1
  33. package/lib/matchers/optional.js +3 -7
  34. package/lib/matchers/optional.js.map +1 -1
  35. package/lib/matchers/repeat.d.ts +1 -1
  36. package/lib/matchers/repeat.js +6 -10
  37. package/lib/matchers/repeat.js.map +1 -1
  38. package/lib/matchers/tap.d.ts +1 -1
  39. package/lib/matchers/tap.js +1 -5
  40. package/lib/matchers/tap.js.map +1 -1
  41. package/lib/matchers/text-content-equals.d.ts +1 -1
  42. package/lib/matchers/text-content-equals.js +3 -7
  43. package/lib/matchers/text-content-equals.js.map +1 -1
  44. package/lib/matchers/text-content-includes.d.ts +1 -1
  45. package/lib/matchers/text-content-includes.js +3 -7
  46. package/lib/matchers/text-content-includes.js.map +1 -1
  47. package/lib/matchers/text-content-matches.d.ts +1 -1
  48. package/lib/matchers/text-content-matches.js +3 -7
  49. package/lib/matchers/text-content-matches.js.map +1 -1
  50. package/lib/matchers/text-node.d.ts +1 -1
  51. package/lib/matchers/text-node.js +10 -14
  52. package/lib/matchers/text-node.js.map +1 -1
  53. package/lib/matchers/xpath.d.ts +1 -1
  54. package/lib/matchers/xpath.js +8 -12
  55. package/lib/matchers/xpath.js.map +1 -1
  56. package/lib/types.d.ts +7 -6
  57. package/lib/types.js +1 -2
  58. package/lib/utils/match-multiple.d.ts +4 -1
  59. package/lib/utils/match-multiple.js +6 -7
  60. package/lib/utils/match-multiple.js.map +1 -1
  61. package/lib/utils/match-one-by-one.d.ts +1 -1
  62. package/lib/utils/match-one-by-one.js +10 -10
  63. package/lib/utils/match-one-by-one.js.map +1 -1
  64. package/lib/utils/merge-in-place.d.ts +2 -5
  65. package/lib/utils/merge-in-place.js +5 -9
  66. package/lib/utils/merge-in-place.js.map +1 -1
  67. package/lib/utils/next-element-sibling.js +2 -6
  68. package/lib/utils/next-element-sibling.js.map +1 -1
  69. package/lib/utils/next-sibling.js +2 -6
  70. package/lib/utils/next-sibling.js.map +1 -1
  71. package/package.json +30 -29
  72. package/src/index.ts +17 -17
  73. package/src/match-element.ts +9 -5
  74. package/src/match.ts +9 -5
  75. package/src/matchers/any-of.ts +1 -1
  76. package/src/matchers/child-nodes.ts +6 -5
  77. package/src/matchers/children.ts +6 -5
  78. package/src/matchers/css.ts +4 -4
  79. package/src/matchers/element.ts +42 -17
  80. package/src/matchers/multiple.ts +3 -3
  81. package/src/matchers/node.ts +35 -13
  82. package/src/matchers/optional.ts +5 -3
  83. package/src/matchers/repeat.ts +3 -3
  84. package/src/matchers/tap.ts +1 -1
  85. package/src/matchers/text-content-equals.ts +2 -2
  86. package/src/matchers/text-content-includes.ts +2 -2
  87. package/src/matchers/text-content-matches.ts +2 -2
  88. package/src/matchers/text-node.ts +13 -9
  89. package/src/matchers/xpath.ts +10 -10
  90. package/src/types.ts +7 -4
  91. package/src/utils/match-multiple.ts +1 -1
  92. package/src/utils/match-one-by-one.ts +3 -3
  93. package/src/utils/merge-in-place.ts +3 -3
  94. package/lib/utils/concat.d.ts +0 -1
  95. package/lib/utils/concat.js +0 -14
  96. package/lib/utils/concat.js.map +0 -1
  97. 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/types'
3
- import { concat } from '@utils/concat'
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
- args as [strings: TemplateStringsArray, ...values: string[]]
26
- const name = concat(strings, values).join('')
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
- args as [name: string, ...matchers: Array<ITerminalMatcher<Node>>]
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)
@@ -1,12 +1,12 @@
1
- import { ITerminalMatcher, IReadonlyContext } from '@src/types'
2
- import { isString } from '@blackglory/types'
3
- import { concat } from '@utils/concat'
4
- import { assert } from '@blackglory/errors'
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
- ? XPathResult.UNORDERED_NODE_ITERATOR_TYPE
9
- : 4
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('//*'), 'XPath expressions must start with "//*"')
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).join('')
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 ((value = iterator.iterateNext()) !== null) {
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 | 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 | 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>(this: IReadonlyContext, node: T) =>
23
- boolean | number | Iterable<number>
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/types'
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.length === 0) return true
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/types'
1
+ import { Arrayable, isArray } from '@blackglory/prelude'
2
2
 
3
3
  export function mergeInPlace<T>(
4
- target: { [key: string]: T | T[] }
5
- , source: { [key: string]: T | T[] }
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]) {
@@ -1 +0,0 @@
1
- export declare function concat(strings: TemplateStringsArray, values: string[]): string[];
@@ -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
@@ -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"}
@@ -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
- }