Skip to content

Commit

Permalink
feat: stringIdAsync, stringIdUnsafe, export nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Sep 24, 2019
1 parent 36090ba commit 7e66202
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/dotenv": "^6.1.1",
"@types/hapi__joi": "^15.0.1",
"@types/lru-cache": "^5.1.0",
"@types/nanoid": "^2.0.0",
"debug": "^4.1.1",
"dotenv": "^8.1.0",
"lru-cache": "^5.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
ALPHABET_LOWERCASE,
ALPHABET_NUMBER,
ALPHABET_UPPERCASE,
nanoid,
stringId,
stringIdAsync,
stringIdUnsafe,
} from './security/id.util'
import {
getSecretMap,
Expand Down Expand Up @@ -124,6 +127,9 @@ export {
requireEnvKeys,
LRUMemoCache,
stringId,
stringIdAsync,
stringIdUnsafe,
nanoid,
ALPHABET_NUMBER,
ALPHABET_LOWERCASE,
ALPHABET_UPPERCASE,
Expand Down
20 changes: 19 additions & 1 deletion src/security/id.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringId } from '../index'
import { stringId, stringIdAsync, stringIdUnsafe } from '../index'

test('stringId', () => {
const id = stringId()
Expand All @@ -8,3 +8,21 @@ test('stringId', () => {

expect(stringId(32).length).toBe(32)
})

test('stringIdUnsafe', () => {
const id = stringIdUnsafe()
expect(id).not.toBeUndefined()
expect(id.length).toBe(16)
expect(id.toLowerCase()).toBe(id)

expect(stringId(32).length).toBe(32)
})

test('stringIdAsync ', async () => {
const id = await stringIdAsync()
expect(id).not.toBeUndefined()
expect(id.length).toBe(16)
expect(id.toLowerCase()).toBe(id)

expect(stringId(32).length).toBe(32)
})
20 changes: 17 additions & 3 deletions src/security/id.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type NanoidGenerate = (alphabet: string, length?: number) => string

const nanoidGenerate = require('nanoid/generate') as NanoidGenerate
import nanoidAsyncGenerate = require('nanoid/async/generate')
import nanoidGenerate = require('nanoid/generate')
import nanoidNonSecureGenerate = require('nanoid/non-secure/generate')

export const ALPHABET_NUMBER = '0123456789'
export const ALPHABET_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
Expand All @@ -18,3 +18,17 @@ export const ALPHABET_ALPHANUMERIC = [ALPHABET_NUMBER, ALPHABET_LOWERCASE, ALPHA
export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
return nanoidGenerate(alphabet, length)
}

export async function stringIdAsync(
length = 16,
alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE,
): Promise<string> {
return await nanoidAsyncGenerate(alphabet, length)
}

export function stringIdUnsafe(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
return nanoidNonSecureGenerate(alphabet, length)
}

// re-export nanoid
export const nanoid = require('nanoid')
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,13 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/nanoid@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/nanoid/-/nanoid-2.0.0.tgz#b59002c475e6dfcc26e67ba563ff61b512e5ebf8"
integrity sha512-NtwPHfAyU3IDXdKAB2OMPpAauHBg9gUjpOYr3FAzI84D70nWdS8k5mryteLvT/s1ACeAFAkGg132/XJVN4qx/w==
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@^12.0.0", "@types/node@^12.0.2":
version "12.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.5.tgz#e19436e7f8e9b4601005d73673b6dc4784ffcc2f"
Expand Down

0 comments on commit 7e66202

Please sign in to comment.