File

src/utils/services/utils.service.ts

Index

Methods

Methods

Static capitalizeName
capitalizeName(name: string)
Parameters :
Name Type Optional
name string No
Returns : string
Static generateHash
generateHash(password: string)
Parameters :
Name Type Optional
password string No
Returns : string
Static generateRandomInteger
generateRandomInteger(min: number, max: number)
Parameters :
Name Type Optional
min number No
max number No
Returns : number
Static generateRandomString
generateRandomString(length: number)
Parameters :
Name Type Optional
length number No
Returns : string
Static getAge
getAge(d1: Date, d2?: Date)
Parameters :
Name Type Optional
d1 Date No
d2 Date Yes
Returns : number
Static toDto
toDto(model, entity: E, options?: any)
Type parameters :
  • T
  • E
Parameters :
Name Type Optional
model No
entity E No
options any Yes
Returns : T
Static toDto
toDto(model, entity: E[], options?: any)
Type parameters :
  • T
  • E
Parameters :
Name Type Optional
model No
entity E[] No
options any Yes
Returns : T[]
Static toDto
toDto(model, entity: E | E[], options?: any)
Type parameters :
  • T
  • E
Parameters :
Name Type Optional
model No
entity E | E[] No
options any Yes
Returns : T | []
Static validateHash
validateHash(password: string, hash: string)
Parameters :
Name Type Optional
password string No
hash string No
Returns : Promise<boolean>
import * as bcrypt from 'bcrypt';
import * as _ from 'lodash';

export class UtilsService {
  public static toDto<T, E>(
    model: new (entity: E, options?: any) => T,
    entity: E,
    options?: any,
  ): T;
  public static toDto<T, E>(
    model: new (entity: E, options?: any) => T,
    entity: E[],
    options?: any,
  ): T[];
  public static toDto<T, E>(
    model: new (entity: E, options?: any) => T,
    entity: E | E[],
    options?: any,
  ): T | T[] {
    if (_.isArray(entity)) {
      return entity.map((u) => new model(u, options));
    }

    return new model(entity, options);
  }

  static generateHash(password: string): string {
    return bcrypt.hashSync(password, 10);
  }

  static validateHash(password: string, hash: string): Promise<boolean> {
    return bcrypt.compare(password, hash || '');
  }

  static generateRandomInteger(min: number, max: number): number {
    return Math.floor(min + Math.random() * (max + 1 - min));
  }

  static generateRandomString(length: number): string {
    return Math.random()
      .toString(36)
      .replace(/[^a-zA-Z0-9]+/g, '')
      .toUpperCase()
      .substr(0, length);
  }

  static getAge(d1: Date, d2?: Date): number {
    d2 = d2 || new Date();
    const diff = d2.getTime() - d1.getTime();
    return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
  }

  static capitalizeName(name: string): string {
    return _.capitalize(name);
  }
}

result-matching ""

    No results matching ""