Comparing version 2.2.1 to 2.3.0
# Changelog | ||
## v2.3.0 - 2023-02-18 | ||
### Added | ||
- `Scru128Id#bytes`, `Scru128Id.fromInner()`, and `Scru128Id.fromBytes()` | ||
### Maintenance | ||
- Updated dev dependencies | ||
## v2.2.1 - 2023-01-31 | ||
@@ -4,0 +14,0 @@ |
@@ -36,6 +36,21 @@ /** | ||
export declare class Scru128Id { | ||
private readonly bytes; | ||
/** | ||
* 16-byte byte array containing the 128-bit unsigned integer representation | ||
* in the big-endian (network) byte order. | ||
*/ | ||
readonly bytes: Readonly<Uint8Array>; | ||
/** Creates an object from a 16-byte byte array. */ | ||
private constructor(); | ||
/** | ||
* Creates an object from the internal representation, a 16-byte byte array | ||
* containing the 128-bit unsigned integer representation in the big-endian | ||
* (network) byte order. | ||
* | ||
* This method does NOT shallow-copy the argument, and thus the created object | ||
* holds the reference to the underlying buffer. | ||
* | ||
* @throws TypeError if the length of the argument is not 16. | ||
*/ | ||
static fromInner(bytes: Uint8Array): Scru128Id; | ||
/** | ||
* Creates an object from field values. | ||
@@ -67,2 +82,11 @@ * | ||
/** | ||
* Creates an object from an array of Base36 digit values representing a | ||
* 25-digit string representation. | ||
* | ||
* @throws SyntaxError if the argument does not contain a valid string | ||
* representation. | ||
* @category Conversion | ||
*/ | ||
private static fromDigitValues; | ||
/** | ||
* Returns the 25-digit canonical string representation. | ||
@@ -74,5 +98,22 @@ * | ||
/** | ||
* Creates an object from a byte array representing either a 128-bit unsigned | ||
* integer or a 25-digit Base36 string. | ||
* | ||
* This method shallow-copies the content of the argument, so the created | ||
* object holds another instance of the byte array. | ||
* | ||
* @param value - an array of 16 bytes that contains a 128-bit unsigned | ||
* integer in the big-endian (network) byte order or an array of 25 ASCII code | ||
* points that reads a 25-digit Base36 string. | ||
* @throws SyntaxError if conversion fails. | ||
* @category Conversion | ||
*/ | ||
static fromBytes(value: ArrayLike<number>): Scru128Id; | ||
/** | ||
* Creates an object from a byte array that represents a 128-bit unsigned | ||
* integer. | ||
* | ||
* This method shallow-copies the content of the argument, so the created | ||
* object holds another instance of the byte array. | ||
* | ||
* @param value - 16-byte buffer that represents a 128-bit unsigned integer in | ||
@@ -79,0 +120,0 @@ * the big-endian (network) byte order. |
@@ -65,2 +65,15 @@ /** | ||
/** | ||
* Creates an object from the internal representation, a 16-byte byte array | ||
* containing the 128-bit unsigned integer representation in the big-endian | ||
* (network) byte order. | ||
* | ||
* This method does NOT shallow-copy the argument, and thus the created object | ||
* holds the reference to the underlying buffer. | ||
* | ||
* @throws TypeError if the length of the argument is not 16. | ||
*/ | ||
static fromInner(bytes) { | ||
return new Scru128Id(bytes); | ||
} | ||
/** | ||
* Creates an object from field values. | ||
@@ -139,6 +152,17 @@ * | ||
src[i] = (_a = DECODE_MAP[value.charCodeAt(i)]) !== null && _a !== void 0 ? _a : 0x7f; | ||
if (src[i] === 0x7f) { | ||
throw new SyntaxError("invalid digit: " + value.charAt(i)); | ||
} | ||
} | ||
return Scru128Id.fromDigitValues(src); | ||
} | ||
/** | ||
* Creates an object from an array of Base36 digit values representing a | ||
* 25-digit string representation. | ||
* | ||
* @throws SyntaxError if the argument does not contain a valid string | ||
* representation. | ||
* @category Conversion | ||
*/ | ||
static fromDigitValues(src) { | ||
if (src.length !== 25) { | ||
throw new SyntaxError("invalid length: " + src.length); | ||
} | ||
const dst = new Uint8Array(16); | ||
@@ -150,3 +174,7 @@ let minIndex = 99; // any number greater than size of output array | ||
for (let j = i < 0 ? 0 : i; j < i + 8; j++) { | ||
carry = carry * 36 + src[j]; | ||
const e = src[j]; | ||
if (e < 0 || e > 35 || !Number.isInteger(e)) { | ||
throw new SyntaxError("invalid digit"); | ||
} | ||
carry = carry * 36 + e; | ||
} | ||
@@ -199,5 +227,35 @@ // iterate over output array from right to left while carry != 0 but at | ||
/** | ||
* Creates an object from a byte array representing either a 128-bit unsigned | ||
* integer or a 25-digit Base36 string. | ||
* | ||
* This method shallow-copies the content of the argument, so the created | ||
* object holds another instance of the byte array. | ||
* | ||
* @param value - an array of 16 bytes that contains a 128-bit unsigned | ||
* integer in the big-endian (network) byte order or an array of 25 ASCII code | ||
* points that reads a 25-digit Base36 string. | ||
* @throws SyntaxError if conversion fails. | ||
* @category Conversion | ||
*/ | ||
static fromBytes(value) { | ||
for (let i = 0; i < value.length; i++) { | ||
const e = value[i]; | ||
if (e < 0 || e > 0xff || !Number.isInteger(e)) { | ||
throw new SyntaxError("invalid byte value"); | ||
} | ||
} | ||
if (value.length === 16) { | ||
return new Scru128Id(Uint8Array.from(value)); | ||
} | ||
else { | ||
return Scru128Id.fromDigitValues(Uint8Array.from(value, (c) => { var _a; return (_a = DECODE_MAP[c]) !== null && _a !== void 0 ? _a : 0x7f; })); | ||
} | ||
} | ||
/** | ||
* Creates an object from a byte array that represents a 128-bit unsigned | ||
* integer. | ||
* | ||
* This method shallow-copies the content of the argument, so the created | ||
* object holds another instance of the byte array. | ||
* | ||
* @param value - 16-byte buffer that represents a 128-bit unsigned integer in | ||
@@ -204,0 +262,0 @@ * the big-endian (network) byte order. |
{ | ||
"name": "scru128", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "SCRU128: Sortable, Clock and Random number-based Unique identifier", | ||
@@ -55,3 +55,3 @@ "type": "module", | ||
"mocha": "^10.2.0", | ||
"typedoc": "^0.23.24", | ||
"typedoc": "^0.23.25", | ||
"typescript": "^4.9.5", | ||
@@ -58,0 +58,0 @@ "webpack": "^5.75.0", |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
69172
1431
1