aboutsummaryrefslogtreecommitdiff
path: root/src/matcher/options.ts
diff options
context:
space:
mode:
authorTom Harley2018-02-06 14:58:10 +0000
committerTom Harley2018-02-06 14:58:10 +0000
commit13a2c19212bf208e8a06ef1673ebd4595bcb849b (patch)
treece7856e21a6afcaa64b886786cf538b95602c4c7 /src/matcher/options.ts
parentEdited testing script (diff)
downloadmedicine-13a2c19212bf208e8a06ef1673ebd4595bcb849b.tar.gz
medicine-13a2c19212bf208e8a06ef1673ebd4595bcb849b.zip

started work on type checking

Diffstat (limited to 'src/matcher/options.ts')
-rw-r--r--src/matcher/options.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/matcher/options.ts b/src/matcher/options.ts
new file mode 100644
index 0000000..5b7c2d9
--- /dev/null
+++ b/src/matcher/options.ts
@@ -0,0 +1,47 @@
+import { default as types } from './types';
+
+export function rough(template: Object, value: Object): boolean {
+ for (let k of Object.keys(template)) {
+ if (!template[k](value[k])) {
+ return false;
+ }
+ }
+ return true;
+}
+
+export function match(template: Object): (Object) => boolean {
+ return rough.bind(null, template);
+}
+
+export function exact(template: Object): (Object) => boolean {
+ let m = match(template);
+ let revm = (value: Object) => rough(value, template);
+ return (v) => m(v) && revm(v);
+}
+
+export function optional(template: Function): (any) => boolean {
+ return (v) => types.undefined(v)
+ || template(v);
+}
+
+export function alternative(templates: any[]) {
+ if (templates.length === 0) {
+ return () => false;
+ } else {
+ let t = templates.map((x) => {
+ if (types.function(x)) {
+ return x;
+ } else if (types.object(x)) {
+ return match(x);
+ } else {
+ return (v) => x === v;
+ }
+ });
+ return (v) => t.some((x) => x(v));
+ }
+}
+
+export function array(template: Object): (Array) => boolean {
+ let m = match(template);
+ return (v) => v.every(m);
+}