aboutsummaryrefslogtreecommitdiff
path: root/src/matcher/options.ts
diff options
context:
space:
mode:
authorTom Harley2018-02-06 21:05:58 +0000
committerTom Harley2018-02-06 21:05:58 +0000
commitb9bb0662a9c23cad3b5f200cf2a9588539ed5f0c (patch)
tree5a5ed0848fb636254cc4da8931213e6d750af5e0 /src/matcher/options.ts
parentstarted work on type checking (diff)
downloadmedicine-b9bb0662a9c23cad3b5f200cf2a9588539ed5f0c.tar.gz
medicine-b9bb0662a9c23cad3b5f200cf2a9588539ed5f0c.zip

curried option functions

Diffstat (limited to 'src/matcher/options.ts')
-rw-r--r--src/matcher/options.ts56
1 files changed, 39 insertions, 17 deletions
diff --git a/src/matcher/options.ts b/src/matcher/options.ts
index 5b7c2d9..1ffa24e 100644
--- a/src/matcher/options.ts
+++ b/src/matcher/options.ts
@@ -1,6 +1,13 @@
import { default as types } from './types';
-export function rough(template: Object, value: Object): boolean {
+type matchType<T = any>
+ = (boolean | ((T) => boolean));
+
+export function match(template: Object, value?: Object): matchType<Object> {
+ if (value === undefined) {
+ return match.bind(null, template);
+ }
+
for (let k of Object.keys(template)) {
if (!template[k](value[k])) {
return false;
@@ -9,24 +16,34 @@ export function rough(template: Object, value: Object): boolean {
return true;
}
-export function match(template: Object): (Object) => boolean {
- return rough.bind(null, template);
-}
+export function exact(template: Object, value?: Object): matchType<Object> {
+ if (value === undefined) {
+ return exact.bind(null, template);
+ }
+
+ let k1 = Object.keys(template);
+ let k2 = Object.keys(value);
-export function exact(template: Object): (Object) => boolean {
- let m = match(template);
- let revm = (value: Object) => rough(value, template);
- return (v) => m(v) && revm(v);
+ return match(template, value)
+ && k2.every((x) => k1.indexOf(x) < 0);
}
-export function optional(template: Function): (any) => boolean {
- return (v) => types.undefined(v)
- || template(v);
+export function optional(template: Function, value?: any): matchType<any> {
+ if (value === undefined) {
+ return optional.bind(null, template);
+ }
+
+ return types.undefined(value)
+ || template(value);
}
-export function alternative(templates: any[]) {
+export function alternative(templates: any[], value?: any): matchType<any> {
+ if (value === undefined) {
+ return alternative.bind(null, templates);
+ }
+
if (templates.length === 0) {
- return () => false;
+ return false;
} else {
let t = templates.map((x) => {
if (types.function(x)) {
@@ -37,11 +54,16 @@ export function alternative(templates: any[]) {
return (v) => x === v;
}
});
- return (v) => t.some((x) => x(v));
+ return t.some((x) => x(value));
}
}
-export function array(template: Object): (Array) => boolean {
- let m = match(template);
- return (v) => v.every(m);
+export function array(template: Object, values?: Array<Object>):
+matchType<Array<Object>> {
+ if (values === undefined) {
+ return array.bind(null, template);
+ }
+
+ let m = match(template) as ((Object) => boolean);
+ return values.every(m);
}