Skip to content

Commit

Permalink
Merge pull request #19 from blocoio/fix-regex
Browse files Browse the repository at this point in the history
Fix regex and add jvm annotations
  • Loading branch information
sdsantos authored Jun 7, 2023
2 parents 9487813 + 540f339 commit 9e07c32
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 56 deletions.
42 changes: 21 additions & 21 deletions lib/src/main/kotlin/io/bloco/faker/Faker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ import java.io.IOException
import java.io.InputStream

class Faker(val locale: String = DEFAULT_LOCALE) {
val address: Address
val app: App
val avatar: Avatar
val book: Book
val bool: Bool
val business: Business
val color: Color
val commerce: Commerce
val company: Company
val date: Date
val food: Food
val internet: Internet
val lorem: Lorem
val name: Name
val number: Number
val placeholdit: Placeholdit
val phoneNumber: PhoneNumber
val slackEmoji: SlackEmoji
val team: Team
val time: Time
val university: University
@JvmField val address: Address
@JvmField val app: App
@JvmField val avatar: Avatar
@JvmField val book: Book
@JvmField val bool: Bool
@JvmField val business: Business
@JvmField val color: Color
@JvmField val commerce: Commerce
@JvmField val company: Company
@JvmField val date: Date
@JvmField val food: Food
@JvmField val internet: Internet
@JvmField val lorem: Lorem
@JvmField val name: Name
@JvmField val number: Number
@JvmField val placeholdit: Placeholdit
@JvmField val phoneNumber: PhoneNumber
@JvmField val slackEmoji: SlackEmoji
@JvmField val team: Team
@JvmField val time: Time
@JvmField val university: University
private val data: FakerData

init {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/kotlin/io/bloco/faker/FakerComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ abstract class FakerComponent(private val data: FakerData) {

companion object {
private const val DIGIT_SYMBOL = "#"
private const val PARSE_REGEXP = "#\\{(.+?)}"
private const val PARSE_REGEXP = "\\#\\{(.+?)\\}"
}
}
3 changes: 2 additions & 1 deletion lib/src/main/kotlin/io/bloco/faker/components/Avatar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import io.bloco.faker.FakerComponent
import io.bloco.faker.FakerData

class Avatar(data: FakerData) : FakerComponent(data) {
@JvmOverloads
fun image(
slug: String? = "image",
size: String = "300x300",
format: String = "png",
set: String = "set1",
bgset: String? = null
): String {
require(size.matches("^\\d+x\\d+$".toRegex())) {
require(size.matches("^[0-9]+x[0-9]+$".toRegex())) {
"Size should be specified in format 300x300"
}
require(SUPPORTED_FORMATS.contains(format)) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/kotlin/io/bloco/faker/components/Bool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.bloco.faker.FakerComponent
import io.bloco.faker.FakerData

class Bool(data: FakerData) : FakerComponent(data) {
@JvmOverloads
fun bool(trueRatio: Float = 0.5f): Boolean {
return Math.random() < trueRatio
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/main/kotlin/io/bloco/faker/components/Commerce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Commerce(data: FakerData) : FakerComponent(data) {
return call("color.colorName")
}

@JvmOverloads
fun department(max: Int = 3, fixedAmount: Boolean = false): String {
val num = if (fixedAmount) max else randomHelper.number(max) + 1
val categories = getCategories(num)
return if (num > 1) mergeCategories(categories) else categories.first()
}


fun productName(): String {
return listOf(
fetch("commerce.product_name.adjective"),
Expand All @@ -25,15 +25,16 @@ class Commerce(data: FakerData) : FakerComponent(data) {
).joinToString(" ")
}


fun material(): String {
return fetch("commerce.product_name.material")
}

@JvmOverloads
fun price(min: Int = 0, max: Int = 100): BigDecimal {
return BigDecimal(randomHelper.range(min, max)).setScale(2, RoundingMode.HALF_UP)
}

@JvmOverloads
fun promotionCode(digits: Int = 6): String {
return listOf(
fetch("commerce.promotion_code.adjective"),
Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/kotlin/io/bloco/faker/components/Date.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.Date
import kotlin.random.Random

class Date(fakerData: FakerData) : FakerComponent(fakerData) {
private var formatter: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
private val formatter = SimpleDateFormat("yyyy-MM-dd")

fun between(from: String, to: String): Date {
return try {
Expand All @@ -28,14 +28,17 @@ class Date(fakerData: FakerData) : FakerComponent(fakerData) {
return between(from.toLocalDate(), to.toLocalDate())
}

@JvmOverloads
fun forward(numberOfDays: Int = DEFAULT_NUM_OF_DAYS): Date {
return between(today(), today().plusDays(numberOfDays.toLong()))
}

@JvmOverloads
fun backward(numberOfDays: Int = DEFAULT_NUM_OF_DAYS): Date {
return between(today().minusDays(numberOfDays.toLong()), today())
}

@JvmOverloads
fun birthday(minAge: Int = DEFAULT_MIN_AGE, maxAge: Int = DEFAULT_MAX_AGE): Date {
return between(today().plusYears(minAge.toLong()), today().plusYears(maxAge.toLong()))
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/main/kotlin/io/bloco/faker/components/Internet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import io.bloco.faker.FakerComponent
import io.bloco.faker.FakerData

class Internet(data: FakerData) : FakerComponent(data) {

@JvmOverloads
fun email(name: String? = null): String {
return userName(name) + "@" + domainName()
}

@JvmOverloads
fun freeEmail(name: String? = null): String {
return userName(name) + "@" + fetch("internet.free_email")
}

@JvmOverloads
fun safeEmail(name: String? = null): String {
return userName(name) + "@" + SAFE_EMAIL_HOST + randomHelper.sample(SAFE_EMAIL_TLDS)
}

@JvmOverloads
fun userName(specifier: String? = null, separators: List<String> = DEFAULT_SEPARATORS): String {
val separator = randomHelper.sample(separators)
return when {
Expand All @@ -34,6 +39,7 @@ class Internet(data: FakerData) : FakerComponent(data) {
}
}

@JvmOverloads
fun password(
minLength: Int = PASSWORD_MIN_LENGTH,
maxLength: Int = PASSWORD_MAX_LENGTH,
Expand Down Expand Up @@ -105,6 +111,7 @@ class Internet(data: FakerData) : FakerComponent(data) {
return "http://$host$path"
}

@JvmOverloads
fun slug(
words: List<String> = listOf(fetch("lorem.words"), fetch("lorem.words")),
glue: String = randomHelper.sample(DEFAULT_SLUG_GLUE)
Expand Down
8 changes: 7 additions & 1 deletion lib/src/main/kotlin/io/bloco/faker/components/Lorem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Lorem(data: FakerData?) : FakerComponent(data!!) {
return fetch("lorem.supplemental")
}

@JvmOverloads
fun words(num: Int = DEFAULT_NUM, supplemental: Boolean = DEFAULT_SUPPLEMENTAL): List<String> {
return List(num) {
if (supplemental && randomHelper.randBoolean()) {
Expand All @@ -30,6 +31,7 @@ class Lorem(data: FakerData?) : FakerComponent(data!!) {
return List(count) { character() }.joinToString("")
}

@JvmOverloads
fun sentence(
wordCount: Int = DEFAULT_WORD_COUNT,
supplemental: Boolean = DEFAULT_SUPPLEMENTAL,
Expand All @@ -41,13 +43,15 @@ class Lorem(data: FakerData?) : FakerComponent(data!!) {
.replaceFirstChar { it.titlecase() } + "."
}

@JvmOverloads
fun sentences(
sentenceCount: Int = DEFAULT_SENTENCE_COUNT,
supplemental: Boolean = DEFAULT_SUPPLEMENTAL
): List<String> {
return List(sentenceCount) { sentence(DEFAULT_WORD_COUNT, supplemental) }
}

@JvmOverloads
fun paragraph(
sentenceCount: Int = DEFAULT_SENTENCE_COUNT,
supplemental: Boolean = DEFAULT_SUPPLEMENTAL,
Expand All @@ -58,13 +62,15 @@ class Lorem(data: FakerData?) : FakerComponent(data!!) {
return sentences.joinToString(" ")
}

@JvmOverloads
fun paragraphs(
paragraphCount: Int = DEFAULT_PARAGRAPH_COUNT,
supplemental: Boolean = DEFAULT_SUPPLEMENTAL
): List<String> {
return List(paragraphCount) { paragraph(DEFAULT_SENTENCE_COUNT, supplemental) }
}

@JvmOverloads
fun question(
wordCount: Int = 4,
supplemental: Boolean = false,
Expand All @@ -77,7 +83,7 @@ class Lorem(data: FakerData?) : FakerComponent(data!!) {
return "$sentence?"
}


@JvmOverloads
fun questions(questionsCount: Int = 3, supplemental: Boolean = false): List<String> {
return List(questionsCount) { question(3, supplemental) }
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/kotlin/io/bloco/faker/components/Name.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Name(data: FakerData) : FakerComponent(data) {
return parse(fetch("name.name_with_middle"))
}

@Suppress("UNCHECKED_CAST")
fun jobTitles(): List<String> {
return getMap("name", "title")["job"] as List<String>
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/main/kotlin/io/bloco/faker/components/Number.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Number(data: FakerData) : FakerComponent(data) {
return "%0${digits}x".format(num)
}

@JvmOverloads
fun between(from: Int = DEFAULT_FROM, to: Int = DEFAULT_TO): Int {
return randomHelper.range(from, to)
}
Expand All @@ -56,6 +57,7 @@ class Number(data: FakerData) : FakerComponent(data) {
return randomHelper.range(from, to)
}

@JvmOverloads
fun positive(from: Int = DEFAULT_FROM, to: Int = DEFAULT_TO): Int {
return abs(between(from, to))
}
Expand All @@ -68,6 +70,7 @@ class Number(data: FakerData) : FakerComponent(data) {
return abs(between(from, to))
}

@JvmOverloads
fun negative(from: Int = DEFAULT_FROM, to: Int = DEFAULT_TO): Int {
return abs(between(from, to)) * -1
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/main/kotlin/io/bloco/faker/components/Placeholdit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.bloco.faker.FakerComponent
import io.bloco.faker.FakerData

class Placeholdit(data: FakerData) : FakerComponent(data) {

@JvmOverloads
fun image(
size: String = "300x300",
format: String = "png",
Expand Down Expand Up @@ -32,7 +34,7 @@ class Placeholdit(data: FakerData) : FakerComponent(data) {
companion object {
val SUPPORTED_FORMATS = listOf("png", "jpg", "jpeg", "gif")
private const val PLACEHOLDER_URL = "https://placehold.it/"
private const val SIZE_REGEX = "^\\d+x\\d+$"
private const val HEX_REGEX = "(^[\\da-fA-F]{3}$|^[\\da-fA-F]{6}$)(?!.*[^\\da-fA-F])"
private const val SIZE_REGEX = "^[0-9]+x[0-9]+$"
private const val HEX_REGEX = "((?:^[0-9a-fA-F]{3}$)|(?:^[0-9a-fA-F]{6}$)){1}(?!.*[^0-9a-fA-F])"
}
}
2 changes: 2 additions & 0 deletions lib/src/main/kotlin/io/bloco/faker/components/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Time(fakerData: FakerData) : FakerComponent(fakerData) {
return between(from, to, Period.All)
}

@JvmOverloads
fun forward(numberOfDays: Int, period: Period = Period.All): Date {
return randomTime(now(), period).plusDays(numberOfDays.toLong()).toDate()
}

@JvmOverloads
fun backward(numberOfDays: Int, period: Period = Period.All): Date {
return randomTime(now(), period).minusDays(numberOfDays.toLong()).toDate()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/kotlin/io/bloco/faker/helpers/StringHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StringHelper {
}

fun camelToSnake(input: String): String {
return input.replace("(?<=[a-zA-Z\\d])(\\p{Upper})".toRegex()) { matcher ->
return input.replace("(?<=\\w)(\\p{Upper})".toRegex()) { matcher ->
"_" + matcher.groupValues[1].lowercase(Locale.getDefault())
}.lowercase(Locale.getDefault())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/kotlin/io/bloco/faker/components/ColorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ColorTest {

@Test
fun hexColor() {
assertTrue(faker.color.hexColor().matches(Regex("^#[\\da-f]{6}\$")))
assertTrue(faker.color.hexColor().matches(Regex("^#[0-9a-f]{6}$")))
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/kotlin/io/bloco/faker/components/FoodTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FoodTest {
@Test
fun measurement() {
assertNotNull(faker.food.measurement())
assertTrue(faker.food.measurement().matches(Regex("[\\d/]+ \\w+")))
assertTrue(faker.food.measurement().matches(Regex("[\\d\\/]+ \\w+")))
}

@Test
Expand Down
Loading

0 comments on commit 9e07c32

Please sign in to comment.