Module: Unnatural::Split

Defined in:
lib/unnatural/split.rb

Constant Summary collapse

SPLITTER =
/(?<=\d)(?=\D)|(?<=\D)(?=\d)/
PRED =
['0'.ord.pred.chr].freeze

Class Method Summary collapse

Class Method Details

.compare(a, b) ⇒ Object



10
11
12
# File 'lib/unnatural/split.rb', line 10

def self.compare(a, b)
  split(a) <=> split(b)
end

.sort(enumerable) ⇒ Object



6
7
8
# File 'lib/unnatural/split.rb', line 6

def self.sort(enumerable)
  enumerable.sort_by { |s| split(s) }
end

.split(string) ⇒ Object



14
15
16
17
18
# File 'lib/unnatural/split.rb', line 14

def self.split(string)
  array = string.downcase.split(SPLITTER)
  array = PRED + array if ('0'..'9').cover?(array[0])
  array.map.with_index { |e, i| i.odd? ? e.to_i : e }
end