This repository has been archived by the owner on Nov 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
helpers.js
166 lines (131 loc) · 3.33 KB
/
helpers.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
let R = require("ramda")
let {append, curry, drop, pipe, split, update} = require("ramda")
let fst = (xs) => xs[0]
let snd = (xs) => xs[1]
// a -> b -> a
let always = curry((x, y) => x)
// String -> Lens
let lens = curry((path) => {
return path ?
R.lensPath(split(".", path)) :
R.lensPath([])
})
// (a -> Boolean) -> a -> [a] -> [a]
let updateBy = curry((pred, val, array) => {
let i = findIndex(pred, array);
if (i >= 0) {
return update(i, val, array);
} else {
return array;
}
})
// (a -> Boolean) -> (a -> b) -> [a] -> [a]
let adjustBy = curry((pred, adjustFn, array) => {
let i = findIndex(pred, array);
if (i >= 0) {
return adjust(adjustFn, i, array);
} else {
return array;
}
})
let swap = curry((i1, i2, array) => {
let v1 = array[i1]
let v2 = array[i2]
return pipe(
update(i1, v2),
update(i2, v1)
)(array)
})
let randomInt = curry((min, max) => {
return Math.floor(Math.random() * (max - min)) + min
})
let pickRandom = curry((array) => {
return array[randomInt(0, array.length)]
})
let shuffle = curry((array) => {
let counter = array.length
while (counter > 0) {
let index = randomInt(0, counter)
counter -= 1
array = swap(index, counter, array)
}
return array
})
let isPlainObject = curry((value) => {
return value && value.constructor.prototype === Object.prototype
})
let flattenObject = curry((target) => {
let result = {}
function step(object, prev) {
Object.keys(object).forEach((key) => {
let value = object[key]
let newKey = prev ? prev + `.` + key : key
if (isPlainObject(value) && Object.keys(value).length) {
return step(value, newKey)
}
result[newKey] = value
})
}
step(target)
return result
})
let unflattenObject = curry((target) => {
let result = {}
if (!isPlainObject(target)) {
return target
}
function getkey(key) {
let parsedKey = Number(key)
return isNaN(parsedKey) || key.indexOf(`.`) !== -1 ? key : parsedKey
}
Object.keys(target).forEach((key) => {
let split = key.split(`.`)
let key1 = getkey(split.shift())
let key2 = getkey(split[0])
let recipient = result
while (typeof key2 !== `undefined`) {
if (!isPlainObject(recipient[key1])) {
recipient[key1] = {}
}
recipient = recipient[key1]
if (split.length > 0) {
key1 = getkey(split.shift())
key2 = getkey(split[0])
}
}
recipient[key1] = unflattenObject(target[key])
})
return result
})
let withPrefix = curry((s, st) => {
if (st.startsWith(s)) { return st }
else { return s + st }
})
let withSuffix = curry((s, st) => {
if (st.endsWith(s)) { return st }
else { return st + s }
})
let appendSliding = curry((n, x, xs) => {
let ys = append(x, xs)
if (ys.length > n) {
return drop(ys.length - n, ys)
} else {
return ys
}
})
exports.always = always
exports.fst = fst
exports.snd = snd
exports.adjustBy = adjustBy
exports.updateBy = updateBy
exports.swap = swap
exports.lens = lens
exports.randomInt = randomInt
exports.pickRandom = pickRandom
exports.shuffle = shuffle
exports.isPlainObject = isPlainObject
exports.flattenObject = flattenObject
exports.unflattenObject = unflattenObject
exports.withPrefix = withPrefix
exports.withSuffix = withSuffix
exports.appendSliding = appendSliding