-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.rb
More file actions
40 lines (36 loc) · 1.21 KB
/
autocomplete.rb
File metadata and controls
40 lines (36 loc) · 1.21 KB
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
# autocomplete.rb
#
# Autocompletion is case-sensitive by default. To do case-insensitive
# autcompletion, set case_sensitive to false:
# ac.case_sensitive = false
#
# By default, the autocomplete function matches only entries that start with "word". If you'd like to instead search the
# corpos for matches of "word" in any position, run autocomplete with the optional second argument "true"
#
# Example:
# ac = Autocomplete.new(['can', 'Canada', 'soda', 'sock', 'song', 'sound', 'sand', 'zebra'])
#
# ca = ac.autocomplete('ca') #=> can, Canada (if case_sensitive is false)
# so = ac.autocomplete('so') #=> soda, sock, song, sound
# s = ac.autocomplete('s') #=> soda, sock, song, sound, sand
# ou_any = ac.autocomplete('ou', true) #=> sound
class Autocomplete
attr_accessor :case_sensitive
def initialize(corpus, case_sensitive=true)
@corpus = corpus
@case_sensitive = case_sensitive
end
def autocomplete(word, anypos=false)
@corpus.reject do |contained|
pos = transform_case?(contained.to_s).index(transform_case?(word.to_s))
anypos ? pos.nil? : pos != 0
end
end
private
def transform_case?(word)
unless @case_sensitive
word = word.upcase
end
word
end
end