Skip to content

Commit

Permalink
Change generated project name to become /^(random_word)-[0-9a-zA-z]{6}$/
Browse files Browse the repository at this point in the history
  • Loading branch information
pkong-ds committed Aug 23, 2024
1 parent c3eb30c commit a937313
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
33 changes: 16 additions & 17 deletions portal/src/util/projectname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,35 @@ describe("maskNumber", () => {
});

describe("deterministicProjectName", () => {
it("deterministicProjectName(0) is 'abandon-abandon-0'", () => {
it("deterministicProjectName(0) should starts with 'abandon-' and ends with 6 alphanumeric characters", () => {
// 0 is 0b00000000000_00000000000_0000000000
// 0b00000000000 is abandon
// 0b0000000000 is 0
// So the name is abandon-abandon-0
expect(deterministicProjectName(0)).toEqual("abandon-abandon-0");
// So the name starts with abandon
expect(deterministicProjectName(0)).toMatch(/^abandon-[0-9A-Za-z]{6}$/);
});

it("deterministicProjectName(1) is ''", () => {
it("deterministicProjectName(0) should starts with 'abandon-' and ends with 6 alphanumeric characters", () => {
// 1 is 0b00000000000_00000000000_0000000001
// 0b00000000000 is abandon
// 0b0000000001 is 1
// So the name is abandon-abandon-1
expect(deterministicProjectName(1)).toEqual("abandon-abandon-1");
// So the name starts with abandon
expect(deterministicProjectName(1)).toMatch(/^abandon-[0-9A-Za-z]{6}$/);
});

it("deterministicProjectName(87878787) is ''", () => {
it("deterministicProjectName(87878787) should starts with 'ahead' and ends with 6 alphanumeric characters", () => {
// 87878787 is 0b00000101001_11100111011_0010000011
// 0b00000101001 is ahead
// 0b11100111011 is trash
// 0b0010000011 is 131
// So the name is ahead-trash-131
expect(deterministicProjectName(87878787)).toEqual("ahead-trash-131");
// So the name starts with ahead
expect(deterministicProjectName(87878787)).toMatch(
/^ahead-[0-9A-Za-z]{6}$/
);
});

it("deterministicProjectName(4294967295) is ''", () => {
it("deterministicProjectName(4294967295) should starts with 'zoo-' and ends with 6 alphanumeric characters", () => {
// 4294967295 (2^32 - 1) is 0b11111111111_11111111111_1111111111
// 0b11111111111 is zoo
// 0b1111111111 is 1023
// So the name is zoo-zoo-1023
expect(deterministicProjectName(4294967295)).toEqual("zoo-zoo-1023");
// So the name starts with zoo-
expect(deterministicProjectName(4294967295)).toMatch(
/^zoo-[0-9A-Za-z]{6}$/
);
});
});
21 changes: 17 additions & 4 deletions portal/src/util/projectname.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// ref https://github.com/bitcoinjs/bip39/blob/master/src/wordlists/english.json
import * as wordlist from "./wordlist.json";

const RANDOM_ALPHA_NUMERIC_STRING_LENGTH = 6;

function determineWord(index: number): string {
return wordlist[index];
}
Expand All @@ -15,15 +17,26 @@ export function maskNumber(num: number, startAt: number, bits: number): number {
return (num >> startAt) & ((1 << bits) - 1);
}

export function getRandomAlphaNumericString(len: number): string {
let result = "";
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < len; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

export function deterministicProjectName(num: number): string {
const randomNumber = maskNumber(num, 0, 10);
const secondRandomStringIndex = maskNumber(num, 10, 11);
const firstRandomStringIndex = maskNumber(num, 21, 11);

const firstRandomString = determineWord(firstRandomStringIndex);
const secondRandomString = determineWord(secondRandomStringIndex);

return `${firstRandomString}-${secondRandomString}-${randomNumber}`;
const randomAlphaNumericString = getRandomAlphaNumericString(
RANDOM_ALPHA_NUMERIC_STRING_LENGTH
);

return `${firstRandomString}-${randomAlphaNumericString}`;
}

export function randomProjectName(): string {
Expand Down

0 comments on commit a937313

Please sign in to comment.