UNPKG

@coorpacademy/progression-engine

Version:

42 lines (34 loc) 1.53 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } import reduce from 'lodash/fp/reduce'; import set from 'lodash/fp/set'; import update from 'lodash/fp/update'; 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 _reduce = reduce(apply, _extends({ lives: fromState.lives, stars: fromState.stars }, fromState.variables), instructions), { lives, stars } = _reduce, variables = _objectWithoutProperties(_reduce, ['lives', 'stars']); return _extends({}, fromState, { lives, stars, variables }); }; export default updateVariables; //# sourceMappingURL=apply-instructions.js.map