📅  最后修改于: 2023-12-03 15:03:31.367000             🧑  作者: Mango
Pandemonium is a library for generating random data in a controlled and repeatable way. It is ideal for use in tests and mocks where you need to generate data that looks real, but don't want to hard-code it.
To get started with Pandemonium, you will first need to install it:
npm install pandemonium
Then, you can start generating random data using the pandemonium
module:
const pandemonium = require('pandemonium');
// Generate a random integer between 0 and 100
const num = pandemonium.integer(0, 100);
console.log(num); // => 42
// Generate a random string with 10 characters
const str = pandemonium.string(10);
console.log(str); // => 'c1m%(ElzH9'
// Generate a random date between 2020-01-01 and 2020-12-31
const date = pandemonium.date('2020-01-01', '2020-12-31');
console.log(date); // => new Date('2020-06-20T01:23:45.678Z')
You can control the length and format of generated values using options.
For example, to generate a random string with only letters:
const str = pandemonium.string(10, { alpha: true });
console.log(str); // => 'XkbvlOmgNj'
Or to generate a random date with a specific format:
const date = pandemonium.date('2020-01-01', '2020-12-31', { format: 'YYYY/MM/DD' });
console.log(date); // => '2020/06/20'
You can generate unique values by using the unique
method. This guarantees that each value is unique within a specified range. For example:
const uniqueNums = pandemonium.unique(pandemonium.integer, 0, 100, 10);
console.log(uniqueNums); // => [42, 14, 87, 33, 95, 56, 71, 19, 62, 7]
This will generate 10 unique integers between 0 and 100.
Pandemonium is a powerful library for generating random data in a controlled and repeatable way. With its simple API and wide range of features, it is an essential tool for any developer who needs to generate test or mock data.