short_scale 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c5ce2c81bf4c7ec35a1e4373515738ce646e830
4
- data.tar.gz: 98e60a39fb421131c7e9f4c6c9b24c7dfec0ece3
3
+ metadata.gz: ade3c051fef9388f4ed7481cabe77f192ba935c7
4
+ data.tar.gz: bd5f3901df0430c003544303cba6413207cba78f
5
5
  SHA512:
6
- metadata.gz: 34bf990fd9968ec0e3a7544003ee62dc04103d07071ace51cdbddba014ff6460be81e34a5caa0f2c4031d456051c85e6ad61ee38debce9e3fd3b93f1179f109b
7
- data.tar.gz: 18a277ae8063ee5a2b57ddee2412b74ebc3245fc28bb8ea804f8f5338d983d65cc390260a21aecf57c2c87b9cb40a50c2e083a82d0683c681591d352eb0d1fe9
6
+ metadata.gz: 278e4c436fd2cda493856809a704c51d6fedcafc661140efdad241d82a8ebd9d64fb5997b954e378bacfcba708e857ad42c16dce833ce8a82af0f45d00fac8a5
7
+ data.tar.gz: 35613b3786fe92417ef4d7747526661bd9017810b02bdfb1607ec8a40a09513ca099b87a4b23871bf4566d79be31341516cbcb73d6730aa474db83b0a8be028d
@@ -0,0 +1,20 @@
1
+ module ShortScale
2
+ class Formatter
3
+ def initialize(config = {})
4
+ @base = config[:base]
5
+ @tier = config[:tier]
6
+ end
7
+
8
+ def format(number)
9
+ value(number) + @tier[:unit]
10
+ end
11
+
12
+ private
13
+ def value(number)
14
+ # This truncates n to a single decimal and remove .0 when necessary
15
+ n = number / @base ** (@tier[:magnitude] - 1)
16
+
17
+ "#{n.to_i / @base.to_f}".sub('.0', '')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module ShortScale
2
+ class Prettifier
3
+ def initialize(config = {})
4
+ # Default base is decimal
5
+ @base = config[:base] || 10
6
+
7
+ @tier_calc = ShortScale::TierCalculator.new(config)
8
+ end
9
+
10
+ def prettify(number)
11
+ negative = number < 0
12
+ number *= -1 if negative
13
+
14
+ tier = @tier_calc.tier(number)
15
+ formatter = ShortScale::Formatter.new(base: @base, tier: tier)
16
+
17
+ "#{'-' if negative}" + formatter.format(number)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ module ShortScale
2
+ class TierCalculator
3
+ @@default_config = {
4
+ base: 10,
5
+ tiers: {
6
+ 6 => 'M',
7
+ 9 => 'B',
8
+ 12 => 'T'
9
+ }
10
+ }
11
+
12
+ def initialize(config = {})
13
+ # Prioritize provided configuration over default
14
+ @base = config[:base] || @@default_config[:base]
15
+ @tiers = config[:tiers] || @@default_config[:tiers]
16
+
17
+ # Add zero magnitude tier (units) if not provided
18
+ @tiers[0] ||= ''
19
+ end
20
+
21
+ # Calculates the tier for a given number
22
+ def tier(number)
23
+ _magnitude = magnitude(number)
24
+
25
+ # If provided number is lesser than 1 then magnitude defaults to zero
26
+ if _magnitude > 0
27
+ tier_magnitude = @tiers.keys.select { |limit| limit <= _magnitude }.max
28
+ else
29
+ tier_magnitude = 0
30
+ end
31
+
32
+ {
33
+ magnitude: tier_magnitude,
34
+ unit: @tiers[tier_magnitude]
35
+ }
36
+ end
37
+
38
+ private
39
+ # Calculates the magnitude of a number if a given base
40
+ def magnitude(number)
41
+ Math.log10(number) / Math.log10(@base)
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module ShortScale
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: short_scale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Érik Escobedo"
@@ -59,17 +59,11 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".travis.yml"
64
- - Gemfile
65
- - LICENSE.txt
66
- - README.md
67
- - Rakefile
68
- - bin/console
69
- - bin/setup
70
62
  - lib/short_scale.rb
63
+ - lib/short_scale/formatter.rb
64
+ - lib/short_scale/prettifier.rb
65
+ - lib/short_scale/tier_calculator.rb
71
66
  - lib/short_scale/version.rb
72
- - short_scale.gemspec
73
67
  homepage: http://github.com/erik-escobedo/short_scale
