From 79b80371d94e258c2cc1eeee41625ccc212369f6 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Wed, 7 Feb 2018 12:22:49 +0000 Subject: removed types from options.ts to remove ts-node ambiguity --- src/matcher/options.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/matcher') diff --git a/src/matcher/options.ts b/src/matcher/options.ts index 1ffa24e..b356c7b 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -1,9 +1,6 @@ import { default as types } from './types'; -type matchType - = (boolean | ((T) => boolean)); - -export function match(template: Object, value?: Object): matchType { +export function match(template: Object, value?: Object) { if (value === undefined) { return match.bind(null, template); } @@ -16,7 +13,7 @@ export function match(template: Object, value?: Object): matchType { return true; } -export function exact(template: Object, value?: Object): matchType { +export function exact(template: Object, value?: Object) { if (value === undefined) { return exact.bind(null, template); } @@ -28,7 +25,7 @@ export function exact(template: Object, value?: Object): matchType { && k2.every((x) => k1.indexOf(x) < 0); } -export function optional(template: Function, value?: any): matchType { +export function optional(template: Function, value?: any) { if (value === undefined) { return optional.bind(null, template); } @@ -37,7 +34,7 @@ export function optional(template: Function, value?: any): matchType { || template(value); } -export function alternative(templates: any[], value?: any): matchType { +export function alternative(templates: any[], value?: any) { if (value === undefined) { return alternative.bind(null, templates); } @@ -58,8 +55,7 @@ export function alternative(templates: any[], value?: any): matchType { } } -export function array(template: Object, values?: Array): -matchType> { +export function array(template: Object, values?: Array) { if (values === undefined) { return array.bind(null, template); } -- cgit v1.2.1 From f60e3d9858f6037db3b27d5574ac95310127d302 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Wed, 7 Feb 2018 12:48:48 +0000 Subject: matching now supports nested functions --- src/matcher/options.test.ts | 20 ++++++++++++++++++++ src/matcher/options.ts | 8 +++++++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'src/matcher') 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; } } -- cgit v1.2.1 From 5e482ce4408f2e1926d1e9af3270a52ffcc83df6 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Wed, 7 Feb 2018 14:18:55 +0000 Subject: added literal matching --- src/matcher/options.test.ts | 20 +++++++++++++++++++- src/matcher/options.ts | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src/matcher') diff --git a/src/matcher/options.test.ts b/src/matcher/options.test.ts index 058ef9e..39c4b3e 100644 --- a/src/matcher/options.test.ts +++ b/src/matcher/options.test.ts @@ -75,7 +75,7 @@ describe('Object Matching:', () => { let matchee = { a: 432, b: { - ba: "barracus", + ba: "baracus", bb: new Function(), }, c: true, @@ -83,5 +83,23 @@ describe('Object Matching:', () => { }; options.match(template, matchee).should.be.true; }); + it('accepts literal matches', () => { + let template = { + a: "literal" + }; + let matchee = { + a: "literal" + }; + options.match(template, matchee).should.be.true; + }); + it('rejects incorrect literal matches', () => { + let template = { + g: 99 + }; + let matchee = { + g: 80 + }; + options.match(template, matchee).should.be.false; + }); }); }); diff --git a/src/matcher/options.ts b/src/matcher/options.ts index e93c2fd..2e65105 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -9,9 +9,12 @@ export function match(template: Object, value?: Object) { let f; if (types.object(template[k])) { f = match(template[k]); - } else { + } else if (types.function(template[k])) { f = template[k]; + } else { + f = (v) => v === template[k]; } + if (!f(value[k])) { return false; } -- cgit v1.2.1 From 05f1604a853bb3e2a066ceab1305abbc11d1d99d Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 8 Feb 2018 18:33:40 +0000 Subject: Revert "Revert "Merge branch 'testing'"" This reverts commit 0a9ebb01c27ed0a648922d243e877194977ce4d1. --- src/matcher/options.ts | 3 +++ src/matcher/types.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/matcher/types.ts (limited to 'src/matcher') diff --git a/src/matcher/options.ts b/src/matcher/options.ts index 2e65105..7412675 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -1,5 +1,8 @@ import { default as types } from './types'; +type matchType + = (boolean | ((T) => boolean)); + export function match(template: Object, value?: Object) { if (value === undefined) { return match.bind(null, template); diff --git a/src/matcher/types.ts b/src/matcher/types.ts new file mode 100644 index 0000000..70b2d47 --- /dev/null +++ b/src/matcher/types.ts @@ -0,0 +1,33 @@ +function typeCheckerGenerator(test: T): (v: any) => v is T { + let f = (value): value is T => + value != null && value.constructor === test.constructor; + return f; +} + +let isObject = typeCheckerGenerator({}); +let isNumber = typeCheckerGenerator(0); +let isString = typeCheckerGenerator(""); +let isBoolean = typeCheckerGenerator(true); +let isArray = typeCheckerGenerator>([]); +let isFunction = typeCheckerGenerator(new Function()); +let isNull = (v) => v === null; +let isUndefined = (v) => v === undefined; +let isPresent = (v) => !isUndefined(v); +let isInteger = (value: any) => isNumber(value) + && Number.isInteger(value as number); + +let types = { + object: isObject, + number: isNumber, + string: isString, + boolean: isBoolean, + array: isArray, + function: isFunction, + integer: isInteger, + null: isNull, + not_present: isUndefined, + undefined: isUndefined, + anything: isPresent +}; + +export default types; -- cgit v1.2.1 From 7913e19e2f83296dc96a67e6437f6006b1d894ac Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 9 Feb 2018 05:59:48 +0000 Subject: various work I forgot to commit --- src/matcher/options.ts | 9 +++++++++ src/matcher/types.ts | 25 ++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src/matcher') diff --git a/src/matcher/options.ts b/src/matcher/options.ts index 7412675..c1d1aeb 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -75,3 +75,12 @@ export function array(template: Object, values?: Array) { let m = match(template) as ((Object) => boolean); return values.every(m); } + +const options = { + match = match, + exact = exact, + optional = optional, + alternative = alternative, + array = array +}; +export default options; diff --git a/src/matcher/types.ts b/src/matcher/types.ts index 70b2d47..3870df4 100644 --- a/src/matcher/types.ts +++ b/src/matcher/types.ts @@ -4,19 +4,19 @@ function typeCheckerGenerator(test: T): (v: any) => v is T { return f; } -let isObject = typeCheckerGenerator({}); -let isNumber = typeCheckerGenerator(0); -let isString = typeCheckerGenerator(""); -let isBoolean = typeCheckerGenerator(true); -let isArray = typeCheckerGenerator>([]); -let isFunction = typeCheckerGenerator(new Function()); -let isNull = (v) => v === null; -let isUndefined = (v) => v === undefined; -let isPresent = (v) => !isUndefined(v); -let isInteger = (value: any) => isNumber(value) - && Number.isInteger(value as number); +export const isObject = typeCheckerGenerator({}); +export const isNumber = typeCheckerGenerator(0); +export const isString = typeCheckerGenerator(""); +export const isBoolean = typeCheckerGenerator(true); +export const isArray = typeCheckerGenerator>([]); +export const isFunction = typeCheckerGenerator(new Function()); +export const isNull = (v) => v === null; +export const isUndefined = (v) => v === undefined; +export const isPresent = (v) => !isUndefined(v); +export const isInteger = (value: any) => isNumber(value) + && Number.isInteger(value as number); -let types = { +const types = { object: isObject, number: isNumber, string: isString, @@ -29,5 +29,4 @@ let types = { undefined: isUndefined, anything: isPresent }; - export default types; -- cgit v1.2.1 From 3b4320c569dbf962af8a35ca0f16519a1de128db Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 9 Feb 2018 16:17:35 +0000 Subject: minor fixes --- src/matcher/options.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/matcher') diff --git a/src/matcher/options.ts b/src/matcher/options.ts index c1d1aeb..9b07163 100644 --- a/src/matcher/options.ts +++ b/src/matcher/options.ts @@ -77,10 +77,10 @@ export function array(template: Object, values?: Array) { } const options = { - match = match, - exact = exact, - optional = optional, - alternative = alternative, - array = array + match : match, + exact : exact, + optional : optional, + alternative : alternative, + array : array }; export default options; -- cgit v1.2.1