diff options
| author | Tom Harley | 2018-02-07 12:48:48 +0000 |
|---|---|---|
| committer | Tom Harley | 2018-02-07 12:50:02 +0000 |
| commit | f60e3d9858f6037db3b27d5574ac95310127d302 (patch) | |
| tree | 73d93b7a57862bfae9a560fafb983d076a1ba637 | |
| parent | removed types from options.ts to remove ts-node ambiguity (diff) | |
| download | medicine-f60e3d9858f6037db3b27d5574ac95310127d302.tar.gz medicine-f60e3d9858f6037db3b27d5574ac95310127d302.zip | |
matching now supports nested functions
| -rw-r--r-- | src/matcher/options.test.ts | 20 | ||||
| -rw-r--r-- | src/matcher/options.ts | 8 |
2 files changed, 27 insertions, 1 deletions
diff --git a/src/matcher/options.test.ts b/src/matcher/options.test.ts index f3d498c..058ef9e 100644 --- a/src/matcher/options.test.ts +++ b/src/matcher/options.test.ts @@ -63,5 +63,25 @@ describe('Object Matching:', () => { }; options.match(template, matchee).should.be.true; }); + it('accepts nested objects that match', () => { + let template = { + a: types.number, + b: { + ba: types.string, + bb: types.function + }, + c: types.boolean + }; + let matchee = { + a: 432, + b: { + ba: "barracus", + bb: new Function(), + }, + c: true, + x: false + }; + options.match(template, matchee).should.be.true; + }); }); }); diff --git a/src/matcher/options.ts b/src/matcher/options.ts index b356c7b..e93c2fd 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -6,7 +6,13 @@ export function match(template: Object, value?: Object) { } for (let k of Object.keys(template)) { - if (!template[k](value[k])) { + let f; + if (types.object(template[k])) { + f = match(template[k]); + } else { + f = template[k]; + } + if (!f(value[k])) { return false; } } |
