@lyra/form-builder
Version:
Lyra form builder
108 lines (84 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _findIndex2 = require('lodash/findIndex');
var _findIndex3 = _interopRequireDefault(_findIndex2);
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; };
exports.default = apply;
var _hasOwn = require('../utils/hasOwn');
var _hasOwn2 = _interopRequireDefault(_hasOwn);
var _applyPatch = require('./applyPatch');
var _applyPatch2 = _interopRequireDefault(_applyPatch);
var _arrayInsert = require('./arrayInsert');
var _arrayInsert2 = _interopRequireDefault(_arrayInsert);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
function move(arr, from, to) {
const nextValue = arr.slice();
const val = nextValue[from];
nextValue.splice(from, 1);
nextValue.splice(to, 0, val);
return nextValue;
}
function findTargetIndex(array, pathSegment) {
if (typeof pathSegment === 'number') {
return pathSegment;
}
const index = (0, _findIndex3.default)(array, pathSegment);
return index === -1 ? false : index;
}
function apply(value, patch) {
const nextValue = value.slice(); // make a copy for internal mutation
if (patch.path.length === 0) {
// its directed to me
if (patch.type === 'setIfMissing') {
if (!Array.isArray(patch.value)) {
// eslint-disable-line max-depth
throw new Error('Cannot set value of an array to a non-array');
}
return value === undefined ? patch.value : value;
} else if (patch.type === 'set') {
if (!Array.isArray(patch.value)) {
// eslint-disable-line max-depth
throw new Error('Cannot set value of an array to a non-array');
}
return patch.value;
} else if (patch.type === 'unset') {
return undefined;
} else if (patch.type === 'move') {
if (!patch.value || !(0, _hasOwn2.default)(patch.value, 'from') || !(0, _hasOwn2.default)(patch.value, 'to')) {
// eslint-disable-line max-depth
throw new Error(`Invalid value of 'move' patch. Expected a value with "from" and "to" indexes, instead got: ${JSON.stringify(patch.value)}`);
}
return move(nextValue, patch.value.from, patch.value.to);
}
throw new Error(`Invalid array operation: ${patch.type}`);
}
var _patch$path = _toArray(patch.path);
const head = _patch$path[0],
tail = _patch$path.slice(1);
const index = findTargetIndex(value, head);
// If the given selector could not be found, return as-is
if (index === false) {
return nextValue;
}
if (tail.length === 0) {
if (patch.type === 'insert') {
const position = patch.position,
items = patch.items;
return (0, _arrayInsert2.default)(value, position, index, items);
} else if (patch.type === 'unset') {
if (typeof index !== 'number') {
throw new Error(`Expected array index to be a number, instead got "${index}"`);
}
nextValue.splice(index, 1);
return nextValue;
}
}
// The patch is not directed to me
nextValue[index] = (0, _applyPatch2.default)(nextValue[index], _extends({}, patch, {
path: tail
}));
return nextValue;
}