js-util-random

1.0.4 • Public • Published

js-util-random

A JS toolkit with random numbers, random time, random human elements, and more

Install

npm i js-util-random

Usage

const random = require('js-util-random'); // CommonJS
import random from 'js-util-random'; // ES6 module
random.random(); // 5
random.random(0, 100); // 42
random.random(0, 100, 2); // 42.42
random.randomWithWeight([
  { value: 'a', weight: 1 },
  { value: 'b', weight: 2 },
  { value: 'c', weight: 9 },
]); // 'c'

API

random([min], [max], [accuracy])

Generates a random number between min and max with accuracy decimal places.

min and max default to 0 and 9 respectively.

accuracy defaults to 0.

Examples:

random(); // 5
random(0, 100); // 42
random(0, 100, 2); // 42.42

randomWithWeight([items])

Generates a random item from items based on the weight of each item.

items is an array of objects with value and weight properties.

Example:

randomWithWeight([
  { value: 'a', weight: 1 },
  { value: 'b', weight: 2 },
  { value: 'c', weight: 9 },
]); // 'c'

randomHumanAge([min],[max])

Generates human age random number between min and max.

min and max default to 0 and 120 respectively.

Example:

randomHumanAge(); // 5
randomHumanAge(0, 100); // 42

randomHumanGender([maleAlias],[femaleAlias])

Generates random human gender.

maleAlias and femaleAlias default to 0 and 1 respectively.

Example:

randomHumanGender(); // 0
randomHumanGender('男', '女'); // 男

randomBoolean()

Generates random boolean.

Example:

randomBoolean(); // true

randomHumanChineseName([isFemale])

Generates random chinese name.

isFemale default to false.

Example:

randomHumanChineseName(); // '张三'
randomHumanChineseName(true); // '李四'

randomDateTime([start],[end])

Generates random date time.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomDateTime(); // '2019-01-01T00:00:00.000Z'

randomDate([start],[end])

Generates random date.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomDate(); // '2019-01-01'

randomTime([start],[end])

Generates random time.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomTime(); // '00:00:00'

randomDateTimeString([start],[end])

Generates random date time string.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomDateTimeString(); // 2020/7/22 01:10:15'

randomDateString([start],[end])

Generates random date string.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomDateString(); // '2020/7/22'

randomTimeString([start],[end])

Generates random time string.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomTimeString(); // '01:10:15'

randomTimeStamp([start],[end])

Generates random timestamp.

start and end default to new Date(1970, 0, 1) and new Date() respectively.

Example:

randomTimeStamp(); // 1595364615000

randomChar([chars])

Generates random char.

chars default to 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.

Example:

randomChar(); // 'a'

License

MIT License

Package Sidebar

Install

npm i js-util-random

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

10.9 kB

Total Files

3

Last publish

Collaborators

  • yangguifang