-1

I know that the format of uuid is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. But I dont want to create a uuid, I want to create a unique identifier with a different format such as XXX-XXXX-XXX.

How do I do that using plain javascript and no libraries.

And is there any way to ensure that the identifier will be unique?

Drkawashima
  • 8,837
  • 5
  • 41
  • 52
  • `XXX-XXXX-XXX` **is not a UUID** though. So what are you _really_ wanting to do? – Dai Oct 10 '21 at 10:26
  • Is this for a distributed system at all? – Dai Oct 10 '21 at 10:26
  • i think you want to generate unique keys matching your format `XXX-XXXX-XXX` but not specifically uuid, right ? – Oriun Oct 10 '21 at 10:31
  • 1
    Check out the crypto api https://developer.mozilla.org/en-US/docs/Web/API/Crypto. Be careful since it’s not supported in all browsers – evolutionxbox Oct 10 '21 at 10:40
  • 1
    This isn't a "real" uuid but you could write a recursive function that randomly generates each of the fields of the uuid and keeps a memoized copy of the already generated uuids, rerunning itself until a new one is created every time it's called. – Dov Rine Oct 10 '21 at 10:44
  • @DovRine What does recursion have to do with it? – Dai Oct 10 '21 at 10:54
  • @Dai: It's a way for the function to re-call itself if the generated uuid already exists. I'll post an example as an answer in a minute – Dov Rine Oct 10 '21 at 10:58
  • @DovRine Yes, I know - but my (implicit) point was that recursion isn't necessary to implement memoization, and will make it harder to maintain. Unlike with, say, tree graph traversal or factorial number generation, there's nothing _naturally recursive_ about UUID generation nor memoization. – Dai Oct 10 '21 at 11:06
  • @Dai: The OP didn't actuall ask for UUID. It asked for, "how can we create a code like XXX-XXXX-XXX using no libraries in plain javascript. And is there any way to ensure that no uid matches at all?" This is what I was answering. – Dov Rine Oct 10 '21 at 11:18

1 Answers1

-1

This is an example of how to create a unique fake uuid looking string. Note that this is not a real uuid.

let uuids = [];

function getRandomUuidChar(count = 1) {
  // valid uuid chars: 0-9, a-f, A-F
  const chars = "1234567890abcdefABCDEF".split('');
  let retval = "";
  for (let i = 0; i < count; i++) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    retval += chars[randomIndex];
  }
  return retval;
}

function generatePseudoUuid() {
  //  XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  const charCounts = [8, 4, 4, 4, 12];
  const uuid = charCounts
    .reduce((retval, count) => {
      retval.push(getRandomUuidChar(count));
      return retval;
    }, [])
    .join("-");
  if (!uuids.includes(uuid)) {
    uuids.push(uuid);
    return uuid;
  }
  generateUuid();
}
for(let i=0; i<10; i++){
  console.log("uuid", generateUuid());
}
console.log(uuids);

output:

uuid 05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64
uuid 10dc0BCb-1DfD-8efB-EA7c-3f056930315C
uuid 8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E
uuid b484Ca84-Ce89-eb91-94F4-8216b86A7245
uuid De70ABB8-D4E9-DBba-7783-b8109E1b5E27
uuid F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0
uuid aEffE093-faf8-FBA2-bb88-08865b446412
uuid a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C
uuid C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7
uuid E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9
[
  '05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64',
  '10dc0BCb-1DfD-8efB-EA7c-3f056930315C',
  '8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E',
  'b484Ca84-Ce89-eb91-94F4-8216b86A7245',
  'De70ABB8-D4E9-DBba-7783-b8109E1b5E27',
  'F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0',
  'aEffE093-faf8-FBA2-bb88-08865b446412',
  'a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C',
  'C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7',
  'E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9'
]
Dov Rine
  • 810
  • 6
  • 12
  • Your `generateUuid` function does not implement the _real_ UUID algorithm to avoid collisions - this is just a concatenation of pseudo-random digits. – Dai Oct 10 '21 at 11:08
  • The output doesn’t match the desired format – evolutionxbox Oct 10 '21 at 11:08
  • @Dai: That's why I said originally that this isn't a "real" uuid – Dov Rine Oct 10 '21 at 11:13
  • You didn't say that in your answer though. And the fact the function is named `generateUuid` _implies_ it's a spec-compliant implementation. – Dai Oct 10 '21 at 11:13
  • @evolutionxbox: I copied the format from the original post, XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. I just put it in a for loop to demonstrate the memoization and potentially recursive calls. – Dov Rine Oct 10 '21 at 11:14
  • @Dai: fair enough. In my head, the answer was linked to my original comment. I'll edit it shortly. – Dov Rine Oct 10 '21 at 11:15