Created
June 21, 2022 13:18
-
-
Save nilsreichardt/3d6e0576fdcab435c4ec0068d773349d to your computer and use it in GitHub Desktop.
Id generator in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function generateId(length: number): string { | |
let outString: string = ''; | |
const inOptions: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
for (let i = 0; i < length; i++) { | |
outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length)); | |
} | |
return outString; | |
} | |
export function generateFirestoreId(): string { | |
return generateId(20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from "chai"; | |
import { describe, it } from "mocha"; | |
import { | |
generateFirestoreId, | |
generateId, | |
} from "../../../src/core/utils/id_generator"; | |
describe("generateId()", () => { | |
it("should generate an id with the given length", () => { | |
const id = generateId(25); | |
expect(id.length).is.equal(25); | |
}); | |
}); | |
describe("generateFirestoreId()", () => { | |
it("should generate an id with a length of 20", () => { | |
const id = generateFirestoreId(); | |
expect(id.length).is.equal(20); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment