2017-11-21 15:09:26 +00:00
|
|
|
import * as validator from 'validator';
|
2017-11-24 17:45:34 +00:00
|
|
|
const uuid = require('uuid');
|
2017-11-21 15:09:26 +00:00
|
|
|
|
|
|
|
|
export class UUID
|
|
|
|
|
{
|
|
|
|
|
private mUUID = '00000000-0000-0000-0000-000000000000';
|
|
|
|
|
|
|
|
|
|
static zero(): UUID
|
|
|
|
|
{
|
|
|
|
|
return new UUID();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 17:45:34 +00:00
|
|
|
constructor(buf?: Buffer | string, pos?: number)
|
2017-11-21 15:09:26 +00:00
|
|
|
{
|
2017-11-24 17:45:34 +00:00
|
|
|
if (buf !== undefined)
|
2017-11-21 15:09:26 +00:00
|
|
|
{
|
2017-11-24 17:45:34 +00:00
|
|
|
if (typeof buf === 'string')
|
|
|
|
|
{
|
|
|
|
|
this.setUUID(buf);
|
|
|
|
|
}
|
|
|
|
|
else if (pos !== undefined)
|
|
|
|
|
{
|
|
|
|
|
this.mUUID = uuid.unparse(buf, pos);
|
|
|
|
|
}
|
2017-11-21 15:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setUUID(val: string): boolean
|
|
|
|
|
{
|
|
|
|
|
if (validator.isUUID(val, 4))
|
|
|
|
|
{
|
|
|
|
|
this.mUUID = val;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public toString = (): string =>
|
|
|
|
|
{
|
|
|
|
|
return this.mUUID;
|
2017-11-24 17:45:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
writeToBuffer(buf: Buffer, pos: number)
|
|
|
|
|
{
|
|
|
|
|
uuid.parse(this.mUUID, buf, pos);
|
2017-11-21 15:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|