aboutsummaryrefslogtreecommitdiff
path: root/src/matcher
diff options
context:
space:
mode:
authorjw2552018-02-06 22:27:11 +0000
committerjw2552018-02-06 22:27:11 +0000
commitb360ce041e80e02cbd7c91f964b48a7621392ff0 (patch)
tree77a6308ab80dec124d7c2a9d321d2425c5ff08a3 /src/matcher
parentMerge branch 'testing' into trh/payload-checker (diff)
parentcurried option functions (diff)
downloadmedicine-b360ce041e80e02cbd7c91f964b48a7621392ff0.tar.gz
medicine-b360ce041e80e02cbd7c91f964b48a7621392ff0.zip

Merge branch ‘trh/payload-checker’ of https://gitlab.cs.st-andrews.ac.uk/cs3099group-be-4/project-code into trh/payload-checker

Diffstat (limited to 'src/matcher')
-rw-r--r--src/matcher/options.test.ts23
-rw-r--r--src/matcher/options.ts56
2 files changed, 58 insertions, 21 deletions
diff --git a/src/matcher/options.test.ts b/src/matcher/options.test.ts
index 6c2cfe5..f3d498c 100644
--- a/src/matcher/options.test.ts
+++ b/src/matcher/options.test.ts
@@ -14,7 +14,7 @@ describe('Object Matching:', () => {
let matchee = {
a: "yes"
};
- options.rough(template, matchee).should.be.true;
+ options.match(template, matchee).should.be.true;
});
it('accepts objects with superfluous properties', () => {
let template = {
@@ -24,7 +24,7 @@ describe('Object Matching:', () => {
a: 40,
b: 20
};
- options.rough(template, matchee).should.be.true;
+ options.match(template, matchee).should.be.true;
});
it('rejects objects missing some properties', () => {
let template = {
@@ -35,7 +35,7 @@ describe('Object Matching:', () => {
let matchee = {
b: 5
};
- options.rough(template, matchee).should.be.false;
+ options.match(template, matchee).should.be.false;
});
it('accepts objects that omit not_present properties', () => {
let template = {
@@ -46,7 +46,22 @@ describe('Object Matching:', () => {
let matchee = {
a: new Function()
};
- options.rough(template, matchee).should.be.true;
+ options.match(template, matchee).should.be.true;
+ });
+ it('rejects empty objects for non-empty templates', () => {
+ let template = {
+ a: types.boolean
+ };
+ let matchee = {
+ };
+ options.match(template, matchee).should.be.false;
+ });
+ it('accepts empty objects for empty templates', () => {
+ let template = {
+ };
+ let matchee = {
+ };
+ options.match(template, matchee).should.be.true;
});
});
});
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);
}