All files / type-json-mapper index.ts

51% Statements 51/100
33.87% Branches 21/62
66.67% Functions 6/9
51.11% Lines 46/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 2031x 1x     1x               1x 10x       10x               1x 2x       2x                 1x 1x       1x           1x                                                                                                                             1x                               5x   3x       3x       3x   3x   3x   3x   13x 13x 13x     26x 13x   13x       13x 13x     13x 13x 1x 1x 1x       13x 13x 2x 1x     2x 1x       13x   3x           2x   1x       1x       1x    
import 'reflect-metadata';
import { transType } from './src/transform';
import { GenericObject, MetadataObject, MetadataDeepObject, MetadataFilterObject } from './libs/config';
import { TYPE_NAME } from './libs/types';
import { getJsonProperty, hasAnyNullOrUndefined, isArray, isObject, setProperty } from './src/utils';
 
/**
 * 属性装饰器
 * @param {string} value - 键名
 * @param {TYPE_NAME} typeName - 转换类型
 * @return {(target:Object, targetKey:string | symbol)=> void} decorator function
*/
export function mapperProperty(value: string, typeName?: TYPE_NAME): (target: Object, targetKey: string | symbol)=> void {
  const metadata: MetadataObject = {
    typeName,
    localKey: value
  };
  return setProperty(metadata);
}
/**
 * 深层属性装饰器
 * @param {string} value - 键名
 * @param {any} Clazz
 * @return {(target:Object, targetKey:string | symbol)=> void} decorator function
*/
export function deepMapperProperty(value: string, Clazz: any): (target: Object, targetKey: string | symbol)=> void  {
  const metadata: MetadataDeepObject = {
    localKey: value,
    Clazz
  };
  return setProperty(metadata);
}
 
/**
 * 自定义属性装饰器
 * @param {string} value - 键名
 * @param {Function} filter
 * @return {(target:Object, targetKey:string | symbol)=> void} decorator function
*/
export function filterMapperProperty(value: string, filter: Function): (target: Object, targetKey: string | symbol)=> void  {
  const metadata: MetadataFilterObject = {
    localKey: value,
    filter
  };
  return setProperty(metadata);
}
 
/**
 * 序列化
*/
export function serialize<T extends GenericObject>(Clazz: { new(): T }, json: GenericObject, ignore: boolean = true) {
  if (hasAnyNullOrUndefined(Clazz, json)) {
    throw new Error('(type-json-mapper)serialize:missing Clazz or json');
  }
  
  if (!isObject(json)) {
    throw new Error('(type-json-mapper)serialize:json is not a object');
  }
 
  const result = {};
 
  let instance: GenericObject = new Clazz();
 
  const keys = Object.keys(instance);
 
  for (const key of keys) {
    let value = json[key];
    // ignore
    if (ignore && typeof value === 'undefined') {
      continue;
    }
    let localKey = key;
    const metaObj = getJsonProperty(instance, key);
    if (!metaObj) {
      continue
    }
 
    const {typeName, localKey: interfaceKey} = metaObj;
    if (typeof interfaceKey !== 'undefined') {
      localKey = interfaceKey;
    }
 
    if (typeof value !== 'undefined') {
      value = transType(value, typeName);
    }
 
    const { filter } = metaObj;
    if (typeof filter === 'function') {
      const tempVal = filter(value);
      if (typeof tempVal !== 'undefined') {
        value = tempVal;
      }
    }
 
    const {Clazz: childClazz} = metaObj;
    if (typeof childClazz !== 'undefined') { 
      if (isObject(value)) {
        value = serialize(childClazz, value);
      }
      
      if (isArray(value)) {
        value = serializeArr(childClazz, value);
      }
    }
  
    result[localKey] = value;
  }
  return result as T;
}
 
/**
 * 数组序列化
*/
export function serializeArr(Clazz: { new(): GenericObject }, list: GenericObject[], ignore: boolean = true) {
 
  if (hasAnyNullOrUndefined(Clazz, list)) {
    throw new Error('(type-json-mapper)serializeArr:missing Clazz or list');
  }
  
  if (!isArray(list)) {
    throw new Error('(type-json-mapper)serializeArr:list is not a array');
  }
 
  return list.map((ele: GenericObject) => serialize(Clazz, ele, ignore));
}
 
/**
 * 反序列化
*/
export function deserialize<T extends GenericObject>(Clazz: { new(): T }, json: GenericObject, ignore: boolean = true) {
 
  Iif (hasAnyNullOrUndefined(Clazz, json)) {
    throw new Error('(type-json-mapper)deserialize:missing Clazz or json');
  }
  
  Iif (!isObject(json)) {
    throw new Error('(type-json-mapper)deserialize:json is not a object');
  }
 
  let result = {};
 
  let instance: GenericObject = new Clazz();
 
  const keys = Object.keys(instance);
 
  result = instance;
 
  for (const key of keys) {
    const metaObj = getJsonProperty(instance, key);
    Iif (!metaObj) {
      continue;
    }
    const {typeName, localKey} = metaObj;
    let value = json[localKey];
    // ignore
    Iif (ignore && typeof value === 'undefined') {
      continue;
    }
 
    Eif (typeof value !== 'undefined') {
      value = transType(value, typeName);
    }
 
    const { filter } = metaObj;
    if (typeof filter === 'function') {
      const tempVal = filter(value);
      Eif (typeof tempVal !== 'undefined') {
        value = tempVal;
      }
    }
 
    const { Clazz: childClazz } = metaObj;
    if (typeof childClazz !== 'undefined') {
      if (isObject(value)) {
        value = deserialize(childClazz, value);
      }
 
      if (isArray(value)) {
        value = deserializeArr(childClazz, value);
      }
    }
 
    result[key] = value;
  }
  return result as T;
}
 
/**
 * 数组反序列化
*/
export function deserializeArr(Clazz: { new(): GenericObject }, list: GenericObject[], Eignore: boolean = true) {
 
  Iif (hasAnyNullOrUndefined(Clazz, list)) {
    throw new Error('(type-json-mapper)deserializeArr:missing Clazz or list');
  }
  
  Iif (!isArray(list)) {
    throw new Error('(type-json-mapper)deserializeArr:list is not a array');
  }
 
  return list.map((ele: GenericObject) => deserialize(Clazz, ele, ignore));
}