nanoid 3.1.32 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nanoid.cjs +55 -2
- package/package.json +1 -1
package/bin/nanoid.cjs
CHANGED
@@ -1,5 +1,58 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
|
3
|
-
let { nanoid } = require('..')
|
3
|
+
let { nanoid, customAlphabet } = require('..')
|
4
4
|
|
5
|
-
|
5
|
+
function print(msg) {
|
6
|
+
process.stdout.write(msg + '\n')
|
7
|
+
}
|
8
|
+
|
9
|
+
function error(msg) {
|
10
|
+
process.stderr.write(msg + '\n')
|
11
|
+
process.exit(1)
|
12
|
+
}
|
13
|
+
|
14
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
15
|
+
print(`
|
16
|
+
Usage
|
17
|
+
$ nanoid [options]
|
18
|
+
|
19
|
+
Options
|
20
|
+
-s, --size Generated ID size
|
21
|
+
-a, --alphabet Alphabet to use
|
22
|
+
-h, --help Show this help
|
23
|
+
|
24
|
+
Examples
|
25
|
+
$ nano --s 15
|
26
|
+
S9sBF77U6sDB8Yg
|
27
|
+
|
28
|
+
$ nano --size 10 --alphabet abc
|
29
|
+
bcabababca`)
|
30
|
+
process.exit()
|
31
|
+
}
|
32
|
+
|
33
|
+
let alphabet, size
|
34
|
+
for (let i = 2; i < process.argv.length; i++) {
|
35
|
+
let arg = process.argv[i]
|
36
|
+
if (arg === '--size' || arg === '-s') {
|
37
|
+
size = Number(process.argv[i + 1])
|
38
|
+
i += 1
|
39
|
+
if (Number.isNaN(size) || size <= 0) {
|
40
|
+
error('Size must be positive integer')
|
41
|
+
}
|
42
|
+
} else if (arg === '--alphabet' || arg === '-a') {
|
43
|
+
alphabet = process.argv[i + 1]
|
44
|
+
i += 1
|
45
|
+
} else {
|
46
|
+
error('Unknown argument ' + arg)
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
if (alphabet) {
|
51
|
+
if (typeof size === 'undefined') {
|
52
|
+
error('You must also specify size option, when using custom alphabet')
|
53
|
+
}
|
54
|
+
let customNanoid = customAlphabet(alphabet, size)
|
55
|
+
print(customNanoid())
|
56
|
+
} else {
|
57
|
+
print(nanoid(size))
|
58
|
+
}
|