-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
123 lines (99 loc) · 2.43 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
(function() {
// dependencies
var app = self.app;
var u = app.util;
// methods
u.noop = function noop() {
// noop
};
u.extend = function extend(dest, src) {
for (var key in src) dest[key] = src[key];
};
u.log = function log(arg) {
console.log(arg);
};
u.map = function map(array, fn) {
for (var i = 0; i < array.length; i++) {
array[i] = fn(array[i]);
}
return array;
};
u.push = function push(a, b) {
return function(arg) {
b(a(arg));
};
};
u.pull = function pull(key) {
return function(obj) {
return obj[key];
};
};
u.toArrayBuffer = u.pull('buffer');
u.toFloat32Array = function toFloat32Array(bufferSize) {
return function(buffer) {
return new Float32Array(buffer || bufferSize);
};
};
u.isMonophonic = function isMonophonic(sample) {
return !Array.isArray(sample);
};
u.isNumber = function isNumber(sample) {
if (isNaN(sample)) return new Error('sample is NaN');
else if (Math.abs(sample) === Infinity) return new Error('sample is Infinity');
};
u.toStereo = function toStereo(fn) {
var sample = [0, 0];
return function(t, frame) {
sample[0] =
sample[1] = fn(t, frame);
return sample;
};
};
u.stereoOr = function stereoOr(sample, fn) {
return fn(sample[0]) || fn(sample[1]);
};
// http://stackoverflow.com/a/28483558
u.insertTab = function insertTab(ev) {
ev.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
var val = this.value;
var selected = val.substring(start, end);
var re, match, count;
if (ev.shiftKey) {
re = /^\t/gm;
match = selected.match(re);
count = -match.length;
this.value = val.substring(0, start) + selected.replace(re, '') + val.substring(end);
// TODO: add support for shift-tabbing without a selection
} else {
re = /^/gm;
match = selected.match(re);
count = match.length;
this.value = val.substring(0, start) + selected.replace(re, '\t') + val.substring(end);
}
if (start === end) {
this.selectionStart = end + count;
} else {
this.selectionStart = start;
}
this.selectionEnd = end + count;
};
u.debounce = function debounce(fn, ms) {
var timeout = null;
return function(arg) {
clearTimeout(timeout);
timeout = setTimeout(fn, ms, arg);
};
};
u.errorFrom = function errorFrom(error) {
var err = new Error(error.message);
err.stack = error.stack;
return err;
};
u.swap = function swap(b, a, key) {
var tmp = a[key];
a[key] = b;
return tmp;
};
})();