simpleObject
objectWithKeys
fromList
subList
listOf
string
word
sentence
paragraph
integer
float
boolean
url
email
date
Produces a simple object with a random number of key/value pairs. Each key is a random word, and each value is a random string
any.simpleObject();
// { ahe: 'pPwLKrme*OU4@AR1)*^', lel: '6ExqUAyD!', fegcim: 'j03do)OUDhvJ', ucalu: 'L#)zHg5@AV9#vD5O' }
Produces a simple object with the keys defined from the provided list of keys
keys
(required): list of keys to be used for the produced object's keysoptions
: object to provide configuration optionsfactory
: factory function to be used to create each of the values for the produced object- receives the current key as the first argument and the index as the second
- defaults to
string
, if not provided
any.objectWithKeys(['foo', 'bar', 'baz']);
// { foo: 'eUnRumYAyoS@YVj', bar: 'GvdfJH%]OhNCSDe', baz: 'ozLo2]R' }
any.objectWithKeys(['foo', 'bar', 'baz'], {factory: any.url});
// { foo: 'http://hopap.lr/dupro', bar: 'http://kolore.np/cozmavje', baz: 'http://udegowum.cd/ohobasmo' }
any.objectWithKeys(['foo', 'bar', 'baz'], {factory: id => ({...any.simpleObject(), id})});
/*
{
foo: {
ipsawiw: '$McveM',
comgahra: ']TORpXOYqKQz&G]h',
ranimedo: 'Q^Y)ga2FG^jPzs](',
owabopmom: 'YQz[MoIEVHLbVG',
jebaru: 'qd&TpwaMEp8Vz#al8C',
zuwbedil: 'LC&c3$gPxI',
id: 'foo'
},
bar: {
jahaluz: '#HD0Zqs$QLxAC@Lrb*',
ekofesi: 'uhAOs',
veuhotuh: 'ob1K2TDc5Q',
zuufciw: 'L&hqNtx@',
riptutru: 'mYSXLZh#OmWvaJC2',
obebetmi: '[E@yC^xTBia*]kdvfC',
hugmoug: 'DfV8Qs9f8O433etJ',
zusurare: '0I0!xzrxEN$f4',
gonefo: 'YkLh9Sg&f7o%K)VDT',
watbihli: '3F2]ofS',
elaecave: 'iY@uTDWy@J',
id: 'bar'
},
baz: {
zehitek: '7$GlW8N',
upagpu: '9^UDxLQKp',
aniheiju: '9$lJPW6$eXhPf$GFa',
ekaradcun: ')nD1)rC&c^',
molilji: 'qvr7dW$Ve0uR3fkZ]',
jebowibuf: '0G6vTn)71NsXWsr]ypd',
wednotso: '2hHmqvDlw&&!^ycJ0',
arewibvuf: '*N^&UXo$c&eAiF^',
gutuet: '^JHXnzETi*VSuUy(3b',
molwadi: 'x!dAw',
fuglekpe: 'WqDOaU2[LdAvHm',
ireakiuso: '@VG9BF$)F',
id: 'baz'
}
}
*/
const qux = ['a', 'b', 'c'];
any.objectWithKeys(['foo', 'bar', 'baz'], {factory: (id, index) => ({id, letter: qux[index]})});
/*
{
foo: { id: 'foo', letter: 'a' },
bar: { id: 'bar', letter: 'b' },
baz: { id: 'baz', letter: 'c' }
}
*/
Direct usage of pickone()
from chance.js.
list
(required): list of items to choose from
any.fromList(['foo', 'bar', 'baz']);
// bar
An almost direct usage of pickset()
from chance.js.
list
(required): list of items to choose fromoptions
: (required) object to provide configuration optionssize
: (required) length of sub-list that should be produced by choosing unique items from the provided list
any.subList(['foo', 'bar', 'baz'], {size: 2});
// ['bar', 'foo']
Produces a list of random items from the provided factory function
factory
(required): the factory function for producing the items for the list.- receives the current index as the first argument
options
: object to provide configuration optionssize
: length of list that should be produced- defaults to a random number between
1
and20
, if not provided - the default range of potential sizes can be overridden by providing options for integer
- defaults to a random number between
uniqueOn
: property on the produced objects that needs to be unique within the resulting list
any.listOf(any.string);
// [ 'iQw2[CgBJ1FF8', '58jp*w', 'G]5MoXnKe#F43H3#1', 'oO*a7tY^gbY', 'f$AFjgd$Gg2' ]
any.listOf(any.string, {size: 2});
// [ 'wDPGiMtH]fg6o8QxMS', 'OYElY9vO9k4' ]
any.listOf(() => ({ id: any.integer(), foo: any.string() }), {uniqueOn: 'id'});
// [ { id: 173289733357568, foo: 'T8XCg*!Q((*Z5@BUFO' }, { id: 8233254624690176, foo: 'ziY]4e%cDoR07g)IhA' } ]
Direct usage of string()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
Be aware that string()
can include special characters that may not be suitable
for some use cases. If they would cause issues, you may prefer word()
instead.
any.string();
// 1%BU8#64p%Z
Direct usage of word()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.word();
// nitte
Direct usage of sentence()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.sentence();
// 'Witpevze mappos isoletu fo res bi geow pofin mu rupoho revzi utva ne.'
Direct usage of paragraph()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.paragraph();
// 'Lel fi huepe jupu akse zej ire vesik kojvulom zon is biwuwkef pa. Uv hokivej voh ebu
// numdogi akolo hik uwlez ta vacev ofdaimi acunetum suvet uhdab ir soglazo ju pafbeb. Pub
// cezeh fuc kebamnul he ok luumoabi rawkig me fov pin zup biv risugra. Ralpunad apkomgib
// alnirciw akel wa lus wahfum burog buol vecotihe abadahoj ugolo wovki ucojal fec.'
Produces a random integer >= 0. Direct usage of
natural()
from chance.js.
Options are passed directly to the chance method so refer to its
documentation for what is available.
any.integer();
// 8526341888540672
Direct usage of floating()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.float();
// -114981703621.0176
Produces a random boolean value (true
or false
).
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.boolean();
// true
Direct usage of url()
from chance.js.
Options are passed directly to the chance method so refer to its
documentation for what is available.
any.url();
// http://vek.fo/derpu
Direct usage of email()
from chance.js.
Options are passed directly to the chance method so refer to its documentation
for what is available.
any.email();
// [email protected]
An almost direct usage of date()
from chance.js,
but {string: true}
is passed so the default behavior is to generate a date
string by default.
Options are appended to the default of {string: true}
when calling the chance
method, so refer to its documentation for what is available.
Pass {string: false}
in order to return Date
objects instead of date strings.
any.date();
// 10/24/2028