UNPKG

@sanity/form-builder

Version:
36 lines (35 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = apply; var OPERATIONS = { replace(currentValue, nextValue) { return nextValue; }, set(currentValue, nextValue) { return nextValue; }, setIfMissing(currentValue, nextValue) { return currentValue === undefined ? nextValue : currentValue; }, unset(currentValue, nextValue) { return undefined; }, inc(currentValue, nextValue) { return currentValue + nextValue; }, dec(currentValue, nextValue) { return currentValue - nextValue; } }; var SUPPORTED_PATCH_TYPES = Object.keys(OPERATIONS); function apply(value, patch) { if (!SUPPORTED_PATCH_TYPES.includes(patch.type)) { throw new Error("Received patch of unsupported type \"".concat(patch.type, "\" for primitives. This is most likely a bug.")); } if (patch.path.length > 0) { throw new Error("Cannot apply deep operations on primitive values. Received patch with type \"".concat(patch.type, "\" and path \"").concat(patch.path.map(path => JSON.stringify(path)).join('.'), " that targeted the value \"").concat(JSON.stringify(value), "\"")); } return OPERATIONS[patch.type](value, patch.value); }