Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. This doesn't warrant an article, but since it's common enough...

This doesn't warrant an article, but since it's common enough...

Scheduled Pinned Locked Moved The Lounge
questionworkspace
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #1

    Had to implement a globally unique ID generator that would work in distributed/disconnected system for a project in TS today. Doesn't warrant an article (I think??), so here it is if anyone wants it. Much like YouTube IDs, etc. the result is in base62 to keep it as short as possible. Runs fast enough to generate 100,000 IDs in 340ms in a WSL environment. Big ol' edit: Made this a tip/trick. Nothing to see here now. La la la.

    Jeremy Falcon

    J J 2 Replies Last reply
    0
    • J Jeremy Falcon

      Had to implement a globally unique ID generator that would work in distributed/disconnected system for a project in TS today. Doesn't warrant an article (I think??), so here it is if anyone wants it. Much like YouTube IDs, etc. the result is in base62 to keep it as short as possible. Runs fast enough to generate 100,000 IDs in 340ms in a WSL environment. Big ol' edit: Made this a tip/trick. Nothing to see here now. La la la.

      Jeremy Falcon

      J Offline
      J Offline
      Jeremy Falcon
      wrote on last edited by
      #2

      Dem unit tests...

      import { createUniqueId, toBase62 } from '@/app/helpers';

      // example id, 2JoUqI7KA4PYaOZ12LayQjMO
      const ID_LEN = 24;

      const isArrayUnique = (array: T[]) => {
      return Array.isArray(array) && new Set(array).size === array.length;
      };

      const isMatchDataLength = (array: T[], dataLength: number) => {
      if (Array.isArray(array)) {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const count = array.reduce((a, c) => a + ((c as any)?.length === dataLength ? 1 : 0), 0);
      return count === array.length;
      } else
      return false;
      };

      describe('Identify', () => {
      describe('createUniqueId', () => {
      test('Expect 1,000 ids to be unique when generated in back-to-back.', () => {
      const ids = Array(1000).fill(0).map(() => createUniqueId());

        expect(isArrayUnique(ids)).toBeTruthy();
        expect(isMatchDataLength(ids, ID\_LEN)).toBeTruthy();
      });
      

      });

      describe('toBase62', () => {
      const expected = [
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
      'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
      'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '10', '11'
      ] as string[];

      test('Expect to be able to count from zero to 63 and convert correctly.', () => {
        const result = Array(64).fill(0).map((\_e, x) => toBase62(x));
        expect(result).toStrictEqual(expected);
      });
      
      test('Expect to have the same result as the examples on the base62.js GitHub site.', () => {
        expect(toBase62(999)).toEqual('g7');
        expect(toBase62(9999)).toEqual('2Bh');
        expect(toBase62(238327)).toEqual('ZZZ');
      });
      
      // see https://normaltool.com/dencoders/base62-encoder
      test('Expect to have the same result as random examples converted by normaltool.com.', () => {
        expect(toBase62(16424)).toEqual('4gU');
        expect(toBase62(24610)).toEqual('6oW');
        expect(toBase62(30456)).toEqual('7Ve');
        expect(toBase62(47200)).toEqual('chi');
        expect(toBase62(66420)).toEqual('hhi');
        expect(toBase62(75514)).toEqual('jDY');
      });
      

      });
      });

      Jeremy Falcon

      1 Reply Last reply
      0
      • J Jeremy Falcon

        Had to implement a globally unique ID generator that would work in distributed/disconnected system for a project in TS today. Doesn't warrant an article (I think??), so here it is if anyone wants it. Much like YouTube IDs, etc. the result is in base62 to keep it as short as possible. Runs fast enough to generate 100,000 IDs in 340ms in a WSL environment. Big ol' edit: Made this a tip/trick. Nothing to see here now. La la la.

        Jeremy Falcon

        J Offline
        J Offline
        jeron1
        wrote on last edited by
        #3

        Perhaps Tip/Trick or Weird and Wonderful material?

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        J 2 Replies Last reply
        0
        • J jeron1

          Perhaps Tip/Trick or Weird and Wonderful material?

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          jeron1 wrote:

          Perhaps Tip/Trick or Weird and Wonderful material?

          Oh crap, I'm gonna seem like an old fart now... what's the tip/trick thing?

          Jeremy Falcon

          1 Reply Last reply
          0
          • J jeron1

            Perhaps Tip/Trick or Weird and Wonderful material?

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

            J Offline
            J Offline
            Jeremy Falcon
            wrote on last edited by
            #5

            Ooooooooh, wait. I see it now. Um, it's been a while since I posted a new article, as you can tell. :laugh: :-O Thanks.

            Jeremy Falcon

            J 1 Reply Last reply
            0
            • J Jeremy Falcon

              Ooooooooh, wait. I see it now. Um, it's been a while since I posted a new article, as you can tell. :laugh: :-O Thanks.

              Jeremy Falcon

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #6

              :thumbsup: :)

              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups