-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
38 lines (34 loc) · 980 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const R = require('ramda')
const using = require('./index')
// Data
const response = [
{ name: 'Zulu', age: 12, role: 'admin' },
{ name: 'John', age: 20, role: 'user' },
{ name: 'Don', age: null, role: 'owner' },
]
// Params
const onlyAdults = false
const sortBy = 'role'
// Chain
const persons = using(response)
// Get only with age
.do(R.filter(person => person.age))
// Get only adluts if it's required (boolean condition example)
.doIf(onlyAdults, R.filter(person => person.age >= 18))
// Throw error unless persons array is not empty (functional condition example)
.doUnless(persons => persons.length, () => { throw new Error('Empty array') })
// Alow only sorting by age and name (default)
.switch(sortBy, {
age: R.sortBy(R.prop('age')),
default: R.sortBy(R.prop('name'))
})
// Get value
.value()
console.log(persons)
// >
/*
[
{ name: 'John', age: 20, role: 'user' },
{ name: 'Zulu', age: 12, role: 'admin' },
]
*/