• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

datafaker-net/datafaker: Brings the popular ruby faker gem to Java and Kotlin

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

datafaker-net/datafaker

开源软件地址(OpenSource Url):

https://github.com/datafaker-net/datafaker

开源编程语言(OpenSource Language):

Java 86.7%

开源软件介绍(OpenSource Introduction):

Data Faker

Maven Status License codecov

This library is a modern port of java-faker, built on Java 8, with up to date libraries and several newly added Fake Generators.

This library generates fake data, similar to other fake data generators, such as:

It's useful when you're developing a new project and need some pretty data for showcase.

Usage

In the pom.xml, add the following fragment to the dependencies section:

<dependency>
    <groupId>net.datafaker</groupId>
    <artifactId>datafaker</artifactId>
    <version>1.4.0</version>
</dependency>

For Gradle users, add the following to your build.gradle file.

dependencies {
    implementation 'net.datafaker:datafaker:1.4.0'
}

You can also use the snapshot version (1.5.0-SNAPSHOT), which automatically gets published after every push to the master branch of this repository. Binary repository URL for snapshots download is https://s01.oss.sonatype.org/content/repositories/snapshots/.

Get started

In your Java code:

Faker faker = new Faker();

String name = faker.name().fullName(); // Miss Samanta Schmidt
String firstName = faker.name().firstName(); // Emory
String lastName = faker.name().lastName(); // Barton

String streetAddress = faker.address().streetAddress(); // 60018 Sawayn Brooks Suite 449

Or in your Kotlin code:

val faker = Faker()

val name = faker.name().fullName() // Miss Samanta Schmidt
val firstName = faker.name().firstName() // Emory
val lastName = faker.name().lastName() // Barton

val streetAddress = faker.address().streetAddress() // 60018 Sawayn Brooks Suite 449

Expressions

Faker faker = new Faker();
faker.expression("#{letterify 'test????test'}"); // testqwastest
faker.expression("#{numerify '#test#'}"); // 3test5
faker.expression("#{templatify 'test','t','q','@'}"); // @esq
faker.expression("#{examplify 'test'}"); // ghjk
faker.expression("#{regexify '[a-z]{4,10}'}"); // wbevoa
faker.expression("#{options.option '23','2','5','$','%','*'}"); // *
faker.expression("#{date.birthday 'yy DDD hh:mm:ss'}"); // 61 327 08:11:45
faker.expression("#{csv '1','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");
// "name_column","last_name_column"
// "Sabrina","Kihn"
faker.expression("#{json 'person','#{json ''first_name'',''#{Name.first_name}'',''last_name'',''#{Name.last_name}''}','address','#{json ''country'',''#{Address.country}'',''city'',''#{Address.city}''}'}");
// {"person": {"first_name": "Barbie", "last_name": "Durgan"}, "address": {"country": "Albania", "city": "East Catarinahaven"}}

also more examples at https://www.datafaker.net/documentation/expressions/

Collections

Faker faker = new Faker();
List<String> names = faker.collection(
                              () -> faker.name().firstName(),
                              () -> faker.name().lastName())
                         .len(3, 5)
                         .generate();
System.out.println(names);
// [Skiles, O'Connell, Lorenzo, West]

more examples about that at https://www.datafaker.net/documentation/collections/

File formats

csv

String csv = Format.toCsv(faker.collection(faker::name).build())
                .headers(() -> "first_name", () -> "last_name")
                .columns(Name::firstName, Name::lastName)
                .separator(" ; ")
                .limit(2).build().get();
// "first_name" ; "last_name"
// "Kimberely" ; "Considine"
// "Mariela" ; "Krajcik"

json

Faker faker = new Faker();
String json = Format.toJson(
                faker.collection(faker::name)
                  .maxLen(2)
                  .build())
              .set("firstName", Name::firstName)
              .set("lastName", Name::lastName)
              .build()
              .generate();
// [{"firstName": "Oleta", "lastName": "Toy"},
// {"firstName": "Gerard", "lastName": "Windler"}]

More complex examples and other formats like YAML, XML could be found at https://www.datafaker.net/documentation/file-formats/

Custom provider

Add your own custom provider in your app following steps from https://www.datafaker.net/documentation/custom-providers/

Documentation

https://www.datafaker.net/documentation/providers/

Contributions

See CONTRIBUTING.md

Providers

  • Address
  • Ancient
  • Animal
  • App
  • Appliance
  • Aqua Teen Hunger Force
  • Artist
  • Australia
  • Avatar
  • Aviation
  • AWS
  • Babylon5
  • Back To The Future
  • Barcode
  • Basketball
  • Battlefield1
  • Beer
  • Big Bang Theory
  • Blood Type
  • Bojack Horseman
  • Book
  • Bool
  • Bossa Nova
  • Breaking Bad
  • BrooklynNineNine
  • Buffy
  • Business
  • CNPJ Brazilian National Registry of Legal Entities
  • CPF Brazilian individual taxpayer registry identification
  • Camera
  • Cat
  • ChuckNorris
  • Code
  • Coin
  • Color
  • Commerce
  • Company
  • Country
  • CreditCardType
  • Crypto
  • Currency
  • DateAndTime
  • Demographic
  • Departed
  • Dessert
  • Device
  • Disease
  • Dog
  • DcComics
  • Domain
  • DragonBall
  • Dune
  • Durations
  • Educator
  • EldenRing
  • ElderScrolls
  • ElectricalComponents
  • EnglandFootBall
  • Esports
  • Famous Last Words
  • File
  • Finance
  • Food
  • Formula 1 (

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap