Module: Unnatural::Scan

Defined in:
lib/unnatural/scan.rb

Class Method Summary collapse

Class Method Details

.compare(a_string, b_string) ⇒ Object



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
# File 'lib/unnatural/scan.rb', line 17

def self.compare(a_string, b_string)
  if a_string.is_a?(Array) && b_string.is_a?(Array)
    a_string = a_string.first
    b_string = b_string.first
  end

  a = StringScanner.new(a_string)
  b = StringScanner.new(b_string)

  loop do
    a_number = a.scan(/\d+/)
    b_number = b.scan(/\d+/)

    if a_number.nil? && b_number.nil?
    # move on
    elsif a_number.nil?
      return +1
    elsif b_number.nil?
      return -1
    else
      compare = a_number.to_i <=> b_number.to_i
      return compare unless compare == 0
    end

    a_segment = a.scan(/\D+/)
    b_segment = b.scan(/\D+/)

    if a_segment.nil? && b_segment.nil?
    # move on
    elsif a_segment.nil?
      return -1
    elsif b_segment.nil?
      return +1
    else
      compare = a_segment.casecmp(b_segment)
      return compare unless compare == 0
    end

    return 0 if a.eos? && b.eos?
    return -1 if a.eos?
    return +1 if b.eos?
  end
end

.sort(enumerable) ⇒ Object



5
6
7
# File 'lib/unnatural/scan.rb', line 5

def self.sort(enumerable)
  enumerable.sort { |a, b| compare(a, b) }
end

.sort_by(enumerable) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/unnatural/scan.rb', line 9

def self.sort_by(enumerable)
  raise ArgumentError, "Block expected but none given" unless block_given?
  enumerable
    .map { |e| [(yield e), e] }
    .sort { |a, b| compare(a, b) }
    .map { |ary| ary[1] }
end