cldr-plurals-runtime-rb 1.0.1 → 1.1.0
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 +5 -5
- data/Gemfile +1 -2
- data/README.md +1 -0
- data/cldr-plurals-runtime-rb.gemspec +1 -2
- data/lib/cldr-plurals/ruby-runtime/str_num.rb +100 -0
- data/lib/cldr-plurals/ruby-runtime/version.rb +1 -1
- data/lib/cldr-plurals/ruby_runtime.rb +30 -50
- data/spec/ruby_runtime_spec.rb +29 -1
- data/spec/spec_helper.rb +1 -2
- metadata +9 -9
- data/History.txt +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6541eea401e167602c7129ea578f7f8f74bbd469001a5d1908a68379aec40282
|
4
|
+
data.tar.gz: 0f33eea5ee8731118f19cc057fe0e02702f4c494d7455b75d453bef9e3d71e3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9018fc7fa6f67dfb5675d66381e0536dab8b4a8685068ef051148fa53ef0b24654b17bf9ff23be47b3fe340ead020d94e96e9e3eb1b4e696048c76f63e5efd3
|
7
|
+
data.tar.gz: f72d23ad45b5a7cda6756c69ec61242feeb58e6eeafc3e825b54a3cf5fe41a7db277dd24ca184b6dd0a759ef7fdd7cdd72f17cc52df76c0153167d339184b564
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -27,6 +27,7 @@ The CLDR data set contains [plural information](http://unicode.org/cldr/trac/bro
|
|
27
27
|
| w | number of visible fraction digits in n, without trailing zeros.|
|
28
28
|
| f | visible fractional digits in n, with trailing zeros. |
|
29
29
|
| t | visible fractional digits in n, without trailing zeros. |
|
30
|
+
| c/e | compact decimal exponent value |
|
30
31
|
|
31
32
|
cldr-plurals-runtime-rb is an implementation of these calculations in Ruby. You can use them via the `CldrPlurals::RubyRuntime` module. Note that all methods take a stringified number as input:
|
32
33
|
|
@@ -7,11 +7,10 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.authors = ["Cameron Dutro"]
|
8
8
|
s.email = ["camertron@gmail.com"]
|
9
9
|
s.homepage = "http://github.com/camertron"
|
10
|
+
s.license = "MIT"
|
10
11
|
|
11
12
|
s.description = s.summary = 'Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).'
|
12
|
-
|
13
13
|
s.platform = Gem::Platform::RUBY
|
14
|
-
s.has_rdoc = true
|
15
14
|
|
16
15
|
s.require_path = 'lib'
|
17
16
|
s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "LICENSE.txt", "README.md", "Rakefile", "cldr-plurals-runtime-rb.gemspec"]
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module CldrPlurals
|
2
|
+
module RubyRuntime
|
3
|
+
|
4
|
+
class StrNum
|
5
|
+
def self.from_string(str)
|
6
|
+
sign, int, frac, exp = str.scan(/([+-])?(\d+)\.?(\d+)?[eEcC]?(-?\d+)?/).flatten
|
7
|
+
new(sign || '', int, frac || '', exp.to_i)
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :sign, :int, :frac, :exp
|
11
|
+
|
12
|
+
def initialize(sign, int, frac, exp)
|
13
|
+
@sign = sign
|
14
|
+
@int = int
|
15
|
+
@frac = frac
|
16
|
+
@exp = exp
|
17
|
+
end
|
18
|
+
|
19
|
+
def int_val
|
20
|
+
int.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def frac_val
|
24
|
+
# remove leading zeroes
|
25
|
+
frac.sub(/\A0*/, '')
|
26
|
+
end
|
27
|
+
|
28
|
+
def apply_exp
|
29
|
+
new_int, new_frac = if exp > 0
|
30
|
+
shift_right(exp)
|
31
|
+
else
|
32
|
+
shift_left(exp.abs)
|
33
|
+
end
|
34
|
+
|
35
|
+
self.class.new(sign, new_int || '', new_frac || '', 0)
|
36
|
+
end
|
37
|
+
|
38
|
+
def abs
|
39
|
+
self.class.new('', int, frac, exp)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
''.tap do |result|
|
44
|
+
result << "#{sign}#{int}"
|
45
|
+
result << ".#{frac}" unless frac.empty?
|
46
|
+
result << "e#{exp}" if exp != 0
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def strip
|
51
|
+
self.class.new(sign, int, frac.sub(/0+\z/, ''), exp)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_val
|
55
|
+
str = to_s
|
56
|
+
|
57
|
+
if str.include?('.')
|
58
|
+
str.to_f
|
59
|
+
else
|
60
|
+
str.to_i * (10 ** exp)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def shift_right(n)
|
67
|
+
return [int, frac] if exp == 0
|
68
|
+
|
69
|
+
new_int = "#{int}#{frac[0...n]}"
|
70
|
+
|
71
|
+
if n - frac.length > 0
|
72
|
+
new_int << '0' * (n - frac.length)
|
73
|
+
end
|
74
|
+
|
75
|
+
new_frac = frac[n..-1]
|
76
|
+
new_frac = (!new_frac || new_frac.empty?) && !frac.empty? ? '0' : new_frac
|
77
|
+
|
78
|
+
[new_int, new_frac]
|
79
|
+
end
|
80
|
+
|
81
|
+
def shift_left(n)
|
82
|
+
return [int, frac] if exp == 0
|
83
|
+
|
84
|
+
new_frac = ''
|
85
|
+
|
86
|
+
if n - int.length > 0
|
87
|
+
new_frac << '0' * (n - int.length)
|
88
|
+
end
|
89
|
+
|
90
|
+
new_frac << int[0...n]
|
91
|
+
new_frac << frac
|
92
|
+
new_int = int[n..-1]
|
93
|
+
new_int = !new_int || new_int.empty? ? '0' : new_int
|
94
|
+
|
95
|
+
[new_int, new_frac]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
@@ -1,80 +1,60 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'cldr-plurals/ruby-runtime/str_num'
|
4
|
+
|
3
5
|
module CldrPlurals
|
4
6
|
module RubyRuntime
|
5
7
|
class << self
|
6
8
|
|
7
9
|
def build_args_for(num_str)
|
10
|
+
num = StrNum.from_string(num_str)
|
11
|
+
|
8
12
|
[
|
9
|
-
n(
|
10
|
-
t(
|
13
|
+
n(num), i(num), f(num),
|
14
|
+
t(num), v(num), w(num),
|
15
|
+
e(num)
|
11
16
|
]
|
12
17
|
end
|
13
18
|
|
14
|
-
def n(str)
|
15
|
-
to_num(
|
16
|
-
if str.include?('.')
|
17
|
-
_n(str).gsub(/([0]+\z)/, '').chomp('.')
|
18
|
-
else
|
19
|
-
_n(str)
|
20
|
-
end
|
21
|
-
)
|
22
|
-
end
|
23
|
-
|
24
|
-
def i(str)
|
25
|
-
to_num(_i(str))
|
26
|
-
end
|
27
|
-
|
28
|
-
def f(str)
|
29
|
-
to_num(_f(str))
|
30
|
-
end
|
31
|
-
|
32
|
-
def t(str)
|
33
|
-
to_num(_t(str))
|
34
|
-
end
|
35
|
-
|
36
|
-
def v(str)
|
37
|
-
to_num(_v(str))
|
38
|
-
end
|
39
|
-
|
40
|
-
def w(str)
|
41
|
-
to_num(_w(str))
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def to_num(str)
|
47
|
-
str.include?('.') ? str.to_f : str.to_i
|
48
|
-
end
|
49
|
-
|
50
19
|
# absolute value of the source number (integer and decimals).
|
51
|
-
def
|
52
|
-
|
20
|
+
def n(num)
|
21
|
+
wrap(num).abs.strip.to_val
|
53
22
|
end
|
54
23
|
|
55
24
|
# integer digits of n.
|
56
|
-
def
|
57
|
-
|
25
|
+
def i(num)
|
26
|
+
wrap(num).apply_exp.int_val
|
58
27
|
end
|
59
28
|
|
60
29
|
# visible fractional digits in n, with trailing zeros.
|
61
|
-
def
|
62
|
-
|
30
|
+
def f(num)
|
31
|
+
wrap(num).apply_exp.frac_val.to_i
|
63
32
|
end
|
64
33
|
|
65
34
|
# visible fractional digits in n, without trailing zeros.
|
66
|
-
def
|
67
|
-
|
35
|
+
def t(num)
|
36
|
+
wrap(num).apply_exp.strip.frac_val.to_i
|
68
37
|
end
|
69
38
|
|
70
39
|
# number of visible fraction digits in n, with trailing zeros.
|
71
|
-
def
|
72
|
-
|
40
|
+
def v(num)
|
41
|
+
wrap(num).apply_exp.frac.length
|
73
42
|
end
|
74
43
|
|
75
44
|
# number of visible fraction digits in n, without trailing zeros.
|
76
|
-
def
|
77
|
-
|
45
|
+
def w(num)
|
46
|
+
wrap(num).apply_exp.strip.frac_val.length
|
47
|
+
end
|
48
|
+
|
49
|
+
def e(num)
|
50
|
+
wrap(num).exp
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def wrap(str_or_num)
|
56
|
+
return str_or_num if str_or_num.is_a?(StrNum)
|
57
|
+
StrNum.from_string(str_or_num)
|
78
58
|
end
|
79
59
|
|
80
60
|
end
|
data/spec/ruby_runtime_spec.rb
CHANGED
@@ -34,6 +34,34 @@ describe CldrPlurals::RubyRuntime do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
context 'with an exponent' do
|
38
|
+
let(:num) { '1.203e1' }
|
39
|
+
|
40
|
+
it '#n returns n without trailing zeroes' do
|
41
|
+
expect(rt.n(num)).to eq(12.03)
|
42
|
+
end
|
43
|
+
|
44
|
+
it '#i returns the int value multiplied by the power' do
|
45
|
+
expect(rt.i(num)).to eq(12)
|
46
|
+
end
|
47
|
+
|
48
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
49
|
+
expect(rt.v(num)).to eq(2)
|
50
|
+
end
|
51
|
+
|
52
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
53
|
+
expect(rt.w(num)).to eq(1)
|
54
|
+
end
|
55
|
+
|
56
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
57
|
+
expect(rt.f(num)).to eq(3)
|
58
|
+
end
|
59
|
+
|
60
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
61
|
+
expect(rt.t(num)).to eq(3)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
37
65
|
context 'with a zero decimal' do
|
38
66
|
let(:num) { '1.0' }
|
39
67
|
|
@@ -162,7 +190,7 @@ describe CldrPlurals::RubyRuntime do
|
|
162
190
|
end
|
163
191
|
|
164
192
|
it '#w returns num of visible fraction digits (without zeroes)' do
|
165
|
-
expect(rt.w(num)).to eq(
|
193
|
+
expect(rt.w(num)).to eq(1)
|
166
194
|
end
|
167
195
|
|
168
196
|
it '#f returns visible fractional digits (with zeroes)' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cldr-plurals-runtime-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
|
14
14
|
email:
|
@@ -18,19 +18,20 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- Gemfile
|
21
|
-
- History.txt
|
22
21
|
- LICENSE.txt
|
23
22
|
- README.md
|
24
23
|
- Rakefile
|
25
24
|
- cldr-plurals-runtime-rb.gemspec
|
25
|
+
- lib/cldr-plurals/ruby-runtime/str_num.rb
|
26
26
|
- lib/cldr-plurals/ruby-runtime/version.rb
|
27
27
|
- lib/cldr-plurals/ruby_runtime.rb
|
28
28
|
- spec/ruby_runtime_spec.rb
|
29
29
|
- spec/spec_helper.rb
|
30
30
|
homepage: http://github.com/camertron
|
31
|
-
licenses:
|
31
|
+
licenses:
|
32
|
+
- MIT
|
32
33
|
metadata: {}
|
33
|
-
post_install_message:
|
34
|
+
post_install_message:
|
34
35
|
rdoc_options: []
|
35
36
|
require_paths:
|
36
37
|
- lib
|
@@ -45,9 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
46
|
- !ruby/object:Gem::Version
|
46
47
|
version: '0'
|
47
48
|
requirements: []
|
48
|
-
|
49
|
-
|
50
|
-
signing_key:
|
49
|
+
rubygems_version: 3.1.4
|
50
|
+
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
|
53
53
|
test_files: []
|
data/History.txt
DELETED