-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
174 lines (145 loc) · 3.74 KB
/
util.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
export const toFinite = n => Number.isFinite(n) ? n : 0
export const clamp = (min, max, n) => Math.max(min, Math.min(max, n))
export const parseFn = fn => {
let s = fn.toString()
let [args, body] = (s.split('\n')[0].includes('=>')
? s.split('=>')
: [s.slice(s.indexOf('('), s.indexOf(')')+1), s.slice(s.indexOf(' {'))]
).map(s => s.trim())
args = args.replace(/[\(\)]/g, '').split(',')
let argNames = args.map(a => a.split('=')[0])
let inner = body[0] === '{' ? body.split('\n').slice(1,-1).join('\n').trim() : body
return { args, argNames, body, inner }
}
const notes = 'ccddeffggaab'
export const stringToNote = s => {
s = s.split('')
let octave = parseInt(s[s.length - 1], 10)
if (isNaN(octave)) octave = 4
const note = s[0].toLowerCase()
const flat = s[1] === 'b'
const sharp = s[1] === '#'
return notes.indexOf(note) + (octave * 12) + sharp - flat
}
const EXCESS_WHITESPACE = / {1,}|\n/g
const HAS_LETTER = /[a-zA-Z]/
export const parsePattern = x => x
.replace(EXCESS_WHITESPACE, ' ') // remove excess whitespace
.trim() // and trim
.split(' ') // split to array of values
.map(n => toFinite(
HAS_LETTER.test(n)
? stringToNote(n) // has a letter then it is a musical note
: parseFloat(n) // otherwise it's a scalar
))
// export const ProxyChain = () => {
// const acc = []
// const add = (a0,a1,a2,a3,a4) => {
// acc[acc.length-1].push(a0,a1,a2,a3,a4)
// return proxy
// }
// const handler = {
// get (obj, prop) {
// acc.push([prop])
// return add
// },
// apply () {
// return acc.splice(0)
// }
// }
// const proxy = new Proxy(() => {}, handler)
// const init = () => {
// acc.splice(0)
// return proxy
// }
// return init
// }
export const actlessProxy = (capture = () => {}) => {
const chain = () => proxy
const handler = {
get (obj, prop) {
capture(prop)
return chain
},
apply () {
return proxy
}
}
const proxy = new Proxy(chain, handler)
return proxy
}
export const captureAllProxy = (context, begin, capture) => {
const acc = []
const add = (a0,a1,a2,a3,a4) => {
acc[acc.length-1].push(a0,a1,a2,a3,a4)
return proxy
}
const handler = {
get (obj, prop) {
acc.push([prop])
return add
},
apply () {
return proxy
}
}
const proxy = new Proxy(() => {}, handler)
const actless = actlessProxy(capture)
return (a0,a1,a2,a3,a4) => {
if (context.i === 0) {
acc.splice(0)
begin(acc,a0,a1,a2,a4,a4)
return proxy
} else {
return actless
}
}
}
export const captureManyProxy = (context,parent,begin,end) => {
const acc = []
const add = (a0,a1,a2,a3,a4) => {
acc[acc.length-1].push(a0,a1,a2,a3,a4)
return proxy
}
const run = calls => () => {
calls.forEach(([method,a0,a1,a2,a3,a4]) =>
parent[method](a0,a1,a2,a3,a4))
}
const handler = {
get (obj, prop) {
if (prop === 'out' || prop === 'on' || prop === Symbol.toPrimitive) {
const rest = acc.splice(1)
proxy()
run(rest)()
return parent[prop]
}
acc.push([prop])
return add
},
apply () {
end(run(acc.splice(0)), context)
return parent
}
}
const proxy = new Proxy(() => {}, handler)
return (a0,a1,a2,a3,a4) => {
acc.splice(0)
begin(context,a0,a1,a2,a3,a4)
return proxy
}
}
export const captureOneProxy = (context,parent,begin,end) => {
const handler = {
get (obj, prop) {
return (a0,a1,a2,a3,a4) => {
end(() => { parent[prop](a0,a1,a2,a3,a4) }, context)
return parent
}
}
}
const proxy = new Proxy(() => {}, handler)
return (a0,a1,a2,a3,a4) => {
begin(context,a0,a1,a2,a3,a4)
return proxy
}
}