diff --git a/types/fuzzaldrin-plus/fuzzaldrin-plus-tests.ts b/types/fuzzaldrin-plus/fuzzaldrin-plus-tests.ts index 97730fe2e5..b5d83d4e9c 100644 --- a/types/fuzzaldrin-plus/fuzzaldrin-plus-tests.ts +++ b/types/fuzzaldrin-plus/fuzzaldrin-plus-tests.ts @@ -1,4 +1,8 @@ -import * as fz from "fuzzaldrin-plus"; +import * as fz from 'fuzzaldrin-plus' + +// +// Simple use cases +// ---------------------------------------------------------------------- const candidates = [ 'Find And Replace: Select All', @@ -7,14 +11,201 @@ const candidates = [ 'Application: Install Update', 'Install' ] -const objectCandidates = candidates.map(s => ({ foo: s })); +const objectCandidates = candidates.map(s => ({ foo: s })) +const options: fz.IOptions = { allowErrors: true } +const filterOptions: fz.IFilterOptions = { + allowErrors: true, + key: 'foo' +} -fz.filter(candidates, 'install'); -fz.filter(objectCandidates, 'install', { key: 'foo' }); +fz.filter(candidates, 'install') +fz.filter(candidates, 'install', options) +fz.filter(candidates, 'install', filterOptions) +fz.filter(objectCandidates, 'install', { key: 'foo' }) -const prepQuery = fz.prepQuery('install'); -fz.score('Application: Install Update', 'install'); -fz.score('Application: Install Update', 'install', prepQuery); +const preparedQuery: fz.Query = fz.prepareQuery('install') -fz.match('Application: Install Update', 'install'); -fz.match('Application: Install Update', 'install', prepQuery); +fz.score('Maybe', 'me') +fz.score('Maybe', 'me', undefined) +fz.score('Maybe', 'me', null) +fz.score('Maybe', 'me', {}) +fz.score('Maybe', 'me', { allowErrors: true }) +fz.score('Application: Install Update', 'install') +fz.score('Application: Install Update', 'install', { preparedQuery }) + +fz.match('Maybe', 'me') +fz.match('Application: Install Update', 'install') +fz.match('Application: Install Update', 'install', { preparedQuery }) + +fz.wrap('Maybe', 'me') + +// +// Complex filter cases, using all the options +// ---------------------------------------------------------------------- + +fz.filter(['Maybe', 'Me'], 'me') +fz.filter(['Maybe', 'Me'], 'me', undefined) +fz.filter(['Maybe', 'Me'], 'me', null) +fz.filter(['Maybe', 'Me'], 'me', {}) +fz.filter(['Maybe', 'Me'], 'me', { allowErrors: true }) +fz.filter(['Maybe', 'Me'], 'me', { key: 'key' }) +fz.filter(['Maybe', 'Me'], 'me', { + preparedQuery: fz.prepareQuery('me') +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '/' +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '\\', + optCharRegEx: new RegExp('') +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '\\', + optCharRegEx: new RegExp(''), + wrap: { + tagOpen: '', + tagClass: '', + tagClose: '' + } +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '\\', + optCharRegEx: new RegExp(''), + wrap: { + tagOpen: '', + tagClass: '', + tagClose: '' + }, + maxResults: 10 +}) +fz.filter(['Maybe', 'Me'], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '\\', + optCharRegEx: new RegExp(''), + wrap: { + tagOpen: '', + tagClass: '', + tagClose: '' + }, + maxResults: 10, + maxInners: 10 +}) +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + allowErrors: true, + usePathScoring: true, + useExtensionBonus: true, + pathSeparator: '\\', + optCharRegEx: new RegExp(''), + wrap: { + tagOpen: '', + tagClass: '', + tagClose: '' + }, + maxResults: 10, + maxInners: 10, + key: 'title' +}) +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + allowErrors: true, + key: 'title' +}) +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + key: 'title' +}) + +/** + * ====================================================================== + * Incorrect Usage: + * ====================================================================== + */ + +// $ExpectError +fz.score() +// $ExpectError +fz.match() +// $ExpectError +fz.wrap() +// $ExpectError +fz.prepareQuery() +// $ExpectError +fz.score('Maybe', 'me', true) +// $ExpectError +fz.score('Maybe', 'me', 'string') +// $ExpectError +fz.score('Maybe', 'me', 1) + +const items = ['Maybe', 'Me'] +// $ExpectError +fz.score(items, 'me') +// $ExpectError +fz.score('Maybe', 'me', { unknownProperty: true }) +// $ExpectError +fz.match(items, 'me') +// $ExpectError +fz.wrap(items, 'me') +// $ExpectError +fz.prepareQuery() + +// $ExpectError +const incorrectOptions: fz.IFilterOptions = { allowErrors: 'not a boolean' } + +// $ExpectError +fz.filter(candidates, 'install', { allowErrors: 'not a boolean' }) +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + // $ExpectError + incorrectProperty: true, + key: 'title' +}) + +// $ExpectError +fz.score('Maybe', 'me', undefined, options) +// $ExpectError +fz.match('Maybe', 'me', undefined, options) + +// $ExpectError +fz.filter() +// $ExpectError +fz.filter('not an array', 'query') +// $ExpectError +fz.filter(['Maybe', 'Me'], 'me', { allowErrors: 'not a boolean' }) +// $ExpectError +fz.filter(['Maybe', 'Me'], 'me', { key: true }) +// $ExpectError +fz.filter(['Maybe', 'Me'], 'me', { preparedQuery: {} }) +// $ExpectError +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + key: 1 +}) +// $ExpectError +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + allowErrors: 'not a boolean', + key: 1 +}) +// $ExpectError +fz.filter([{ title: 'Maybe' }, { title: 'Me' }], 'me', { + allowErrors: 'not a boolean', + key: 'title' +}) diff --git a/types/fuzzaldrin-plus/index.d.ts b/types/fuzzaldrin-plus/index.d.ts index 34d6949ace..cd52c501d4 100644 --- a/types/fuzzaldrin-plus/index.d.ts +++ b/types/fuzzaldrin-plus/index.d.ts @@ -1,29 +1,39 @@ -// Type definitions for fuzzaldrin-plus +// Type definitions for fuzzaldrin-plus 0.6.0 // Project: https://github.com/jeancroy/fuzzaldrin-plus/ -// Definitions by: Jean Christophe Roy , Jason Killian +// Definitions by: Jean Christophe Roy , +// Jason Killian +// Ronald Rey // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 -export as namespace fuzzaldrin; +export as namespace fuzzaldrin -export interface IQueryOptions { - pathSeparator?: string; - optCharRegEx?: RegExp; +export class Query { + $$__internal: Symbol } -export interface IScoringOptions extends IQueryOptions { - allowErrors?: boolean; - isPath?: boolean; - useExtensionBonus?: boolean; +export interface IOptions { + allowErrors?: boolean + usePathScoring?: boolean + useExtensionBonus?: boolean + pathSeparator?: '/' | '\\' | string + optCharRegEx?: RegExp + wrap?: { tagOpen?: string; tagClass?: string; tagClose?: string } + preparedQuery?: Query } -export interface IFilterOptions extends IScoringOptions { - key?: string; - maxResults?: number; +export type IFilterOptions = IOptions & { + key?: string + maxResults?: number + maxInners?: number } -export type PreparedQuery = { __internalAPIBrand: string; }; - -export function filter(data: T[], query: string, options?: IFilterOptions): T[]; -export function score(str: string, query: string, preparedQuery?: PreparedQuery, options?: IScoringOptions): number; -export function match(str: string, query: string, preparedQuery?: PreparedQuery, options?: IScoringOptions): number[]; -export function prepQuery(query: string, options?: IQueryOptions): PreparedQuery; +export function filter( + data: T[], + query: string, + options?: IFilterOptions +): T[] +export function score(str: string, query: string, options?: IOptions): number +export function match(str: string, query: string, options?: IOptions): number[] +export function wrap(str: string, query: string, options?: IOptions): string +export function prepareQuery(query: string, options?: IOptions): Query