Sha256: f5944c3bfcf4cb42ef5688f7b9710a9ffa5bf7b94a22a1afa5cf3101fad34a0c

Contents?: true

Size: 781 Bytes

Versions: 10

Compression:

Stored size: 781 Bytes

Contents

# frozen_string_literal: true

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 == 0
          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

10 entries across 10 versions & 1 rubygems

Version Path
image_optim-0.31.2 lib/image_optim/space.rb
image_optim-0.31.1 lib/image_optim/space.rb
image_optim-0.31.0 lib/image_optim/space.rb
image_optim-0.30.0 lib/image_optim/space.rb
image_optim-0.29.0 lib/image_optim/space.rb
image_optim-0.28.0 lib/image_optim/space.rb
image_optim-0.27.1 lib/image_optim/space.rb
image_optim-0.27.0 lib/image_optim/space.rb
image_optim-0.26.5 lib/image_optim/space.rb
image_optim-0.26.4 lib/image_optim/space.rb