@jplorg/jpl
Version:
JPL interpreter
49 lines (48 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _library = require("../../../library");
var _utils = require("../utils");
var _default = exports.default = {
/** { by: [op] } */
op(runtime, input, target, params, scope, next) {
return runtime.executeInstructions(params.by ?? [], [input], scope, async by => next(await runtime.alterValue(target, a => {
const b = runtime.unwrapValue(by);
const ta = runtime.type(a);
const tb = runtime.type(b);
if (ta === 'null' || tb === 'null') {
if (ta !== 'null') return a;
if (tb !== 'null') return b;
return null;
}
switch (ta) {
case 'number':
if (tb === 'number') return a + b;
break;
case 'array':
if (tb === 'array') {
if (b.length === 0) return a;
if (a.length === 0) return b;
return [...a, ...b];
}
break;
case 'string':
if (tb === 'string') return a + b;
break;
case 'object':
if (tb === 'object') return (0, _library.applyObject)(a, Object.entries(b));
break;
default:
}
throw new _library.JPLTypeError('%s (%*<100v) and %s (%*<100v) cannot be added together', ta, a, tb, b);
})));
},
/** { by: function } */
map(runtime, params) {
return {
by: (0, _utils.call)(params.by)
};
}
};