typescript generate uuid from string code example

Example: create uuid typescript

// Method 1: use 'uuid' NPM library
//  $ npm i uuid

import { v4 as uuid } from 'uuid';
// ----------------or----------------
const uuid = require('uuid/v4');

const myUUID = uuid();

// Method 2: manually write uuid function
const uuid = () =>
  "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    var r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });

const myUUID = uuid();

/*
	Hope this answered your question. Happy coding :)
*/