@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
52 lines • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const dictionary_1 = require("../common/dictionary");
function typeOf(obj) {
const t = typeof (obj);
return t === 'object' ?
Array.isArray(obj) ?
"array" :
"object" :
t;
}
exports.typeOf = typeOf;
/** identifies if a given refable is a reference or an instance */
function isReference(item) {
return item.$ref ? true : false;
}
exports.isReference = isReference;
/** gets an object instance for the item, regardless if it's a reference or not. */
function dereference(document, item, stack = new Array()) {
let name = undefined;
if (isReference(item)) {
let node = document;
const path = item.$ref;
if (stack.indexOf(path) > -1) {
throw new Error(`Circular $ref in Model -- ${path} :: ${JSON.stringify(stack)} `);
}
stack.push(path);
let parts = path.replace("#/", "").split("/");
for (name of parts) {
if (!node[name]) {
throw new Error(`Invalid Reference ${name} -- ${path}`);
}
node = node[name];
}
if (isReference(node)) {
// it's a ref to a ref.
return dereference(document, node, stack);
}
return { instance: node, name: name };
}
return { instance: item, name: undefined };
}
exports.dereference = dereference;
function clone(object) {
return object ? JSON.parse(JSON.stringify(object)) : undefined;
}
exports.clone = clone;
function getExtensionProperties(dictionary) {
return dictionary_1.ToDictionary(dictionary_1.includeXDash(dictionary), each => dictionary[each]);
}
exports.getExtensionProperties = getExtensionProperties;
//# sourceMappingURL=common.js.map