index.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @typedef {import('unist').Node} Node
  3. * @typedef {import('unist').Parent} Parent
  4. */
  5. /**
  6. * @template Fn
  7. * @template Fallback
  8. * @typedef {Fn extends (value: any) => value is infer Thing ? Thing : Fallback} Predicate
  9. */
  10. /**
  11. * @callback Check
  12. * Check that an arbitrary value is a node.
  13. * @param {unknown} this
  14. * The given context.
  15. * @param {unknown} [node]
  16. * Anything (typically a node).
  17. * @param {number | null | undefined} [index]
  18. * The node’s position in its parent.
  19. * @param {Parent | null | undefined} [parent]
  20. * The node’s parent.
  21. * @returns {boolean}
  22. * Whether this is a node and passes a test.
  23. *
  24. * @typedef {Record<string, unknown> | Node} Props
  25. * Object to check for equivalence.
  26. *
  27. * Note: `Node` is included as it is common but is not indexable.
  28. *
  29. * @typedef {Array<Props | TestFunction | string> | Props | TestFunction | string | null | undefined} Test
  30. * Check for an arbitrary node.
  31. *
  32. * @callback TestFunction
  33. * Check if a node passes a test.
  34. * @param {unknown} this
  35. * The given context.
  36. * @param {Node} node
  37. * A node.
  38. * @param {number | undefined} [index]
  39. * The node’s position in its parent.
  40. * @param {Parent | undefined} [parent]
  41. * The node’s parent.
  42. * @returns {boolean | undefined | void}
  43. * Whether this node passes the test.
  44. *
  45. * Note: `void` is included until TS sees no return as `undefined`.
  46. */
  47. /**
  48. * Check if `node` is a `Node` and whether it passes the given test.
  49. *
  50. * @param {unknown} node
  51. * Thing to check, typically `Node`.
  52. * @param {Test} test
  53. * A check for a specific node.
  54. * @param {number | null | undefined} index
  55. * The node’s position in its parent.
  56. * @param {Parent | null | undefined} parent
  57. * The node’s parent.
  58. * @param {unknown} context
  59. * Context object (`this`) to pass to `test` functions.
  60. * @returns {boolean}
  61. * Whether `node` is a node and passes a test.
  62. */
  63. export const is: (<Condition extends string>(
  64. node: unknown,
  65. test: Condition,
  66. index?: number | null | undefined,
  67. parent?: Parent | null | undefined,
  68. context?: unknown
  69. ) => node is import('unist').Node & {
  70. type: Condition
  71. }) &
  72. (<Condition_1 extends Props>(
  73. node: unknown,
  74. test: Condition_1,
  75. index?: number | null | undefined,
  76. parent?: Parent | null | undefined,
  77. context?: unknown
  78. ) => node is import('unist').Node & Condition_1) &
  79. (<Condition_2 extends TestFunction>(
  80. node: unknown,
  81. test: Condition_2,
  82. index?: number | null | undefined,
  83. parent?: Parent | null | undefined,
  84. context?: unknown
  85. ) => node is import('unist').Node &
  86. Predicate<Condition_2, import('unist').Node>) &
  87. ((node?: null | undefined) => false) &
  88. ((
  89. node: unknown,
  90. test?: null | undefined,
  91. index?: number | null | undefined,
  92. parent?: Parent | null | undefined,
  93. context?: unknown
  94. ) => node is import('unist').Node) &
  95. ((
  96. node: unknown,
  97. test?: Test,
  98. index?: number | null | undefined,
  99. parent?: Parent | null | undefined,
  100. context?: unknown
  101. ) => boolean)
  102. /**
  103. * Generate an assertion from a test.
  104. *
  105. * Useful if you’re going to test many nodes, for example when creating a
  106. * utility where something else passes a compatible test.
  107. *
  108. * The created function is a bit faster because it expects valid input only:
  109. * a `node`, `index`, and `parent`.
  110. *
  111. * @param {Test} test
  112. * * when nullish, checks if `node` is a `Node`.
  113. * * when `string`, works like passing `(node) => node.type === test`.
  114. * * when `function` checks if function passed the node is true.
  115. * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
  116. * * when `array`, checks if any one of the subtests pass.
  117. * @returns {Check}
  118. * An assertion.
  119. */
  120. export const convert: (<Condition extends string>(
  121. test: Condition
  122. ) => (
  123. node: unknown,
  124. index?: number | null | undefined,
  125. parent?: Parent | null | undefined,
  126. context?: unknown
  127. ) => node is import('unist').Node & {
  128. type: Condition
  129. }) &
  130. (<Condition_1 extends Props>(
  131. test: Condition_1
  132. ) => (
  133. node: unknown,
  134. index?: number | null | undefined,
  135. parent?: Parent | null | undefined,
  136. context?: unknown
  137. ) => node is import('unist').Node & Condition_1) &
  138. (<Condition_2 extends TestFunction>(
  139. test: Condition_2
  140. ) => (
  141. node: unknown,
  142. index?: number | null | undefined,
  143. parent?: Parent | null | undefined,
  144. context?: unknown
  145. ) => node is import('unist').Node &
  146. Predicate<Condition_2, import('unist').Node>) &
  147. ((
  148. test?: null | undefined
  149. ) => (
  150. node?: unknown,
  151. index?: number | null | undefined,
  152. parent?: Parent | null | undefined,
  153. context?: unknown
  154. ) => node is import('unist').Node) &
  155. ((test?: Test) => Check)
  156. export type Node = import('unist').Node
  157. export type Parent = import('unist').Parent
  158. export type Predicate<Fn, Fallback> = Fn extends (
  159. value: any
  160. ) => value is infer Thing
  161. ? Thing
  162. : Fallback
  163. /**
  164. * Check that an arbitrary value is a node.
  165. */
  166. export type Check = (
  167. this: unknown,
  168. node?: unknown,
  169. index?: number | null | undefined,
  170. parent?: Parent | null | undefined
  171. ) => boolean
  172. /**
  173. * Object to check for equivalence.
  174. *
  175. * Note: `Node` is included as it is common but is not indexable.
  176. */
  177. export type Props = Record<string, unknown> | Node
  178. /**
  179. * Check for an arbitrary node.
  180. */
  181. export type Test =
  182. | Array<Props | TestFunction | string>
  183. | Props
  184. | TestFunction
  185. | string
  186. | null
  187. | undefined
  188. /**
  189. * Check if a node passes a test.
  190. */
  191. export type TestFunction = (
  192. this: unknown,
  193. node: Node,
  194. index?: number | undefined,
  195. parent?: Parent | undefined
  196. ) => boolean | undefined | void