extract.d.ts 1.5 KB

1234567891011121314151617181920212223242526272829
  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 interface ExtractMap {
  11. [key: string]: ExtractValue;
  12. }
  13. type ExtractedValue<V extends ExtractValue, M extends ExtractMap> = V extends [
  14. string | ExtractDescriptor
  15. ] ? NonNullable<ExtractedValue<V[0], M>>[] : V extends string ? string | undefined : V extends ExtractDescriptor ? V['value'] extends ExtractMap ? ExtractedMap<V['value']> | undefined : V['value'] extends ExtractDescriptorFn ? ReturnType<V['value']> | undefined : ReturnType<typeof prop> | undefined : never;
  16. export type ExtractedMap<M extends ExtractMap> = {
  17. [key in keyof M]: ExtractedValue<M[key], M>;
  18. };
  19. /**
  20. * Extract multiple values from a document, and store them in an object.
  21. *
  22. * @param map - An object containing key-value pairs. The keys are the names of
  23. * the properties to be created on the object, and the values are the
  24. * selectors to be used to extract the values.
  25. * @returns An object containing the extracted values.
  26. */
  27. export declare function extract<M extends ExtractMap, T extends AnyNode>(this: Cheerio<T>, map: M): ExtractedMap<M>;
  28. export {};
  29. //# sourceMappingURL=extract.d.ts.map