"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constant_1 = require("./constant");
const point_1 = require("./geo/point");
const serverDate_1 = require("./serverDate");
class Util {
}
Util.formatResDocumentData = (documents) => {
return documents.map(document => {
return Util.formatField(document);
});
};
Util.formatField = document => {
const keys = Object.keys(document);
let protoField = {};
if (Array.isArray(document)) {
protoField = [];
}
keys.forEach(key => {
const item = document[key];
const type = Util.whichType(item);
let realValue;
switch (type) {
case constant_1.FieldType.GeoPoint:
realValue = new point_1.Point(item.coordinates[0], item.coordinates[1]);
break;
case constant_1.FieldType.Timestamp:
realValue = new Date(item.$date);
break;
case constant_1.FieldType.Object:
case constant_1.FieldType.Array:
realValue = Util.formatField(item);
break;
case constant_1.FieldType.ServerDate:
realValue = new Date(item.$date);
break;
default:
realValue = item;
}
if (Array.isArray(protoField)) {
protoField.push(realValue);
}
else {
protoField[key] = realValue;
}
});
return protoField;
};
Util.whichType = (obj) => {
let type = Object.prototype.toString.call(obj).slice(8, -1);
if (type === constant_1.FieldType.Object) {
Iif (obj instanceof point_1.Point) {
return constant_1.FieldType.GeoPoint;
}
else Iif (obj instanceof Date) {
return constant_1.FieldType.Timestamp;
}
else Iif (obj instanceof serverDate_1.ServerDate) {
return constant_1.FieldType.ServerDate;
}
Iif (obj.$timestamp) {
type = constant_1.FieldType.Timestamp;
}
else if (obj.$date) {
type = constant_1.FieldType.ServerDate;
}
else if (Array.isArray(obj.coordinates) && obj.type === "Point") {
type = constant_1.FieldType.GeoPoint;
}
}
return type;
};
Util.generateDocId = () => {
let chars = "ABCDEFabcdef0123456789";
let autoId = "";
for (let i = 0; i < 24; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length));
}
return autoId;
};
exports.Util = Util;
|