Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution to implement url-safe strings (#347) #514

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bin/nanoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ Options
-s, --size Generated ID size
-a, --alphabet Alphabet to use
-h, --help Show this help
-u, --url-safe Generate url-safe string

Examples
$ nanoid -s 15
S9sBF77U6sDB8Yg

$ nanoid --size 10 --alphabet abc
bcabababca)
$ nanoid --size 5 -u 1
bcabababca`)
process.exit()
}

let alphabet, size
let alphabet, size, url_safe
for (let i = 2; i < process.argv.length; i++) {
let arg = process.argv[i]
if (arg === '--size' || arg === '-s') {
Expand All @@ -41,13 +44,16 @@ for (let i = 2; i < process.argv.length; i++) {
} else if (arg === '--alphabet' || arg === '-a') {
alphabet = process.argv[i + 1]
i += 1
} else if (arg === '--url-safe' || arg === '-u') {
url_safe = process.argv[i + 1]
i += 1
} else {
error('Unknown argument ' + arg)
}
}

if (alphabet) {
let customNanoid = customAlphabet(alphabet, size)
let customNanoid = customAlphabet(alphabet, size, url_safe)
print(customNanoid())
} else {
print(nanoid(size))
Expand Down
15 changes: 14 additions & 1 deletion non-secure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
// `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
let urlAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
let urlSafeAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict'

export let customAlphabet = (alphabet, defaultSize = 21) => {
export let customAlphabet = (alphabet = urlAlphabet, defaultSize = 21,url_safe=0) => {
return (size = defaultSize) => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = size | 0
switch(u){
case 0:
while (i--) {
// `| 0` is more compact and faster than `Math.floor()`.
id += alphabet[(Math.random() * alphabet.length) | 0]
}
break;
case 1:
while (i--) {
// `| 0` is more compact and faster than `Math.floor()`.
id += alphabet[(Math.random() * alphabet.length) | 0]
}
id += urlSafeAlphabet[(Math.random() * urlSafeAlphabet.length) | 0];
}

return id
}
}
Expand Down
2 changes: 2 additions & 0 deletions url-alphabet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
// Same as in non-secure/index.js
export const urlAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
export const urlSafeAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict'