All files / project-code/src/matcher options.ts

63.04% Statements 29/46
46.67% Branches 14/30
77.78% Functions 7/9
65.91% Lines 29/44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x                               1x 35x 12x 23x 13x 10x   10x 10x   14x 14x               1x 22x 1x   21x       1x 1x 1x                             1x 11x 11x           1x 3x 3x                   1x 14x 14x                 1x             1x  
import { Atom, default as types } from './types';
 
type Checker = ((value: Value | undefined) => boolean);
 
export type TemplateValue =
	Atom | Template | Checker;
 
export interface Template {
	[key: string]: TemplateValue | undefined;
}
 
export type Value =
	{ [key: string]: Value | undefined } | Atom;
 
const matchNoCurry:
	(template: TemplateValue, value: Value | undefined) => boolean =
	(template: TemplateValue, value: Value | undefined): boolean => {
	if (types.atom(template)) {
		return template === value;
	} else if (types.function(template)) {
		return template(value);
	} else Iif (types.atom(value) || value === undefined) {
		return false;
	} else Eif (types.object(template)) {
		return Object.keys(template)
			.every((key: string): boolean => {
				const v: Value | undefined = value[key];
				return matchNoCurry(template[key] as TemplateValue, v);
			});
	} else {
		throw new Error(`Unknown template value: ${template}`);
	}
};
 
// tslint:disable-next-line:typedef
export const match = (template: TemplateValue, value?: Value) => {
	if (value === undefined) {
		return matchNoCurry.bind(null, template);
	}
	return matchNoCurry(template, value);
};
 
// tslint:disable-next-line:typedef
export const exact = (template: Template, value?: Value) => {
	Eif (value === undefined) {
		return exact.bind(null, template);
	}
	if (!match(template, value)) {
		return false;
	}
	if (!types.atom(value)) {
		const k1: string[] = Object.keys(template);
		const k2: string[] = Object.keys(value);
		return k2.every((x: string) => k1.indexOf(x) < 0);
	} else {
		return true;
	}
};
 
// tslint:disable-next-line:typedef
export const optional = (template: TemplateValue, value?: Value) => {
	Eif (value === undefined) {
		return optional.bind(null, template);
	}
	return types.undefined(value) || match(template, value);
};
 
// tslint:disable-next-line:typedef
export const alternative = (templates: TemplateValue[], value?: Value) => {
	Eif (value === undefined) {
		return alternative.bind(null, templates);
	}
	if (templates.length === 0) {
		return false;
	} else {
		return templates.some((t: TemplateValue): boolean => match(t, value));
	}
};
 
// tslint:disable-next-line:typedef
export const array = (template: TemplateValue, values?: Value[]) => {
	Eif (values === undefined) {
		return array.bind(null, template);
	}
	const m: Checker = types.function(template)
		? template
		: match.bind(null, template);
	return values.every(m);
};
 
// tslint:disable-next-line:typedef
const options = {
	match,
	exact,
	optional,
	alternative,
	array
};
export default options;