74
68
  licenses:
75
69
  - MIT
@@ -77,11 +71,7 @@ metadata: {}
77
71
  post_install_message:
78
72
  rdoc_options: []
79
73
  require_paths:
80
- - lib/short_scale/formatter.rb
81
- - lib/short_scale/prettifier.rb
82
- - lib/short_scale/tier_calculator.rb
83
- - lib/short_scale/version.rb
84
- - lib/short_scale.rb
74
+ - lib
85
75
  required_ruby_version: !ruby/object:Gem::Requirement
86
76
  requirements:
87
77
  - - ">="
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.2
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in short_scale.gemspec
4
- gemspec
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 Érik Escobedo
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,80 +0,0 @@
1
- # ShortScale
2
-
3
- ShortScale provides a `Prettifier` class capable of display numbers in a
4
- "Short Scale" format (https://en.wikipedia.org/wiki/Long_and_short_scales).
5
-
6
- ## Installation
7
-
8
- Add this line to your application's Gemfile:
9
-
10
- ```ruby
11
- gem 'short_scale'
12
- ```
13
-
14
- And then execute:
15
-
16
- $ bundle
17
-
18
- Or install it yourself as:
19
-
20
- $ gem install short_scale
21
-
22
- ## Usage
23
-
24
- ```ruby
25
- require 'short_scale'
26
-
27
- # If no configuration is provided, it will use 'M' for 10^6, 'B' for 10^9 and 'T' for
28
- # 10^12 and bigger numbers.
29
- prettifier = ShortScale::Prettifier.new
30
-
31
- prettifier.prettify(1_000_000) #=> '1M'
32
- prettifier.prettify(512.5) #=> '512.5'
33
- prettifier.prettify(2_345_000.123) #=> '2.3M'
34
- prettifier.prettify(123_456_789_123) #=> '123.4B'
35
- prettifier.prettify(9_123_456_789_123) #=> '9.1T'
36
-
37
-
38
- # You can also provide a set of tiers in the following format
39
- prettifier = ShortScale::Prettifier.new(tiers: {
40
- 0 => ' units',
41
- 3 => ' thousands',
42
- 6 => ' millions',
43
- 9 => ' billions'
44
- })
45
-
46
- prettifier.prettify(1_000_000) #=> '1 millions' (no support for singular/plural inflections)
47
- prettifier.prettify(5125.4) #=> '5.1 thousands'
48
- prettifier.prettify(345.123) #=> '345.1 units'
49
- prettifier.prettify(123_456_789_123) #=> '123.4 billions'
50
- prettifier.prettify(9_123_456_789_123) #=> '9123.4 billions' (since trillions tier was not provided)
51
-
52
- # It also works with bases different than 10
53
- prettifier = ShortScale::Prettifier.new(base: 2, tiers: {
54
- 0 => 'B',
55
- 10 => 'KB',
56
- 20 => 'MB',
57
- 30 => 'GB',
58
- 40 => 'TB'
59
- })
60
-
61
- prettifier.prettify(1023) #=> '1023B'
62
- prettifier.prettify(1024) #=> '1KB'
63
- prettifier.prettify(1048064) #=> '1023.5KB'
64
- prettifier.prettify(1048576) #=> '1MB'
65
- prettifier.prettify(9_123_456_789_123) #=> '8TB'
66
- ```
67
-
68
- ## Development
69
-
70
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
71
-
72
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
-
74
- ## Contributing
75
-
76
- 1. Fork it ( https://github.com/erik-escobedo/short_scale/fork )
77
- 2. Create your feature branch (`git checkout -b my-new-feature`)
78
- 3. Commit your changes (`git commit -am 'Add some feature'`)
79
- 4. Push to the branch (`git push origin my-new-feature`)
80
- 5. Create a new Pull Request
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "short_scale"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here
data/short_scale.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'short_scale/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "short_scale"
8
- spec.version = ShortScale::VERSION
9
- spec.authors = ["Érik Escobedo"]
10
- spec.email = ["erik@codigojade.com"]
11
-
12
- spec.summary = %q{Prettify big numbers using Short Scale format.}
13
- spec.description = %q{Prettify big numbers using Short Scale format.}
14
- spec.homepage = "http://github.com/erik-escobedo/short_scale"
15
- spec.license = "MIT"
16
-
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = Dir["lib/**/*.rb"]
21
-
22
- spec.add_development_dependency "bundler", "~> 1.9"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.3"
25
- end