Skip to content

Commit

Permalink
feat(x): 新增 nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 11, 2021
1 parent f0cc9ba commit c601213
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"ioredis": "^4.19.4",
"lodash-uni": "^1.1.0",
"miniprogram-api-typings": "^3.2.0",
"nanoid": "^3.1.20",
"react-use": "^15.3.4",
"tough-cookie": "^4.0.0",
"tough-cookie-redisstore": "^0.0.4",
Expand Down
5 changes: 3 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './createXml'
export * from './cuid'
export * from './nanoid'
export * from './parseXml'
export * from './RedisCookieJar'
export * from './sha1'
Expand Down
12 changes: 12 additions & 0 deletions src/x/nanoid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { nanoid } from './nanoid'

describe('nanoid', () => {
test('默认长度是21', () => {
expect(nanoid().length).toBe(21)
})

test('可自定义长度', () => {
expect(nanoid(10).length).toBe(10)
expect(nanoid(5).length).toBe(5)
})
})
14 changes: 14 additions & 0 deletions src/x/nanoid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { nanoid as _nanoid } from 'nanoid'

/**
* 生成一个 [nanoid](https://github.com/ai/nanoid/)。
*
* @param size 生成的 ID 长度,默认 21
* @example
* ```typescript
* nanoid() // => "Uakgb_J5m9g-0JDMbcJqL"
* ```
*/
export function nanoid(size?: number): string {
return _nanoid(size)
}

0 comments on commit c601213

Please sign in to comment.