Utilities built on top of Ramda.
### compareProps ( props , a , b )
Returns a curried comparator function that can be used with R.sort
. Supports any number of sort orders (i.e. property1 ascending, property 2 descending, etc.). It also supports type based sorting (i.e. recognizes Date
, Number
, etc. and sorts them appropriately).
Arguments
props
- A string or an array of strings used to determine the sort order to apply. Each string signifies a property name to compare. A '+'
prefix is used to signify ascending order and a '-'
prefix is used to signify descending order.a
- A value to compare.b
- A value to compare.
Example
var list = [
{ b: true, n: 10, s: 'test', d: new Date('1/1/2015') },
{ b: false, n: -1, s: 'aaaa', d: new Date('1/1/2015') },
{ b: true, n: 12, s: 'aaaa', d: new Date('1/1/2015') },
{ b: false, n: 3, s: 'xyz', d: new Date('1/1/2000') },
{ b: false, n: 12, s: 'aaaa', d: new Date('1/1/2015') }
];
var props = ['-d', '+s', '-n', 'b'];
R.sort(Ru.compareProps(props), list);
### defaults ( def , obj )
Creates a new object with the own properties of def
merged with the defined own properties of obj
.
Any properties of obj
that resolve to undefined
will be replaced by the corresponding properties in def
if they exist. Otherwise, properties that resolve to undefined
in both obj
and def
will be omitted in the returned object.
Arguments
def
- An object containing default properties.obj
- An object to default.
Example
var def = {a: 1, b: 2, c: 3};
var obj = {a: 4, b: undefined};
var defObj = Ru.defaults(def, obj);
### subsetOf ( set , sub )
Tests if an array is a subset of another.
Arguments
set
- An array that defines the complete set.sub
- An array to test.
Example
var set = [1, 2, 3, 4];
var sub = [2, 3];
var notSub = [4, 5];
Ru.subsetOf(set, sub)
Ru.subsetOf(set, notSub))