All files / tcb-admin-node/src/database util.ts

88.24% Statements 45/51
80.77% Branches 21/26
100% Functions 6/6
88.24% Lines 45/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1243x 3x 3x                       3x             3x 1x 1x                       3x 3x 3x     3x 1x     3x 14x 14x     14x   1x 1x           2x 2x   2x 2x     9x       14x 3x   11x     3x               3x 16x   16x   4x   4x       4x       4x   4x 2x 2x 1x     16x               3x 1x 1x 1x 24x   1x      
import { FieldType } from "./constant";
import { Point } from "./geo/point";
import { ServerDate } from "./serverDate";
 
interface DocumentModel {
  _id: string;
}
 
 
/**
 * 工具模块
 *
 * @author haroldhu
 */
export class Util {
 
  /**
   * 格式化后端返回的文档数据
   *
   * @param document - 后端文档数据
   */
  public static formatResDocumentData = (documents: DocumentModel[]) => {
    return documents.map(document => {
      return Util.formatField(document);
    });
  };
 
  /**
   * 格式化字段
   *
   * 主要是递归数组和对象,把地理位置和日期时间转换为js对象。
   *
   * @param document
   * @internal
   */
  private static 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);
      // console.log(type, item)
      let realValue;
      switch (type) {
        case FieldType.GeoPoint:
          realValue = new Point(item.coordinates[0], item.coordinates[1]);
          break;
        case FieldType.Timestamp:
          realValue = new Date(item.$date);
          break;
        case FieldType.Object:
        case FieldType.Array:
          realValue = Util.formatField(item);
          break;
        case FieldType.ServerDate:
          realValue = new Date(item.$date);
          break;
 
        default:
          realValue = item;
 
      }
 
      if (Array.isArray(protoField)) {
        protoField.push(realValue);
      } else {
        protoField[key] = realValue;
      }
    });
    return protoField;
  };
 
  /**
   * 查看数据类型
   *
   * @param obj
   */
  public static whichType = (obj: any): String => {
    let type = Object.prototype.toString.call(obj).slice(8, -1);
 
    if (type === FieldType.Object) {
      // console.log(obj)
      Iif (obj instanceof Point) {
        return FieldType.GeoPoint;
      } else Iif (obj instanceof Date) {
        return FieldType.Timestamp;
      }/* else if (obj instanceof Command) {
        return FieldType.Command;
      } */else Iif (obj instanceof ServerDate) {
        return FieldType.ServerDate
      }
 
      Iif (obj.$timestamp) {
        type = FieldType.Timestamp;
      } else if (obj.$date) {
        type = FieldType.ServerDate;
      } else if (Array.isArray(obj.coordinates) && obj.type === "Point") {
        type = FieldType.GeoPoint;
      }
    }
    return type;
  };
 
  /**
   * 生成文档ID
   *
   * 为创建新文档使用
   */
  public static generateDocId = () => {
    let chars = "ABCDEFabcdef0123456789";
    let autoId = "";
    for (let i = 0; i < 24; i++) {
      autoId += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return autoId;
  };
}