uuid.ts 2.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

/**
 * Represents a UUID as defined by rfc4122.
 */
export interface UUID {

	/**
13
	 * @returns the canonical representation in sets of hexadecimal numbers separated by dashes.
E
Erich Gamma 已提交
14
	 */
J
Johannes Rieken 已提交
15
	asHex(): string;
E
Erich Gamma 已提交
16 17 18 19
}

class ValueUUID implements UUID {

J
Johannes Rieken 已提交
20
	constructor(public _value: string) {
E
Erich Gamma 已提交
21 22 23
		// empty
	}

J
Johannes Rieken 已提交
24
	public asHex(): string {
E
Erich Gamma 已提交
25 26 27 28 29 30 31 32 33 34
		return this._value;
	}
}

class V4UUID extends ValueUUID {

	private static _chars = ['0', '1', '2', '3', '4', '5', '6', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];

	private static _timeHighBits = ['8', '9', 'a', 'b'];

J
Johannes Rieken 已提交
35
	private static _oneOf(array: string[]): string {
J
Joao Moreno 已提交
36
		return array[Math.floor(array.length * Math.random())];
E
Erich Gamma 已提交
37 38
	}

J
Johannes Rieken 已提交
39
	private static _randomHex(): string {
E
Erich Gamma 已提交
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
		return V4UUID._oneOf(V4UUID._chars);
	}

	constructor() {
		super([
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			'-',
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			'-',
			'4',
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			'-',
			V4UUID._oneOf(V4UUID._timeHighBits),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			'-',
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
			V4UUID._randomHex(),
		].join(''));
	}
}

J
Johannes Rieken 已提交
85
export function v4(): UUID {
E
Erich Gamma 已提交
86 87 88
	return new V4UUID();
}

J
Joao Moreno 已提交
89 90 91 92 93
const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

export function isUUID(value: string): boolean {
	return _UUIDPattern.test(value);
}
E
Erich Gamma 已提交
94 95 96 97 98

/**
 * Parses a UUID that is of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
 * @param value A uuid string.
 */
J
Johannes Rieken 已提交
99 100
export function parse(value: string): UUID {
	if (!isUUID(value)) {
E
Erich Gamma 已提交
101 102
		throw new Error('invalid uuid');
	}
J
Joao Moreno 已提交
103

E
Erich Gamma 已提交
104 105 106
	return new ValueUUID(value);
}

J
Johannes Rieken 已提交
107
export function generateUuid(): string {
E
Erich Gamma 已提交
108
	return v4().asHex();
J
Johannes Rieken 已提交
109
}