All files / type-json-mapper/src utils.ts

96.43% Statements 27/28
70% Branches 7/10
100% Functions 11/11
95.65% Lines 22/23

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 881x 1x             1x 13x             1x 13x             1x 5x             1x 3x             16x 8x             1x 19x               2x 1x 1x     1x               3x 1x             1x               1x  
import { META_KEY } from "../libs/config";
import { BASIC_TYPE } from "../libs/types";
 
/**
 * @param {any} target object
 * @param {string} propertyKey
 * @return {any}
 */
export function getJsonProperty(target: any, propertyKey: string): any {
  return Reflect.getMetadata(META_KEY, target, propertyKey);
}
 
/**
 * @param {any} value
 * @return {(target:Object, targetKey:string | symbol)=> void} decorator function
 */
export function setProperty(value: any): (target: Object, targetKey: string | symbol)=> void {
  return Reflect.metadata(META_KEY, value);
}
 
/**
 * @param {any} target object
 * @return {Boolean}
 */
export function isObject(target: any): boolean {
  return Object.prototype.toString.call(target) === '[object Object]';
}
 
/**
 * @param {any} target
 * @return {Boolean}
 */
export function isArray(target: any): boolean {
  return Object.prototype.toString.call(target) === '[object Array]';
}
 
/**
 * @param {...args:any[]} any arguments
 * @return {Boolean}
 */
export function hasAnyNullOrUndefined(...args: any[]): boolean {
  return args.some((arg: any) => arg === null || arg === undefined);
}
 
/**
 * @param {any} target
 * @return {Boolean}
 */
export function isNotBasicType(target: any): boolean {
  return BASIC_TYPE.every(type => type !== typeof target);
}
 
/**
 * @param {any} timestamp
 * @param {string} format
 * @return {string} 
 */
export function formatDate(timestamp: any, Eformat: string = 'Y-M-D h:m:s'): string {
  let date = new Date(timestamp);
  Iif (isInvalidDate(date)) {
    return timestamp;
  }
  let dateInfo = {
    Y: `${date.getFullYear()}`,
    M: `${date.getMonth() + 1}`,
    D: `${date.getDate()}`,
    h: date.getHours(),
    m: date.getMinutes(),
    s: date.getSeconds(),
  }
  let formatNumber = (n: number) => n >= 10 ? `${n}` : `0${n}`;
  let res = format
    .replace('Y', dateInfo.Y)
    .replace('M', dateInfo.M)
    .replace('D', dateInfo.D)
    .replace('h', formatNumber(dateInfo.h))
    .replace('m', formatNumber(dateInfo.m))
    .replace('s', formatNumber(dateInfo.s))
  return res
}
 
/**
 * @param {any} date
 * @return {boolean} 
 */
function isInvalidDate(date: any): boolean {
  return date instanceof Date && isNaN(date.getTime());
}