Sha256: e8c94642d434d6ec86bbe488250248355725d0ac12cd5a7a6fa8a42b2668bece

Contents?: true

Size: 676 Bytes

Versions: 1

Compression:

Stored size: 676 Bytes

Contents

# frozen_string_literal: true

module AppInfo
  # AppInfo Util
  module Util
    FILE_SIZE_UNITS = %w[B KB MB GB TB].freeze

    def self.file_size(file, humanable)
      file_size = File.size(file)
      humanable ? size_to_humanable(file_size) : file_size
    end

    def self.size_to_humanable(number)
      if number.to_i < 1024
        exponent = 0
      else
        max_exp = FILE_SIZE_UNITS.size - 1
        exponent = (Math.log(number) / Math.log(1024)).to_i
        exponent = max_exp if exponent > max_exp
        number = format('%<number>.2f', number: (number / (1024**exponent.to_f)))
      end

      "#{number} #{FILE_SIZE_UNITS[exponent]}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
app-info-2.0.0.beta4 lib/app_info/util.rb