-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
118 lines (103 loc) · 2.44 KB
/
Copy pathutils.js
File metadata and controls
118 lines (103 loc) · 2.44 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { crocks, isHyperErr, R } from './deps.js'
const { Async, Either, eitherToAsync } = crocks
const { Left, Right } = Either
const {
ifElse,
complement,
isNil,
toLower,
head,
toPairs,
compose,
equals,
allPass,
omit,
transduce,
append,
pluck,
map,
filter,
is: rIs,
cond,
defaultTo,
identity,
T,
} = R
export const isNotEqual = complement(equals)
export const isDefined = complement(isNil)
export const isDesignDoc = (doc) => (/^_design/.test(doc._id))
export const isNotDesignDoc = complement(isDesignDoc)
export const omitRev = omit(['rev', '_rev'])
/**
* A transduce allows us to iterate the array only once,
* performing composed transformations inlined with reducing,
* -- hence "trans"-"duce".
*
* This prevents iterating the array multiple times to perform multiple
* transformations
*
* NOTE: compositions passed to transduce run top -> bottom instead of the usual
* bottom to top. This is becase we are composing transformers which are functions
* not values
*/
export const foldWith = (transformer) => (iter) =>
transduce(transformer, (acc, item) => append(item, acc), [], iter)
export const sanitizeDocs = foldWith(
compose(
filter(allPass([isDefined, isNotDesignDoc])),
map(omitRev),
),
)
/**
* Each row is like
* {
key: '1',
id: '1',
value: { rev: '1' },
doc: { _id: '1', _rev: '1', hello: 'world' },
}
So pluck the doc first to pass into sanitizing
*/
export const sanitizeRows = foldWith(
compose(
pluck('doc'),
filter(allPass([isDefined, isNotDesignDoc])),
map(omitRev),
),
)
const is = (pred) => (value) => (pred(value) ? Right(value) : Left(value))
export const asyncIs = compose(eitherToAsync, is)
export const handleHyperErr = ifElse(
isHyperErr,
Async.Resolved,
Async.Rejected,
)
/**
* Given an array of hyper sort criteria,
* return an array of Couch sort criteria.
*
* If no, sort criteria is provided, then this noops
*
* @param {string[] | Object[]} [sort]
* @returns {string[] | Object[] | undefined}
*/
export const mapSort = (sort) => {
if (!sort || !sort.length) return sort
return sort.map(cond([
[rIs(String), identity],
[
rIs(Object),
compose(
([k, v]) => ({ [k]: toLower(v) }),
head,
toPairs,
),
],
[T, identity],
]))
}
/**
* Given a hyper selector, default to an empty object
* if the selector is nil
*/
export const mapSelector = defaultTo({})