EthanRutherford / fast-fuzzy

Fast fuzzy search utility
ISC License
374 stars 8 forks source link

Typescript typings #8

Closed EHadoux closed 4 years ago

EHadoux commented 4 years ago

Hello,

I've written the Typescript typings for the lib. It seems to be broadly okay.

declare module 'fast-fuzzy' {
  interface CoreOptions {
    ignoreCase?: boolean,
    ignoreSymbols?: boolean,
    normalizeWhitespace?: boolean,
    useDamerau?: boolean,
    useSellers?: boolean,
  }

  type FuzzyOptions = CoreOptions & { returnMatchData?: false };
  type FuzzyOptionsMatchData = CoreOptions & { returnMatchData: true };

  interface AdditionalOptions<T> {
    keySelector?: (s: T) => string | string[],
    threshold?: number,
  };

  type FullOptions<T> = FuzzyOptions & AdditionalOptions<T>;
  type FullOptionsMatchData<T> = FuzzyOptionsMatchData & AdditionalOptions<T>;

  export interface MatchData<T> {
    item: T,
    original: string,
    key: string,
    score: number,
    match: {
      index: number,
      length: number,
    },
  };

  interface TrieNode<T> {
    children: {
      [key: string]: TrieNode<T>,
    },
    candidates: T[],
    depth: number,
  };

  export function fuzzy(term: string, candidate: string, options?: FuzzyOptions): number;
  export function fuzzy(term: string, candidate: string, options: FuzzyOptionsMatchData): MatchData<string>;
  export function search<T extends (string | object)>(term: string, candidates: T[], options?: FullOptions<T>): T[];
  export function search<T extends (string | object)>(term: string, candidates: T[], options: FullOptionsMatchData<T>): MatchData<T>[];
  export class Searcher<T extends (string | object)> {
    options: FullOptions<T> | FullOptionsMatchData<T>;
    trie: TrieNode<T>;
    count: number;

    constructor(candidates?: T[], options?: FullOptions<T>): this is { options: FullOptions<T> };
    constructor(candidates?: T[], options: FullOptionsMatchData<T>): this is { options: FullOptionsMatchData<T> };
    add(...candidates: T[]);
    search(term: string, options?: FullOptions<T>): this extends { options: FullOptionsMatchData<T> } ? MatchData<T>[] : T[];
    search(term: string, options: FullOptionsMatchData<T>): MatchData<T>[];
  };
}

Do you want to test on your side or should I make a PR? Happy to tweak the names and obviously fix the bugs.

Cheers

EthanRutherford commented 4 years ago

PRs are welcome, thanks!

EHadoux commented 4 years ago

Amazing, thanks for the merge, I hope the commit is alright. I'll check every now and then for the release on NPM. In the meantime, I've got the exact same typings file internally so if it breaks in my code, I'll update it in this repo as well.