This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (135 loc) · 4.22 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
var memoize = require('meemo');
function hashArgs () {
return [].slice.call(arguments).join('//');
}
function serializeArgs () {
return [].slice.call(arguments).map(function (arg) {
return typeof arg === 'object' ? JSON.stringify(arg) : arg;
}).join('//');
}
var isPlainPosition = memoize(function (position) {
return !!position.match(/^\d+\.\d+\.\d+$/);
});
var getFragments = memoize(function (position) {
return position.split('.').map(function (fragment) {
var fragmentNumber = parseInt(fragment, 10);
if (fragment.match(/^\d+$/) && !isNaN(fragmentNumber)) return fragmentNumber;
return fragment;
});
});
var skipInRange = memoize(function (min, end, offset, step) {
var all = [];
while (offset <= end) {
if (offset > min) {
all.push(offset);
}
offset += step;
}
return all;
}, serializeArgs);
var getRange = memoize(function (start, end) {
var all = [];
var i = start;
while (i <= end) {
all.push(i);
i++;
}
return all;
}, hashArgs);
var filterModulus = memoize(function (range, mod, res) {
return range.filter(function (n) {
return n % mod === res;
});
}, hashArgs);
var gtRe = />(\d+)/;
var ltRe = /<(\d+)/;
function expandToTree (fragment, tree, end, expander) {
var start = 1;
if (/^\d+$/.test(fragment)) {
tree[fragment] = {};
return;
}
var gtMatch = fragment.match(gtRe) || 0;
if (gtMatch) {
gtMatch = gtMatch && parseInt(gtMatch[1], 10);
start = gtMatch + 1;
fragment = fragment.replace(gtRe, '');
}
var ltMatch = fragment.match(ltRe) || 0;
if (ltMatch) {
ltMatch = ltMatch && parseInt(ltMatch[1], 10);
end = ltMatch - 1;
fragment = fragment.replace(ltRe, '');
}
var range = getRange(start, end);
// start new chain here, in case range was changed by gt/lt
if (!fragment || fragment === '*') {
range.forEach(function (n) {
tree[n] = {};
});
}
else if (fragment === 'odd' || fragment === 'even') {
var rest = fragment === 'even' ? 0 : 1;
filterModulus(range, 2, rest).forEach(function (n) {
tree[n] = {};
});
}
else if (typeof fragment === 'string' && ~fragment.indexOf('%')) {
var pieces = fragment.split('%');
var offset = pieces[0] && parseInt(pieces[0], 10) || 1;
var step = parseInt(pieces[1], 10);
skipInRange(gtMatch, end, offset, step).forEach(function (n) {
tree[n] = {};
});
}
else if (typeof expander === 'function') {
var result = expander(fragment, start, end);
if (Array.isArray(result)) {
result.forEach(function (n) {
tree[n] = {};
});
}
}
}
function flattenTree (tree) {
var all = [];
Object.keys(tree).forEach(function (bar) {
Object.keys(tree[bar]).forEach(function (beat) {
Object.keys(tree[bar][beat]).forEach(function (tick) {
tick = tick < 10 ? '0' + tick : tick;
all.push([bar, beat, tick].join('.'));
});
});
});
return all;
}
var expandExpression = memoize(function (position, bars, beats, expander) {
var fragments = getFragments(position);
var tree = {};
expandToTree(fragments[0], tree, bars, expander);
Object.keys(tree).forEach(function (bar) {
expandToTree(fragments[1], tree[bar], beats, expander);
Object.keys(tree[bar]).forEach(function (beat) {
expandToTree(fragments[2], tree[bar][beat], 96, expander);
});
});
return flattenTree(tree);
}, hashArgs);
function expressions (notes, options) {
if (!notes) throw new Error('Invalid "notes" array');
if (!options || typeof options !== 'object') throw new Error('Invalid "options" object');
if (typeof options.beatsPerBar !== 'number' || options.beatsPerBar < 0) throw new Error('Invalid options: beatsPerBar is not a valid number');
if (typeof options.barsPerLoop !== 'number' || options.barsPerLoop < 0) throw new Error('Invalid options: barsPerLoop is not a valid number');
var all = [];
notes.forEach(function (event) {
var position = event[0];
if (isPlainPosition(position)) return all.push(event);
expandExpression(position, options.barsPerLoop, options.beatsPerBar, options.expander).forEach(function (expanded) {
var clone = event.slice();
clone[0] = expanded;
all.push(clone);
});
});
return all;
}
module.exports = expressions;