Skip to content

Commit d33d5fb

Browse files
authored
Merge pull request #9 from ianfabs/fix-cli
Resolve issues by cleaning up cli code
2 parents 598a57b + ee7e35f commit d33d5fb

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

cli.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44
*/
55
import nanoid from "./mod.ts";
66
import customAlphabet from "./customAlphabet.ts"
7-
import url from "./url.ts";
87
import { parse } from "https://deno.land/std/flags/mod.ts";
98
// import { red, bold } from "https://deno.land/std/fmt/colors.ts";
109

1110
// Grab CLI arguments
1211
const flags = parse(Deno.args);
13-
14-
// Logging functions
15-
const log = async (message: string, type: Deno.File = Deno.stdin) => {
16-
const encoder = new TextEncoder();
17-
const buf = new Deno.Buffer(encoder.encode(message+"\n"));
18-
Deno.copy(Deno.stdout, buf);
19-
};
2012
// const error = (message: string) => log(bold("Error: ") + message, Deno.stderr);
21-
13+
console.log(`nanoid v3.0.0\n------------------`);
2214
// Display help message, if requested
2315
if (flags.h || flags.help) {
24-
log(`
16+
console.log(`original by Andrey Sitnik
17+
port for deno by Ian Fabs
18+
2519
Usage
2620
$ nanoid
2721
Options
@@ -40,12 +34,16 @@ if (flags.h || flags.help) {
4034
`);
4135
} else {
4236
// Check if the alphabet supplied is the same as the default one
43-
const equivalent = (a: string, b: string): boolean => JSON.stringify(a.split('').sort()) === JSON.stringify(b.split('').sort());
4437
const size = flags.s ?? flags.size;
4538
const alphabet = flags.a ?? flags.alphabet;
46-
const id = alphabet && !equivalent(alphabet, url) ? customAlphabet(alphabet, size ?? 12) : nanoid(size);
4739

48-
log(id);
40+
let id;
41+
if (alphabet)
42+
id = customAlphabet(alphabet, size)();
43+
else
44+
id = nanoid(size);
45+
46+
console.log(`\n${id}\n`);
4947
}
5048

5149
Deno.stdin.close();

0 commit comments

Comments
 (0)