Support string concatenation for add() and variants

This commit is contained in:
alex 2022-05-02 09:40:06 +01:00
parent 4779dd13c0
commit 6273b34254

View file

@ -832,6 +832,11 @@ function _composeOp(a, b, func) {
return func(a, b); return func(a, b);
} }
// Make composers
(function() {
const num = (pat) => pat._asNumber();
const numOrString = (pat) => pat._asNumber(false, true);
// pattern composers // pattern composers
const composers = { const composers = {
set: [(a, b) => b], set: [(a, b) => b],
@ -839,18 +844,19 @@ const composers = {
keepif: [(a, b) => (b ? a : undefined)], keepif: [(a, b) => (b ? a : undefined)],
// numerical functions // numerical functions
add: [(a, b) => a + b, true], add: [(a, b) => a + b, numOrString], // support string concatenation
sub: [(a, b) => a - b, true], sub: [(a, b) => a - b, num],
mul: [(a, b) => a * b, true], mul: [(a, b) => a * b, num],
div: [(a, b) => a / b, true], div: [(a, b) => a / b, num],
mod: [mod, true], mod: [mod, num],
pow: [Math.pow, true], pow: [Math.pow, num],
_and: [(a, b) => a & b, true], _and: [(a, b) => a & b, num],
_or: [(a, b) => a | b, true], _or: [(a, b) => a | b, num],
_xor: [(a, b) => a ^ b, true], _xor: [(a, b) => a ^ b, num],
_lshift: [(a, b) => a << b, true], _lshift: [(a, b) => a << b, num],
_rshift: [(a, b) => a >> b, true], _rshift: [(a, b) => a >> b, num],
// TODO - force numerical comparison if both look like numbers?
lt: [(a, b) => a < b], lt: [(a, b) => a < b],
gt: [(a, b) => a > b], gt: [(a, b) => a > b],
lte: [(a, b) => a <= b], lte: [(a, b) => a <= b],
@ -867,14 +873,14 @@ const composers = {
}; };
// generate methods to do what and how // generate methods to do what and how
for (const [what, [op, numerical]] of Object.entries(composers)) { for (const [what, [op, preprocess]] of Object.entries(composers)) {
for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) { for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) {
Pattern.prototype[what + how] = function (...other) { Pattern.prototype[what + how] = function (...other) {
var pat = this; var pat = this;
other = sequence(other); other = sequence(other);
if (numerical) { if (preprocess) {
pat = pat._asNumber(); pat = preprocess(pat);
other = other._asNumber(); other = preprocess(other);
} }
var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op)); var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op));
// hack to remove undefs when doing 'keepif' // hack to remove undefs when doing 'keepif'
@ -908,6 +914,7 @@ Pattern.prototype.reset = Pattern.prototype.keepifTrig;
Pattern.prototype.resetAll = Pattern.prototype.keepTrig; Pattern.prototype.resetAll = Pattern.prototype.keepTrig;
Pattern.prototype.restart = Pattern.prototype.keepifTrigzero; Pattern.prototype.restart = Pattern.prototype.keepifTrigzero;
Pattern.prototype.restartAll = Pattern.prototype.keepTrigzero; Pattern.prototype.restartAll = Pattern.prototype.keepTrigzero;
})();
// methods of Pattern that get callable factories // methods of Pattern that get callable factories
Pattern.prototype.patternified = [ Pattern.prototype.patternified = [