Skip to content

Instantly share code, notes, and snippets.

@danpecher
Last active April 28, 2017 14:20
Show Gist options
  • Save danpecher/7b0c7d14b05f3d9d95d8da2cc33c7f3b to your computer and use it in GitHub Desktop.
Save danpecher/7b0c7d14b05f3d9d95d8da2cc33c7f3b to your computer and use it in GitHub Desktop.
SIZES = %w(XXS XS S M L XL 2XL XXL 3XL) + ((20..49).step(0.3333333333333).map { |n| n.round(1) } + (35..49).map { |n| n + 0.5 }).sort
SIZES_STR = SIZES.map { |s| s.to_s.gsub(/\.|0/, '') }
def self.sort_sizes(a, b)
# convert e.g. 433 to "43.3"
a = a.scan(/\d{2}|.+/).join('.') if a.is_a?(String) and a.length == 3
b = b.scan(/\d{2}|.+/).join('.') if b.is_a?(String) and b.length == 3
# convert e.g. "43.3" to 43.4
a = a.to_f unless a.match /[A-Z]+/
b = b.to_f unless b.match /[A-Z]+/
return 0 unless SIZES.include?(b)
return 1 unless SIZES.include?(a)
SIZES.index(a) <=> SIZES.index(b)
end
def self.pretify_size(size)
size = size.sub ',', ''
match = size.match(/^[0-9]{2}(\d)$/)
return size unless match
fraction = match[1]
dict = {
'3': '<span><sup>1</sup>&frasl;<sub>3</sub></span>',
'5': '<span><sup>1</sup>&frasl;<sub>2</sub></span>',
'7': '<span><sup>2</sup>&frasl;<sub>3</sub></span>'
}
size[0...-1] + dict[fraction.to_sym]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment