Sha256: 94457b76e375a233fdc9c08d176159ad0e9b290f29c244ab7f151e4e7b9fde44

Contents?: true

Size: 751 Bytes

Versions: 13

Compression:

Stored size: 751 Bytes

Contents

class ImageOptim
  # Present size in readable form as fixed length string
  module Space
    SIZE_SYMBOLS = %w[B K M G T P E].freeze
    BASE = 1024.0
    PRECISION = 1
    LENGTH = 4 + PRECISION + 1

    EMPTY_SPACE = ' ' * LENGTH

    def self.space(size)
      case size
      when 0, nil
        EMPTY_SPACE
      else
        log_denominator = Math.log(size.abs) / Math.log(BASE)
        degree = [log_denominator.floor, SIZE_SYMBOLS.length - 1].min
        number_string = if degree.zero?
          size.to_s
        else
          denominator = BASE**degree
          number = size / denominator
          format("%.#{PRECISION}f", number)
        end
        "#{number_string}#{SIZE_SYMBOLS[degree]}".rjust(LENGTH)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
image_optim-0.26.3 lib/image_optim/space.rb
image_optim-0.26.2 lib/image_optim/space.rb
discourse_image_optim-0.26.2 lib/image_optim/space.rb
discourse_image_optim-0.26.1 lib/image_optim/space.rb
image_optim-0.26.1 lib/image_optim/space.rb
image_optim-0.26.0 lib/image_optim/space.rb
image_optim-0.25.0 lib/image_optim/space.rb
discourse_image_optim-0.24.5 lib/image_optim/space.rb
image_optim-0.24.3 lib/image_optim/space.rb
discourse_image_optim-0.24.4 lib/image_optim/space.rb
image_optim-0.24.2 lib/image_optim/space.rb
image_optim-0.24.1 lib/image_optim/space.rb
image_optim-0.24.0 lib/image_optim/space.rb