-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d03090a
commit 5a6f239
Showing
4 changed files
with
31 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* nanoid 中导出 {@link https://github.com/ai/nanoid} | ||
*/ | ||
const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' | ||
|
||
/** | ||
* 生成简易 {@link https://github.com/ai/nanoid|nanoid},使用 nanoid 中导出的字母表 | ||
* | ||
* @param size | ||
* @param dict | ||
* @returns id | ||
* @example nid() // => 'nZZyknCBT5ra_gGE' | ||
*/ | ||
export function nid(size = 16, dict = urlAlphabet): string { | ||
let id = '' | ||
let i = size | ||
const len = dict.length | ||
while (i--) | ||
id += dict[(Math.random() * len) | 0] | ||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { nid } from '../src/id' | ||
|
||
describe('string', () => { | ||
describe('nid Function', () => { | ||
it('应该生成随机字符串', () => { | ||
expect(nid()).toHaveLength(16) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters