UNPKG

@sanity/migrate

Version:

Tooling for running data migrations on Sanity.io projects

83 lines (82 loc) 3.3 kB
function _array_like_to_array(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 _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(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(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } import { getValueType } from './getValueType.js'; export var SKIP_MARKER = { _: 'SKIP_MARKER' }; function callMap(mapFn, value, path) { var res = mapFn(value, path); return Array.isArray(res) ? res : [ res ]; } function getPathWithKey(item, index, container) { if (item && Array.isArray(container) && (typeof item === "undefined" ? "undefined" : _type_of(item)) === 'object' && '_key' in item && typeof item._key === 'string') { return { _key: item._key }; } return index; } // Reduce depth first function mapObject(reducerFn, object, path) { return _to_consumable_array(callMap(reducerFn, object, path)).concat(_to_consumable_array(Object.keys(object).flatMap(function(key) { var value = object[key]; if (value === undefined) return []; return flatMapAny(reducerFn, value, _to_consumable_array(path).concat([ getPathWithKey(value, key, object) ])); }))); } // Reduce depth first function mapArray(mapFn, array, path) { return _to_consumable_array(callMap(mapFn, array, path)).concat(_to_consumable_array(array.flatMap(function(item, index) { return flatMapAny(mapFn, item, _to_consumable_array(path).concat([ getPathWithKey(item, index, array) ])); }))); } function flatMapAny(mapFn, val, path) { var type = getValueType(val); if (type === 'object') { return mapObject(mapFn, val, path); } if (type === 'array') { return mapArray(mapFn, val, path); } return callMap(mapFn, val, path); } /** * Iterating depth first over the JSON tree, calling the mapFn for parents before children * @param value - the value to map deeply over * @param mapFn - the mapFn to call for each value */ export function flatMapDeep(value, mapFn) { return flatMapAny(mapFn, value, []); }