diff --git a/src/tools/index.ts b/src/tools/index.ts index 1bcd9317..d7884d26 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,5 +1,14 @@ +import { LockOpen } from '@vicons/tabler'; import type { ToolCategory } from './Tool'; -export const toolsByCategory: ToolCategory[] = []; +import { tool as tokenGenerator } from './token-generator'; + +export const toolsByCategory: ToolCategory[] = [ + { + name: 'Crypto', + icon: LockOpen, + components: [tokenGenerator], + }, +]; export const tools = toolsByCategory.flatMap(({ components }) => components); diff --git a/src/tools/token-generator/index.ts b/src/tools/token-generator/index.ts index b6196196..70a47e98 100644 --- a/src/tools/token-generator/index.ts +++ b/src/tools/token-generator/index.ts @@ -1,3 +1,4 @@ +import { ArrowsShuffle } from '@vicons/tabler'; import type { ITool } from './../Tool'; export const tool: ITool = { @@ -6,4 +7,5 @@ export const tool: ITool = { description: 'Generate random string with the chars you want: uppercase or lowercase letters, numbers and/or symbols.', keywords: ['token', 'random', 'string', 'alphanumeric', 'symbols', 'number', 'letters', 'lowercase', 'uppercase'], component: () => import('./token-generator.tool.vue'), + icon: ArrowsShuffle, }; diff --git a/src/tools/token-generator/token-generator.service.test.ts b/src/tools/token-generator/token-generator.service.test.ts new file mode 100644 index 00000000..ed9dab31 --- /dev/null +++ b/src/tools/token-generator/token-generator.service.test.ts @@ -0,0 +1,98 @@ +import { expect, describe, it } from 'vitest'; +import { createToken } from './token-generator.service'; + +describe('token-generator', () => { + describe('createToken', () => { + it('should generate an empty string when all params are false', () => { + const token = createToken({ + withLowercase: false, + withUppercase: false, + withNumbers: false, + withSymbols: false, + length: 10, + }); + + expect(token).toHaveLength(0); + }); + + it('should generate a random string with the specified length', () => { + const createTokenWithLength = (length: number) => + createToken({ + withLowercase: true, + withUppercase: true, + withNumbers: true, + withSymbols: true, + length, + }); + + expect(createTokenWithLength(5)).toHaveLength(5); + expect(createTokenWithLength(10)).toHaveLength(10); + expect(createTokenWithLength(100)).toHaveLength(100); + }); + + it('should generate a random string with just uppercase if only withUppercase is set', () => { + const token = createToken({ + withLowercase: false, + withUppercase: true, + withNumbers: false, + withSymbols: false, + length: 256, + }); + + expect(token).toHaveLength(256); + expect(token).toMatch(/^[A-Z]+$/); + }); + + it('should generate a random string with just lowercase if only withLowercase is set', () => { + const token = createToken({ + withLowercase: true, + withUppercase: false, + withNumbers: false, + withSymbols: false, + length: 256, + }); + + expect(token).toHaveLength(256); + expect(token).toMatch(/^[a-z]+$/); + }); + + it('should generate a random string with just numbers if only withNumbers is set', () => { + const token = createToken({ + withLowercase: false, + withUppercase: false, + withNumbers: true, + withSymbols: false, + length: 256, + }); + + expect(token).toHaveLength(256); + expect(token).toMatch(/^[0-9]+$/); + }); + + it('should generate a random string with just symbols if only withSymbols is set', () => { + const token = createToken({ + withLowercase: false, + withUppercase: false, + withNumbers: false, + withSymbols: true, + length: 256, + }); + + expect(token).toHaveLength(256); + expect(token).toMatch(/^[.,;:!?./\-"'#{([-|\\@)\]=}*+]+$/); + }); + + it('should generate a random string with just letters (case incensitive) with withLowercase and withUppercase', () => { + const token = createToken({ + withLowercase: true, + withUppercase: true, + withNumbers: false, + withSymbols: false, + length: 256, + }); + + expect(token).toHaveLength(256); + expect(token).toMatch(/^[a-zA-Z]+$/); + }); + }); +}); diff --git a/src/tools/token-generator/token-generator.service.ts b/src/tools/token-generator/token-generator.service.ts new file mode 100644 index 00000000..bf6d9ac3 --- /dev/null +++ b/src/tools/token-generator/token-generator.service.ts @@ -0,0 +1,24 @@ +import { shuffleString } from '@/utils/random'; + +export function createToken({ + withUppercase = true, + withLowercase = true, + withNumbers = true, + withSymbols = false, + length = 64, +}: { + withUppercase?: boolean; + withLowercase?: boolean; + withNumbers?: boolean; + withSymbols?: boolean; + length?: number; +}) { + const alphabet = [ + ...(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : ''), + ...(withLowercase ? 'abcdefghijklmopqrstuvwxyz' : ''), + ...(withNumbers ? '0123456789' : ''), + ...(withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : ''), + ].join(''); + + return shuffleString(alphabet.repeat(length)).substring(0, length); +} diff --git a/src/tools/token-generator/token-generator.tool.vue b/src/tools/token-generator/token-generator.tool.vue index 0d27eeec..48812de3 100644 --- a/src/tools/token-generator/token-generator.tool.vue +++ b/src/tools/token-generator/token-generator.tool.vue @@ -1,71 +1,81 @@