From c1a8fde25626a204513e2b73022a6ffa4e79e016 Mon Sep 17 00:00:00 2001 From: LiosK Date: Sun, 22 Jan 2023 21:38:49 +0900 Subject: [PATCH] refactor!: convert _init() method to private constructor --- dist/uuid.d.ts | 3 +-- dist/uuid.js | 18 ++++++++---------- src/uuid.ts | 22 +++++++++------------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/dist/uuid.d.ts b/dist/uuid.d.ts index 6faae8f..c257329 100644 --- a/dist/uuid.d.ts +++ b/dist/uuid.d.ts @@ -95,7 +95,6 @@ export declare class UUID { readonly hexNoDelim: string; readonly hexString: string; readonly urn: string; - private constructor(); /** * Initializes a {@link UUID} object. * @private @@ -108,7 +107,7 @@ export declare class UUID { * @param {number} [node=0] node field (octet 10-15, uint48). * @returns {UUID} this. */ - private _init; + private constructor(); /** * Converts an integer to a zero-filled binary string. * @private diff --git a/dist/uuid.js b/dist/uuid.js index 9da294e..e19b959 100644 --- a/dist/uuid.js +++ b/dist/uuid.js @@ -77,7 +77,7 @@ export class UUID { */ static genV4() { var rand = UUID._getRandomInt; - return new UUID()._init(rand(32), // time_low + return new UUID(rand(32), // time_low rand(16), // time_mid 0x4000 | rand(12), // time_hi_and_version 0x80 | rand(6), // clock_seq_hi_and_reserved @@ -98,12 +98,11 @@ export class UUID { if (l + t === "" || (l === "{" && t === "}") || (l.toLowerCase() === "urn:uuid:" && t === "")) { - return new UUID()._init(parseInt(r[2], 16), parseInt(r[3], 16), parseInt(r[4], 16), parseInt(r[5], 16), parseInt(r[6], 16), parseInt(r[7], 16)); + return new UUID(parseInt(r[2], 16), parseInt(r[3], 16), parseInt(r[4], 16), parseInt(r[5], 16), parseInt(r[6], 16), parseInt(r[7], 16)); } } return null; } - constructor() { } /** * Initializes a {@link UUID} object. * @private @@ -116,7 +115,7 @@ export class UUID { * @param {number} [node=0] node field (octet 10-15, uint48). * @returns {UUID} this. */ - _init(_timeLow, _timeMid, _timeHiAndVersion, _clockSeqHiAndReserved, _clockSeqLow, _node) { + constructor(_timeLow, _timeMid, _timeHiAndVersion, _clockSeqHiAndReserved, _clockSeqLow, _node) { var names = UUID.FIELD_NAMES, sizes = UUID.FIELD_SIZES; var bin = UUID._binAligner, hex = UUID._hexAligner; /** @@ -176,7 +175,6 @@ export class UUID { * @type {string} */ this.urn = "urn:uuid:" + this.hexString; - return this; } /** * Converts an integer to a zero-filled binary string. @@ -226,7 +224,7 @@ export class UUID { */ static genV1() { if (UUID._state == null) { - UUID.resetState(); + UUID._state = new UUIDState(); } var now = new Date().getTime(), st = UUID._state; if (now != st.timestamp) { @@ -252,7 +250,7 @@ export class UUID { st.sequence &= 0x3fff; var cshar = (st.sequence >>> 8) | 0x80; // set variant '10' var csl = st.sequence & 0xff; - return new UUID()._init(tl, tf.mid, thav, cshar, csl, st.node); + return new UUID(tl, tf.mid, thav, cshar, csl, st.node); } /** * Re-initializes the internal state for version 1 UUID creation. @@ -288,7 +286,7 @@ export class UUID { */ static genV6() { if (UUID._state == null) { - UUID.resetState(); + UUID._state = new UUIDState(); } var now = new Date().getTime(), st = UUID._state; if (now != st.timestamp) { @@ -316,7 +314,7 @@ export class UUID { st.sequence &= 0x3fff; var cshar = (st.sequence >>> 8) | 0x80; // set variant '10' var csl = st.sequence & 0xff; - return new UUID()._init(th, tm, tlav, cshar, csl, st.node); + return new UUID(th, tm, tlav, cshar, csl, st.node); } } _a = UUID; @@ -367,7 +365,7 @@ UUID.FIELD_SIZES = [32, 16, 16, 8, 8, 48]; * @constant * @since v3.4.0 */ -UUID.NIL = new UUID()._init(0, 0, 0, 0, 0, 0); +UUID.NIL = new UUID(0, 0, 0, 0, 0, 0); /** * Persistent internal state for version 1 UUID creation. * @private diff --git a/src/uuid.ts b/src/uuid.ts index 34cbf65..68bd30f 100644 --- a/src/uuid.ts +++ b/src/uuid.ts @@ -135,7 +135,7 @@ export class UUID { */ static genV4(): UUID { var rand = UUID._getRandomInt; - return new UUID()._init( + return new UUID( rand(32), // time_low rand(16), // time_mid 0x4000 | rand(12), // time_hi_and_version @@ -163,7 +163,7 @@ export class UUID { (l === "{" && t === "}") || (l.toLowerCase() === "urn:uuid:" && t === "") ) { - return new UUID()._init( + return new UUID( parseInt(r[2], 16), parseInt(r[3], 16), parseInt(r[4], 16), @@ -209,8 +209,6 @@ export class UUID { readonly hexString: string; readonly urn: string; - private constructor() {} - /** * Initializes a {@link UUID} object. * @private @@ -223,14 +221,14 @@ export class UUID { * @param {number} [node=0] node field (octet 10-15, uint48). * @returns {UUID} this. */ - private _init( + private constructor( _timeLow: number, _timeMid: number, _timeHiAndVersion: number, _clockSeqHiAndReserved: number, _clockSeqLow: number, _node: number - ): this { + ) { var names = UUID.FIELD_NAMES, sizes = UUID.FIELD_SIZES; var bin = UUID._binAligner, @@ -304,8 +302,6 @@ export class UUID { * @type {string} */ this.urn = "urn:uuid:" + this.hexString; - - return this; } /** @@ -358,7 +354,7 @@ export class UUID { * @constant * @since v3.4.0 */ - static readonly NIL: UUID = new UUID()._init(0, 0, 0, 0, 0, 0); + static readonly NIL: UUID = new UUID(0, 0, 0, 0, 0, 0); // }}} @@ -371,7 +367,7 @@ export class UUID { */ static genV1(): UUID { if (UUID._state == null) { - UUID.resetState(); + UUID._state = new UUIDState(); } var now = new Date().getTime(), st = UUID._state; @@ -399,7 +395,7 @@ export class UUID { var cshar = (st.sequence >>> 8) | 0x80; // set variant '10' var csl = st.sequence & 0xff; - return new UUID()._init(tl, tf.mid, thav, cshar, csl, st.node); + return new UUID(tl, tf.mid, thav, cshar, csl, st.node); } /** @@ -452,7 +448,7 @@ export class UUID { */ static genV6(): UUID { if (UUID._state == null) { - UUID.resetState(); + UUID._state = new UUIDState(); } var now = new Date().getTime(), st = UUID._state; @@ -482,7 +478,7 @@ export class UUID { var cshar = (st.sequence >>> 8) | 0x80; // set variant '10' var csl = st.sequence & 0xff; - return new UUID()._init(th, tm, tlav, cshar, csl, st.node); + return new UUID(th, tm, tlav, cshar, csl, st.node); } // }}}