UNPKG

@coorpacademy/progression-engine

Version:

40 lines 980 B
import _update from "lodash/fp/update"; import _set from "lodash/fp/set"; import _reduce from "lodash/fp/reduce"; const apply = (variables, instruction) => { const { type, value, field } = instruction; switch (type) { case 'set': return _set(field, value, variables); case 'add': return _update(field, v => { if (typeof v === typeof value && (typeof v === 'number' || typeof v === 'string') && (typeof value === 'number' || typeof value === 'string')) return v + value; return v; }, variables); default: return variables; } }; const updateVariables = instructions => fromState => { const { lives, stars, ...variables } = _reduce(apply, { lives: fromState.lives, stars: fromState.stars, ...fromState.variables }, instructions); return { ...fromState, lives, stars, variables }; }; export default updateVariables; //# sourceMappingURL=apply-instructions.js.map