Skip to content

Commit

Permalink
feat: (*) Add '--count' flag for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
dperuo committed Jan 11, 2022
1 parent 1062cd2 commit 4f65227
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ deno run https://deno.land/x/sweetid/cli.ts --xlong || -x
# NsiHnUqK3cbADQ9cIzsi0Og0
```

## Generate Multiple IDs

Use the `--count <NUMBER>` (`-c <NUMBER>`) flag on the command line to generate
multiple IDs of the same length.

```zsh
deno run https://deno.land/x/sweetid/cli.ts --count 3 || -c 3
# hrWYHA
# GlTos0
# YRH3Xe

deno run https://deno.land/x/sweetid/cli.ts --medium --count 3 || -m -c 3
# C71kkaC0BRSn
# CkBfX6pEjPDL
# siQRcoFa8fTz
```

## TypeScript Types

Use [`SweetId`](src/const.ts) and [`SweetIdSize`](src/const.ts) to add type info
Expand Down
5 changes: 4 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import { getSizeString } from "./src/service.ts";
const args = parse(Deno.args, options);

const size = getSizeString(args);
const { count } = args;

sweetid(size);
for (let i = count; i > 0; i--) {
console.log(sweetid(size));
}
6 changes: 6 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const enum CharSets {
}

export interface Sizes extends Args {
count: number;
short: boolean;
medium: boolean;
long: boolean;
Expand All @@ -29,9 +30,14 @@ export interface Sizes extends Args {

export const options: ArgParsingOptions = {
alias: {
count: "c",
short: "s",
medium: "m",
long: "l",
xlong: "x",
},

default: {
count: 1,
},
};
5 changes: 1 addition & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { generate, getIdLength, isValid } from "./service.ts";
export function sweetid(size: SweetIdSize = "short"): SweetId {
const id = generate(getIdLength(size));

if (isValid(id)) {
console.log(id);
return id;
}
if (isValid(id)) return id;

return sweetid(size);
}

0 comments on commit 4f65227

Please sign in to comment.