ID Generator
idGenerator.js
js
function* idGenerator(prefix = "", initialIndex = 0) {
let index = initialIndex;
while (true) yield `${prefix}${index++}`;
}idGenerator.test.js
js
describe("idGenerator", () => {
it("should generate user ids", () => {
const userIds = idGenerator("USER_");
for (let idx = 0; idx < 42; idx++) {
userIds.next();
}
expect(userIds.next().value).toEqual("USER_42");
});
it("should generate post ids", () => {
const postIds = idGenerator("POST_", 1000);
for (let idx = 0; idx < 111; idx++) {
postIds.next();
}
expect(postIds.next().value).toEqual("POST_1111");
});
});