UNPKG

@azure/search-documents

Version:
89 lines (88 loc) 2.87 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var walk_exports = {}; __export(walk_exports, { walk: () => walk }); module.exports = __toCommonJS(walk_exports); var import_core_util = require("@azure/core-util"); function walk(v, fn) { const seen = /* @__PURE__ */ new Set(); const mutated = /* @__PURE__ */ new Map(); deepLazyApply(v); for (const value of mutated.values()) { replaceChildren(value); } return mutated.get(v) ?? v; function deepLazyApply(value) { if (seen.has(value)) { return; } seen.add(value); const children = getChildren(cachedApply(value) ?? value); children?.forEach(deepLazyApply); if (children?.some((node) => mutated.has(node))) { mutated.set(value, mutated.get(value) ?? shallowCopy(value)); } } function cachedApply(value) { const cached = mutated.get(value); if ((0, import_core_util.isDefined)(cached)) { return cached; } const applied = fn(value); if (value !== applied) { mutated.set(value, applied); } return mutated.get(value); } function replaceChildren(applied) { if (!isComplex(applied)) { return; } else if (Array.isArray(applied)) { applied.forEach((e, i) => { applied[i] = mutated.get(e) ?? e; }); } else if (typeof applied === "object" && applied !== null) { Object.keys(applied).forEach((key) => { applied[key] = mutated.get(applied[key]) ?? applied[key]; }); } } } function isComplex(v) { return Array.isArray(v) ? isComplex(v[0]) : typeof v === "object" && v !== null; } function getChildren(v) { if (!isComplex(v)) { return void 0; } if (Array.isArray(v)) { return v; } return Object.values(v); } function shallowCopy(value) { const maybeCopy = Array.isArray(value) ? value.map((v) => v) : typeof value === "object" && value !== null ? { ...value } : value; return value === maybeCopy ? value : Object.setPrototypeOf(maybeCopy, Object.getPrototypeOf(value)); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { walk }); //# sourceMappingURL=walk.js.map