这是 Ruby 的 Faker 库的 Swift 版本,它可以生成模拟数据。
你是否还在为无意义的随机字符字符串感到烦恼?现在放松一下,把这个任务留给 Fakery。当你需要使用一些模拟数据来测试、开发期间填充数据库等时,它非常有用。
注意:生成数据相当真实,支持多种语言环境,但无法保证返回的值是唯一的。
目录
使用方法
import Fakery
let faker = Faker(locale: "nb-NO")
let firstName = faker.name.firstName() //=> "Emilie"
let lastName = faker.name.lastName() //=> "Hansen"
let city = faker.address.city() //=> "Oslo"
本地化
Fakery 在生成特定地区的数据方面非常强大。在 Resources/Locales
中,您可以找到超过 20 种语言的 JSON 文件,当然,这不是限制。随时贡献并添加更多!
默认地区设置为英语。当您使用可用的生成器之一,并且所选地区对应的键未在当前地区所选择的JSON文件中找到时,Fakery 也会检查它是否存在“en.json”文件中。
生成器
地址
faker.address.city() //=> "Oslo"
faker.address.streetName() //=> "North Avenue"
faker.address.secondaryAddress() //=> "Apt. 123"
faker.address.streetAddress(includeSecondary: Bool) //=> "12 North Avenue"
faker.address.buildingNumber() //=> "123"
faker.address.postcode(stateAbbreviation: String) //=> "0884"
faker.address.timeZone() //=> "America/Los_Angeles"
faker.address.streetSuffix() //=> "Avenue"
faker.address.citySuffix() //=> "town"
faker.address.cityPrefix() //=> "North"
faker.address.stateAbbreviation() //=> "CA"
faker.address.state() //=> "California"
faker.address.county() //=> "Autauga County"
faker.address.country() //=> "United States of America"
faker.address.countryCode() //=> "US"
faker.address.latitude() //=> -58.17256227443719
faker.address.longitude() //=> -156.65548382095133
faker.address.coordinate() //=> CLLocationCoordinate2D(latitude: 40.97868, longitude: 29.09306)
应用
faker.app.name() //=> "Namfix"
faker.app.version() //=> "0.1.1"
faker.app.author() //=> "Ida Adams"
商业
faker.business.creditCardNumber() //=> "1234-2121-1221-1211"
faker.business.creditCardType() //=> "visa"
faker.business.creditCardExpiryDate() //=> "2020-10-12"
猫
faker.cat.name() //=> "Shadow"
faker.cat.breed() //=> "British Semipi-longhair"
faker.cat.registry() //=> "American Cat Fanciers Association"
商业
faker.commerce.color() //=> "black"
faker.commerce.department(maximum: Int, fixedAmount: Bool) //=> "Music"
faker.commerce.productName() //=> "Awesome Wooden Hat"
faker.commerce.price() // 90.5
公司
faker.company.name() //=> "Adams Inc"
faker.company.suffix() //=> "Inc"
faker.company.catchPhrase() //=> "Universal software"
faker.company.bs() //=> "implement innovative methodologies"
faker.company.logo() // "http://pigment.github.io/fake-logos/logos/medium/color/1.png"
塞尔达
faker.zelda.game() //=> "Ocarina of Time"
性别
faker.gender.type() //=> "Agender"
faker.gender.binaryType() //=> "Male"
互联网
faker.internet.username(separator: String?) //=> "ida4"
faker.internet.domainName(alphaNumericOnly: Bool) //=> "example.com"
faker.internet.domainWord(alphaNumericOnly: Bool) //=> "domainword"
faker.internet.domainSuffix() //=> "com"
faker.internet.email() // => "[email protected]"
faker.internet.freeEmail() //=> "gmail.com"
faker.internet.safeEmail() //=> "[email protected]"
faker.internet.password(minimumLength: Int, maximumLength: Int) //=> "e2dddhwd1g5qhvhgfi"
faker.internet.ipV4Address() //=> "24.29.18.175"
faker.internet.ipV6Address() //=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df"
faker.internet.url() //=> "http://example.com/ida4"
faker.internet.image() //=> "http://lorempixel.com/320/200"
faker.internet.templateImage() //=> "http://dummyimage.com/320x200/000000/ffffff"
faker.internet.hashtag() //=> "#art"
洛伦兹文本
faker.lorem.word() //=> "repellendus"
faker.lorem.words(amount: Int) //=> ["dolores", "adipisci", "nesciunt"]
faker.lorem.character() //=> "a"
faker.lorem.characters(amount: Int) // Default = 255
faker.lorem.sentence(wordsAmount: Int) // Default = 4
faker.lorem.sentences(amount: Int) // Default = 3
faker.lorem.paragraph(sentencesAmount: Int) // Default = 3
faker.lorem.paragraphs(amount: Int) // Default = 3
名称
faker.name.name() //=> "Ida Adams"
faker.name.firstName() //=> "Ida"
faker.name.lastName() //=> "Adams"
faker.name.prefix() //=> "Mrs."
faker.name.suffix() //=> "PhD"
faker.name.title() //=> "Lead"
数字
faker.number.randomBool() //=> true or false
faker.number.randomInt() //=> some Int between 0 and 1000
faker.number.randomInt(min: -100, max:50) //=> some Int between -100 and 50
faker.number.randomFloat() //=> some Float between 0 and 1000
faker.number.randomFloat(min: -10.4, max:50) //=> some Float between -10.4 and 50
faker.number.randomCGFloat() //=> some CGFloat between 0 and 1000
faker.number.randomCGFloat(min: 42.42, max:86) //=> some CGFloat between -42.42 and 86
faker.number.randomDouble() //=> some Double between 0 and 1000
faker.number.randomDouble(min: 0, max:1) //=> some Double between 0 and 1
faker.number.increasingUniqueId() //=> every call returns an unique int
电话号码
faker.phoneNumber.phoneNumber() //=> "1-333-333-3333"
faker.phoneNumber.cellPhone() //=> "333-333-3333"
faker.phoneNumber.areaCode() //=> "201"
faker.phoneNumber.exchangeCode() //=> "201"
faker.phoneNumber.subscriberNumber() //=> "1234"
faker.phoneNumber.numberExtension(length: Int) // "123"
团队
faker.team.name() //=> "bats"
faker.team.creature() //=> "Alabama bats"
faker.team.state() // => "Alabama"
银行
faker.bank.name() //=> "ABN AMRO CORPORATE FINANCE LIMITED"
faker.bank.swiftBic() //=> "AAFMGB21"
faker.bank.iban() // => "NL45BUNQ2209931378"
faker.bank.bban() //=> ABNA0136468471
霍比特人
faker.hobbit.character() //=> "Bilbo Baggins"
faker.hobbit.thorinsCompany() //=> "Thorin Oakenshield"
faker.hobbit.quote() // => "Do you wish me a good morning, or mean that it is a good morning whether I want it or not; or that you feel good this morning; or that it is a morning to be good on?"
faker.hobbit.location() //=> "Bree"
汽车
faker.car.brand() //=> "BMW"
编程语言
faker.programming_language.name() //=> "Elixir"
faker.programming_language.creator() //=> "José Valim"
车辆
faker.vehicle.manufacture() //=> "Volkswagen"
faker.vehicle.make() //=> "BMW"
faker.vehicle.colors() //=> "Red"
火腿
faker.ham.name() //=> "Taylor Ham"
房子
faker.house.furniture() //=> "chair"
faker.house.room() //=> "living room"
安装
Fakery可以通过CocoaPods获取。要安装它,只需在Podfile中添加以下行即可
pod 'Fakery'
或者使用Swift包管理器
let package = Package(
//…
dependencies[
.package(name: "Fakery", url: "https://github.com/vadymmarkov/Fakery", from: "5.0.0"))
],
targets: [
.target(name: "Foo", dependencies: ["Fakery"]
]
)
使用Swift包管理器需要Swift >=5.3。
贡献
有关贡献的指南,请参阅我们的playbook。
作者
Vadym Markov, [email protected]
许可
Fakery遵循MIT许可。更多信息请参阅LICENSE文件。