From 6a2833f1879d3b57f28255400af9913481db2791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=C5=9Fk=C4=B1n=20Gedik?= Date: Tue, 12 Jan 2016 09:00:47 +0200 Subject: [PATCH] regexify --- src/faker.cr | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/faker.cr b/src/faker.cr index 4b2cf62..b2a2599 100644 --- a/src/faker.cr +++ b/src/faker.cr @@ -1,8 +1,9 @@ -require "./faker/data.cr" +require "./data.cr" require "./faker/*" module Faker def self.numerify(number_string) + number_string = number_string as String number_string.sub(/#/) { (rand(9) + 1).to_s }.gsub(/#/) { rand(10).to_s } end @@ -14,6 +15,19 @@ module Faker self.letterify(self.numerify(string)) end + def self.regexify(re) + 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 * ($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] }).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 } + end + def self.fetch(data) d = data as Array d.sample