fat_core 4.10.0 → 4.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fat_core/string.rb +21 -5
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/string_spec.rb +18 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8607195b8f5ef6d04df1f438fbd59d3a22ca3a730d0d5bb9430ff37166d07174
|
4
|
+
data.tar.gz: e86aedc2aafebf6fc02efdf6f3f35f39ad93059b08a8bc4e635190e5964f2942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94983a4893c2c4882b360236f488c3a741ea8927488a69785ae4a7cc8f93612e11aa1f6899b359fcc2bb25742d6f062d3c76f5cbd75dfc64c24edba44be45755
|
7
|
+
data.tar.gz: 9700b48e6c73ec9ec49ed7da22679b4ea90676be71011b4c4c912cb4b53c2d613bb1712b8c59f440990570a33ffe12ab51ec0986ace906638408b3bab85f59d9
|
data/lib/fat_core/string.rb
CHANGED
@@ -245,28 +245,44 @@ module FatCore
|
|
245
245
|
#
|
246
246
|
# 1. Remove all periods, commas, apostrophes, and asterisks (the punctuation
|
247
247
|
# characters) from both self and `matcher`,
|
248
|
-
# 2. Treat ':' in the matcher as the equivalent of '.*' in a
|
249
|
-
# expression, that is, match anything in self,
|
250
|
-
# 3.
|
251
|
-
#
|
248
|
+
# 2. Treat internal ':' in the matcher as the equivalent of '.*' in a
|
249
|
+
# regular expression, that is, match anything in self,
|
250
|
+
# 3. Treat leading ':' in the matcher as anchoring the match to the
|
251
|
+
# beginning of the target string,
|
252
|
+
# 4. Treat ending ':' in the matcher as anchoring the match to the
|
253
|
+
# end of the target string,
|
254
|
+
# 5. Require each component to match the beginning of a word boundary
|
255
|
+
# 6. Ignore case in the match
|
252
256
|
#
|
253
257
|
# @example
|
254
258
|
# "St. Luke's Hospital".fuzzy_match('st lukes') #=> 'St Lukes'
|
255
259
|
# "St. Luke's Hospital".fuzzy_match('luk:hosp') #=> 'Lukes Hosp'
|
256
260
|
# "St. Luke's Hospital".fuzzy_match('st:spital') #=> 'St Lukes Hospital'
|
257
261
|
# "St. Luke's Hospital".fuzzy_match('st:laks') #=> nil
|
262
|
+
# "St. Luke's Hospital".fuzzy_match(':lukes') #=> nil
|
263
|
+
# "St. Luke's Hospital".fuzzy_match('lukes:hospital:') #=> 'Lukes Hospital'
|
258
264
|
#
|
259
265
|
# @param matcher [String] pattern to test against where ':' is wildcard
|
260
266
|
# @return [String] the unpunctuated part of self that matched
|
261
267
|
# @return [nil] if self did not match matcher
|
262
268
|
def fuzzy_match(matcher)
|
263
269
|
# Remove periods, asterisks, commas, and apostrophes
|
264
|
-
matcher = matcher.gsub(/[\*.,']/, '')
|
270
|
+
matcher = matcher.strip.gsub(/[\*.,']/, '')
|
271
|
+
if matcher.start_with?(':')
|
272
|
+
begin_anchor = true
|
273
|
+
matcher.sub!(/\A:/, '')
|
274
|
+
end
|
275
|
+
if matcher.end_with?(':')
|
276
|
+
end_anchor = true
|
277
|
+
matcher.sub!(/:\z/, '')
|
278
|
+
end
|
265
279
|
target = gsub(/[\*.,']/, '')
|
266
280
|
matchers = matcher.split(/[: ]+/)
|
267
281
|
regexp_string = matchers.map { |m| ".*?\\b#{Regexp.escape(m)}.*?" }.join('[: ]')
|
268
282
|
regexp_string.sub!(/^\.\*\?/, '')
|
269
283
|
regexp_string.sub!(/\.\*\?$/, '')
|
284
|
+
regexp_string.sub!(/\A/, '\\A') if begin_anchor
|
285
|
+
regexp_string.sub!(/\z/, '\\z') if end_anchor
|
270
286
|
regexp = /#{regexp_string}/i
|
271
287
|
matched_text =
|
272
288
|
if (match = regexp.match(target))
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/string_spec.rb
CHANGED
@@ -269,6 +269,21 @@ the people, for the people, shall not perish from the earth."
|
|
269
269
|
expect('Hello:world'.fuzzy_match('hel:ox')).to be_falsy
|
270
270
|
end
|
271
271
|
|
272
|
+
it 'requires end-anchor for ending colon' do
|
273
|
+
expect('Hello, to the world'.fuzzy_match('hel:world:')).to eq('Hello to the world')
|
274
|
+
expect('Hello, to the world today'.fuzzy_match('to:world:')).to be_nil
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'requires start-anchor for leading colon' do
|
278
|
+
expect('Hello, to the world'.fuzzy_match(':hel:the')).to eq('Hello to the')
|
279
|
+
expect('Hello, to the world today'.fuzzy_match(':world:today')).to be_nil
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'requires start-anchor and end-anchor for leading and ending colon' do
|
283
|
+
expect('Hello, to the world'.fuzzy_match(':hel:world:')).to eq('Hello to the world')
|
284
|
+
expect('Hello, to the world today'.fuzzy_match('hel:world:')).to be_falsy
|
285
|
+
end
|
286
|
+
|
272
287
|
it 'should return the matched text' do
|
273
288
|
expect('Hello:world'.fuzzy_match('hel')).to eq('Hel')
|
274
289
|
expect('Hello:world'.fuzzy_match('hel:wor')).to eq('Hello:wor')
|
@@ -280,6 +295,9 @@ the people, for the people, shall not perish from the earth."
|
|
280
295
|
expect('St Lukes'.fuzzy_match('st. luke\'s')).to eq('St Lukes')
|
281
296
|
expect('St Lukes, Inc.'.fuzzy_match('st luke inc')).to eq('St Lukes Inc')
|
282
297
|
expect('E*TRADE'.fuzzy_match('etrade')).to eq('ETRADE')
|
298
|
+
# Does not recognize non-alphanumerics as start of string.
|
299
|
+
expect('The 1 Dollar Store'.fuzzy_match('1 stor')).to be_truthy
|
300
|
+
expect('The $1 Dollar Store'.fuzzy_match('$1 stor')).to be_falsy
|
283
301
|
end
|
284
302
|
end
|
285
303
|
end
|