🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

randomthing-js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

randomthing-js

快速生成随机内容的轻量级脚本库

latest
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

RandomThing.js

快速生成随机内容的轻量级脚本库

安装

cdn 安装

<!-- jsdelivr cdn -->
<script src="https://cdn.jsdelivr.net/npm/randomthing-js@latest/dist/randomthing.min.js"></script>

<!-- unpkg cdn -->
<script src="https://unpkg.com/randomthing-js@latest/dist/randomthing.min.js"></script>

<script>
    const en = RT.en( 10 );
    console.log( en );
</script>

node 安装

// 安装
npm i randomthing-js -S

// 引入
const RT = require( "randomthing-js" );

// 使用
const en = RT.en( 10 );
console.log( en );

方法汇总

RT.number();
RT.en();
RT.EN();
RT.En();
RT.number_en();
RT.number_EN();
RT.number_En();
RT.cn();
RT.number_cn();
RT.en_cn();
RT.EN_cn();
RT.En_cn();
RT.number_en_cn();
RT.number_EN_cn();
RT.number_En_cn();
RT.specialChars();
RT.chars();
RT.custom();
RT.uuid();
RT.repeat();
RT.id();
RT.boolean();
RT.ip();
RT.email();
RT.date();
RT.time();
RT.dateTime();
RT.hex();
RT.rgb();
RT.rgba();
RT.url();
RT.image();
RT.array();
RT.extend();

用法示例

0 - 1 之间的随机数

语法:RT.number()

RT.number();

指定长度(或长度范围)的随机整数

