index.d.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @import {Node, Parent} from 'unist'
  3. */
  4. /**
  5. * @template Fn
  6. * @template Fallback
  7. * @typedef {Fn extends (value: any) => value is infer Thing ? Thing : Fallback} Predicate
  8. */
  9. /**
  10. * @callback Check
  11. * Check that an arbitrary value is a node.
  12. * @param {unknown} this
  13. * The given context.
  14. * @param {unknown} [node]
  15. * Anything (typically a node).
  16. * @param {number | null | undefined} [index]
  17. * The node’s position in its parent.
  18. * @param {Parent | null | undefined} [parent]
  19. * The node’s parent.
  20. * @returns {boolean}
  21. * Whether this is a node and passes a test.
  22. *
  23. * @typedef {Record<string, unknown> | Node} Props
  24. * Object to check for equivalence.
  25. *
  26. * Note: `Node` is included as it is common but is not indexable.
  27. *
  28. * @typedef {Array<Props | TestFunction | string> | ReadonlyArray<Props | TestFunction | string> | Props | TestFunction | string | null | undefined} Test
  29. * Check for an arbitrary node.
  30. *
  31. * @callback TestFunction
  32. * Check if a node passes a test.
  33. * @param {unknown} this
  34. * The given context.
  35. * @param {Node} node
  36. * A node.
  37. * @param {number | undefined} [index]
  38. * The node’s position in its parent.
  39. * @param {Parent | undefined} [parent]
  40. * The node’s parent.
  41. * @returns {boolean | undefined | void}
  42. * Whether this node passes the test.
  43. *
  44. * Note: `void` is included until TS sees no return as `undefined`.
  45. */
  46. /**
  47. * Check if `node` is a `Node` and whether it passes the given test.
  48. *
  49. * @param {unknown} node
  50. * Thing to check, typically `Node`.
  51. * @param {Test} test
  52. * A check for a specific node.
  53. * @param {number | null | undefined} index
  54. * The node’s position in its parent.
  55. * @param {Parent | null | undefined} parent
  56. * The node’s parent.
  57. * @param {unknown} context
  58. * Context object (`this`) to pass to `test` functions.
  59. * @returns {boolean}
  60. * Whether `node` is a node and passes a test.
  61. */
  62. export const is: ((<Condition extends ReadonlyArray<string>>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {
  63. type: Condition[number];
  64. }) & (<Condition extends Array<string>>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {
  65. type: Condition[number];
  66. }) & (<Condition extends string>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {
  67. type: Condition;
  68. }) & (<Condition extends Props>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Condition) & (<Condition extends TestFunction>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Predicate<Condition, Node>) & ((node?: null | undefined) => false) & ((node: unknown, test?: null | undefined, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node) & ((node: unknown, test?: Test, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => boolean));
  69. /**
  70. * Generate an assertion from a test.
  71. *
  72. * Useful if you’re going to test many nodes, for example when creating a
  73. * utility where something else passes a compatible test.
  74. *
  75. * The created function is a bit faster because it expects valid input only:
  76. * a `node`, `index`, and `parent`.
  77. *
  78. * @param {Test} test
  79. * * when nullish, checks if `node` is a `Node`.
  80. * * when `string`, works like passing `(node) => node.type === test`.
  81. * * when `function` checks if function passed the node is true.
  82. * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
  83. * * when `array`, checks if any one of the subtests pass.
  84. * @returns {Check}
  85. * An assertion.
  86. */
  87. export const convert: ((<Condition extends string>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {
  88. type: Condition;
  89. }) & (<Condition extends Props>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Condition) & (<Condition extends TestFunction>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Predicate<Condition, Node>) & ((test?: null | undefined) => (node?: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node) & ((test?: Test) => Check));
  90. export type Predicate<Fn, Fallback> = Fn extends (value: any) => value is infer Thing ? Thing : Fallback;
  91. /**
  92. * Check that an arbitrary value is a node.
  93. */
  94. export type Check = (this: unknown, node?: unknown, index?: number | null | undefined, parent?: Parent | null | undefined) => boolean;
  95. /**
  96. * Object to check for equivalence.
  97. *
  98. * Note: `Node` is included as it is common but is not indexable.
  99. */
  100. export type Props = Record<string, unknown> | Node;
  101. /**
  102. * Check for an arbitrary node.
  103. */
  104. export type Test = Array<Props | TestFunction | string> | ReadonlyArray<Props | TestFunction | string> | Props | TestFunction | string | null | undefined;
  105. /**
  106. * Check if a node passes a test.
  107. */
  108. export type TestFunction = (this: unknown, node: Node, index?: number | undefined, parent?: Parent | undefined) => boolean | undefined | void;
  109. import type { Parent } from 'unist';
  110. import type { Node } from 'unist';
  111. //# sourceMappingURL=index.d.ts.map