<commit_before>import type { ParseSelector } from 'typed-query-selector/parser';
import { duffReduce } from 'spica/duff';

export function querySelectorWith<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T] | null;
export function querySelectorWith<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T] | null;
export function querySelectorWith<T extends string>(node: ParentNode, selector: T): ParseSelector<T>;
export function querySelectorWith<T extends Element>(node: ParentNode, selector: string): T | null;
export function querySelectorWith(node: ParentNode | Element, selector: string): Element | null {
  return 'matches' in node && node.matches(selector)
    ? node
    : node.querySelector(selector);
}

export function querySelectorAllWith<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T][];
export function querySelectorAllWith<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T][];
export function querySelectorAllWith<T extends string>(node: ParentNode, selector: T): ParseSelector<T>[];
export function querySelectorAllWith<T extends Element>(node: ParentNode, selector: string): T[];
export function querySelectorAllWith(node: ParentNode | Element, selector: string): Element[] {
  const acc: Element[] = [];
  if ('matches' in node && node.matches(selector)) {
    acc.push(node);
  }
  return duffReduce(node.querySelectorAll(selector), (acc, node) => (acc.push(node), acc), acc);
}

// for文との二重反復をコールバックで解消しても変化なし
export function querySelectorAll<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T][];
export function querySelectorAll<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T][];
export function querySelectorAll<T extends string>(node: ParentNode, selector: T): ParseSelector<T>[];
export function querySelectorAll<T extends Element>(node: ParentNode, selector: string): T[];
export function querySelectorAll(node: ParentNode | Element, selector: string): Element[] {
  return duffReduce(node.querySelectorAll(selector), (acc, node) => (acc.push(node), acc), [] as Element[]);
}<commit_msg>Refactoring<commit_after>import type { ParseSelector } from 'typed-query-selector/parser';
import { duffReduce } from 'spica/duff';

export function querySelectorWith<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T] | null;
export function querySelectorWith<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T] | null;
export function querySelectorWith<T extends string>(node: ParentNode, selector: T): ParseSelector<T>;
export function querySelectorWith<T extends Element>(node: ParentNode, selector: string): T | null;
export function querySelectorWith(node: ParentNode | Element, selector: string): Element | null {
  return 'matches' in node && node.matches(selector)
    ? node
    : node.querySelector(selector);
}

export function querySelectorAllWith<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T][];
export function querySelectorAllWith<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T][];
export function querySelectorAllWith<T extends string>(node: ParentNode, selector: T): ParseSelector<T>[];
export function querySelectorAllWith<T extends Element>(node: ParentNode, selector: string): T[];
export function querySelectorAllWith(node: ParentNode | Element, selector: string): Element[] {
  const acc: Element[] = [];
  if ('matches' in node && node.matches(selector)) {
    acc.push(node);
  }
  return duffReduce(node.querySelectorAll(selector), (acc, el) => (acc.push(el), acc), acc);
}

// for文との二重反復をコールバックで解消しても変化なし
export function querySelectorAll<T extends keyof HTMLElementTagNameMap>(node: ParentNode, selector: T): HTMLElementTagNameMap[T][];
export function querySelectorAll<T extends keyof SVGElementTagNameMap>(node: ParentNode, selector: T): SVGElementTagNameMap[T][];
export function querySelectorAll<T extends string>(node: ParentNode, selector: T): ParseSelector<T>[];
export function querySelectorAll<T extends Element>(node: ParentNode, selector: string): T[];
export function querySelectorAll(node: ParentNode | Element, selector: string): Element[] {
  return duffReduce(node.querySelectorAll(selector), (acc, el) => (acc.push(el), acc), [] as Element[]);
}
{'fraction_non_alphanumeric': 0.11994727592267135, 'fraction_numerical': 0.0, 'mean_word_length': 8.015841584158416, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4252634', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1319, 'n_words': 366}