Skip to content

Commit

Permalink
fix: apply @aduth's sugguestions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Mar 20, 2023
1 parent d2ddc69 commit 3312c04
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 103 deletions.
144 changes: 96 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 27 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const getDocument = (() => {
};
})();

/**
* Given a markup string or DOM element, creates an object aligning with the
* shape of the matchers object, or the value returned by the matcher.
*
* @param source Source content
* @param matchers Matcher function or object of matchers
*/
export function parse(source: string | Element, matchers?: undefined): undefined;

/**
* Given a markup string or DOM element, creates an object aligning with the
* shape of the matchers object, or the value returned by the matcher.
Expand Down Expand Up @@ -67,15 +76,6 @@ export function parse<F extends MatcherFn, O extends MatcherObj>(
matchers: O | F
): MatcherObjResult<F, O> | ReturnType<F>;

/**
* Given a markup string or DOM element, creates an object aligning with the
* shape of the matchers object, or the value returned by the matcher.
*
* @param source Source content
* @param matchers Matcher function or object of matchers
*/
export function parse(source: string | Element, matchers?: undefined): undefined;

/**
* Given a markup string or DOM element, creates an object aligning with the
* shape of the matchers object, or the value returned by the matcher.
Expand Down Expand Up @@ -124,7 +124,7 @@ export function parse<F extends MatcherFn, O extends MatcherObj>(
* @param name Property name
* @return Property value
*/
export function prop<E extends Element, K extends keyof E>(name: string): MatcherFn<E[K]>;
export function prop<N extends keyof Element>(name: string): MatcherFn<Element[N]>;

/**
* Generates a function which matches node of type selector, returning an
Expand All @@ -135,10 +135,10 @@ export function prop<E extends Element, K extends keyof E>(name: string): Matche
* @param name Property name
* @return Property value
*/
export function prop<E extends Element, K extends keyof E>(
export function prop<N extends keyof Element>(
selector: string | undefined,
name: string
): MatcherFn<E[K]>;
name: N
): MatcherFn<Element[N]>;

/**
* Generates a function which matches node of type selector, returning an
Expand All @@ -149,10 +149,10 @@ export function prop<E extends Element, K extends keyof E>(
* @param name Property name
* @return Property value
*/
export function prop<E extends Element, K extends keyof E>(
export function prop<N extends keyof Element>(
arg1: string | undefined,
arg2?: string
): MatcherFn<E[K]> {
): MatcherFn<Element[N]> {
let name: string;
let selector: string | undefined;
if (1 === arguments.length) {
Expand All @@ -162,15 +162,15 @@ export function prop<E extends Element, K extends keyof E>(
name = arg2 as string;
selector = arg1;
}
return function (node: E): E[K] | undefined {
return function (node: Element): Element[N] | undefined {
let match: Element | null = node;
if (selector) {
match = node.querySelector(selector);
}
if (match) {
return getPath(match, name) as E[K] | undefined;
return getPath(match, name) as Element[N] | undefined;
}
} as MatcherFn<E[K]>;
} as MatcherFn<Element[N]>;
}

/**
Expand Down Expand Up @@ -230,7 +230,7 @@ export function attr(arg1: string | undefined, arg2?: string): MatcherFn<string>
* @return Inner HTML
*/
export function html(selector?: string) {
return prop(selector, 'innerHTML');
return prop(selector, 'innerHTML') as MatcherFn<string>;
}

/**
Expand All @@ -242,7 +242,7 @@ export function html(selector?: string) {
* @return Text content
*/
export function text(selector?: string) {
return prop(selector, 'textContent');
return prop(selector, 'textContent') as MatcherFn<string>;
}

/**
Expand All @@ -254,11 +254,14 @@ export function text(selector?: string) {
*
* @param selector Selector to match
* @param matchers Matcher function or object of matchers
* @return Matcher function which returns an array of matched value(s)
* @return Matcher function which returns an array of matched value(s)
*/
export function query(selector: string, matchers: MatcherObj) {
return function (node: Element): any[] {
export function query<F extends MatcherFn, O extends MatcherObj>(
selector: string,
matchers?: F | O
) {
return function (node: Element) {
const matches = node.querySelectorAll(selector);
return [].map.call(matches, (match) => parse(match, matchers));
return [].map.call(matches, (match) => parse(match, matchers!)) as MatcherObjResult<F, O>[];
};
}
File renamed without changes.
Loading

0 comments on commit 3312c04

Please sign in to comment.