diff options
| author | Tom Harley | 2018-02-06 21:05:58 +0000 |
|---|---|---|
| committer | Tom Harley | 2018-02-06 21:05:58 +0000 |
| commit | b9bb0662a9c23cad3b5f200cf2a9588539ed5f0c (patch) | |
| tree | 5a5ed0848fb636254cc4da8931213e6d750af5e0 /src | |
| parent | started work on type checking (diff) | |
| download | medicine-b9bb0662a9c23cad3b5f200cf2a9588539ed5f0c.tar.gz medicine-b9bb0662a9c23cad3b5f200cf2a9588539ed5f0c.zip | |
curried option functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/matcher/options.test.ts | 23 | ||||
| -rw-r--r-- | src/matcher/options.ts | 56 |
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); } |
