Skip to content

Commit

Permalink
Merge pull request #20 from sibevin/fix-readme
Browse files Browse the repository at this point in the history
Fix readme
  • Loading branch information
sibevin authored May 24, 2024
2 parents 80c8ef3 + 985fda4 commit be60918
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![doccument](https://img.shields.io/badge/document-blue)](https://sibevin.github.io/random-token.js/)
[![npm version](https://img.shields.io/npm/v/%40sibevin%2Frandom-token)](https://www.npmjs.com/package/@sibevin/random-token)
[![github](https://img.shields.io/badge/github-sibevin%40random--token.js-blue)](https://github.com/sibevin/random-token.js)
[![coverage](https://coveralls.io/repos/github/sibevin/random-token.js/badge.svg?branch=master)](https://coveralls.io/github/sibevin/random-token.js?branch=master)
[![coverage](https://coveralls.io/repos/github/sibevin/random-token.js/badge.svg?branch=prod)](https://coveralls.io/github/sibevin/random-token.js?branch=prod)

A simple way to generate a random token.

Expand Down
86 changes: 50 additions & 36 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,105 @@ import { describe, it, expect } from "vitest";
import * as RandomToken from "../src/index";

describe("RandomToken", () => {
describe("when no parameter is given", () => {
it("generates a default length, alphabet-number mixed-casing token", () => {
const token = RandomToken.gen();
expect(token.length).toEqual(RandomToken.DEFAULT_TOKEN_SIZE);
expect(token).toMatch(/^[0-9A-Za-z]+$/);
});
});
describe(".gen", () => {
describe("(length)", () => {
it("should generate a token with the given length", () => {
it("generates a token with the given length", () => {
const length = Math.floor(Math.random() * Math.floor(100));
const token = RandomToken.gen({ length });
expect(token.length).toEqual(length);
});
describe("when no length is given", () => {
it("should generate a token with the default length", () => {
it("generates a token with the default length", () => {
const token = RandomToken.gen({});
expect(token.length).toEqual(RandomToken.DEFAULT_TOKEN_SIZE);
});
});
});
describe("(seed)", () => {
describe("(seed: null)", () => {
it("should generate an alphabet-number mixed-casing token", () => {
it("generates an alphabet-number mixed-casing token", () => {
const token = RandomToken.gen({});
expect(token).toMatch(/^[0-9A-Za-z]+$/);
});
});
describe("(seed: number)", () => {
it("should generate a number token", () => {
it("generates a number token", () => {
const seedOptions: RandomToken.Seed[] = [10, 1, "n", "number"];
seedOptions.forEach((seed) => {
const token = RandomToken.gen({ seed });
expect(token).toMatch(/^[0-9]+$/);
});
});
describe("when the casing parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "number", casing: "lower" }),
).toThrow(/casing/);
});
});
describe("when the friendly parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "number", friendly: true }),
).toThrow(/friendly/);
});
});
});
describe("(seed: octal)", () => {
it("should generate an octal token", () => {
it("generates an octal token", () => {
const seedOptions: RandomToken.Seed[] = [8, "o", "oct", "octal"];
seedOptions.forEach((seed) => {
const token = RandomToken.gen({ seed });
expect(token).toMatch(/^[0-8]+$/);
});
});
describe("when the casing parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "octal", casing: "lower" }),
).toThrow(/casing/);
});
});
describe("when the friendly parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "octal", friendly: true }),
).toThrow(/friendly/);
});
});
});
describe("(seed: binary)", () => {
it("should generate a binary token", () => {
it("generates a binary token", () => {
const seedOptions: RandomToken.Seed[] = [2, "b", "binary"];
seedOptions.forEach((seed) => {
const token = RandomToken.gen({ seed });
expect(token).toMatch(/^[01]+$/);
});
});
describe("when the casing parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "binary", casing: "lower" }),
).toThrow(/casing/);
});
});
describe("when the friendly parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "binary", friendly: true }),
).toThrow(/friendly/);
});
});
});
describe("(seed: hexadecimal)", () => {
it("should generate a lowercase hexadecimal token", () => {
it("generates a lowercase hexadecimal token", () => {
const seedOptions: RandomToken.Seed[] = [
16,
"h",
Expand All @@ -106,7 +113,7 @@ describe("RandomToken", () => {
});
});
describe('when the "upper" casing parameter is given', () => {
it("should generate an uppercase hexadecimal token", () => {
it("generates an uppercase hexadecimal token", () => {
const token = RandomToken.gen({
seed: "hexadecimal",
casing: "upper",
Expand All @@ -115,30 +122,30 @@ describe("RandomToken", () => {
});
});
describe('when the "mixed" casing parameter is given', () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "hexadecimal", casing: "mixed" }),
).toThrow(/casing/);
});
});
describe("when the friendly parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "hexadecimal", friendly: true }),
).toThrow(/friendly/);
});
});
});
describe("(seed: alphabet)", () => {
it("should generate a lowercase alphabet-only token", () => {
it("generates a lowercase alphabet-only token", () => {
const seedOptions = ["a", "alphabet", "l", "letter"];
seedOptions.forEach((seed) => {
const token = RandomToken.gen({ seed });
expect(token).toMatch(/^[a-zA-Z]+$/);
});
});
describe('when the "upper" casing parameter is given', () => {
it("should generate a uppercase alphabet-only token", () => {
it("generates a uppercase alphabet-only token", () => {
const token = RandomToken.gen({
seed: "alphabet",
casing: "upper",
Expand All @@ -147,7 +154,7 @@ describe("RandomToken", () => {
});
});
describe('when the "mixed" casing parameter is given', () => {
it("should generate a mixed-case alphabet-only token", () => {
it("generates a mixed-case alphabet-only token", () => {
const token = RandomToken.gen({
seed: "alphabet",
casing: "mixed",
Expand All @@ -156,15 +163,15 @@ describe("RandomToken", () => {
});
});
describe("when the friendly parameter is given", () => {
it("should generate a friendly token", () => {
it("generates a friendly token", () => {
const token = RandomToken.gen({ seed: "alphabet", friendly: true });
RandomToken.FRIENDLY_MASK.split("").forEach((singleMask) => {
expect(token.split("")).not.toContain(singleMask);
});
});
});
describe("when the mask parameter is given", () => {
it("should generate a masked token", () => {
it("generates a masked token", () => {
const mask = "abcdefghijk";
const token = RandomToken.gen({ seed: "alphabet", mask });
mask.split("").forEach((singleMask) => {
Expand All @@ -174,41 +181,41 @@ describe("RandomToken", () => {
});
});
describe("(seed: customized)", () => {
it("should generate a token with given seeds", () => {
it("generates a token with given seeds", () => {
const seed = "12345abcdefGHIJK";
const token = RandomToken.gen({ seed });
expect(token).toMatch(/^[1-5a-fG-K]+$/);
});
describe('when the "upper" casing parameter is given', () => {
it("should generate a uppercase alphabet-only token", () => {
it("generates a uppercase alphabet-only token", () => {
const seed = "12345abcdefGHIJK";
const token = RandomToken.gen({ seed, casing: "upper" });
expect(token).toMatch(/^[1-5A-K]+$/);
});
});
describe('when the "lower" casing parameter is given', () => {
it("should generate a lowercase alphabet-only token", () => {
it("generates a lowercase alphabet-only token", () => {
const seed = "12345abcdefGHIJK";
const token = RandomToken.gen({ seed, casing: "lower" });
expect(token).toMatch(/^[1-5a-k]+$/);
});
});
describe('when the "mixed" casing parameter is given', () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "12345abcdefGHIJK", casing: "mixed" }),
).toThrow(/casing/);
});
});
describe("when the friendly parameter is given", () => {
it("should throw an error", () => {
it("throws an error", () => {
expect(() =>
RandomToken.gen({ seed: "12345abcdefGHIJK", friendly: true }),
).toThrow(/friendly/);
});
});
describe("when the mask parameter is given", () => {
it("should generate a masked token", () => {
it("generates a masked token", () => {
const seed = "12345abcdefGHIJK";
const mask = "123abcDEFGHIjk";
const token = RandomToken.gen({ seed, mask });
Expand All @@ -222,19 +229,19 @@ describe("RandomToken", () => {
});
describe("(casing)", () => {
describe("when no casing parameter is given", () => {
it("should generate a mixed-case token by default", () => {
it("generates a mixed-case token by default", () => {
const token = RandomToken.gen({});
expect(token).toMatch(/^[0-9A-Za-z]+$/);
});
});
describe('when a "null" casing parameter is given', () => {
it("should generate a mixed-case token", () => {
it("generates a mixed-case token", () => {
const token = RandomToken.gen({ casing: null });
expect(token).toMatch(/^[0-9A-Za-z]+$/);
});
});
describe('when the "upper" casing parameter is given', () => {
it("should generate an uppercase token", () => {
it("generates an uppercase token", () => {
const casingOptions: RandomToken.Casing[] = ["u", "upper"];
casingOptions.forEach((casing) => {
const token = RandomToken.gen({ casing });
Expand All @@ -243,7 +250,7 @@ describe("RandomToken", () => {
});
});
describe('when the "lower" casing parameter is given', () => {
it("should generate a lowercase token", () => {
it("generates a lowercase token", () => {
const casingOptions: RandomToken.Casing[] = ["l", "lower"];
casingOptions.forEach((casing) => {
const token = RandomToken.gen({ casing });
Expand All @@ -252,7 +259,7 @@ describe("RandomToken", () => {
});
});
describe('when the "mixed" casing parameter is given', () => {
it("should generate a mixed-case token", () => {
it("generates a mixed-case token", () => {
const casingOptions: RandomToken.Casing[] = ["m", "mixed"];
casingOptions.forEach((casing) => {
const token = RandomToken.gen({ casing });
Expand All @@ -263,7 +270,7 @@ describe("RandomToken", () => {
});
describe("(friendly)", () => {
describe("when the friendly parameter is given", () => {
it("should generate a friendly token", () => {
it("generates a friendly token", () => {
const token = RandomToken.gen({ friendly: true });
RandomToken.FRIENDLY_MASK.split("").forEach((singleMask) => {
expect(token.split("")).not.toContain(singleMask);
Expand All @@ -273,7 +280,7 @@ describe("RandomToken", () => {
});
describe("(mask)", () => {
describe("when the mask parameter is given", () => {
it("should generate a masked token", () => {
it("generates a masked token", () => {
const mask = "123abcDEFGHIjk";
const token = RandomToken.gen({ mask });
mask.split("").forEach((singleMask) => {
Expand All @@ -284,11 +291,18 @@ describe("RandomToken", () => {
});
});
describe(".genf", () => {
it("should apply the friendly option by default", () => {
const token = RandomToken.genf({});
it("applies the friendly option by default", () => {
const token = RandomToken.genf();
RandomToken.FRIENDLY_MASK.split("").forEach((singleMask) => {
expect(token.split("")).not.toContain(singleMask);
});
});
describe("when the seed parameter is given", () => {
it("throws an error", () => {
expect(() => RandomToken.genf({ seed: "12345abcdefGHIJK" })).toThrow(
/friendly/,
);
});
});
});
});

0 comments on commit be60918

Please sign in to comment.