util.js 3.4 KB

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