-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuuid.ts
More file actions
68 lines (54 loc) · 1.39 KB
/
uuid.ts
File metadata and controls
68 lines (54 loc) · 1.39 KB
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
import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm';
import { validate } from 'uuid';
import { Scalar } from './scalar.js';
import { isInvalid, NULL_VALUE } from './util.js';
export class UUID implements Scalar<string> {
private _valid = false;
private _value = '';
public constructor(v?: unknown) {
this.value = v;
return this;
}
public get dataType() {
return new ArrowString();
}
public get valid(): boolean {
return this._valid;
}
public get value(): string {
return this._value;
}
public set value(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}
if (typeof value === 'string') {
this._value = value;
this._valid = validate(value);
return;
}
if (value instanceof Uint8Array) {
this._value = new TextDecoder().decode(value);
this._valid = validate(this._value);
return;
}
if (value instanceof UUID) {
this._value = value.value;
this._valid = value.valid;
return;
}
if (typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString) {
this._value = value!.toString();
this._valid = validate(this._value);
return;
}
throw new Error(`Unable to set '${value}' as UUID`);
}
public toString() {
if (this._valid) {
return this._value;
}
return NULL_VALUE;
}
}