RandomThing.js
无任何依赖的可快速生成随机内容的轻量级 JavaScript 脚本库
特点
- 支持 node 端和浏览器端
- 无任何第三方依赖
- 可在 IE11+, Chrome60+, Firefox60+, edge18+, safari12+, node11+ 中完美运行
- 体积小巧
仓库
Gitee 代码仓库
Github 代码仓库
安装
浏览器直接引入
<script src="randomthing.js"></script>
通过 node 安装并引入
npm i randomthing-js -S
const RT = require( "randomthing-js" );
方法
0 - 1 之间的随机数
语法:RT.number()
RT.number();
RT.number();
RT.number();
指定长度(或长度范围)的随机整数
语法:
RT.number( length )
RT.number( [ minLength, maxLength ] )
参数:
length, minLength, maxLength 有效范围 1 - 16
minLength 必须小于 maxLength
RT.number( 5 );
RT.number( 14 );
RT.number( 16 );
RT.number( [ 3, 6 ] );
RT.number( [ 3, 16 ] );
RT.number( [ 7, 9 ] );
RT.number( [ 5, 14 ] );
指定范围的随机整数
语法:RT.number( min, max )
参数:min 必须小于 max(生成的随机数包含 min 和 max 这两个边界值)
RT.number( 10, 15 );
RT.number( 10, 15 );
RT.number( 10, 15 );
随机小写字母
语法:
RT.en( length )
RT.en( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.en( 5 );
RT.en( 125 );
RT.en( 9999 );
RT.en( [ 5, 10 ] );
RT.en( [ 12, 130 ] );
RT.en( [ 150, 2000 ] );
随机大写字母
语法:
RT.EN( length )
RT.EN( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.EN( 5 );
RT.EN( 125 );
RT.EN( 9999 );
RT.EN( [ 5, 10 ] );
RT.EN( [ 12, 130 ] );
RT.EN( [ 150, 2000 ] );
随机大小写字母
语法:
- RT.En( length )
- RT.En( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.En( 5 );
RT.En( 125 );
RT.En( 9999 );
RT.En( [ 5, 10 ] );
RT.En( [ 12, 130 ] );
RT.En( [ 150, 2000 ] );
随机 “数字 + 小写字母”
语法:
RT.number_en( length )
RT.number_en( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_en( 5 );
RT.number_en( 125 );
RT.number_en( 9999 );
RT.number_en( [ 5, 10 ] );
RT.number_en( [ 12, 130 ] );
RT.number_en( [ 150, 2000 ] );
随机 “数字 + 大写字母”
语法:
RT.number_EN( length )
RT.number_EN( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_EN( 5 );
RT.number_EN( 125 );
RT.number_EN( 9999 );
RT.number_EN( [ 5, 10 ] );
RT.number_EN( [ 12, 130 ] );
RT.number_EN( [ 150, 2000 ] );
随机 “数字 + 大小写字母”
语法:
RT.number_En( length )
RT.number_En( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_En( 5 );
RT.number_En( 125 );
RT.number_En( 9999 );
RT.number_En( [ 5, 10 ] );
RT.number_En( [ 12, 130 ] );
RT.number_En( [ 150, 2000 ] );
随机汉字
语法:
RT.cn( length )
RT.cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.cn( 5 );
RT.cn( 125 );
RT.cn( 9999 );
RT.cn( [ 5, 10 ] );
RT.cn( [ 12, 130 ] );
RT.cn( [ 150, 2000 ] );
随机 “数字 + 汉字”
语法:
RT.number_cn( length )
RT.number_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_cn( 500 );
RT.number_cn( 1250 );
RT.number_cn( 99990 );
RT.number_cn( [ 50, 100 ] );
RT.number_cn( [ 120, 1300 ] );
RT.number_cn( [ 550, 2000 ] );
随机 “小写字母 + 汉字”
语法:
RT.en_cn( length )
RT.en_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.en_cn( 500 );
RT.en_cn( 1250 );
RT.en_cn( 99990 );
RT.en_cn( [ 50, 100 ] );
RT.en_cn( [ 120, 1300 ] );
RT.en_cn( [ 550, 2000 ] );
随机 “大写字母 + 汉字”
语法:
RT.EN_cn( length )
RT.EN_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.EN_cn( 500 );
RT.EN_cn( 1250 );
RT.EN_cn( 99990 );
RT.EN_cn( [ 50, 100 ] );
RT.EN_cn( [ 120, 1300 ] );
RT.EN_cn( [ 550, 2000 ] );
随机 “大小写字母 + 汉字”
语法:
RT.En_cn( length )
RT.En_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.En_cn( 500 );
RT.En_cn( 1250 );
RT.En_cn( 99990 );
RT.En_cn( [ 50, 100 ] );
RT.En_cn( [ 120, 1300 ] );
RT.En_cn( [ 550, 2000 ] );
随机 “数字 + 小写字母 + 汉字”
语法:
RT.number_en_cn( length )
RT.number_en_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_en_cn( 500 );
RT.number_en_cn( 1250 );
RT.number_en_cn( 99990 );
RT.number_en_cn( [ 50, 100 ] );
RT.number_en_cn( [ 120, 1300 ] );
RT.number_en_cn( [ 550, 2000 ] );
随机 “数字 + 大写字母 + 汉字”
语法:
RT.number_EN_cn( length )
RT.number_EN_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_EN_cn( 500 );
RT.number_EN_cn( 1250 );
RT.number_EN_cn( 99990 );
RT.number_EN_cn( [ 50, 100 ] );
RT.number_EN_cn( [ 120, 1300 ] );
RT.number_EN_cn( [ 550, 2000 ] );
随机 “数字 + 大小写字母 + 汉字”
语法:
RT.number_En_cn( length )
RT.number_En_cn( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
RT.number_En_cn( 500 );
RT.number_En_cn( 1250 );
RT.number_En_cn( 99990 );
RT.number_En_cn( [ 50, 100 ] );
RT.number_En_cn( [ 120, 1300 ] );
RT.number_En_cn( [ 550, 2000 ] );
随机特殊字符
语法:
RT.specialChars( length )
RT.specialChars( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
特殊字符包括:-+/~`!@#$%^&()_=[]{};:'"\?<>,.|
RT.specialChars( 5 );
RT.specialChars( 15 );
RT.specialChars( 30 );
RT.specialChars( [ 5, 10 ] );
RT.specialChars( [ 12, 130 ] );
RT.specialChars( [ 55, 200 ] );
随机 “数字 + 大小写字母 + 特殊字符”
语法:
RT.chars( length )
RT.chars( [ minLength, maxlength ] )
参数:
length, minLength, maxLength 有效范围 1 - 9007199254740991
minLength 必须小于 maxLength
特殊字符包括:-+/~`!@#$%^&()_=[]{};:'"\?<>,.|
RT.chars( 5 );
RT.chars( 15 );
RT.chars( 30 );
RT.chars( [ 5, 10 ] );
RT.chars( [ 12, 130 ] );
RT.chars( [ 55, 200 ] );
自定义随机字符
语法:RT.custom( opt )
参数:
opt.chars 自定义的字符集合
opt.count 长度
RT.custom({
chars: "abcd1234",
count: 5
});
RT.custom({
chars: "abcd1234",
count: [ 5, 10 ]
});
uuid
语法:RT.uuid( opt )
参数:
opt.withoutLine 去掉横线
opt.uppercase 转换成大写形式
RT.uuid();
RT.uuid({
withoutLine: true,
uppercase: true
});
重复字符
语法:RT.repeat( opt )
参数:
opt.chars 自定义的字符
opt.count 重复次数
RT.repeat({
chars: "abc",
count: 3
});
RT.repeat({
chars: "abc",
count: [ 3, 6 ]
});
随机不重复的 id
语法:RT.id( opt )
参数:
opt.prefix 前缀
opt.suffix 后缀
RT.id();
RT.id({
prefix: "aa-",
suffix: "-aa"
});
扩展机制
RandomThing 默认只提供了生成随机 “数字,字母,汉字” 等内容,这是最基础的随机内容,但有时开发者希望生成的随机内容更能符合一些特定的场景,比如:随机颜色值,随机网络地址,随机邮箱等等。由于这样的需求场景包罗万象,因此 RandomThing 自身不会集成这些功能,不过开发者可以使用 RT.extend() 扩展方法来封装想要的功能,这样就可以根据自身需求来丰富 RandomThing 功能库了。
RandomThing 作者额外提供了一些扩展,开发者可根据需要单独引入。
语法:RT.extend( obj )
参数:
obj 是一个纯对象,用来设置自定义的功能集合
obj 对象中的键值(即:自定义功能)不能与 RandomThing 本身提供的功能相同,否则无效。
RT.extend({
number () {
return 0;
}
})
RT.extend({
url () {
const suffix = [ "com", "cn", "com.cn", "net", "org.cn", "cc" ];
return `http://www.${ RT.en( [ 5, 10 ] ) }.${ suffix[ RT.number( 0, suffix.length - 1 ) ] }`;
}
})