generate-random-username
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -17,10 +17,2 @@ export type Options = { | ||
/** | ||
The maximum length of a username to generate. | ||
For lengths under 10, it may not always find a username under that length, and will fallback to returning a longer username. | ||
@default null | ||
*/ | ||
readonly maxLength?: number; | ||
/** | ||
Whether to capitalize each word used in the username. | ||
@@ -36,3 +28,3 @@ | ||
Options exist for a custom separator between the username, limiting username to a maximum length, adding random digits, and capitalizing the words in the username. | ||
Options exist for a custom separator between the username, adding random digits, and capitalizing the words in the username. | ||
@param options - options to customize the username | ||
@@ -54,5 +46,2 @@ @returns a randomly generated username | ||
generateRandomUsername({ maxLength: 10 }); | ||
//=> 'clear-eel' | ||
generateRandomUsername({ digits: 3 }); | ||
@@ -59,0 +48,0 @@ //=> 'unaffected-cat-184' |
20
index.js
import adjectives from './adjectives.js'; | ||
import animals from './animals.js'; | ||
// Max retries for generating username if usernames are above maximum length | ||
const MAX_RETRIES = 1000; | ||
function getRandomFromArray(arr) { | ||
@@ -24,3 +21,3 @@ const randomIndex = Math.floor(Math.random() * arr.length); | ||
export default function generateRandomUsername({ separator = '-', digits = 0, maxLength = null, capitalize = false } = {}) { | ||
export default function generateRandomUsername({ separator = '-', digits = 0, capitalize = false } = {}) { | ||
if (typeof separator !== 'string') { | ||
@@ -34,17 +31,2 @@ separator = ''; | ||
if (typeof maxLength !== 'number') { | ||
maxLength = null; | ||
} | ||
if (maxLength !== null) { | ||
for (let i = 0; i < MAX_RETRIES; i++) { | ||
const username = generateUsername({ separator, digits, capitalize }); | ||
if (username.length <= maxLength) return username; | ||
} | ||
} | ||
return generateUsername({ separator, digits, capitalize }); | ||
} | ||
function generateUsername({ separator = '-', digits = 0, capitalize = false } = {}) { | ||
let first = getRandomFromArray(adjectives); | ||
@@ -51,0 +33,0 @@ let second = getRandomFromArray(animals); |
{ | ||
"name": "generate-random-username", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "A tiny module to generate a random username from a list of adjectives and animals.", | ||
@@ -5,0 +5,0 @@ "license": "CC0-1.0", |
@@ -7,3 +7,3 @@ # generate-random-username | ||
Options exist for a custom separator between the username, limiting username to a maximum length, adding random digits, and capitalizing the words in the username. | ||
Options exist for a custom separator between the username, adding random digits, and capitalizing the words in the username. | ||
@@ -30,5 +30,2 @@ ## Install | ||
generateRandomUsername({ maxLength: 10 }); | ||
//=> 'clear-eel' | ||
generateRandomUsername({ digits: 3, capitalize: true }); | ||
@@ -66,9 +63,1 @@ //=> 'Unaffected-Cat-184' | ||
Number of random digits to append to end of username. | ||
##### maxLength | ||
Type: `number`\ | ||
Default: `null` | ||
The maximum length of a username to generate. | ||
For lengths under 10, it may not always find a username under that length, and will fallback to returning a longer username. |
26443
1156
61