Sha256: aedd40525600dbee49ed9bde2a561f5274a0d8fdd5237b59633ed17e69d10dbf

Contents?: true

Size: 1016 Bytes

Versions: 124

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

class ReeNumber::NumberToDelimited
  include Ree::FnDSL

  fn :number_to_delimited

  DEFAULTS = {
    separator: ".",
    delimiter: ",",
    pattern: /(\d)(?=(\d\d\d)+(?!\d))/
  }.freeze

  doc(<<~DOC)
    Converts Integer, Float or String to a String with delimiter.

    number_to_delimited(12345678)
    # => "12,345,678"
    
    number_to_delimited(123456.789)
    # => "123,456.789"

    number_to_delimited(123456.78, pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/))
    # => "1,23,456.78"
  DOC
  
  contract(
    Or[Integer, Float, String], 
    Ksplat[
      separator?: String, 
      delimiter?: String, 
      pattern?: Regexp
    ] => String
  )
  def call(number, **opts)
    options = DEFAULTS.merge(opts)
    left, right = number.to_s.split(".")
    
    delimited_left = left.gsub(options[:pattern] || options[:pattern]) do |digit_to_delimit|
      "#{digit_to_delimit}#{options[:delimiter]}"
    end

    [delimited_left, right].compact.join(options[:separator])
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb