惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
A
About on SuperTechFans
量子位
Engineering at Meta
Engineering at Meta
B
Blog
The Cloudflare Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
J
Java Code Geeks
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 网无忌

体验Coding Plan 本地安装Dify 关于Docker Desktop的常用配置 渐变文字的小技巧 机器学习基础 pydantic中关于属性必填和选填的区别 CentOS的常用命令 密码中含有特殊字符造成mysqldump备份失败的一个小教训 通过模拟Cron执行环境来复现脚本执行失败的过程 查看mysql当前的执行任务,并关闭其中的指定任务 配置WSL2实现与宿主机的网络互通 超简单的 rsync 命令,实现文件的增量同步 十九年白驹过隙,老园子聊发少年狂 Linux中安装anaconda 矢量数据库Chromadb的入门信息 在wsl中部署puppeteer的相关笔记 向量数据库横比 整理了一下目前各Linux发行版的清单 盘点各领域的包管理器 使用 jstat 命令查看 JVM 的GC信息 使用Puppeter实现的全屏网页截图的小工具 开启 mysql 的 general_log
记录一个在js环境生成随机(伪造)数据的小插件,方便生成调...
网无忌 · 2024-04-08 · via 博客园 - 网无忌

插件名称:faker
官网地址:https://fakerjs.dev/
 

安装插件

# npm
npm install @faker-js/faker --save-dev

# yarn
yarn add @faker-js/faker --dev

工具封装

 
dataGener.js:

import { fakerZH_CN as faker } from '@faker-js/faker'

/**
 * 生成UUID
 * @returns
 */
function uuid() {
  return faker.string.uuid()
}

/**
 * 生成随机整数
 * @param {*} min 最小
 * @param {*} max 最大
 * @returns
 */
function number(min = 0, max = 100) {
  return faker.number.int({ min, max })
}

/**
 * 生成随机浮点数
 * @param {*} min 最小
 * @param {*} max 最大
 * @param {*} fixed 小数位数
 * @returns
 */
function float(min = 0, max = 1, fixed = 2) {
  return faker.number.float({ min, max, fractionDigits: fixed })
}

/**
 * 按正则生成随机字符串
 * @param {*} reg 模板
 * @returns
 */
function regexp(reg) {
  return faker.helpers.fromRegExp(`${reg}`)
}

/**
 * 按字典生成随机字符串
 * @param {*} chars 字典母表
 * @param {*} len 长度
 * @returns
 */
function string(chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$&', len = 20) {
  return faker.string.fromCharacters(chars, len)
}

/**
 * 生成随机单词
 * @returns
 */
function word() {
  return faker.lorem.word()
}

/**
 * 生成随机句子
 * @returns
 */
function sentence() {
  return faker.lorem.sentence()
}

/**
 * 生成随机段落
 * @returns
 */
function paragraphs() {
  return faker.lorem.paragraphs()
}

/**
 * 生成随机文本
 * @returns
 */
function text() {
  return faker.lorem.text()
}

/**
 * 生成姓名
 * @returns
 */
function name() {
  const surname = faker.person.lastName()
  const firstName = faker.person.firstName()
  return `${surname}${firstName}`
}

/**
 * 生成性别
 * @returns
 */
function sex() {
  const maps = ['男', '女']
  return faker.helpers.arrayElement(maps)
}

/**
 * 生成民族
 * @returns
 */
function nation() {
  const maps = ['汉族', '壮族', '满族', '回族', '维吾尔族', '蒙古族']
  return faker.helpers.arrayElement(maps)
}

/**
 * 生成政治面貌
 * @returns
 */
function polStatus() {
  const maps = ['中共党员', '共青团员', '无党派人士', '群众']
  return faker.helpers.arrayElement(maps)
}

/**
 * 生成年龄(18~35)
 * @returns
 */
function age() {
  return faker.number.int({ min: 18, max: 35 })
}

/**
 * 生成出生日期
 * @returns
 */
function birthday() {
  const dt = faker.date.birthdate({ min: 18, max: 35, mode: 'age' })
  return formatDate(dt)
}

/**
 * 生成城市
 * @returns
 */
function city() {
  return faker.location.city()
}

/**
 * 生成地址
 * @returns
 */
function addr() {
  return faker.location.streetAddress({ useFullAddress: true })
}

/**
 * 生成手机号
 * @returns
 */
function mobile() {
  const prefixNum = faker.helpers.arrayElement(['30', '35', '37', '58', '86'])
  const otherNum = faker.number.int({ min: 10000000, max: 99999999 })
  return `1${prefixNum}${otherNum}`
}

/**
 * 生成座机号
 * @returns
 */
function phone() {
  return faker.phone.number()
}

/**
 * 生成Email
 * @returns
 */
function email() {
  return faker.internet.email()
}

/**
 * 生成随机密码
 * @returns
 */
function password(len = 18) {
  return faker.internet.password({ length: len })
}

/**
 * 生成随机IP地址(默认v4)
 * @returns
 */
function ip() {
  return ipv4()
}

/**
 * 生成随机的IPv4地址
 * @returns
 */
function ipv4() {
  return faker.internet.ipv4()
}

/**
 * 生成随机的IPv6地址
 * @returns
 */
function ipv6() {
  return faker.internet.ipv6()
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = (1 + date.getMonth()).toString()
  month = month.padStart(2, '0')
  var day = date.getDate().toString()
  day = day.padStart(2, '0')
  return `${year}-${month}-${day}`
}

export default {
  uuid,
  number,
  float,
  regexp,
  string,
  word,
  sentence,
  paragraphs,
  text,
  name,
  sex,
  age,
  birthday,
  city,
  addr,
  nation,
  polStatus,
  mobile,
  phone,
  email,
  password,
  ip,
  ipv4,
  ipv6
}

使用示例

console.log('uuid: ' + dataGener.uuid())
console.log('number: ' + dataGener.number())
console.log('float: ' + dataGener.float())
console.log('string: ' + dataGener.string())
console.log('word: ' + dataGener.word())
console.log('sentence: ' + dataGener.sentence())
console.log('paragraphs: ' + dataGener.paragraphs())
console.log('text: ' + dataGener.text())
console.log('name: ' + dataGener.name())
console.log('sex: ' + dataGener.sex())
console.log('birthday: ' + dataGener.birthday())
console.log('age: ' + dataGener.age())
console.log('city: ' + dataGener.city())
console.log('addr: ' + dataGener.addr())
console.log('mobile: ' + dataGener.mobile())
console.log('phone: ' + dataGener.phone())
console.log('email: ' + dataGener.email())
console.log('ipv4: ' + dataGener.ip())
console.log('ipv6: ' + dataGener.ipv6())
console.log('nation: ' + dataGener.nation())
console.log('polStatus: ' + dataGener.polStatus())

 
输出:

uuid: 1f204084-80d3-4216-9f8e-1dfea5e7f1c1
number: 12
float: 0.61
string: PI&M#P!8E2RY!KT@D9DR
word: contabesco
sentence: Talus vivo communis.
paragraphs: Arguo rem cubo. Aegrus trado velut voluntarius vilis canto. Aestus error caritas crastinus tergiversatio.
Magni carbo suscipio veritas id. Paens agnitio est caput autus. Vita causa nihil rerum collum convoco verecundia cultellus velut eligendi.
Subvenio cotidie constans tergeo vorax quaerat saepe deinde taceo. Cursim sumo quam stella. Demo fuga usus iure spiritus auctor conduco ventosus.
text: Demo aliquid iste comburo. Cubitum terminatio tabesco bos virgo laudantium tantillus suadeo. Quo coaegresco reprehenderit ventus conturbo adversus sperno tolero. Quibusdam conturbo tracto harum sodalitas virga depono aetas vesica.
name: 冀鹏飞
sex: 男
birthday: 1990-03-11
age: 19
city: 珠林市
addr: 韩栋87139号 Apt. 933
mobile: 13787726911
phone: 098-34992885
email: fvpts4.o5i@21cn.com
ipv4: 38.72.86.90
ipv6: 35f0:aa6a:ff2b:afb5:cbd2:9f1b:3e5b:ea8a
nation: 蒙古族
polStatus: 无党派人士