Skip to content

uuid使用

js
import {
  v4 as uuidv4,
  NIL as NIL_UUID,
  parse as uuidParse,
  stringify as uuidStringify,
  v1 as uuidv1,
} from "uuid";

//NIL_UUID
console.log("NIL_UUID: ", NIL_UUID);

//uuidv4
console.log("uuidv4: ", uuidv4());

const v4options = {
  random: [
    0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, 0x71, 0xb4, 0xef, 0xe1,
    0x67, 0x1c, 0x58, 0x36,
  ],
};
console.log("uuidv4: ", uuidv4(v4options));

// Parse a UUID
const bytes = uuidParse("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b");

// Convert to hex strings to show byte order (for documentation purposes)
console.log(
  "uuidParse",
  [...bytes].map((v) => v.toString(16).padStart(2, "0"))
);

//uuidStringify
const uuidBytes = [
  0x6e, 0xc0, 0xbd, 0x7f, 0x11, 0xc0, 0x43, 0xda, 0x97, 0x5e, 0x2a, 0x8a, 0xd9,
  0xeb, 0xae, 0x0b,
];
console.log("uuidBytes", uuidStringify(uuidBytes));

//uuidv1();
console.log("uuidv1", uuidv1());

const v1options = {
  node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
  clockseq: 0x1234,
  msecs: new Date("2011-11-01").getTime(),
  nsecs: 5678,
};
console.log("uuidv1", uuidv1(v1options));