Package Exports
- tmatch
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tmatch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
tmatch
This module exists to facilitate the t.match()
method in
tap
.
It checks whether a value matches a given "pattern". A pattern is an object with a set of fields that must be in the test object, or a regular expression that a test string must match, or any combination thereof.
The algorithm is borrowed heavily from
only-shallow
, with some notable
differences with respect to the handling of missing properties and the
way that regular expressions are compared to strings.
usage
var matches = require('tmatch')
if (!matches(testObject, pattern)) console.log("yay! diversity!");
// somewhat more realistic example..
http.get(someUrl).on('response', function (res) {
var expect = {
statusCode: 200,
headers: {
server: /express/
}
}
if (!tmatch(res, expect)) {
throw new Error('Expect 200 status code from express server')
}
})
details
Copied from the source, here are the details of only-shallow
's algorithm:
- If the object is a string, and the pattern is a RegExp, then return
true if
pattern.test(object)
. - Use loose equality (
==
) only for all other value types (non-objects).tmatch
cares more about shape and contents than type. This step will also catch functions, with the useful (default) property that only references to the same function are considered equal. 'Ware the halting problem! null
is an object – a singleton value object, in fact – so if either isnull
, return object == pattern.- Since the only way to make it this far is for
object
orpattern
to be an object, ifobject
orpattern
is not an object, they're clearly not a match. - It's much faster to compare dates by numeric value (
.getTime()
) than by lexical value. - Compare RegExps by their components, not the objects themselves.
- The parts of an arguments list most people care about are the arguments themselves, not the callee, which you shouldn't be looking at anyway.
- Objects are more complex:
- Return
true
ifobject
andpattern
both have no properties. - Ensure that cyclical references don't blow up the stack.
- Ensure that all the key names in
pattern
exist inobject
. - Ensure that all of the associated values match, recursively.
- Return
license
ISC. Go nuts.