Skip to content

Commit

Permalink
refactor!: convert _init() method to private constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
LiosK committed Jan 24, 2023
1 parent 11cc05e commit c1a8fde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions dist/uuid.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 8 additions & 10 deletions dist/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions src/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -209,8 +209,6 @@ export class UUID {
readonly hexString: string;
readonly urn: string;

private constructor() {}

/**
* Initializes a {@link UUID} object.
* @private
Expand All @@ -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,
Expand Down Expand Up @@ -304,8 +302,6 @@ export class UUID {
* @type {string}
*/
this.urn = "urn:uuid:" + this.hexString;

return this;
}

/**
Expand Down Expand Up @@ -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);

// }}}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

// }}}
Expand Down

0 comments on commit c1a8fde

Please sign in to comment.