aboutsummaryrefslogtreecommitdiff
path: root/src/matcher/options.ts
blob: 967a6f99c98b4efcba27d6fae8312bbe3462fed7 (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
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 => {
	console.log("matchNoCurry()")
	console.log(template)
	console.log(value)
	if (types.atom(template)) {
		console.log("atom")
		return template === value;
	} else if (types.function(template)) {
		console.log("function")
		return template(value);
	} else if (types.atom(value) || value === undefined) {
		console.log("atom2")
		return false;
	} else if (types.object(template)) {
		console.log("object")
		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) => {
	console.log("match()")
	console.log(template)
	console.log(value)
	if (value === undefined) {
		console.log("is undefined")
		return matchNoCurry.bind(null, template);
	}
	return matchNoCurry(template, value);
};

// tslint:disable-next-line:typedef
export const exact = (template: Template, value?: Value) => {
	if (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) => {
	if (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) => {
	if (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[]) => {
	if (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;