app/core/services/id-generator.service.ts
Properties |
|
Methods |
Public getId |
getId()
|
Defined in app/core/services/id-generator.service.ts:8
|
Generate new unique ID
Returns :
any
|
Public isValid | ||||||
isValid(str: string)
|
||||||
Defined in app/core/services/id-generator.service.ts:17
|
||||||
Check if gived id is valid
Parameters :
Returns :
boolean
|
Readonly idFormat |
Type : string
|
Default value : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
Defined in app/core/services/id-generator.service.ts:5
|
import { Injectable } from '@angular/core';
@Injectable()
export class IdGeneratorService {
readonly idFormat = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
/** Generate new unique ID */
public getId() {
return this.idFormat.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
const v = (c === 'x') ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/** Check if gived id is valid */
public isValid(str: string): boolean {
const validRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return validRegex.test(str);
}
}