From 34d6aa421f2e92a0a4ca8c0990bcee2e0eb41ab4 Mon Sep 17 00:00:00 2001 From: Milon Kremer Date: Wed, 19 Apr 2023 23:59:25 +0200 Subject: [PATCH] fixed dictionaries path --- .../adjectives.json | 0 .../nouns/animals.json | 0 .../nouns/household.json | 0 dist/index.d.ts | 3 ++- dist/index.js | 26 +++++++++++-------- package.json | 2 +- src/index.ts | 6 ++--- 7 files changed, 21 insertions(+), 16 deletions(-) rename {src/dictionaries => dictionaries}/adjectives.json (100%) rename {src/dictionaries => dictionaries}/nouns/animals.json (100%) rename {src/dictionaries => dictionaries}/nouns/household.json (100%) diff --git a/src/dictionaries/adjectives.json b/dictionaries/adjectives.json similarity index 100% rename from src/dictionaries/adjectives.json rename to dictionaries/adjectives.json diff --git a/src/dictionaries/nouns/animals.json b/dictionaries/nouns/animals.json similarity index 100% rename from src/dictionaries/nouns/animals.json rename to dictionaries/nouns/animals.json diff --git a/src/dictionaries/nouns/household.json b/dictionaries/nouns/household.json similarity index 100% rename from src/dictionaries/nouns/household.json rename to dictionaries/nouns/household.json diff --git a/dist/index.d.ts b/dist/index.d.ts index b2b97a2..ed62837 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,7 @@ type Options = { + maxLength?: number; capitalize?: boolean; - seperator?: string; + separator?: string; numberLength?: number; }; export declare function generateUsername(options?: Options): string; diff --git a/dist/index.js b/dist/index.js index 0594136..f29af99 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,10 +1,10 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateUsername = void 0; -const adjectives = require('./dictionaries/adjectives.json'); +const adjectives = require('../dictionaries/adjectives.json'); const nouns = [ - ...require('./dictionaries/nouns/animals.json'), - ...require('./dictionaries/nouns/household.json') + ...require('../dictionaries/nouns/animals.json'), + ...require('../dictionaries/nouns/household.json') ]; function generateRandomNumber(length) { const min = parseInt('1' + (length > 0 ? '0'.repeat(length - 1) : '')); @@ -15,15 +15,19 @@ function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } function generateUsername(options) { - var _a; + var _a, _b; const dictionaries = [adjectives, nouns]; - const parts = dictionaries.map(dy => { - const word = dy[Math.floor((Math.random() * dy.length))]; - return (options === null || options === void 0 ? void 0 : options.capitalize) ? capitalize(word) : word; - }); - let username = parts.join((_a = options === null || options === void 0 ? void 0 : options.seperator) !== null && _a !== void 0 ? _a : ''); - if (options === null || options === void 0 ? void 0 : options.numberLength) - username += generateRandomNumber(options.numberLength).toString(); + const maxLength = (_a = options === null || options === void 0 ? void 0 : options.maxLength) !== null && _a !== void 0 ? _a : 32; + let username = undefined; + while (!username || (username === null || username === void 0 ? void 0 : username.length) > maxLength) { + const parts = dictionaries.map(dy => { + const word = dy[Math.floor((Math.random() * dy.length))]; + return (options === null || options === void 0 ? void 0 : options.capitalize) ? capitalize(word) : word; + }); + username = parts.join((_b = options === null || options === void 0 ? void 0 : options.separator) !== null && _b !== void 0 ? _b : ''); + if (options === null || options === void 0 ? void 0 : options.numberLength) + username += generateRandomNumber(options.numberLength).toString(); + } return username; } exports.generateUsername = generateUsername; diff --git a/package.json b/package.json index 3705039..ab9c04f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gen-username", "description": "Random Unique Username Generator", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 1d13baf..3b33629 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,11 +5,11 @@ type Options = { numberLength?: number } -const adjectives = require('./dictionaries/adjectives.json') +const adjectives = require('../dictionaries/adjectives.json') const nouns = [ - ...require('./dictionaries/nouns/animals.json'), - ...require('./dictionaries/nouns/household.json') + ...require('../dictionaries/nouns/animals.json'), + ...require('../dictionaries/nouns/household.json') ] function generateRandomNumber(length: number) {