@sanity/form-builder
Version:
Sanity form builder
93 lines (89 loc) • 5.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = apply;
var _findIndex2 = _interopRequireDefault(require("lodash/findIndex"));
var _applyPatch = _interopRequireDefault(require("./applyPatch"));
var _arrayInsert = _interopRequireDefault(require("./arrayInsert"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var hasOwn = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property);
function move(arr, from, to) {
var nextValue = arr.slice();
var val = nextValue[from];
nextValue.splice(from, 1);
nextValue.splice(to, 0, val);
return nextValue;
}
function findTargetIndex(array, pathSegment) {
if (typeof pathSegment === 'number') {
return pathSegment;
}
var index = (0, _findIndex2.default)(array, pathSegment);
return index === -1 ? false : index;
}
function apply(value, patch) {
var 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 || !hasOwn(patch.value, 'from') || !hasOwn(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: ".concat(JSON.stringify(patch.value)));
}
return move(nextValue, patch.value.from, patch.value.to);
}
throw new Error("Invalid array operation: ".concat(patch.type));
}
var _patch$path = _toArray(patch.path),
head = _patch$path[0],
tail = _patch$path.slice(1);
var 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') {
var position = patch.position,
items = patch.items;
return (0, _arrayInsert.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 \"".concat(index, "\""));
}
nextValue.splice(index, 1);
return nextValue;
}
}
// The patch is not directed to me
nextValue[index] = (0, _applyPatch.default)(nextValue[index], _objectSpread(_objectSpread({}, patch), {}, {
path: tail
}));
return nextValue;
}