index.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Find the first node in `parent` after another `node` or after an index,
  3. * that passes `test`.
  4. *
  5. * @param parent
  6. * Parent node.
  7. * @param index
  8. * Child node or index.
  9. * @param [test=undefined]
  10. * Test for child to look for (optional).
  11. * @returns
  12. * A child (matching `test`, if given) or `undefined`.
  13. */
  14. export const findAfter: (<
  15. Kind extends import('unist').Parent,
  16. Check extends Test
  17. >(
  18. parent: Kind,
  19. index: number | Child<Kind>,
  20. test: Check
  21. ) => Matches<Child<Kind>, Check> | undefined) &
  22. (<Kind_1 extends import('unist').Parent>(
  23. parent: Kind_1,
  24. index: number | Child<Kind_1>,
  25. test?: null | undefined
  26. ) => Child<Kind_1> | undefined)
  27. export type UnistNode = import('unist').Node
  28. export type UnistParent = import('unist').Parent
  29. /**
  30. * Test from `unist-util-is`.
  31. *
  32. * Note: we have remove and add `undefined`, because otherwise when generating
  33. * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
  34. * which doesn’t work when publishing on npm.
  35. */
  36. export type Test = Exclude<import('unist-util-is').Test, undefined> | undefined
  37. /**
  38. * Get the value of a type guard `Fn`.
  39. */
  40. export type Predicate<Fn, Fallback> = Fn extends (
  41. value: any
  42. ) => value is infer Thing
  43. ? Thing
  44. : Fallback
  45. /**
  46. * Check whether a node matches a primitive check in the type system.
  47. */
  48. export type MatchesOne<Value, Check> = Check extends null | undefined
  49. ? Value
  50. : Value extends {
  51. type: Check
  52. }
  53. ? Value
  54. : Value extends Check
  55. ? Value
  56. : Check extends Function
  57. ? Predicate<Check, Value> extends Value
  58. ? Predicate<Check, Value>
  59. : never
  60. : never
  61. /**
  62. * Check whether a node matches a check in the type system.
  63. */
  64. export type Matches<Value, Check> = Check extends Array<any>
  65. ? MatchesOne<Value, Check[keyof Check]>
  66. : MatchesOne<Value, Check>
  67. /**
  68. * Collect nodes that can be parents of `Child`.
  69. */
  70. export type Child<Kind extends import('unist').Node> = Kind extends {
  71. children: (infer Child_1)[]
  72. }
  73. ? Child_1
  74. : never