📜  如何使用 Node.js 中的 Faker 模块生成假数据?

📅  最后修改于: 2022-05-13 01:56:23.440000             🧑  作者: Mango

如何使用 Node.js 中的 Faker 模块生成假数据?

Faker模块用于生成假数据,不仅是假数据,还会感染组织良好的假数据。 Faker 模块是一个广泛使用的 NPM 模块,它可以生成虚假的名称、地址、产品名称、价格以及所有这些,您还可以使用这个 faker 包生成虚假的 IP 地址、图像等等。
安装faker模块的命令:

npm install faker

有一些预定义的上下文会为特定上下文创建假数据,如下所述:

Context(object)Aspect(method)
namefirstName, lastName, findName, suffix, jobTitle, jobDiscriptor
addresslatitude, longitude, country, state, city, zipCode, streetName
commerceproduct, productName, price, productMaterial
financeaccount, accountName, amount, currencyName, currencyCode, transactionType
imagepeople, nature, sports, animals, fashion, food, nightLife
internetemail, url, ip, mac, password, domainName

获取虚假数据的语法

faker.context.aspect()

示例 1:

javascript
// Program to generate some fake
// names with their job titles
  
// Requiring faker module
const faker = require('faker')
  
for(let i=0; i<8; i++){
 
// Fake first name
  const firstName = faker.name.firstName()
 
// Fake last name
  const lastName = faker.name.lastName()
 
// Fake suffix
  const suffix = faker.name.suffix()
 
// Fake job Title
  const jobTitle = faker.name.jobTitle()
  
  console.log(`${suffix} ${firstName}
    ${lastName} works as '${jobTitle}'`)
}


javascript
// Program to generate some fake
// products with their details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake product name
    const product = faker.commerce.product()
    // fake price of that product
    const price = faker.commerce.price()
 
    // Fake details
    const productMaterial =
        faker.commerce.productMaterial()
    console.log(`${product} made with
        ${productMaterial}, price ${price}$`)
}


javascript
// Program to generate some fake
// bank transaction details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake account type
    const ac = faker.finance.account()
 
    // Fake account name
    const acName = faker.finance.accountName()
 
    // Fake transaction type
    const tT = faker.finance.transactionType()
 
    // Fake amount transaction
    const amt = faker.finance.amount()
 
    console.log(`${acName}, Account No-${ac},
    transaction Type-${tT}, Amount-${amt}`)
}


javascript
// Program to generate some fake
// domain name and ip addresses
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake ip address
    const ip = faker.internet.ip()
 
    // Fake domain name
    const domainName =
        faker.internet.domainName()
 
    console.log(`Domain name ->
        ${domainName}, ip-address-> ${ip}`)
}


输出:

示例 2:

javascript

// Program to generate some fake
// products with their details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake product name
    const product = faker.commerce.product()
    // fake price of that product
    const price = faker.commerce.price()
 
    // Fake details
    const productMaterial =
        faker.commerce.productMaterial()
    console.log(`${product} made with
        ${productMaterial}, price ${price}$`)
}

输出:

示例 3:

javascript

// Program to generate some fake
// bank transaction details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake account type
    const ac = faker.finance.account()
 
    // Fake account name
    const acName = faker.finance.accountName()
 
    // Fake transaction type
    const tT = faker.finance.transactionType()
 
    // Fake amount transaction
    const amt = faker.finance.amount()
 
    console.log(`${acName}, Account No-${ac},
    transaction Type-${tT}, Amount-${amt}`)
}

输出:

示例 4:

javascript

// Program to generate some fake
// domain name and ip addresses
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake ip address
    const ip = faker.internet.ip()
 
    // Fake domain name
    const domainName =
        faker.internet.domainName()
 
    console.log(`Domain name ->
        ${domainName}, ip-address-> ${ip}`)
}

输出:

参考: NPM faker 包