@logto/shared
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -1,16 +0,19 @@ | ||
import { customAlphabet } from 'nanoid'; | ||
type BuildIdGenerator = { | ||
/** | ||
* Build a nanoid generator function uses numbers (0-9), lowercase letters (a-z), and uppercase letters (A-Z) as the alphabet. | ||
* @param size The default id length for the generator. | ||
*/ | ||
(size: number): ReturnType<typeof customAlphabet>; | ||
/** | ||
* Build a nanoid generator function uses numbers (0-9) and lowercase letters (a-z) as the alphabet. | ||
* @param size The default id length for the generator. | ||
*/ | ||
(size: number, includingUppercase: false): ReturnType<typeof customAlphabet>; | ||
}; | ||
export declare const buildIdGenerator: BuildIdGenerator; | ||
/** | ||
* Generate a standard id with 21 characters, including lowercase letters and numbers. | ||
* | ||
* @see {@link lowercaseAlphabet} | ||
*/ | ||
export declare const generateStandardId: (size?: number | undefined) => string; | ||
export {}; | ||
/** | ||
* Generate a standard short id with 12 characters, including lowercase letters and numbers. | ||
* | ||
* @see {@link lowercaseAlphabet} | ||
*/ | ||
export declare const generateStandardShortId: (size?: number | undefined) => string; | ||
/** | ||
* Generate a standard secret with 32 characters, including uppercase letters, lowercase | ||
* letters, and numbers. | ||
* | ||
* @see {@link alphabet} | ||
*/ | ||
export declare const generateStandardSecret: (size?: number | undefined) => string; |
import { customAlphabet } from 'nanoid'; | ||
const lowercaseAlphabet = '0123456789abcdefghijklmnopqrstuvwxyz'; | ||
const alphabet = `${lowercaseAlphabet}ABCDEFGHIJKLMNOPQRSTUVWXYZ`; | ||
export const buildIdGenerator = (size, includingUppercase = false) => customAlphabet(includingUppercase ? alphabet : lowercaseAlphabet, size); | ||
export const generateStandardId = buildIdGenerator(21); | ||
const buildIdGenerator = (size, includingUppercase = true) => customAlphabet(includingUppercase ? alphabet : lowercaseAlphabet, size); | ||
/** | ||
* Generate a standard id with 21 characters, including lowercase letters and numbers. | ||
* | ||
* @see {@link lowercaseAlphabet} | ||
*/ | ||
export const generateStandardId = buildIdGenerator(21, false); | ||
/** | ||
* Generate a standard short id with 12 characters, including lowercase letters and numbers. | ||
* | ||
* @see {@link lowercaseAlphabet} | ||
*/ | ||
export const generateStandardShortId = buildIdGenerator(12, false); | ||
/** | ||
* Generate a standard secret with 32 characters, including uppercase letters, lowercase | ||
* letters, and numbers. | ||
* | ||
* @see {@link alphabet} | ||
*/ | ||
export const generateStandardSecret = buildIdGenerator(32); |
@@ -1,12 +0,25 @@ | ||
import { buildIdGenerator } from './id.js'; | ||
describe('id generator', () => { | ||
import { generateStandardId, generateStandardSecret, generateStandardShortId } from './id.js'; | ||
describe('standard id generator', () => { | ||
it('should match the input length', () => { | ||
const id = buildIdGenerator(10)(); | ||
expect(id.length).toEqual(10); | ||
const id = generateStandardId(); | ||
expect(id.length).toEqual(21); | ||
}); | ||
it('to random id should not equal', () => { | ||
const id_1 = buildIdGenerator(10)(); | ||
const id_2 = buildIdGenerator(10)(); | ||
expect(id_1).not.toEqual(id_2); | ||
}); | ||
describe('standard short id generator', () => { | ||
it('should match the input length', () => { | ||
const id = generateStandardShortId(); | ||
expect(id.length).toEqual(12); | ||
}); | ||
}); | ||
describe('standard secret generator', () => { | ||
it('should match the input length', () => { | ||
const id = generateStandardSecret(); | ||
expect(id.length).toEqual(32); | ||
}); | ||
it('should generate id with uppercase', () => { | ||
// If it can't generate uppercase, it will timeout | ||
while (!/[A-Z]/.test(generateStandardSecret())) { | ||
// Do nothing | ||
} | ||
}); | ||
}); |
{ | ||
"name": "@logto/shared", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "author": "Silverhand Inc. <contact@silverhand.io>", |
62527
972