chance随机字符串

Chance

随机字符串生成插件,帮助减少编写自动化测试或其它任何需要随机数据时千篇一律的单调性。
可以随机生成数字、名称、域名、地址、邮箱、时间等。
是一个开源的软件,基于MIT开源协议。

安装

npm i chance

使用

import Chance from 'Chance'

const chance = new Chance()

const str = chance.string()

基础

chance.bool()

// 返回一个随机的错误值(false、null、undefined、0、NaN、'' )
chance.falsy()   // false
chance.falsy({pool: [NaN, undefined] })  // NaN

// 返回一个随机的字符串
// 默认情况下,返回一个带有随机字符的字符串
chance.character()   // 'v'
chance.character({ pool: 'abcde' })  // 'c'
// 指定alpha字符
chance.character({ alpha: true })  // 'N'
// 指定数字字符
chance.character({ numeric: true }) // '8'
// 有大小写的,可以指定其中一个
chance.character({ casing: 'lower' }) // 'j'
// 仅返回符号
chance.character({ symbols: true }) // '%'


// 返回一个随机的浮点型数字
// 默认情况下,小数点后有4位数字
chance.floating()
// 小数点后返回7位数字
chance.floating({ fixed: 7 })
// 指定最大值和最小值
chance.floating({ min: 0, max: 100 })
chance.floating({ min: 0, max: 100, fixed: 8 });


// 随机返回一个整型
chance.integer()
chance.integer({ min: -20, max: 20 })

// 随机返回一个字母,默认返回小写
chance.letter()
// 指定返回大写字母
chance.letter({ casing: 'upper' })

// 随机返回一个自然数
chance.natural()
chance.natural({ min: 1, max: 20 })
// 返回指定区间的数
chance.natural({min: 1, max: 5, exclude: [1, 3]});

// 返回一个素数
chance.prime()
chance.prime({ min: 1, max: 20 })

// 随机返回一个字符串。默认情况返回一个长度为5到20字符包含下列字符的字符串。
chance.string()
// 指定长度
chance.string({ length: 5 }) // 'YN%fG'
// 指定返回池
chance.string({ pool: 'abcde' }) // 'cccdeeabedebb'
chance.string({ alpha: true })
chance.string({ casing: 'lower' })
chance.string({ symbols: true })
chance.string({ length: 5, pool: 'abcde' });  //  'cbbdc'
chance.string({ length: 8, casing: 'upper', alpha: true, numeric: true });    // '3THK7GB1'

Text

// 随机返回一个半代词填充的句子生成的随机段落
chance.paragraph()
chance.paragraph({ sentences: 1 })

// 随机返回一个句子,默认单词数是12到18,可以指定单词数
chance.sentence()
chance.sentence({ words: 5 })

// 返回版拼音音节,2到3个字母,都是小写字母
chance.syllable()  // 'fop'

// 返回随机单词,都是小写
chance.word()
chance.word({ syllables: 3 })
chance.word({ length: 5 })

Person

// 随机年龄,默认范围是1到120,可以指定类型type: child, teen, adult, senior
chance.age()
chance.age({ type: 'child' })

// 随机生日,默认返回Date
chance.birthday()
chance.birthday({ string: true })   // '4/1/1968'
chance.birthday({string: true, american: false});   //   '28/6/1993'
chance.birthday({ type: 'child' })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值