index.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @typedef {import('hast').Element} Element
  3. * @typedef {import('hast').Parents} Parents
  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 an element.
  13. * @param {unknown} this
  14. * Context object (`this`) to call `test` with
  15. * @param {unknown} [element]
  16. * Anything (typically a node).
  17. * @param {number | null | undefined} [index]
  18. * Position of `element` in its parent.
  19. * @param {Parents | null | undefined} [parent]
  20. * Parent of `element`.
  21. * @returns {boolean}
  22. * Whether this is an element and passes a test.
  23. *
  24. * @typedef {Array<TestFunction | string> | TestFunction | string | null | undefined} Test
  25. * Check for an arbitrary element.
  26. *
  27. * * when `string`, checks that the element has that tag name
  28. * * when `function`, see `TestFunction`
  29. * * when `Array`, checks if one of the subtests pass
  30. *
  31. * @callback TestFunction
  32. * Check if an element passes a test.
  33. * @param {unknown} this
  34. * The given context.
  35. * @param {Element} element
  36. * An element.
  37. * @param {number | undefined} [index]
  38. * Position of `element` in its parent.
  39. * @param {Parents | undefined} [parent]
  40. * Parent of `element`.
  41. * @returns {boolean | undefined | void}
  42. * Whether this element passes the test.
  43. *
  44. * Note: `void` is included until TS sees no return as `undefined`.
  45. */
  46. /**
  47. * Check if `element` is an `Element` and whether it passes the given test.
  48. *
  49. * @param element
  50. * Thing to check, typically `element`.
  51. * @param test
  52. * Check for a specific element.
  53. * @param index
  54. * Position of `element` in its parent.
  55. * @param parent
  56. * Parent of `element`.
  57. * @param context
  58. * Context object (`this`) to call `test` with.
  59. * @returns
  60. * Whether `element` is an `Element` and passes a test.
  61. * @throws
  62. * When an incorrect `test`, `index`, or `parent` is given; there is no error
  63. * thrown when `element` is not a node or not an element.
  64. */
  65. export const isElement: (<Condition extends TestFunction>(element: unknown, test: Condition, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element & Predicate<Condition, import("hast").Element>) & (<Condition_1 extends string>(element: unknown, test: Condition_1, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element & {
  66. tagName: Condition_1;
  67. }) & ((element?: null | undefined) => false) & ((element: unknown, test?: null | undefined, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element) & ((element: unknown, test?: Test, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => boolean);
  68. /**
  69. * Generate a check from a test.
  70. *
  71. * Useful if you’re going to test many nodes, for example when creating a
  72. * utility where something else passes a compatible test.
  73. *
  74. * The created function is a bit faster because it expects valid input only:
  75. * an `element`, `index`, and `parent`.
  76. *
  77. * @param test
  78. * A test for a specific element.
  79. * @returns
  80. * A check.
  81. */
  82. export const convertElement: (<Condition extends TestFunction>(test: Condition) => (element: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element & Predicate<Condition, import("hast").Element>) & (<Condition_1 extends string>(test: Condition_1) => (element: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element & {
  83. tagName: Condition_1;
  84. }) & ((test?: null | undefined) => (element?: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is import("hast").Element) & ((test?: Test) => Check);
  85. export type Element = import('hast').Element;
  86. export type Parents = import('hast').Parents;
  87. export type Predicate<Fn, Fallback> = Fn extends (value: any) => value is infer Thing ? Thing : Fallback;
  88. /**
  89. * Check that an arbitrary value is an element.
  90. */
  91. export type Check = (this: unknown, element?: unknown, index?: number | null | undefined, parent?: Parents | null | undefined) => boolean;
  92. /**
  93. * Check for an arbitrary element.
  94. *
  95. * * when `string`, checks that the element has that tag name
  96. * * when `function`, see `TestFunction`
  97. * * when `Array`, checks if one of the subtests pass
  98. */
  99. export type Test = Array<TestFunction | string> | TestFunction | string | null | undefined;
  100. /**
  101. * Check if an element passes a test.
  102. */
  103. export type TestFunction = (this: unknown, element: Element, index?: number | undefined, parent?: Parents | undefined) => boolean | undefined | void;