语法:

  • RT.number( length )
  • RT.number( [ minLength, maxLength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 16
  • minLength 必须小于 maxLength
RT.number( 5 );
RT.number( [ 5, 10 ] );

指定范围的随机整数

语法:RT.number( min, max )

参数:min 必须小于 max(生成的随机数包含 minmax 这两个边界值)

RT.number( 10, 50 );

随机小写字母

语法:

  • RT.en( length )
  • RT.en( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.en( 10 );
RT.en( [ 10, 20 ] );

随机大写字母

语法:

  • RT.EN( length )
  • RT.EN( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.EN( 10 );
RT.EN( [ 10, 20 ] );

随机大小写字母

语法:

  • RT.En( length )
  • RT.En( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.En( 10 );
RT.En( [ 10, 20 ] );

随机 “数字 + 小写字母”

语法:

  • RT.number_en( length )
  • RT.number_en( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_en( 10 );
RT.number_en( [ 10, 20 ] );

随机 “数字 + 大写字母”

语法:

  • RT.number_EN( length )
  • RT.number_EN( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_EN( 10 ); 
RT.number_EN( [ 10, 20 ] );

随机 “数字 + 大小写字母”

语法:

  • RT.number_En( length )
  • RT.number_En( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_En( 10 );
RT.number_En( [ 10, 20 ] );

随机汉字

语法:

  • RT.cn( length )
  • RT.cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.cn( 10 );
RT.cn( [ 10, 20 ] );

随机 “数字 + 汉字”

语法:

  • RT.number_cn( length )
  • RT.number_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_cn( 10 );
RT.number_cn( [ 10, 20 ] );

随机 “小写字母 + 汉字”

语法:

  • RT.en_cn( length )
  • RT.en_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.en_cn( 10 );
RT.en_cn( [ 10, 20 ] );

随机 “大写字母 + 汉字”

语法:

  • RT.EN_cn( length )
  • RT.EN_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.EN_cn( 10 );
RT.EN_cn( [ 10, 20 ] );

随机 “大小写字母 + 汉字”

语法:

  • RT.En_cn( length )
  • RT.En_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.En_cn( 10 );
RT.En_cn( [ 10, 20 ] );

随机 “数字 + 小写字母 + 汉字”

语法:

  • RT.number_en_cn( length )
  • RT.number_en_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_en_cn( 10 );
RT.number_en_cn( [ 10, 20 ] );

随机 “数字 + 大写字母 + 汉字”

语法:

  • RT.number_EN_cn( length )
  • RT.number_EN_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_EN_cn( 10 );
RT.number_EN_cn( [ 10, 20 ] );

随机 “数字 + 大小写字母 + 汉字”

语法:

  • RT.number_En_cn( length )
  • RT.number_En_cn( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength
RT.number_En_cn( 10 );
RT.number_En_cn( [ 10, 20 ] );

随机特殊字符

语法:

  • RT.specialChars( length )
  • RT.specialChars( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength

特殊字符包括:-+/~`!@#$%^&()_=[]{};:'"\?<>,.|

RT.specialChars( 10 );
RT.specialChars( [ 10, 20 ] );

随机 “数字 + 大小写字母 + 特殊字符”

语法:

  • RT.chars( length )
  • RT.chars( [ minLength, maxlength ] )

参数:

  • length, minLength, maxLength 有效范围 1 - 9007199254740991
  • minLength 必须小于 maxLength

特殊字符包括:-+/~`!@#$%^&()_=[]{};:'"\?<>,.|

RT.chars( 10 );
RT.chars( [ 10, 20 ] );

自定义随机字符

语法: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: "-kk"
});  

随机布尔值

语法:RT.boolean()

RT.boolean(); 

RT.ip

功能:生成随机 ip

语法:RT.ip()

RT.ip();

RT.email

功能:生成随机邮箱

语法:RT.email()

RT.email();

RT.date

功能:生成随机日期

语法:RT.date( opt )

参数:

  • opt.minYear 最小年份(默认:1970)
  • opt.maxYear 最大年份(默认:当前年份)
  • opt.addZero 月份和天数为单数时自动在前面补零(默认:true)
  • opt.format 格式化模式(默认:YYYY-MM-DD)
RT.date();

RT.date({
    minYear: 1900,
    maxYear: 2200,
    format: "YYYY/MM/DD"
});

RT.time

功能:生成随机时间

语法:RT.time( opt )

参数:

  • opt.minHour 最小小时(默认:0)
  • opt.maxHour 最大小时(默认:23)
  • opt.addZero 时分秒为单数时自动在前面补零(默认:true)
  • opt.format 格式化模式(默认:hh:mm:ss)
RT.time();

RT.time({
    format: "hh时mm分ss秒"
});

RT.dateTime

功能:生成随机日期时间

语法:RT.dateTime( opt )

参数:

  • opt.minYear 最小年份(默认:1970)
  • opt.maxYear 最大年份(默认:当前年份)
  • opt.minHour 最小小时(默认:0)
  • opt.maxHour 最大小时(默认:23)
  • opt.addZero 月日时分秒为单数时自动在前面补零(默认:true)
  • opt.format 格式化模式(默认:YYYY-MM-DD hh:mm:ss)
RT.dateTime();

RT.dateTime({
    minYear: 1900,
    maxYear: 2200,
    minHour:5,
    maxHour:10format: "YYYY年MM月DD日 hh时mm分ss秒"
});

RT.hex

功能:生成随机颜色值(十六进制写法,如:#06a8ff)

语法:RT.hex()

RT.hex();

RT.rgb

功能:生成随机颜色值(rgb 写法)

语法:RT.rgb()

RT.rgb();

RT.rgba

功能:生成随机颜色值(rgb写法,包含透明度)

语法:RT.rgba()

RT.rgba();

RT.url

功能:生成随机网络地址

语法:RT.url( opt )

参数:

  • opt.useHttps 使用 https 协议(默认:false)
  • opt.showProtocol 显示协议(默认:true)
  • opt.showWWW 显示 www(默认:true)
  • opt.domainSuffix 域名后缀集合,多个后缀以逗号分隔(默认:com, cn, com.cn, net, net.cn, org, org.cn, info, cc, edu)
RT.url();

RT.url({
    showProtocol: false,
    domainSuffix: "com, com.cn, cn, net"
});

RT.image

功能:生成随机图片(base64 编码形式)

语法:RT.image( opt )

参数:

  • opt.color 图片颜色(默认:#000)
RT.image({
    color: "#09f"
});

RT.array

功能:以数组的形式生成随机内容

语法:RT.array( length, function )

参数:

  • length 数据条数
  • function 返回内容的函数
RT.array(10, function () {
    return {
        num: RT.number( 5 ),
        id: RT.uuid()
    }
})

扩展机制

RT.extend() 方法可以让开发者根据实际需要自定义返回的内容,从而应对千变万化的需求。

语法:RT.extend( obj )

参数:

  • obj 是一个纯对象,用来设置自定义的功能集合
  • obj 对象中的键值(即:自定义方法)不能与内置方法同名,否则无效。
// 无效扩展,因为 RandomThing 已经内置了 RT.number() 方法
RT.extend({
    number () {
        return 0;
    }
})

// 正确的扩展
RT.extend({
    mine () {
        return "自定义扩展方法";
    }
})

console.log( RT.mine() );

Keywords

random

FAQs

Package last updated on 15 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts