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,82 +832,89 @@ function _composeOp(a, b, func) {
return func(a, b); return func(a, b);
} }
// pattern composers // Make composers
const composers = { (function() {
set: [(a, b) => b], const num = (pat) => pat._asNumber();
keep: [(a, b) => a], const numOrString = (pat) => pat._asNumber(false, true);
keepif: [(a, b) => (b ? a : undefined)],
// numerical functions // pattern composers
add: [(a, b) => a + b, true], const composers = {
sub: [(a, b) => a - b, true], set: [(a, b) => b],
mul: [(a, b) => a * b, true], keep: [(a, b) => a],
div: [(a, b) => a / b, true], keepif: [(a, b) => (b ? a : undefined)],
mod: [mod, true],
pow: [Math.pow, true],
_and: [(a, b) => a & b, true],
_or: [(a, b) => a | b, true],
_xor: [(a, b) => a ^ b, true],
_lshift: [(a, b) => a << b, true],
_rshift: [(a, b) => a >> b, true],
lt: [(a, b) => a < b], // numerical functions
gt: [(a, b) => a > b], add: [(a, b) => a + b, numOrString], // support string concatenation
lte: [(a, b) => a <= b], sub: [(a, b) => a - b, num],
gte: [(a, b) => a >= b], mul: [(a, b) => a * b, num],
eq: [(a, b) => a == b], div: [(a, b) => a / b, num],
eqt: [(a, b) => a === b], mod: [mod, num],
ne: [(a, b) => a != b], pow: [Math.pow, num],
net: [(a, b) => a !== b], _and: [(a, b) => a & b, num],
and: [(a, b) => a && b], _or: [(a, b) => a | b, num],
or: [(a, b) => a || b], _xor: [(a, b) => a ^ b, num],
_lshift: [(a, b) => a << b, num],
_rshift: [(a, b) => a >> b, num],
// bitwise ops // TODO - force numerical comparison if both look like numbers?
func: [(a, b) => b(a)], lt: [(a, b) => a < b],
}; gt: [(a, b) => a > b],
lte: [(a, b) => a <= b],
gte: [(a, b) => a >= b],
eq: [(a, b) => a == b],
eqt: [(a, b) => a === b],
ne: [(a, b) => a != b],
net: [(a, b) => a !== b],
and: [(a, b) => a && b],
or: [(a, b) => a || b],
// generate methods to do what and how // bitwise ops
for (const [what, [op, numerical]] of Object.entries(composers)) { func: [(a, b) => b(a)],
for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) { };
Pattern.prototype[what + how] = function (...other) {
var pat = this; // generate methods to do what and how
other = sequence(other); for (const [what, [op, preprocess]] of Object.entries(composers)) {
if (numerical) { for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) {
pat = pat._asNumber(); Pattern.prototype[what + how] = function (...other) {
other = other._asNumber(); var pat = this;
other = sequence(other);
if (preprocess) {
pat = preprocess(pat);
other = preprocess(other);
}
var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op));
// hack to remove undefs when doing 'keepif'
if (what === 'keepif') {
result = result._removeUndefineds();
}
return result;
};
if (how === 'Squeeze') {
// support 'squeezeIn' longhand
Pattern.prototype[what + 'SqueezeIn'] = Pattern.prototype[what + how];
} }
var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op)); if (how === 'In') {
// hack to remove undefs when doing 'keepif' // default how to 'in', e.g. add == addIn
if (what === 'keepif') { Pattern.prototype[what] = Pattern.prototype[what + how];
result = result._removeUndefineds(); } else {
} // default what to 'set', e.g. squeeze = setSqueeze
return result; if (what === 'set') {
}; Pattern.prototype[how.toLowerCase()] = Pattern.prototype[what + how];
if (how === 'Squeeze') { }
// support 'squeezeIn' longhand
Pattern.prototype[what + 'SqueezeIn'] = Pattern.prototype[what + how];
}
if (how === 'In') {
// default how to 'in', e.g. add == addIn
Pattern.prototype[what] = Pattern.prototype[what + how];
} else {
// default what to 'set', e.g. squeeze = setSqueeze
if (what === 'set') {
Pattern.prototype[how.toLowerCase()] = Pattern.prototype[what + how];
} }
} }
} }
}
// binary composers // binary composers
Pattern.prototype.struct = Pattern.prototype.keepifOut; Pattern.prototype.struct = Pattern.prototype.keepifOut;
Pattern.prototype.structAll = Pattern.prototype.keepOut; Pattern.prototype.structAll = Pattern.prototype.keepOut;
Pattern.prototype.mask = Pattern.prototype.keepifIn; Pattern.prototype.mask = Pattern.prototype.keepifIn;
Pattern.prototype.maskAll = Pattern.prototype.keepIn; Pattern.prototype.maskAll = Pattern.prototype.keepIn;
Pattern.prototype.reset = Pattern.prototype.keepifTrig; 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 = [