This doesn't warrant an article, but since it's common enough...
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
: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