Skip to content

Instantly share code, notes, and snippets.

@nilsreichardt
Created June 21, 2022 13:18
Show Gist options
  • Save nilsreichardt/3d6e0576fdcab435c4ec0068d773349d to your computer and use it in GitHub Desktop.
Save nilsreichardt/3d6e0576fdcab435c4ec0068d773349d to your computer and use it in GitHub Desktop.
Id generator in TypeScript
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);
}
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