Skip to content

Commit

Permalink
feat(x): 新增 cuid2
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 8, 2023
1 parent 8bcca66 commit c71997a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
},
"dependencies": {
"@babel/runtime": "^7.12.18",
"@paralleldrive/cuid2": "^2.2.2",
"@types/benchmark": "^2.1.0",
"@types/ioredis": "^4.19.4",
"@types/tough-cookie": "^4.0.0",
Expand Down
13 changes: 13 additions & 0 deletions src/x/cuid2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @jest-environment node
*/
import { cuid2 } from './cuid2'

describe('cuid2', () => {
test('ok', () => {
expect(cuid2().length).toBe(24)
expect(cuid2(10).length).toBe(10)
expect(cuid2(5).length).toBe(5)
expect(cuid2(30).length).toBe(30)
})
})
17 changes: 17 additions & 0 deletions src/x/cuid2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { init } from '@paralleldrive/cuid2'
import { memoize } from '../utils'

const createIdFactory = memoize(
(length: number) => init({ length }),
length => length,
)

/**
* 生成 Cuid2。
*
* @param length 长度,默认: 24
* @see https://github.com/paralleldrive/cuid2
*/
export function cuid2(length?: number): string {
return createIdFactory(length || 24)()
}
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 './cuid2'
export * from './nanoid'
export * from './parseXml'
export * from './RedisCookieJar'
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,11 @@
resolved "https://registry.npmmirror.com/@microsoft/tsdoc/-/tsdoc-0.14.2.tgz#c3ec604a0b54b9a9b87e9735dfc59e1a5da6a5fb"
integrity sha512-9b8mPpKrfeGRuhFH5iO1iwCLeIIsV6+H1sRfxbkoGXIyQE2BTsPd9zqSqQJ+pv5sJ/hT5M1zvOFL02MnEezFug==

"@noble/hashes@^1.1.5":
version "1.3.2"
resolved "https://registry.npmmirror.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.npmmirror.com/@nodelib/fs.scandir/download/@nodelib/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand All @@ -1381,6 +1386,13 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@paralleldrive/cuid2@^2.2.2":
version "2.2.2"
resolved "https://registry.npmmirror.com/@paralleldrive/cuid2/-/cuid2-2.2.2.tgz#7f91364d53b89e2c9cb9e02e8dd0f129e834455f"
integrity sha512-ZOBkgDwEdoYVlSeRbYYXs0S9MejQofiVYoTbKzy/6GQa39/q5tQU2IX46+shYnUkpEl3wc+J6wRlar7r2EK2xA==
dependencies:
"@noble/hashes" "^1.1.5"

"@rollup/[email protected]":
version "5.3.0"
resolved "https://registry.npmmirror.com/@rollup/plugin-babel/download/@rollup/plugin-babel-5.3.0.tgz#9cb1c5146ddd6a4968ad96f209c50c62f92f9879"
Expand Down

0 comments on commit c71997a

Please sign in to comment.