aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2018-02-07 12:48:48 +0000
committerTom Harley2018-02-07 12:50:02 +0000
commitf60e3d9858f6037db3b27d5574ac95310127d302 (patch)
tree73d93b7a57862bfae9a560fafb983d076a1ba637
parentremoved types from options.ts to remove ts-node ambiguity (diff)
downloadmedicine-f60e3d9858f6037db3b27d5574ac95310127d302.tar.gz
medicine-f60e3d9858f6037db3b27d5574ac95310127d302.zip

matching now supports nested functions

-rw-r--r--src/matcher/options.test.ts20
-rw-r--r--src/matcher/options.ts8
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;
}
}