data-seed
Mock data for js develop.
Why?
When we are developing, we need mock data to simulate the process of online requests, which are random, readable, safe, or even illegal.
Install
npm install data-seed
Usage
const util seed = ; // specific seed or utilconst util: random } seed: ip4 } = ; seed; // 23.4.126.0; // 1.1.1.123
Seeds
seed.avatar()
: return random avatar url from https://robohash.org/.seed.color()
: return a safe web color randomly. likeDarkGreen
seed.date([start=0, end=Date.now()])
: return timestamp randomly.seed.date.toSecond([start=0, end=Date.now()])
: return timestamp in seconds.seed.date.format(format[, start, end])
: return formatted date time. see momentJS#format
seed.email()
: return email randomly.seed.file()
: return file name randomly. like filename.jpgseed.file.extension()
: return file extension randomly.
seed.hexColor()
: return color hex value randomly. like#c9c9c9
seed.hexColor.short()
: return short color hex value randomly. like#666
seed.img()
: return a img url randomly.seed.img.base64()
: return a img base64 string randomly.seed.img.dummy([width, height, background, color])
: return a dummy url, eg. https://dummyimage.com/697x903.png/000/45cb91.
seed.ip4()
: return ip4 address randomly.seed.ip4.cidr()
: return ip4 cidr address, like1.1.1.1/24
seed.ip4.mask([start=1, end=32])
: return ip4 mask, like255.255.0.0
seed.ip4.mask.binary([start=1, end=32])
: return ip4 mask in binary
seed.ip6()
: return ip6 address randomly.seed.ip6.cidr()
: return ip6 cidr address, like65ef:57bd:f2f0:5688:8491:7713:e37a:9dc5/110
seed.ip6.mask([start=1, end=128])
: return ip6 mask, likeffff:ffff:0:0:0:0:0:0
seed.ip6.mask.binary([start=1, end=128])
: return ip6 mask in binary
seed.isbn(version)
: return an isbn string randomly. (version 10 or 13)seed.lorem([min=1, max=5])
: return lorem text randomly.seed.mac()
: return mac address randomly.seed.md5()
: return md5 string randomly.seed.mimeType()
: return MIME Type randomly.seed.name()
: return name randomly.seed.name.en()
: return name.seed.name.en.firstName()
: return first name.seed.name.en.lastName()
: return last name.seed.name.cn()
: return chinese name.seed.name.cn.firstName()
: return chinese first name.seed.name.cn.lastName()
: return chinese last name.
seed.paragraph([min=1, max=10])
: return paragraph text randomly.seed.sha([type=1])
: return SHA1 or SHA256 string randomly.seed.time([hourType=24])
: return time randomly, eg.13:17
in default,1:17 PM
whenhourType=12
seed.url([options = {protocol:true, host: true, path: true, query: true}])
: return a url randomly.seed.url.protocol
: return url protocol randomly, like http, file, ftp.seed.url.host
: return url host randomly.seed.url.domain
: return top level domain randomly, like com, net.seed.url.path
: return url path randomly.seed.url.query
: return url query string randomly.
seed.uuid()
: return uuid.seed.word([min=1, max=10])
: return a word randomly.seed.word.cn()
: return chinese word randomly.
seed.xss()
: return xss script, eg.<script>alert("xss");</script>
seed.xss.noStrict()
: return html text, eg.<a href="javascript:void(0)">click me!</a>
Utils
util.deep
util.deep
accepts a callback function which will return data.
The callback function accepts 4 arguments:
DEEP
: identify data loopDONE
: returnDONE
will stop looplevel
: record the deep level of dataisFirst
: whether first data
Create a tree nodes:
const util: deep = ; let data = ; /* data: { name: 'lxjwlt', nodes: { name: 'lxjwlt' } }*/
util.random
-
random.float([min = 0, max = 1])
: create a float number randomly. -
random.int([min=0, max=1])
: create a integer number randomly. -
random.letter([min='a', max='z'])
: create a letter randomly. -
random.array(arr)
: return a item within the array randomly. -
random.one([...])
: return a item from arguments randomly. -
random.chance(obj)
: specify random probability:random;
util.arr(num, callback)
Callback function return element of array:
const util: arr = ; ; // [0, 1]; // [10, 10]
util.gather(data)
const util: gather = ; let list = ; let item1 = listnext; // one of [1, 2, 3] randomlylet item2 = listnext; // one of [1, 2, 3] randomlylet item3 = listnext; // one of [1, 2, 3] randomly item1 !== item2; // trueitem2 !== item3; // trueitem1 !== item3; // true listnext; // undefined
And support iterator:
for let value of value; // one of [1, 2, 3] randomly
custom seed
const dataSeed = ; dataSeed; dataSeedseed; // 3
hook
Add hook to override data:
const dataSeed = ;const seed = dataSeed; dataSeed; seed; // xss stringseedip4; // xss stringseed; // xss string
Accept the value returned by last hook as first argument:
const random = dataSeedutil; dataSeed; seed; // ip string or null
Hook for specific seed:
dataSeed; seed; // xss stringseed; // ip stringseed; // lorem string