util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const constant_1 = require("./constant");
  4. const index_1 = require("./geo/index");
  5. class Util {
  6. }
  7. exports.Util = Util;
  8. Util.formatResDocumentData = (documents) => {
  9. return documents.map(document => {
  10. return Util.formatField(document);
  11. });
  12. };
  13. Util.formatField = document => {
  14. const keys = Object.keys(document);
  15. let protoField = {};
  16. if (Array.isArray(document)) {
  17. protoField = [];
  18. }
  19. keys.forEach(key => {
  20. const item = document[key];
  21. const type = Util.whichType(item);
  22. let realValue;
  23. switch (type) {
  24. case constant_1.FieldType.GeoPoint:
  25. realValue = new index_1.Point(item.coordinates[0], item.coordinates[1]);
  26. break;
  27. case constant_1.FieldType.GeoLineString:
  28. realValue = new index_1.LineString(item.coordinates.map(point => new index_1.Point(point[0], point[1])));
  29. break;
  30. case constant_1.FieldType.GeoPolygon:
  31. realValue = new index_1.Polygon(item.coordinates.map(line => new index_1.LineString(line.map(([lng, lat]) => new index_1.Point(lng, lat)))));
  32. break;
  33. case constant_1.FieldType.GeoMultiPoint:
  34. realValue = new index_1.MultiPoint(item.coordinates.map(point => new index_1.Point(point[0], point[1])));
  35. break;
  36. case constant_1.FieldType.GeoMultiLineString:
  37. realValue = new index_1.MultiLineString(item.coordinates.map(line => new index_1.LineString(line.map(([lng, lat]) => new index_1.Point(lng, lat)))));
  38. break;
  39. case constant_1.FieldType.GeoMultiPolygon:
  40. realValue = new index_1.MultiPolygon(item.coordinates.map(polygon => new index_1.Polygon(polygon.map(line => new index_1.LineString(line.map(([lng, lat]) => new index_1.Point(lng, lat)))))));
  41. break;
  42. case constant_1.FieldType.Date:
  43. realValue = item;
  44. break;
  45. case constant_1.FieldType.Object:
  46. case constant_1.FieldType.Array:
  47. realValue = Util.formatField(item);
  48. break;
  49. case constant_1.FieldType.ServerDate:
  50. realValue = new Date(item.$date);
  51. break;
  52. default:
  53. realValue = item;
  54. }
  55. if (Array.isArray(protoField)) {
  56. protoField.push(realValue);
  57. }
  58. else {
  59. protoField[key] = realValue;
  60. }
  61. });
  62. return protoField;
  63. };
  64. Util.whichType = (obj) => {
  65. let type = Object.prototype.toString.call(obj).slice(8, -1);
  66. if (type === constant_1.FieldType.Date) {
  67. return constant_1.FieldType.Date;
  68. }
  69. if (type === constant_1.FieldType.Object) {
  70. if (obj.$date) {
  71. type = constant_1.FieldType.ServerDate;
  72. }
  73. else if (index_1.Point.validate(obj)) {
  74. type = constant_1.FieldType.GeoPoint;
  75. }
  76. else if (index_1.LineString.validate(obj)) {
  77. type = constant_1.FieldType.GeoLineString;
  78. }
  79. else if (index_1.Polygon.validate(obj)) {
  80. type = constant_1.FieldType.GeoPolygon;
  81. }
  82. else if (index_1.MultiPoint.validate(obj)) {
  83. type = constant_1.FieldType.GeoMultiPoint;
  84. }
  85. else if (index_1.MultiLineString.validate(obj)) {
  86. type = constant_1.FieldType.GeoMultiLineString;
  87. }
  88. else if (index_1.MultiPolygon.validate(obj)) {
  89. type = constant_1.FieldType.GeoMultiPolygon;
  90. }
  91. }
  92. return type;
  93. };
  94. Util.generateDocId = () => {
  95. let chars = 'ABCDEFabcdef0123456789';
  96. let autoId = '';
  97. for (let i = 0; i < 24; i++) {
  98. autoId += chars.charAt(Math.floor(Math.random() * chars.length));
  99. }
  100. return autoId;
  101. };