static encode()

in lib/JsonEncoder.ts [4:37]


  static encode(obj: any) {
    if (obj instanceof ObjectId) {
      return {
        $type:  'ObjectId',
        $value: obj.toHexString(),
        $date:  obj.getTimestamp().getTime()
      };
    }
    if (obj instanceof Date) {
      return {
        $type: 'Date',
        $value: obj.toISOString()
      };
    }
    if (obj instanceof RegExp) {
      return {
        $type: 'RegExp',
        $value: {
          $pattern: obj.source,
          $flags:   obj.flags
        }
      };
    }
    if (Array.isArray(obj)) {
      return [...obj.map(JsonEncoder.encode)];
    }
    if (obj && typeof obj === 'object') {
      for (const [key, value] of Object.entries(obj)) {
        obj[key] = JsonEncoder.encode(value);
      }
    }

    return obj;
  }