src/utils/matchers.coffee |
|
|---|---|
This file contains a set of matchers that can be used in various situations such as array filters or duck tests. Matchers are curried functions that generates testing functions based on the arguments passed to the generator.
Table Of Content |
|
match |
|
The match function either call Match is used whenever a matcher function allow matchers as arguments. |
match = (m,v) -> if typeof m is 'function' then m v else m is v |
anyOf |
|
Returns
|
anyOf = (matchers...) -> (el) -> return true for m in matchers when match m, el false |
allOf |
|
Returns
|
allOf = (matchers...) -> (el) -> return false for m in matchers when not match m, el true |
equalTo |
|
Compares
|
equalTo = (val) -> (el) -> el is val |
greaterThan |
|
Returns
|
greaterThan = (val) -> (el) -> el > val |
greaterThanOrEqualTo |
|
Returns
|
greaterThanOrEqualTo = (val) -> (el) -> el >= val |
hasProperty |
|
The
|
hasProperty = (prop, val) -> (el) -> el[prop] isnt undefined and if val? then match val, el[prop] else true |
hasProperties |
|
The You can path both string and objects as arguments, whatever order. A string will only test for the existence of the property and an object will test for each property defined in the object, the values of the argument properties being the matchers to use against the tested objet properties.
|
hasProperties = (propsets...) -> (el) -> results = true for propset in propsets if typeof propset is 'string' results &&= el[propset] isnt undefined else for k,v of propset results &&= hasProperty(k,v)(el) results |
isNot |
|
Inverse the boolean value returned by a matcher, or test the difference
with
|
isNot = (m) -> (el) -> not match m, el |
isNotNull |
|
Returns |
isNotNull = -> (el) -> el? |
isNull |
|
Returns |
isNull = -> (el) -> not el? |
isType |
|
Returns
|
isType = (type) -> (el) -> typeof el is type |
lowerThan |
|
Returns
|
lowerThan = (val) -> (el) -> el < val |
lowerThanOrEqualTo |
|
Returns
|
lowerThanOrEqualTo = (val) -> (el) -> el <= val |
quacksLike |
|
Performs a duck test with
|
quacksLike = (def) -> (el) -> el?.quacksLike def module.exports = { allOf, anyOf, equalTo, greaterThan, greaterThanOrEqualTo, hasProperties, hasProperty, isNot, isNotNull, isNull, isType, lowerThan, lowerThanOrEqualTo, quacksLike, } |