Skip to content

Commit

Permalink
feat: random str
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 10, 2022
1 parent 560a0ee commit af5e12d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ export function template(str: string, ...args: any[]): string {
return args[index]
})
}

// port from nanoid
// https://github.com/ai/nanoid
const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
/**
* Generate a random string
* @category String
*/
export function randomStr(size = 16, dict = urlAlphabet) {
let id = ''
let i = size
const len = dict.length
while (i--)
id += dict[(Math.random() * len) | 0]
return id
}

0 comments on commit af5e12d

Please sign in to comment.