structed-garbage
v0.9.7
Published
Garbage and test data generator with no dependencies that can generate random objects and arrays by defined structure
Downloads
5
Maintainers
Readme
structed-garbage
Garbage and test data generator with no dependencies that can generate random objects and arrays by defined structure.
Install
You can install this module by npm:
npm install structed-garbage
Usage
Firstly load module:
var garbage = require('structed-garbage');
Then you can use generator methods:
struct
Generates random data with defined structure.
Structure leafs must be defined as object: { generator: %generator-type%, params: %generator-argument% }. They will be replaced with results of random generators (they are described below).
This example generates object by defined structure:
garbage.struct({
name: {generator: 'name'},
birth: {generator: 'date'},
staticValue: 'some text for example',
staticValueMethod: {generator: 'value', params: {foo: 1, bar: 2, baz: 3}},
likesCount: {generator: 'int', params: {min: 0, max: 10}},
posts: [
{
title: {generator: 'collocation'},
text: {generator: 'string'},
image: {generator: 'imagePicsum', params: {width: 100, height: 100}},
created: {generator: 'date'},
},
{
title: {generator: 'collocation'},
text: {generator: 'string'},
image: {generator: 'imageLorempixel'},
created: {generator: 'date'},
},
],
comments: {
generator: 'array',
params: {
min: 0,
max: 3,
struct: {
text: { generator: 'string' },
date: { generator: 'date'}
}
}
}
});
array
Returns random filled array. Params:
- len - fixed length of the array
- max - max length of the array
- min - min length of the array
- struct - structure of array element
This code generates array with 3 elements, that generated by different random generators (they are described below):
garbage.array({len: 3})
This example generates array with length from 0 to 5 with defined structure:
garbage.array({
min: 0,
max: 5,
struct: {
name: { generator: 'name' },
phone: { generator: 'phone'},
something: { generator: 'object' }
}
});
// elements of this array will be something like this:
// {
// name: 'Emmalynne Gabriele',
// phone: '+45084705071',
// something: {
// '+4QV*$51fIwDkG5lM': '[email protected]',
// 'Mz|uWppr8Oj}+s`ZK': 'http://lorempixel.com/200/300'
// }
// }
object
Returns random filled object. Params:
- len - fixed number of properties
- max - max number of properties
- min - min number of properties
- struct - structure of element
This code generates object with 3 properties, that generated by different random generators (they are described below):
garbage.object({len: 3});
Object keys will be generated by string() method.
value
Just returns defined static value. May be useful in struct method.
garbage.value({foo: 1, bar: 2, baz: 3});
// expected: {foo: 1, bar: 2, baz: 3}
char
Returns single character.
garbage.char();
string
Returns random string. Params:
- min - min size of string (default: 2)
- max - max size of string (default: 20)
- len - fixed length of the string
garbage.string({min: 10, max: 50});
int
Returns random integer. Params:
- min - min value (default: 0)
- max - max value (default: 500)
garbage.string({min: -100, max: 500});
bool
Returns random bool.
garbage.bool();
float
Returns random float from 0 to 1.
garbage.float();
key
Returns random key of the object or array defined as argument.
garbage.key({foo: 1, bar: 2, baz: 3}); // may be foo, bar or baz
garbage.key(['foo', 'bar', 'baz']); // may be 0, 1 or 2
element
Returns random element of the object or array defined as argument.
garbage.element({foo: 1, bar: 2, baz: 3}); // may be 1, 2 or 3
garbage.element(['foo', 'bar', 'baz']); // may be foo, bar or baz
collocation
Returns random string with collocation.
garbage.collocation();
// returns something like: "propitious filling station attendant"
name, firstName, lastName
Returns random string with first and/or last name.
garbage.name(); // returns something like: "Ariella Dal"
garbage.firstName(); // returns something like: "Rosalinda"
garbage.lastName(); // returns something like: "Ripley"
Returns random string with email.
garbage.email();
// returns something like: "[email protected]"
phone
Returns random string with something like phone number. Params:
- len - length of the phone string without + at the start (default: 10)
garbage.phone();
// returns something like: "+05566393145"
site
Returns random string with site domain.
garbage.site();
// returns something like: "framemail.cf"
error
Returns random error object. Params:
- min - min length in words of the text (default: 2)
- max - max length in words of the text (default: 5)
garbage.error({min: 1, max: 7});
date
Returns random date object. Params:
- min - min timestamp (default: 0)
- max - max timestamp (default: Date.now())
garbage.date();
imagePicsum
Returns random image url from picsum.photos. Params:
- heigth - heigth of the image (default: 200)
- width - width of the image (default: 300)
garbage.imagePicsum({width: 100, height: 150});
imageLorempixel
Returns random image url from lorempixel.com. Params:
- heigth - heigth of the image (default: 200)
- width - width of the image (default: 300)
garbage.imageLorempixel({width: 100, height: 150});
text
Returns random text (lorem ipsum). Params:
- min - min length in words of the text (default: 1)
- max - max length in words of the text (default: end of lorem ipsum text)
- len - fixed length of the text in words
garbage.text({len: 10});