-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
182 lines (156 loc) · 3.73 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var reduce = require('universal-reduce')
var getStep = require('./get-step')
var done = require('./done')
var sort = require('./sort')
var basic = {
'string': true,
'number': true,
'boolean': true,
'symbol': true,
'function': true
}
module.exports = photocopy
function photocopy (original, tx, seed, step) {
return reduce.unwrap(_photocopy(original, tx, seed, step))
}
function _photocopy (original, tx, seed, step) {
if (typeof tx !== 'function') tx = identity
if (typeof seed === 'undefined') {
seed = new (original.constructor || Object)
}
if (typeof step !== 'function') step = getStep(seed)
// Initialize transducer
var transducer = tx(step)
return transducer(reduce._reduce(original, transducer, seed))
}
function identity (next) {
return next
}
function cat (next) {
return function (acc, value, key) {
if (done(acc, value, key)) {
return next.apply(null, arguments)
}
return photocopy(value, identity, acc, next)
}
}
function steamroll (next) {
return function inner (acc, value, key) {
if (done(acc, value, key)) {
return next.apply(null, arguments)
}
if (value == null || typeof value in basic) return next(acc, value, key)
return photocopy(value, identity, acc, inner)
}
}
var filter = simple(function (fn, next, acc, val, key) {
return fn(val, key) ? next(acc, val, key) : acc
})
var map = simple(function (fn, next, acc, val, key) {
return next(acc, fn(val, key), key)
})
var keyMap = simple(function (fn, next, acc, val, key) {
return next(acc, val, fn(val, key))
})
function take (n) {
return function (next) {
var i = 0
return function (acc, val, key) {
if (done(acc, val, key)) {
return next.apply(null, arguments)
}
if (i >= n) return acc
i += 1
return next(acc, val, key)
}
}
}
function skip (n) {
return function (next) {
var i = 0
return function (acc, val, key) {
if (done(acc, val, key)) {
return next.apply(null, arguments)
}
if (i >= n) return next(acc, val, key)
i += 1
return acc
}
}
}
function cond (condition, ifTrue, ifFalse) {
ifFalse = ifFalse != null ? ifFalse : identity
return function (next) {
next = condNextWrapper(next)
ifTrue = ifTrue(next)
ifFalse = ifFalse(next)
return function (acc, val, key) {
if (done(acc, val, key)) {
return ifFalse(ifTrue(acc, val, key))
}
var fn = condition(val, key) ? ifTrue : ifFalse
return fn(acc, val, key)
}
}
}
function condNextWrapper (next) {
var isFirstBranch = true
return function (acc, val, key) {
if (done(acc, val, key) && isFirstBranch) {
isFirstBranch = false
return acc
}
return next(acc, val, key)
}
}
photocopy({
identity: identity,
cat: cat,
steamroll: steamroll,
simple: simple,
filter: filter,
map: map,
keyMap: keyMap,
reduced: reduce.reduced,
byKey: byKey,
cond: cond,
comp: comp,
done: done,
take: take,
skip: skip,
sort: sort,
_photocopy: _photocopy
}, identity, photocopy)
function byKey (acc, value, key) {
if (done(acc, value, key)) {
return acc
}
var arr = acc[key] || (acc[key] = [])
arr.push(value)
return acc
}
function simple (setup) {
return function (fn) {
return function (next) {
// setup state here
return function (acc, val, key) {
if (done(acc, val, key)) {
return next.apply(null, arguments)
}
return setup(fn, next, acc, val, key)
}
}
}
}
function comp () {
return photocopy(arguments, identity, null, fnStep)
}
function fnStep (acc, value, key) {
if (acc == null) return value
if (done(acc, value, key)) {
return acc
}
return function (next) {
return acc(value(next))
}
}