avo-inspector
Version:
[](https://badge.fury.io/js/avo-inspector)
96 lines (95 loc) • 3.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoSchemaParser = void 0;
var isArray = function (obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
var AvoSchemaParser = /** @class */ (function () {
function AvoSchemaParser() {
}
AvoSchemaParser.extractSchema = function (eventProperties) {
var _this = this;
if (eventProperties === null || eventProperties === undefined) {
return [];
}
var mapping = function (object) {
if (isArray(object)) {
var list = object.map(function (x) {
return mapping(x);
});
return _this.removeDuplicates(list);
}
else if (typeof object === "object") {
var mappedResult = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
var val = object[key];
var mappedEntry = {
propertyName: key,
propertyType: _this.getPropValueType(val),
};
if (typeof val === "object" && val != null) {
mappedEntry["children"] = mapping(val);
}
mappedResult.push(mappedEntry);
}
}
return mappedResult;
}
else {
return _this.getPropValueType(object);
}
};
var mappedEventProps = mapping(eventProperties);
return mappedEventProps;
};
AvoSchemaParser.removeDuplicates = function (array) {
// XXX TODO fix any types
var primitives = { boolean: {}, number: {}, string: {} };
var objects = [];
return array.filter(function (item) {
var type = typeof item;
if (type in primitives) {
return primitives[type].hasOwnProperty(item)
? false
: (primitives[type][item] = true);
}
else {
return objects.indexOf(item) >= 0 ? false : objects.push(item);
}
});
};
AvoSchemaParser.getPropValueType = function (propValue) {
var propType = typeof propValue;
if (propValue == null) {
return "null";
}
else if (propType === "string") {
return "string";
}
else if (propType === "number" || propType === "bigint") {
if ((propValue + "").indexOf(".") >= 0) {
return "float";
}
else {
return "int";
}
}
else if (propType === "boolean") {
return "boolean";
}
else if (propType === "object") {
if (isArray(propValue)) {
return "list";
}
else {
return "object";
}
}
else {
return "unknown";
}
};
return AvoSchemaParser;
}());
exports.AvoSchemaParser = AvoSchemaParser;