blob: 2289b34e562bb7d1cbe355747ac3e5719f4c0d19 (
plain)
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
|
# frozen_string_literal: true
class GitRefsFinder
def initialize(repository, params = {})
@repository = repository
@params = params
end
protected
attr_reader :repository, :params
def search
@params[:search].presence
end
def sort
@params[:sort].presence || 'name'
end
def by_search(refs)
return refs unless search
case search
when ->(v) { v.starts_with?('^') }
filter_refs_with_prefix(refs, search.slice(1..-1))
when ->(v) { v.ends_with?('$') }
filter_refs_with_suffix(refs, search.chop)
else
matches = filter_refs_by_name(refs, search)
set_exact_match_as_first_result(matches, search)
end
end
def filter_refs_with_prefix(refs, prefix)
refs.select { |ref| ref.name.upcase.starts_with?(prefix.upcase) }
end
def filter_refs_with_suffix(refs, suffix)
refs.select { |ref| ref.name.upcase.ends_with?(suffix.upcase) }
end
def filter_refs_by_name(refs, term)
refs.select { |ref| ref.name.upcase.include?(term.upcase) }
end
def set_exact_match_as_first_result(matches, term)
exact_match_index = find_exact_match_index(matches, term)
matches.insert(0, matches.delete_at(exact_match_index)) if exact_match_index
matches
end
def find_exact_match_index(matches, term)
matches.index { |ref| ref.name.casecmp(term) == 0 }
end
end
|