npm: https://www.npmjs.com/package/faker
# 演示
https://rawgit.com/Marak/faker.js/master/examples/browser/index.html
# 用法
# Browser
<script src = "faker.js" type = "text/javascript"></script>
<script>
var randomName = faker.name.findName(); // Caitlyn Kerluke
var randomEmail = faker.internet.email(); // Rusty@arne.info
var randomCard = faker.helpers.createCard(); // random contact card containing many properties
</script>
# Node.js
var faker = require('faker');
var randomName = faker.name.findName(); // Rowan Nikolaus
var randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
var randomCard = faker.helpers.createCard(); // random contact card
# 应用程序接口
# JSDoc API 浏览器
http://marak.github.io/faker.js/
# API 方法
- address
- zipCode
- zipCodeByState
- city
- cityPrefix
- citySuffix
- streetName
- streetAddress
- streetSuffix
- streetPrefix
- secondaryAddress
- county
- country
- countryCode
- state
- stateAbbr
- latitude
- longitude
- direction
- cardinalDirection
- ordinalDirection
- nearbyGPSCoordinate
- timeZone
- commerce
- color
- department
- productName
- price
- productAdjective
- productMaterial
- product
- productDescription
- company
- suffixes
- companyName
- companySuffix
- catchPhrase
- bs
- catchPhraseAdjective
- catchPhraseDescriptor
- catchPhraseNoun
- bsAdjective
- bsBuzz
- bsNoun
- database
column- type
- collation
- engine
- date
- past
- future
- between
- recent
- soon
- month
- weekday
- fake
- finance
- account
- accountName
- routingNumber
- mask
- amount
- transactionType
- currencyCode
- currencyName
- currencySymbol
- bitcoinAddress
- litecoinAddress
- creditCardNumber
- creditCardCVV
- ethereumAddress
- iban
- bic
- transactionDescription
- git
- branch
- commitEntry
- commitMessage
- commitSha
- shortSha
- hacker
- abbreviation
- adjective
- noun
- verb
- ingverb
- phrase
- helpers
- randomize
- slugify
- replaceSymbolWithNumber
- replaceSymbols
- replaceCreditCardSymbols
- repeatString
- regexpStyleStringParse
- shuffle
- mustache
- createCard
- contextualCard
- userCard
- createTransaction
- image
- image
- avatar
- imageUrl
- abstract
- animals
- business
- cats
- city
- food
- nightlife
- fashion
- people
- nature
- sports
- technics
- transport
- dataUri
- lorempixel
- unsplash
- lorempicsum
- internet
- avatar
- exampleEmail
- userName
- protocol
- url
- domainName
- domainSuffix
- domainWord
- ip
- ipv6
- userAgent
- color
- mac
- password
- lorem
- word
- words
- sentence
- slug
- sentences
- paragraph
- paragraphs
- text
- lines
- music
- genre
- name
- firstName
- lastName
- middleName
- findName
- jobTitle
- gender
- prefix
- suffix
- title
- jobDescriptor
- jobArea
- jobType
- phone
- phoneNumber
- phoneNumberFormat
- phoneFormats
- random
- number
- float
- arrayElement
- arrayElements
- objectElement
- uuid
- boolean
- word
- words
- image
- locale
- alpha
- alphaNumeric
- hexaDecimal
- system
- fileName
- commonFileName
- mimeType
- commonFileType
- commonFileExt
- fileType
- fileExt
- directoryPath
- filePath
- semver
- time
- recent
- unique
- vehicle
- vehicle
- manufacturer
- model
- type
- fuel
- vin
- color
# Faker.fake()
faker.js 包含一个非常有用的生成器方法,Faker.fake 用于使用 mustache 字符串格式组合 faker API 方法。
例子:
console.log(faker.fake("{{name.lastName}}, {{name.firstName}} {{name.suffix}}"));
// 输出:“Marks, Dean Sr.”
这将插值格式字符串的方法的价值 name.lastName (),name.firstName () 和 name.suffix ()
# 本土化
从版本 v2.0.0faker.js 开始支持多个地方。
默认语言区域设置为英语。
设置新的语言环境很简单:
// sets locale to de
faker.locale = "de";
- az
- ar
- cz
- de
- de_AT
- de_CH
- en
- en_AU
- en_AU_ocker
- en_BORK
- en_CA
- en_GB
- en_IE
- en_IND
- en_US
- en_ZA
- es
- es_MX
- fa
- fi
- fr
- fr_CA
- fr_CH
- ge
- id_ID
- it
- ja
- ko
- nb_NO
- ne
- nl
- nl_BE
- pl
- pt_BR
- pt_PT
- ro
- ru
- sk
- sv
- tr
- uk
- vi
- zh_CN
- zh_TW
# 个人本地化包
faker.js 支持语言环境的增量加载。
默认情况下,requiresfaker 将包含所有语言环境数据。
在生产环境中,您可能只想包含特定语言环境集的语言环境数据。
// loads only de locale
var faker = require('faker/locale/de');
# 设置随机种子
如果你想要一致的结果,你可以设置你自己的种子:
faker.seed(123);
var firstRandom = faker.random.number();
// Setting the seed again resets the sequence.
faker.seed(123);
var secondRandom = faker.random.number();
console.log(firstRandom === secondRandom);