cmpr
cmpr is an comparison and validation library with pattern matching that is written to be easily composable - thus extendable to suit any need.
Getting started
One thing to note is that all functions in cmpr are curried. If you haven't heard about currying before, don't worry. All you need to know to use cmpr is that:
// true
...becomes...
1 // true
This makes it easier to compose comparisons and validators for your own use:
;{thistitle = title;thisrating = rating;}const movies ='Sound of Music' 80'Angry Men' 89;const isMovie = ;const isGoodMovie = arating >= 75;const goodMovieTest = ;const hasGoodMovieTaste = ;; // true
Shallow and deep comparison
Shallowly comparing an array or object is very straight forward:
;'Hiromi' 'Chick' 'Jordan'; // true| ||- Has primitive value equality -|name: 'Hiromi Uehara' ; // false| ||---- No referential equality ---|
A shallow compare will compare the values in an array or object by value for primitives and reference for objects.
A deep compare does the same thing but will recurs on objects so it can compare the primitives held inside.
;artists:name: 'Pat Metheny'featuredAlbum: 'Secret Story'; // true
Fuzzy comparisons
Shallow and deep compares have fuzzy versions. This allows us to define subsets to match for in our comparisons.
;'Move' 'Brain' 'Spiral'; // truedrums: 'Morgan Ågren'keyboard: 'Mats Öberg'; // true
;const hasMove =;const albums ='Pat Metheny':'Secret Story':year: 1992'Hiromi Uehara':Move:year: 2012;; // true
Comparators (pattern matching)
cmpr can create comparators that describe how values should be compared. We can nest these descriptive objects in our comparisons to achieve pattern matching.
const theTrioProject =piano: 'Hiromi Uehara'drums: 'Simon Phillips'bass: 'Anthony Jacksson';const isValidLineup =;; // true
...which can be rewritten to be more composable...
const hasPianist = ;const hasDrummer = ;const hasBassist = ;const isValidLineup = ;; // true
const myArr = 1 21 myArr true; // true| | || | by reference| || by value | by comporator1 1 2 true; // true| | || | recursive isDeepEq| || by value | by comporator
Wildcard comparator
cmpr provides a special wildcard comparator that can be used to fill in blanks in your matching, for example:
;// alias: import { wildcard_ } from 'cmpr';x; // truex; // truea: x ; // truex z x; // truea: z c: x ; // truex z z; // falsex; // falsec: x ; // false
Examples
Example uses of cmpr:
- eav - Experimental spy library leveraging Proxy API that uses cmpr for matching called arguments.
- ptrn - Use cmpr to declare functions with pattern matching.
API
Visit the Wiki to read the auto-generated API documentation.