UNPKG

unist-util-find-before

Version:
38 lines 1.67 kB
/** * Find the first node in `parent` before another `node` or before an index, * that passes `test`. * * @param parent * Parent node. * @param index * Child node or index. * @param [test=undefined] * Test for child to look for (optional). * @returns * A child (matching `test`, if given) or `undefined`. */ export const findBefore: ((<Kind extends UnistParent, Check extends Test>(parent: Kind, index: Child<Kind> | number, test: Check) => Matches<Child<Kind>, Check> | undefined) & (<Kind extends UnistParent>(parent: Kind, index: Child<Kind> | number, test?: null | undefined) => Child<Kind> | undefined)); /** * Get the value of a type guard `Fn`. */ export type Predicate<Fn, Fallback> = (Fn extends (value: any) => value is infer Thing ? Thing : Fallback); /** * Check whether a node matches a primitive check in the type system. */ export type MatchesOne<Value, Check> = (Check extends null | undefined ? Value : Value extends { type: Check; } ? Value : Value extends Check ? Value : Check extends Function ? Predicate<Check, Value> extends Value ? Predicate<Check, Value> : never : never); /** * Check whether a node matches a check in the type system. */ export type Matches<Value, Check> = (Check extends Array<any> ? MatchesOne<Value, Check[keyof Check]> : MatchesOne<Value, Check>); /** * Collect nodes that can be parents of `Child`. */ export type Child<Kind extends UnistNode> = (Kind extends { children: Array<infer Child>; } ? Child : never); import type { Parent as UnistParent } from 'unist'; import type { Test } from 'unist-util-is'; import type { Node as UnistNode } from 'unist'; //# sourceMappingURL=index.d.ts.map