diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.ts | 3 | ||||
| -rw-r--r-- | src/matcher/options.test.ts | 67 | ||||
| -rw-r--r-- | src/matcher/options.ts | 69 | ||||
| -rw-r--r-- | src/matcher/types.ts | 33 |
4 files changed, 171 insertions, 1 deletions
@@ -6,6 +6,7 @@ import FileRouter from './FileRouter'; class App { public express + public log_enabled : boolean; constructor () { this.express = express() @@ -15,7 +16,7 @@ class App { // Configure Express middleware. private middleware(): void { - this.express.use(logger('combined')); + if(this.log_enabled) this.express.use(logger('combined')); this.express.use(bodyParser.json()); this.express.use(bodyParser.urlencoded({ extended: false })); } diff --git a/src/matcher/options.test.ts b/src/matcher/options.test.ts new file mode 100644 index 0000000..f3d498c --- /dev/null +++ b/src/matcher/options.test.ts @@ -0,0 +1,67 @@ +import * as mocha from 'mocha'; +import chai = require('chai'); +var should = chai.should(); + +import * as options from './options'; +import { default as types } from './types'; + +describe('Object Matching:', () => { + describe('Rough Matching', () => { + it('accepts basic things', () => { + let template = { + a: types.string + }; + let matchee = { + a: "yes" + }; + options.match(template, matchee).should.be.true; + }); + it('accepts objects with superfluous properties', () => { + let template = { + a: types.number + }; + let matchee = { + a: 40, + b: 20 + }; + options.match(template, matchee).should.be.true; + }); + it('rejects objects missing some properties', () => { + let template = { + a: types.boolean, + b: types.number, + c: types.number + }; + let matchee = { + b: 5 + }; + options.match(template, matchee).should.be.false; + }); + it('accepts objects that omit not_present properties', () => { + let template = { + a: types.function, + b: types.not_present, + c: types.not_present + }; + let matchee = { + a: new Function() + }; + 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 new file mode 100644 index 0000000..1ffa24e --- /dev/null +++ b/src/matcher/options.ts @@ -0,0 +1,69 @@ +import { default as types } from './types'; + +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; + } + } + return true; +} + +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); + + return match(template, value) + && k2.every((x) => k1.indexOf(x) < 0); +} + +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[], value?: any): matchType<any> { + if (value === undefined) { + return alternative.bind(null, templates); + } + + if (templates.length === 0) { + return false; + } else { + let t = templates.map((x) => { + if (types.function(x)) { + return x; + } else if (types.object(x)) { + return match(x); + } else { + return (v) => x === v; + } + }); + return t.some((x) => x(value)); + } +} + +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); +} 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<T>(test: T): (v: any) => v is T { + let f = (value): value is T => + value != null && value.constructor === test.constructor; + return f; +} + +let isObject = typeCheckerGenerator<Object>({}); +let isNumber = typeCheckerGenerator<Number>(0); +let isString = typeCheckerGenerator<String>(""); +let isBoolean = typeCheckerGenerator<Boolean>(true); +let isArray = typeCheckerGenerator<Array<any>>([]); +let isFunction = typeCheckerGenerator<Function>(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; |
