Skip to content

Commit

Permalink
Add match sorter libdefs (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmalov authored and gantoine committed Jan 29, 2018
1 parent 9f7cf2a commit 590736c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type $npm$matchSorter$KeyFn<T> = (item: T) => mixed;
type $npm$matchSorter$KeyObj = {
key: string,
minRanking?: number,
maxRanking?: number
};

type $npm$matchSorter$Rankings = {
CASE_SENSITIVE_EQUAL: number,
EQUAL: number,
STARTS_WITH: number,
WORD_STARTS_WITH: number,
STRING_CASE: number,
STRING_CASE_ACRONYM: number,
CONTAINS: number,
ACRONYM: number,
MATCHES: number,
NO_MATCH: number
};

type $npm$matchSorter$CaseRankings = {
CAMEL: number,
PASCAL: number,
KEBAB: number,
SNAKE: number,
NO_CASE: number
};

declare module 'match-sorter' {
declare type Options<T> = {
keys: Array<string | $npm$matchSorter$KeyFn<T> | $npm$matchSorter$KeyObj>,
threshold?: number,
keepDiacritics?: boolean
};

declare module.exports: {
<T>(collection: Array<T>, query: string, opts?: Options<T>): Array<T>,
rankings: $npm$matchSorter$Rankings,
caseRankings: $npm$matchSorter$CaseRankings
};
}
41 changes: 41 additions & 0 deletions definitions/npm/match-sorter_v2.2.x/test_match-sorter_v2.2.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @flow

import matchSorter, { rankings, caseRankings } from 'match-sorter';

// $ExpectError
const startsWith: string = rankings.STARTS_WITH;

// $ExpectError
rankings.foo;

// $ExpectError
matchSorter(1, 2);

// $ExpectError
matchSorter([], 2);

const found1: Array<number> = matchSorter([1, 2, 3], '2');

// $ExpectError
matchSorter([], 'foo', { foo: bar });

const sampleCollection = [{ baz: '1' }, { baz: '2' }, { baz: 'foooooo' }];

// $ExpectError
matchSorter(sampleCollection, 'foo', { invalidProp: 'bar' });

// $ExpectError
matchSorter(sampleCollection, 'foo', { keys: 1 });

// string key accessor
const [found2] = matchSorter(sampleCollection, 'foo', { keys: ['baz'] });
const prop: string = found2.baz;

// callback key accessor
matchSorter(sampleCollection, 'foo', { keys: [item => item.baz] });

// object key accessor
matchSorter(sampleCollection, 'foo', { keys: [{ key: 'baz' }] });

// $ExpectError
matchSorter(sampleCollection, 'foo', { keys: [{ foo: 'baz' }] });

0 comments on commit 590736c

Please sign in to comment.