npm node-uuid and uuid

uuid

Features

Generate a unique string
As for uniqueness, I also suspected that, referring to [1], there are 5 main usages

  • (1) Time-based
  • (2) DCE safe
  • (3) Name-based
  • (4) Random
  • (5) Name-based (SHA1)

uuid module

usage

Reference [1], there are 5 usages

  • v1: time-based
> uuid.v1()
'3be65050-3a6c-11ea-9a1c-a770f4b5f889'
  • v2: During the DCE safety
    test, I said I could n’t find it. I did n’t go to verify it.
  • v3: Based on the name
    with the same name, the same uuid will be generated
> var v3 = require('uuid/v3')
undefined
> console.log(v3('Hello, World!', MY_NAMESPACE1));
114e1c3c-a1c9-34d4-95e3-9038ea162be9
undefined
>
  • v4: random
> uuid.v4()
'9f2e4328-c171-4c83-b063-71cb6bae9cfa'
  • v5: Name-based (SHA1)
> var v5 = require('uuid/v5')
undefined
> console.log(v5('Hello, World!', MY_NAMESPACE1));
92d6b5a2-22da-5c5c-b93b-0a59d45a961a
undefined
> console.log(v5('Hello, World!', MY_NAMESPACE1));
92d6b5a2-22da-5c5c-b93b-0a59d45a961a

node-uuid module

> var nodeuuid = require('node-uuid')
undefined
> nodeuuid.v1()
'83336050-3a6d-11ea-9541-6313a4ec04df'
> nodeuuid.v2()
Thrown:
TypeError: nodeuuid.v2 is not a function
> nodeuuid.v3()
Thrown:
TypeError: nodeuuid.v3 is not a function
> nodeuuid.v4()
'd66bbcb7-d359-4ff2-a5ba-094926a8a2f1'
> nodeuuid.v5()
Thrown:
TypeError: nodeuuid.v5 is not a function
> var nodeuuidv3 = require('node-uuid/v3')
Thrown:
E:\work\git\MiniServer\miniserver\node_modules\node-uuid\v3.js:7
  if (typeof(name) != 'string) {
                      ^^^^^^^^^^

SyntaxError: Invalid or unexpected token
>

summary

  • Choose different usages according to different functions
    (1) For example, user ID, it is best to use v3 / v5, because if it is the only key, it must be the same;
    (2) If it is temporary, you can use v1 or v4 For example, when processing the message queue, you need a uuid function to temporarily process a request / response. You can generate a function that will not be needed after you run out of it.
  • node-uuid only supports v1 and v5, and if you want v3 or v5, use the uuid module

reference

[1] How does UUID guarantee uniqueness?
[2] TypeError: uuid_1.v5 is not a function

Published 41 original articles · praised 7 · 20,000+ views

Guess you like

Origin blog.csdn.net/pkxpp/article/details/104057407