1.0.2 • Published 2 months ago

generate-unique-string v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Generate Unique String

generate-unique-string generates a random string using english words and numbers. The code is developed based on the random-words package.

Installation

npm install generate-unique-string

Methods

APIDescription
generate()generates a random string

Options

APIDefault ValueTypeDescription
minLengthnullNumberminimum length of the random word
maxLengthnullNumbermaximum length of the random word
minNumberLengthnullNumberminimum length of the random number
maxNumberLength0Numbermaximum length of the random number
minnullNumberminimum number of strings to generate
maxnullNumbermaximum number of strings to generate
exactlynullNumberexact number of strings to generate
joinnullStringstring that is used for joining the generated strings
wordsPerString1Numberthe number of words per a generated string
separator" " (space)Stringthe separator that goes between the words in the generated string
seednullStringseed that is used for initializing the generator
formatternullFunctiona function that formats the generated string

Examples

First of all, import the generate method using the following syntax:

import { generate } from "generate-unique-string";

If you want a string with a word that is 5 letters long followed by 3 numbers, you can do the following:

generate({
  minLength: 5,
  maxLength: 5,
  minNumberLength: 3,
  maxNumberLength: 3,
});
// example result: 'ocean799'

If you want 3 strings and want each of them to be longer than 7 letters, you can do the following:

generate({ minLength: 7, exactly: 3 });
// example result: ['customer', 'licence', 'solution']

If you want 2 to 5 strings and want each of them to be composed of 3 words, you can do the following:

generate({ min: 2, max: 5, wordsPerString: 3 });
// example result: ['cleaners combining wheat', 'hispanic ruled for', 'illinois adam legislative']

The seed will help yield exactly the same results multiple times when the same seed was used and the other inputs are identical

console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
//output: ['plenty', 'pure']

console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
//output: ['plenty', 'pure']

Other examples are as follows:

console.log(generate());
//output: 'army'

console.log(generate(5));
//output: ['army', 'beautiful', 'became', 'if', 'actually']

console.log(generate({ min: 3, max: 10 }));
//output: ['became', 'arrow', 'article', 'therefore']

console.log(generate({ exactly: 2 }));
//output: ['beside', 'between']

console.log(generate({ exactly: 5, join: " " }));
//output: 'army beautiful became if exactly'

console.log(generate({ exactly: 5, join: "" }));
//output: 'armybeautifulbecameifexactly'

console.log(generate({ exactly: 2, minLength: 4 }));
//output: ['atom', 'window']

console.log(generate({ exactly: 5, maxLength: 4 }));
//output: ['army', 'come', 'eye', 'five', 'fur']

console.log(generate({ exactly: 3, minLength: 5, maxLength: 100000 }));
//output: ['understanding', 'should', 'yourself']

console.log(generate({ exactly: 5, wordsPerString: 2 }));
//output: [ 'salt practical', 'also brief', 'country muscle', 'neighborhood beyond', 'grew pig' ]

console.log(generate({ exactly: 5, wordsPerString: 2, separator: "-" }));
//output: [ 'equator-variety', 'salt-usually', 'importance-becoming', 'stream-several', 'goes-fight' ]

console.log(
  generate({
    exactly: 5,
    wordsPerString: 2,
    formatter: (word) => word.toUpperCase(),
  })
);
//output: [ 'HAVING LOAD', 'LOST PINE', 'GAME SLOPE', 'SECRET GIANT', 'INDEED LOCATION' ]
1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago