UNPKG

dereference-json-schema

Version:

Dereference $ref pointers in JSONSchema or OpenAPI documents.

55 lines (54 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "resolveRefSync", { enumerable: true, get: function() { return resolveRefSync; } }); // See the identical WeakMap rationale in dereference.ts. Only the outer map // needs to be weak - ref strings aren't valid WeakMap keys, and the inner // per-schema Map is only ever reachable through the outer entry, so it's // collected along with it once the schema itself is no longer referenced // anywhere else. var cache = new WeakMap(); var resolveRefSync = function(schema, ref) { if (!cache.has(schema)) { cache.set(schema, new Map()); } var schemaCache = cache.get(schema); if (schemaCache.has(ref)) { return schemaCache.get(ref); } var path = ref.split("/").slice(1); var current = schema; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var segment = _step.value; if (!current || typeof current !== "object") { // we've reached a dead end current = null; } var _current_segment; current = (_current_segment = current[segment]) !== null && _current_segment !== void 0 ? _current_segment : null; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } schemaCache.set(ref, current); return current; };