1
0
mirror of https://github.com/askn/faker.git synced 2023-08-10 21:13:01 +03:00
faker/src/faker.cr

79 lines
3.0 KiB
Crystal
Raw Normal View History

2016-01-12 10:00:47 +03:00
require "./data.cr"
2016-01-05 17:49:33 +03:00
require "./faker/*"
module Faker
def self.numerify(number_string)
2016-01-12 10:00:47 +03:00
number_string = number_string as String
2016-01-06 15:21:35 +03:00
number_string.sub(/#/) { (rand(9) + 1).to_s }.gsub(/#/) { rand(10).to_s }
2016-01-05 17:49:33 +03:00
end
def self.letterify(letter_string)
2016-01-06 15:21:35 +03:00
letter_string.gsub(/\?/) { ("A".."Z").to_a.sample }
2016-01-05 17:49:33 +03:00
end
def self.bothify(string)
self.letterify(self.numerify(string))
end
2016-01-06 10:23:15 +03:00
2016-01-12 10:00:47 +03:00
def self.regexify(re)
2016-01-15 11:36:23 +03:00
re = re.source if re.is_a?(Regex)
re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors
.gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1}
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample } # [12]{1,2} becomes [12] or [12][12]
.gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
.gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that"
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever)
.gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C)
.gsub("\\d") { |match| (0..9).to_a.sample }
.gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample }
2016-01-12 10:00:47 +03:00
end
2016-01-06 10:23:15 +03:00
def self.fetch(data)
2016-01-12 10:02:02 +03:00
data = data as Array
fetched = data.sample as String
if fetched.match(/^\//) && fetched.match(/\/$/) # A regex
fetched = Faker.regexify(fetched)
end
Faker.parse(fetched) as String
end
def self.parse(st)
st.gsub(/%\{([^\}]+)\}/) do |str, matches|
2016-01-15 11:36:23 +03:00
find_fn([
Address.building_number,
Address.city_prefix,
Address.city_suffix,
Address.state,
Address.street_name,
Address.street_suffix,
Company.name,
Company.suffix,
Name.first_name,
Name.last_name,
Name.name,
Name.prefix,
Name.suffix,
], $1)
2016-01-12 10:02:02 +03:00
end
end
2016-01-15 11:36:23 +03:00
macro find_fn(list, fn)
2016-01-12 10:02:02 +03:00
case {{fn}}
2016-01-15 11:36:23 +03:00
{% for l in list %}
when "{{l}}"
{{l}}
{% end %}
2016-01-12 10:02:02 +03:00
else
"Hoaydaaa"
end
2016-01-06 10:23:15 +03:00
end
2016-03-27 01:22:56 +03:00
# Generates a random value between the interval
def self.rand_in_range(from, to)
from, to = to, from if to < from
Random.new.rand(from..to)
end
2016-01-05 17:49:33 +03:00
end