Gathering detailed insights and metrics for @avolantis/ts-guid
Gathering detailed insights and metrics for @avolantis/ts-guid
Gathering detailed insights and metrics for @avolantis/ts-guid
Gathering detailed insights and metrics for @avolantis/ts-guid
[deprecated] TypeScript implementation of RFC 4122 GUIDs (UUIDs)
npm install @avolantis/ts-guid
Typescript
Module System
TypeScript (85.6%)
JavaScript (14.4%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
161 Commits
8 Branches
2 Contributors
Updated on Mar 15, 2023
Latest Version
0.1.0-preview.3
Package Id
@avolantis/ts-guid@0.1.0-preview.3
Unpacked Size
269.53 kB
Size
62.78 kB
File Count
48
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
28
@avolantis/ts-guid
TypeScript implementation of RFC 4122 GUIDs (UUIDs)
Features:
src/
)types/
)dist/
):
node.js >= v12
Add this module to your package.json
via yarn, npm or pnpm:
yarn add @avolantis/ts-guid
# OR
npm install --save @avolantis/ts-guid
# OR
pnpm install --save @avolantis/ts-guid
Then in your program files:
1// ES Modules OR 2import { Guid } from "@avolantis/ts-guid"; 3// Common Js require for old versions of node.js 4const { Guid } = require("@avolantis/ts-guid"); 5 6// Simple usage 7console.log(Guid.newGuid()); 8console.log(Guid.EMPTY.toString()); 9console.log(Guid.parse("c959e321-19df-474c-a2e7-df268dbf3998").toByteArray()); 10console.log(JSON.stringify(Guid.newGuid())); 11 12// More features 13const user = { 14 id: Guid.newGuid(), 15 name: "John Doe", 16 email: "john@doe.com" 17}; 18 19// Can be converted to JSON when inside an object 20console.log(JSON.stringify(user, null, 2)); 21 22// Output: 23// { 24// "id": "c959e321-19df-474c-a2e7-df268dbf3998", 25// "name": "John Doe", 26// "email": "john@doe.com" 27// } 28 29console.log(`User ${user.id} not found!`); 30 31// Output: 32// User {c959e321-19df-474c-a2e7-df268dbf3998} not found!
NOTE
If you are targeting browsers without Crypto API support, you either need to polyfill this feature, or enable unsafe random generation (deprecated). Polyfills for the Crypto API are not included in the bundles.
This package includes transpiled and polyfilled code for direct usage via unpkg or
skypack in browsers that support ESM features or satisfies the
> 0.25%, last 2 versions, not dead
browserslist query.
1<!-- Browsers with ESM support --> 2<script type="module" src="https://unpkg.com/@avolantis/ts-guid/dist/ts-guid.esm.min.js"></script> 3<!-- UMD --> 4<script src="https://unpkg.com/@avolantis/ts-guid"></script> 5<!-- IIFE --> 6<script src="https://unpkg.com/@avolantis/ts-guid/dist/ts-guid.iife.min.js"></script> 7 8<!-- Recommended (target both): --> 9<script type="module" src="https://unpkg.com/@avolantis/ts-guid/dist/ts-guid.esm.min.js"></script> 10<script nomodule src="https://unpkg.com/@avolantis/ts-guid"></script>
Then in your scripts:
1const { Guid } = TsGuid; 2// ...
Guid.newGuid()
Generates a new GUID using the generator function.
If no generator function is available, it always returns Guid.EMPTY
.
Guid.parse(value)
(might throw!)Parses a string value that represents a GUID.
Returns the parsed Guid
or Guid.EMPTY
, if given an invalid value.
Throws TypeError
, if value is not a string
and does not coerce null
.
Guid.fromByteArray(array)
(might throw!)Returns the Guid
composed of the given bytes.
Throws TypeError
, if value is not an Array
, it's not of length 16 or any
of the values coerces null
or not within 0 and 255.
Guid.fromJSON(json)
(might throw!)Parses a json string that represents a GUID state.
Returns the parsed Guid
or Guid.EMPTY
, if value is null
or an invalid
string.
Throws TypeError
, if value is undefined
or not a string
.
Guid.isGuid(value)
Returns true if value
is an instance of Guid
.
Guid.compare(guid1, guid2)
Compares two Guid
-s using locale-specific comparison of their string
representations.
The result table:
guid1 < guid2 | guid1 === guid2 | otherwise |
---|---|---|
-1 | 0 | 1 |
This is useful when ordering an array of Guid
-s, like so:
1const arr = [Guid.newGuid(), Guid.newGuid(), Guid.newGuid()]; 2console.log(arr); 3// Sort alphabetically 4console.log(arr.sort(Guid.compare)); 5// Sort alphabetically in reversed order 6console.log(arr.sort(Guid.compare).reverse());
Guid.equals(guid1, guid2)
Returns true, if both
guid1
represents the same GUID as guid2
, andguid1
and guid2
are instances of Guid
1const a = Guid.newGuid(); 2const b = Guid.newGuid(); 3console.log(a.equals(b)); // Prints false 4 5// DO NOT USE THE '==' OR '===' OPERATORS, as they compare objects by reference 6const a = Guid.parse("c959e321-19df-474c-a2e7-df268dbf3998"); 7const b = Guid.parse("c959e321-19df-474c-a2e7-df268dbf3998"); 8console.log(a == b); // Prints false, which is WRONG 9console.log(a === b); // Prints false, which is WRONG
Guid.validate(value)
Determines if a string or bytes array value represents a valid GUID.
Guid.EMPTY
(read-only)The Guid
that represents an empty (NIL) GUID.
Guid.generator
The the generator function used to generate new
Guid
-s.
compare(other)
this < other | this === other | otherwise |
---|---|---|
-1 | 0 | 1 |
equals(other)
Returns true if other
is Guid and represents the same GUID as this one.
isEmpty()
Returns true if the GUID equals to Guid.EMPTY
, otherwise false.
toByteArray()
Returns an array of 16 bytes (8-bit unsigned integers) that this GUID is composed of. If missing (ie. not created from a byte array), the bytes are computed once then cached for frequent access. Internal computation of some fields require the byte array value to be computed, these can cause the value to be already cached upon first direct access.
toJSON()
Returns the json representation of this GUID (which is its string
representation). This method gets automatically called by the JSON.stringify()
method, so usage of the latter is also supported when an object has a field of
type Guid
. For example: JSON.stringify(user)
, where user
has property id
,
which is of type Guid
, the JSON string output contains the string
representation of that GUID.
toString()
Returns the string representation of the GUID. The representation is always calculated and available from cache.
valueOf()
Returns the primitive (string) representation of this GUID. This overload allows
the use of comparison operators (<
, <=
, >
and >=
) with Guid
-s.
Note that javascript compares strings based on character code order,
to preform true alphabetical comparison, use Guid.copmare(a, b)
or
a.compare(b)
.
The following values are computed once, then cached for frequent access
variant
(read-only)The RFC variant identifier of this GUID. Example: GuidVariant.MicrosoftReserved
The value of this field determines the layout of the bytes in the byte array of
the components in the GUID.
version
(read-only)The RFC version number of this GUID. Example: GuidVersion.V4
The following values are computed on demand (from the cached byte array)
clock_seq_low
(read-only)The clock_seq_low
field of this GUID, specified by the RFC. It is an 8-bit
unsigned integer.
clock_seq_hi_and_reserved
(read-only)The clock_seq_hi_and_reserved
field of this GUID, specified by the RFC. It is
an 8-bit unsigned integer.
time_low
(read-only)The time_low
field of this GUID, specified by the RFC. It is a 32-bit unsigned
integer.
time_mid
(read-only)The time_mid
field of this GUID, specified by the RFC. It is a 16-bit unsigned
integer.
time_high_and_version
(read-only)The time_high_and_version
field of this GUID, specified by the RFC. It is a
16-bit unsigened integer.
node
(read-only)The node
field of this GUID. It is an 8-bit unsigned integer array of size 6,
representing the 48-bit unsigned integer specified by the RFC.
Currently, only one generator for RFC version V4 GUIDs is implemented. PR-s are welcome :)
The default implementation for generating new values uses the crypto API to
seed the generation process of V4 GUIDs. However, this can be changed via
setting the Guid.generator
static property to a different generator function,
for example a function, that generates GUID-s sequentially for use with RDBMS.
1// A generator function that returns a byte array 2Guid.generator = function() { 3 return new Array(16).fill(0); 4} 5 6// A generator function that returns a string 7Guid.generator = function() { 8 return "<here comes a new value>"; 9}
If the default generator function is not available due to the missing cryptographic random generator, a warning message is printed to the console. This allows developers to identify this issue on a given platform, and either supply a polyfill or provide another generator function.
DANGER ZONE
This is only for environments, where the incresaed rate of collisions does not affect data safety. Use this only if you know what you are doing.
Only a cryptographic random number generator is recommended to seed the
generation of new GUIDs, however certain JS platforms still does not provide
such API. You can explicitly enable a fallback behavior to Math.random()
using the supplied generator function.
1import { Guid, unsafeRandom } from "@avolantis/ts-guid"; 2 3Guid.generator = unsafeRandom;
See the GitHub issues for more information.
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 1/5 approved changesets -- score normalized to 2
Reason
project is archived
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
33 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More