This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
/
fech_utils.rb
65 lines (60 loc) · 1.75 KB
/
fech_utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Contains helper functions and static variables used by various
# Fech classes.
module FechUtils
# All supported row types pointed to regular expressions that will correctly
# match that row type in the wild. If multiple matches exist, Fech will match
# the longest regex pattern found.
ROW_TYPES = {
:hdr => /^hdr$/i,
:f1 => /^f1/i,
:f1m => /(^f1m[^a|n])/i,
:f2 => /(^f2[^4])/i,
:f24 => /(^f24[an])/i,
:f3 => /^f3[a|n|t]/i,
:f3l => /^f3l[a|n]/i,
:f3p => /(^f3p$)|(^f3p[^s|3])/i,
:f3s => /^f3s/i,
:f3p31 => /^f3p31/i,
:f3ps => /^f3ps/i,
:f3x => /^f3x/i,
:f4 => /^f4[na]/i,
:f5 => /^f5[na]/i,
:f56 => /^f56/i,
:f57 => /^f57/i,
:f6 => /(^f6$)|(^f6[^5])/i,
:f65 => /^f65/i,
:f7 => /^f7[na]/i,
:f76 => /^f76/i,
:f9 => /^f9/i,
:f91 => /^f91/i,
:f92 => /^f92/i,
:f93 => /^f93/i,
:f94 => /^f94/i,
:f99 => /^f99/i,
:sa => /^sa/i,
:sb => /^sb/i,
:sc => /^sc[^1-2]/i,
:sc1 => /^sc1/i,
:sc2 => /^sc2/i,
:sd => /^sd/i,
:se => /^se/i,
:sf => /^sf/i,
:text => /^text/i,
}
# Converts symbols and strings to Regexp objects for use in regex-keyed maps.
# Assumes that symbols should be matched literally, strings unanchored.
# @param [String,Symbol,Regexp] label the object to convert to a Regexp
def regexify(label)
if label.is_a?(Regexp)
Regexp.new(label.source, Regexp::IGNORECASE)
elsif label.is_a?(Symbol)
if ROW_TYPES.keys.include?(label)
ROW_TYPES[label]
else
Regexp.new("^#{label.to_s}$", Regexp::IGNORECASE)
end
else
Regexp.new(Regexp.escape(label.to_s), Regexp::IGNORECASE)
end
end
end