extract.d.ts 1.4 KB

123456789101112131415161718192021222324252627
  1. import type { AnyNode, Element } from 'domhandler';
  2. import type { Cheerio } from '../cheerio.js';
  3. import type { prop } from './attributes.js';
  4. type ExtractDescriptorFn = (el: Element, key: string, obj: Record<string, unknown>) => unknown;
  5. interface ExtractDescriptor {
  6. selector: string;
  7. value?: string | ExtractDescriptorFn | ExtractMap;
  8. }
  9. type ExtractValue = string | ExtractDescriptor | [string | ExtractDescriptor];
  10. export type ExtractMap = Record<string, ExtractValue>;
  11. type ExtractedValue<V extends ExtractValue> = V extends [
  12. string | ExtractDescriptor
  13. ] ? NonNullable<ExtractedValue<V[0]>>[] : V extends string ? string | undefined : V extends ExtractDescriptor ? V['value'] extends infer U ? U extends ExtractMap ? ExtractedMap<U> | undefined : U extends ExtractDescriptorFn ? ReturnType<U> | undefined : ReturnType<typeof prop> | undefined : never : never;
  14. export type ExtractedMap<M extends ExtractMap> = {
  15. [key in keyof M]: ExtractedValue<M[key]>;
  16. };
  17. /**
  18. * Extract multiple values from a document, and store them in an object.
  19. *
  20. * @param map - An object containing key-value pairs. The keys are the names of
  21. * the properties to be created on the object, and the values are the
  22. * selectors to be used to extract the values.
  23. * @returns An object containing the extracted values.
  24. */
  25. export declare function extract<M extends ExtractMap, T extends AnyNode>(this: Cheerio<T>, map: M): ExtractedMap<M>;
  26. export {};
  27. //# sourceMappingURL=extract.d.ts.map