UNPKG

@qrsln/utils

Version:
151 lines (140 loc) 4.81 kB
/** @dynamic */ class Guid { constructor(value) { this.Value = this.Empty; this.ToString = () => this.Value; this.Value = Guid.Empty; if (value && Guid.IsValid(value)) { this.Value = value; } } // public static Empty = '00000000-0000-0000-0000-000000000000'; static get Empty() { return '00000000-0000-0000-0000-000000000000'; } get Empty() { return Guid.Empty; } static Parse(guid) { return new Guid(guid); } static NewGuid() { return new Guid(Guid.Raw()); } static IsValid(guid) { const value = guid.toString(); return guid && (guid instanceof Guid || Guid.validator.test(value)); } static Raw() { return [Guid.Generate(2), Guid.Generate(1), Guid.Generate(1), Guid.Generate(1), Guid.Generate(3)].join('-'); } static Generate(count) { let out = ''; for (let i = 0; i < count; i++) { // tslint:disable-next-line:no-bitwise out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); } return out; } Equals(other) { // Comparing string `value` against provided `guid` will auto-call // toString on `guid` for comparison return Guid.IsValid(other) && this.Value === other.ToString(); } IsEmpty() { return this.Empty === Guid.Empty; } ToJSON() { return { Value: this.Value, }; } } Guid.validator = new RegExp('^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$', 'i'); /* usage import {Guid} from '@qrsln/loot-box/Utils'; ... guid: Guid; GuidStringify = () => JSON.stringify(this.guid); Guid() { this.guid = new Guid(); console.log(this.guid); // Guid {Value: "00000000-0000-0000-0000-000000000000"} console.log(this.guid.ToString()); // 00000000-0000-0000-0000-000000000000 console.log(this.guid.ToJSON()); // {Value: "00000000-0000-0000-0000-000000000000"} console.log(JSON.stringify(this.guid)); // {Value: "00000000-0000-0000-0000-000000000000"} console.log('IsEmpty', this.guid.IsEmpty()); // true } GuidNew() { this.guid = Guid.NewGuid(); console.log(this.guid); // Guid { Value: 'bb90ef83-1a7e-42b1-90ba-39cdebb6366c' } console.log(JSON.stringify(this.guid)); // "bb90ef83-1a7e-42b1-90ba-39cdebb6366c" } GuidCheck() { console.log('IsValid', Guid.IsValid(this.guid)); // true console.log(Guid.Empty); // 00000000-0000-0000-0000-000000000000 console.log('IsValid Guid.IsValid(Guid.Empty)', Guid.IsValid(Guid.Empty)); // false console.log('Equals this.guid.Equals(this.guid));', this.guid.Equals(this.guid)); // true console.log('Equals this.guid.Equals(Guid.NewGuid())', this.guid.Equals(Guid.NewGuid())); // false } */ /** @dynamic */ const Sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); /** @dynamic */ class StopWatch { constructor() { this.startTimeMillis = 0; this.totalTimeMillis = 0; /** * Convert milliseconds to time string (hh:mm:ss:mss). * @param milliseconds Number * @return String */ this.TimeSpan = (milliseconds) => new Date(milliseconds).toISOString().slice(11, -1); } static StartNew() { const stopWatch = new StopWatch(); stopWatch.Start(); return stopWatch; } Reset() { this.startTimeMillis = 0; this.totalTimeMillis = 0; } Start() { this.startTimeMillis = Date.now(); } Stop() { const lastTime = Date.now() - this.startTimeMillis; this.totalTimeMillis += lastTime; // console.log(this.totalTimeMillis, lastTime); return this.Elapsed; } // Get the elapsed time as a TimeSpan value. get Elapsed() { return this.TimeSpan(this.totalTimeMillis); } // Get the elapsed time as a TimeSpan value. get ElapsedMilliseconds() { return this.totalTimeMillis; } } /* import {Sleep, StopWatch} from '@qrsln/loot-box/Utils'; (async () => { const watcher = StopWatch.StartNew(); watcher.Stop(); console.log(watcher.Elapsed); console.log("Tasks take " + watcher.ElapsedMilliseconds + " milliseconds"); const stopWatch = new StopWatch(); stopWatch.Start(); await Sleep(10000); stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. const ts = stopWatch.Elapsed; })(); * */ /** * Generated bundle index. Do not edit. */ export { Guid, Sleep, StopWatch }; //# sourceMappingURL=qrsln-utils-Core.mjs.map