main
Last change
on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago |
Pred finalna verzija
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[0c6b92a] | 1 | // This alphabet uses `A-Za-z0-9_-` symbols.
|
---|
| 2 | // The order of characters is optimized for better gzip and brotli compression.
|
---|
| 3 | // References to the same file (works both for gzip and brotli):
|
---|
| 4 | // `'use`, `andom`, and `rict'`
|
---|
| 5 | // References to the brotli default dictionary:
|
---|
| 6 | // `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
|
---|
[d565449] | 7 | let urlAlphabet =
|
---|
| 8 | 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
|
---|
[0c6b92a] | 9 |
|
---|
[d565449] | 10 | let customAlphabet = (alphabet, defaultSize = 21) => {
|
---|
| 11 | return (size = defaultSize) => {
|
---|
| 12 | let id = ''
|
---|
[0c6b92a] | 13 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
| 14 | let i = size | 0
|
---|
[d565449] | 15 | while (i--) {
|
---|
[0c6b92a] | 16 | // `| 0` is more compact and faster than `Math.floor()`.
|
---|
[d565449] | 17 | id += alphabet[(Math.random() * alphabet.length) | 0]
|
---|
| 18 | }
|
---|
| 19 | return id
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
[0c6b92a] | 22 |
|
---|
[d565449] | 23 | let nanoid = (size = 21) => {
|
---|
| 24 | let id = ''
|
---|
[0c6b92a] | 25 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
| 26 | let i = size | 0
|
---|
[d565449] | 27 | while (i--) {
|
---|
[0c6b92a] | 28 | // `| 0` is more compact and faster than `Math.floor()`.
|
---|
[d565449] | 29 | id += urlAlphabet[(Math.random() * 64) | 0]
|
---|
| 30 | }
|
---|
| 31 | return id
|
---|
| 32 | }
|
---|
[0c6b92a] | 33 |
|
---|
[d565449] | 34 | module.exports = { nanoid, customAlphabet }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.