random-words
Generate one or more common English words
random-words
generates random words for use as sample text. We use it to generate random blog posts when testing Apostrophe.
Cryptographic-quality randomness is NOT the goal, as speed matters for generating sample text and security does not. As such, Math.random()
is used in most cases.
The seed
option can be used with the generate
function for situations that require deterministic output. When given the same seed
with the same input, generate()
will yield deterministic results, in regards to both actual word selection and the number of words returned (when using min
and max
). The underlying implementation of this option utilizes the seedrandom package as a replacement for Math.random()
.
The count
function can be used to calculate the total number of words in the word list that meet the specified minimum and maximum length criteria.
Installation:
npm install random-words
Examples:
import { generate, count } from "random-words";
console.log(generate());
console.log(generate(5));
console.log(generate({ minLength: 2 }));
console.log(generate({ maxLength: 6 }));
console.log(generate({ minLength: 5, maxLength: 5 }));
console.log(generate({ minLength: 11, maxLength: 10000 }));
console.log(generate({ minLength: 10000, maxLength: 5 }));
console.log(generate({ min: 3, max: 10 }));
console.log(generate({ exactly: 2 }));
console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
console.log(generate({ exactly: 5, join: " " }));
console.log(generate({ exactly: 5, join: "" }));
console.log(generate({ exactly: 2, minLength: 4 }));
console.log(generate({ exactly: 5, maxLength: 4 }));
console.log(generate({ exactly: 2, minLength: 3, maxLength: 3 }));
console.log(generate({ exactly: 3, minLength: 5, maxLength: 100000 }));
console.log(generate({ exactly: 5, wordsPerString: 2 }));
console.log(generate({ exactly: 5, wordsPerString: 2, separator: "-" }));
console.log(
generate({
exactly: 5,
wordsPerString: 2,
formatter: (word) => word.toUpperCase(),
})
);
console.log(
generate({
exactly: 5,
wordsPerString: 2,
formatter: (word, index) => {
return index === 0
? word.slice(0, 1).toUpperCase().concat(word.slice(1))
: word;
},
})
);
console.log(count());
console.log(count({ minLength: 5 }));
console.log(count({ maxLength: 7 }));
console.log(count({ minLength: 5, maxLength: 7 }));