File

app/core/services/id-generator.service.ts

Index

Properties
Methods

Methods

Public getId
getId()

Generate new unique ID

Returns : any
Public isValid
isValid(str: string)

Check if gived id is valid

Parameters :
Name Type Optional
str string No
Returns : boolean

Properties

Readonly idFormat
Type : string
Default value : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
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);
	}
}

result-matching ""

    No results matching ""