Skip to content

Commit

Permalink
feat: (*) Add 'xshort' ID length
Browse files Browse the repository at this point in the history
  • Loading branch information
dperuo committed Mar 22, 2024
1 parent b20c794 commit ba51197
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
23 changes: 12 additions & 11 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ export type SweetId = string & {
export const BLOCK_SIZE = 6;

export const BlockCount = {
short: 1,
s: 1,
medium: 2,
m: 2,
long: 4,
l: 4,
xlong: 8,
xl: 8,
xshort: 1,
xs: 1,
short: 2,
s: 2,
medium: 4,
m: 4,
long: 8,
l: 8,
xlong: 16,
xl: 16,
} as const;

export type SweetIdSize = keyof typeof BlockCount;

export const CharSets = {
Alphanumeric:
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
Alphanumeric: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
Decimal: "0123456789",
} as const;

Expand All @@ -35,6 +36,6 @@ export const options: ParseOptions = {

default: {
count: 1,
size: "short",
size: "xshort",
},
};
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SweetId, SweetIdSize } from "./const.ts";
import { generate, getIdLength, validate } from "./utils.ts";

export function sweetid(size: SweetIdSize = "short"): SweetId {
export function sweetid(size: SweetIdSize = "xs"): SweetId {
const id = generate(getIdLength(size));

return validate(id) ? id : sweetid(size);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { customAlphabet } from "../deps.ts";
import { BLOCK_SIZE, BlockCount, CharSets, SweetId, SweetIdSize } from "./const.ts";
import { BLOCK_SIZE, BlockCount, CharSets, type SweetId, type SweetIdSize } from "./const.ts";

export function generate(length: number): SweetId {
return customAlphabet(CharSets.Alphanumeric, length)();
Expand All @@ -10,5 +10,5 @@ export function validate(id: SweetId): boolean {
}

export function getIdLength(size: SweetIdSize): number {
return BlockCount[size] ? BLOCK_SIZE * BlockCount[size] : BLOCK_SIZE;
return (BlockCount[size] ?? 1) * BLOCK_SIZE;
}
20 changes: 11 additions & 9 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { assertStrictEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { sweetid, SweetIdSize } from "./mod.ts";
import { sweetid, type SweetIdSize } from "./mod.ts";

const tests = [
{ type: "short", size: 6 },
{ type: "s", size: 6 },
{ type: "medium", size: 12 },
{ type: "m", size: 12 },
{ type: "long", size: 24 },
{ type: "l", size: 24 },
{ type: "xlong", size: 48 },
{ type: "xl", size: 48 },
{ type: "xshort", size: 6 },
{ type: "xs", size: 6 },
{ type: "short", size: 12 },
{ type: "s", size: 12 },
{ type: "medium", size: 24 },
{ type: "m", size: 24 },
{ type: "long", size: 48 },
{ type: "l", size: 48 },
{ type: "xlong", size: 96 },
{ type: "xl", size: 96 },
] as const satisfies ReadonlyArray<{ type: SweetIdSize; size: number }>;

tests.forEach(({ type, size }) => {
Expand Down

0 comments on commit ba51197

Please sign in to comment.