cldr-plurals-runtime-js 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +13 -0
- data/History.txt +3 -0
- data/README.md +71 -0
- data/Rakefile +18 -0
- data/cldr-plurals-runtime-js.gemspec +20 -0
- data/lib/cldr-plurals/javascript-runtime/version.rb +5 -0
- data/lib/cldr-plurals/javascript_runtime.js +78 -0
- data/lib/cldr-plurals/javascript_runtime.rb +21 -0
- data/spec/javascript_runtime_spec.rb +207 -0
- data/spec/spec_helper.rb +9 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca051f23ae6f1bc2bfcf02127fea20ffea73ef84
|
4
|
+
data.tar.gz: ab1c76440979b47534037e61e1cea2418af7bdad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f1d20a5e984352849a3fff3788e70bc225df96caf31563cfb3bb365c36ad803e3024ca8bc75c496109239219fd8d5adc4414efc4a7036564903fd94a965e111
|
7
|
+
data.tar.gz: 2bd8143495a80457757dc4a8e420d1bb9780e8aa02728cfe1519cd9b2a22fcc531e51872582378f35df2d6ac76af1cfa31fe50b32a055fcacfe7aac174cdb288
|
data/Gemfile
ADDED
data/History.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
cldr-plurals-runtime-js
|
2
|
+
=================
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/camertron/cldr-plurals-runtime-js.svg?branch=master)](http://travis-ci.org/camertron/cldr-plurals-runtime-js)
|
5
|
+
|
6
|
+
Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
`gem install cldr-plurals-runtime-js`
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require 'cldr-plurals/javascript_runtime'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Functionality
|
19
|
+
|
20
|
+
The CLDR data set contains [plural information](http://unicode.org/cldr/trac/browser/tags/release-26-d04/common/supplemental/plurals.xml) for numerous languages in an expression-based [format](http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) defined by Unicode's TR35. The document describes how to determine the various parts of a number and how to use those parts to determine the plural rule. The parts as they appear in TR35 are:
|
21
|
+
|
22
|
+
| Symbol | Value |
|
23
|
+
|:-------|----------------------------------------------------------------|
|
24
|
+
| n | absolute value of the source number (integer and decimals). |
|
25
|
+
| i | integer digits of n. |
|
26
|
+
| v | number of visible fraction digits in n, with trailing zeros. |
|
27
|
+
| w | number of visible fraction digits in n, without trailing zeros.|
|
28
|
+
| f | visible fractional digits in n, with trailing zeros. |
|
29
|
+
| t | visible fractional digits in n, without trailing zeros. |
|
30
|
+
|
31
|
+
cldr-plurals-runtime-js is an implementation of these calculations in Javascript, packaged as a rubygem. You can get the source code for the runtime by querying the `CldrPlurals::JavascriptRuntime` module:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
CldrPlurals::JavascriptRuntime.source
|
35
|
+
```
|
36
|
+
|
37
|
+
The source code simply contains a single Javascript object. It isn't packaged as a CommonJs module or anything fancy like that - it's just a straight js object. This makes it easier to test in Ruby. Also, since all the users of this runtime currently just copy the source into larger projects, there's no need for a package. Let me know if you want one <3
|
38
|
+
|
39
|
+
When evaluated inside a Javascript environment like V8 (via [therubyracer](https://github.com/cowboyd/therubyracer)), the runtime can be passed to [cldr-plurals](https://github.com/camertron/cldr-plurals) to determine the plural form for a number:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'v8'
|
43
|
+
require 'cldr-plurals'
|
44
|
+
require 'cldr-plurals/javascript_runtime'
|
45
|
+
|
46
|
+
context = V8::Context.new
|
47
|
+
runtime = context.eval(CldrPlurals::JavascriptRuntime.source)
|
48
|
+
|
49
|
+
rules = CldrPlurals::Compiler::RuleList.new(:ru).tap do |rule_list|
|
50
|
+
rule_list.add_rule(:one, 'v = 0 and i % 10 = 1 and i % 100 != 11')
|
51
|
+
rule_list.add_rule(:few, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14')
|
52
|
+
rule_list.add_rule(:many, 'v = 0 and i % 10 = 0 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 11..14')
|
53
|
+
end
|
54
|
+
|
55
|
+
js_code = rules.to_code(:javascript)
|
56
|
+
js_obj = context.eval(js_code)
|
57
|
+
|
58
|
+
js_obj.call('3', runtime) # => 'few'
|
59
|
+
```
|
60
|
+
|
61
|
+
## Requirements
|
62
|
+
|
63
|
+
No external requirements.
|
64
|
+
|
65
|
+
## Running Tests
|
66
|
+
|
67
|
+
`bundle exec rake` should do the trick. Alternatively you can run `bundle exec rspec`, which does the same thing.
|
68
|
+
|
69
|
+
## Authors
|
70
|
+
|
71
|
+
* Cameron C. Dutro: http://github.com/camertron
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
require 'rubygems/package_task'
|
8
|
+
|
9
|
+
require './lib/cldr-plurals/javascript_runtime'
|
10
|
+
|
11
|
+
Bundler::GemHelper.install_tasks
|
12
|
+
|
13
|
+
task :default => :spec
|
14
|
+
|
15
|
+
desc 'Run specs'
|
16
|
+
RSpec::Core::RakeTask.new do |t|
|
17
|
+
t.pattern = './spec/**/*_spec.rb'
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), 'lib')
|
2
|
+
require 'cldr-plurals/javascript-runtime/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "cldr-plurals-runtime-js"
|
6
|
+
s.version = ::CldrPlurals::JAVASCRIPT_RUNTIME_VERSION
|
7
|
+
s.authors = ["Cameron Dutro"]
|
8
|
+
s.email = ["camertron@gmail.com"]
|
9
|
+
s.homepage = "http://github.com/camertron"
|
10
|
+
|
11
|
+
s.description = s.summary = 'Javascript runtime methods for CLDR plural rules (see camertron/cldr-plurals).'
|
12
|
+
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.has_rdoc = true
|
15
|
+
|
16
|
+
s.add_development_dependency 'therubyracer', '0.12.0'
|
17
|
+
|
18
|
+
s.require_path = 'lib'
|
19
|
+
s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "README.md", "Rakefile", "cldr-plurals-runtime-js.gemspec"]
|
20
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
(function() {
|
2
|
+
this.buildArgsFor = function(num_str) {
|
3
|
+
return [
|
4
|
+
this.n(num_str), this.i(num_str), this.f(num_str),
|
5
|
+
this.t(num_str), this.v(num_str), this.w(num_str)
|
6
|
+
];
|
7
|
+
}
|
8
|
+
|
9
|
+
this.n = function(str) {
|
10
|
+
if (str.indexOf('.') > -1) {
|
11
|
+
return this.toNum(this._n(str).replace(/([0]+\.$)/, ''));
|
12
|
+
} else {
|
13
|
+
return this.toNum(this._n(str));
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
this.i = function(str) {
|
18
|
+
return this.toNum(this._i(str));
|
19
|
+
}
|
20
|
+
|
21
|
+
this.f = function(str) {
|
22
|
+
return this.toNum(this._f(str));
|
23
|
+
}
|
24
|
+
|
25
|
+
this.t = function(str) {
|
26
|
+
return this.toNum(this._t(str));
|
27
|
+
}
|
28
|
+
|
29
|
+
this.v = function(str) {
|
30
|
+
return this.toNum(this._v(str));
|
31
|
+
}
|
32
|
+
|
33
|
+
this.w = function(str) {
|
34
|
+
return this.toNum(this._w(str));
|
35
|
+
}
|
36
|
+
|
37
|
+
// private
|
38
|
+
|
39
|
+
this.toNum = function(str) {
|
40
|
+
if (str.length == 0) {
|
41
|
+
return 0;
|
42
|
+
} else {
|
43
|
+
return str.indexOf('.') > -1 ? parseFloat(str) : parseInt(str);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
// absolute value of the source number (integer and decimals).
|
48
|
+
this._n = function(str) {
|
49
|
+
return /(-)?(.*)/.exec(str)[2];
|
50
|
+
}
|
51
|
+
|
52
|
+
/// integer digits of n.
|
53
|
+
this._i = function(str) {
|
54
|
+
return /([\d]+)(\..*)?/.exec(this._n(str))[1];
|
55
|
+
}
|
56
|
+
|
57
|
+
// visible fractional digits in n, with trailing zeros.
|
58
|
+
this._f = function(str) {
|
59
|
+
return /([\d]+\.?)(.*)/.exec(this._n(str))[2];
|
60
|
+
}
|
61
|
+
|
62
|
+
// visible fractional digits in n, without trailing zeros.
|
63
|
+
this._t = function(str) {
|
64
|
+
return this._f(str).replace(/([0]+$)/, '');
|
65
|
+
}
|
66
|
+
|
67
|
+
// number of visible fraction digits in n, with trailing zeros.
|
68
|
+
this._v = function(str) {
|
69
|
+
return this._f(str).length.toString();
|
70
|
+
}
|
71
|
+
|
72
|
+
// number of visible fraction digits in n, without trailing zeros.
|
73
|
+
this._w = function(str) {
|
74
|
+
return this._t(str).length.toString();
|
75
|
+
}
|
76
|
+
|
77
|
+
return this;
|
78
|
+
})();
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module CldrPlurals
|
4
|
+
module JavascriptRuntime
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def source
|
8
|
+
@source ||= File.read(source_file)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def source_file
|
14
|
+
@source_file ||= File.expand_path(
|
15
|
+
'./javascript_runtime.js', File.dirname(__FILE__)
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'v8'
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe CldrPlurals::JavascriptRuntime do
|
7
|
+
let(:context) { V8::Context.new }
|
8
|
+
let(:rt) do
|
9
|
+
context.eval(CldrPlurals::JavascriptRuntime.source)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'with an integer' do
|
13
|
+
let(:num) { '1' }
|
14
|
+
|
15
|
+
it '#n returns n without trailing zeroes' do
|
16
|
+
expect(rt.n(num)).to eq(1)
|
17
|
+
end
|
18
|
+
|
19
|
+
it '#i returns the int value' do
|
20
|
+
expect(rt.i(num)).to eq(1)
|
21
|
+
end
|
22
|
+
|
23
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
24
|
+
expect(rt.v(num)).to eq(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
28
|
+
expect(rt.w(num)).to eq(0)
|
29
|
+
end
|
30
|
+
|
31
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
32
|
+
expect(rt.f(num)).to eq(0)
|
33
|
+
end
|
34
|
+
|
35
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
36
|
+
expect(rt.t(num)).to eq(0)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with a zero decimal' do
|
41
|
+
let(:num) { '1.0' }
|
42
|
+
|
43
|
+
it '#n returns n without trailing zeroes' do
|
44
|
+
expect(rt.n(num)).to eq(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
it '#i returns the int value' do
|
48
|
+
expect(rt.i(num)).to eq(1)
|
49
|
+
end
|
50
|
+
|
51
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
52
|
+
expect(rt.v(num)).to eq(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
56
|
+
expect(rt.w(num)).to eq(0)
|
57
|
+
end
|
58
|
+
|
59
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
60
|
+
expect(rt.f(num)).to eq(0)
|
61
|
+
end
|
62
|
+
|
63
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
64
|
+
expect(rt.t(num)).to eq(0)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'with a double-precision zero decimal' do
|
69
|
+
let(:num) { '1.00' }
|
70
|
+
|
71
|
+
it '#n returns n without trailing zeroes' do
|
72
|
+
expect(rt.n(num)).to eq(1)
|
73
|
+
end
|
74
|
+
|
75
|
+
it '#i returns the int value' do
|
76
|
+
expect(rt.i(num)).to eq(1)
|
77
|
+
end
|
78
|
+
|
79
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
80
|
+
expect(rt.v(num)).to eq(2)
|
81
|
+
end
|
82
|
+
|
83
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
84
|
+
expect(rt.w(num)).to eq(0)
|
85
|
+
end
|
86
|
+
|
87
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
88
|
+
expect(rt.f(num)).to eq(0)
|
89
|
+
end
|
90
|
+
|
91
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
92
|
+
expect(rt.t(num)).to eq(0)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'with a non-zero decimal' do
|
97
|
+
let(:num) { '1.3' }
|
98
|
+
|
99
|
+
it '#n returns n without trailing zeroes' do
|
100
|
+
expect(rt.n(num)).to eq(1.3)
|
101
|
+
end
|
102
|
+
|
103
|
+
it '#i returns the int value' do
|
104
|
+
expect(rt.i(num)).to eq(1)
|
105
|
+
end
|
106
|
+
|
107
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
108
|
+
expect(rt.v(num)).to eq(1)
|
109
|
+
end
|
110
|
+
|
111
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
112
|
+
expect(rt.w(num)).to eq(1)
|
113
|
+
end
|
114
|
+
|
115
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
116
|
+
expect(rt.f(num)).to eq(3)
|
117
|
+
end
|
118
|
+
|
119
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
120
|
+
expect(rt.t(num)).to eq(3)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'with a double-precision trailing zero decimal' do
|
125
|
+
let(:num) { '1.30' }
|
126
|
+
|
127
|
+
it '#n returns n without trailing zeroes' do
|
128
|
+
expect(rt.n(num)).to eq(1.3)
|
129
|
+
end
|
130
|
+
|
131
|
+
it '#i returns the int value' do
|
132
|
+
expect(rt.i(num)).to eq(1)
|
133
|
+
end
|
134
|
+
|
135
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
136
|
+
expect(rt.v(num)).to eq(2)
|
137
|
+
end
|
138
|
+
|
139
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
140
|
+
expect(rt.w(num)).to eq(1)
|
141
|
+
end
|
142
|
+
|
143
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
144
|
+
expect(rt.f(num)).to eq(30)
|
145
|
+
end
|
146
|
+
|
147
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
148
|
+
expect(rt.t(num)).to eq(3)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'with a double-precision leading zero decimal' do
|
153
|
+
let(:num) { '1.03' }
|
154
|
+
|
155
|
+
it '#n returns n without trailing zeroes' do
|
156
|
+
expect(rt.n(num)).to eq(1.03)
|
157
|
+
end
|
158
|
+
|
159
|
+
it '#i returns the int value' do
|
160
|
+
expect(rt.i(num)).to eq(1)
|
161
|
+
end
|
162
|
+
|
163
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
164
|
+
expect(rt.v(num)).to eq(2)
|
165
|
+
end
|
166
|
+
|
167
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
168
|
+
expect(rt.w(num)).to eq(2)
|
169
|
+
end
|
170
|
+
|
171
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
172
|
+
expect(rt.f(num)).to eq(3)
|
173
|
+
end
|
174
|
+
|
175
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
176
|
+
expect(rt.t(num)).to eq(3)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'with a triple-precision decimal' do
|
181
|
+
let(:num) { '1.230' }
|
182
|
+
|
183
|
+
it '#n returns n without trailing zeroes' do
|
184
|
+
expect(rt.n(num)).to eq(1.23)
|
185
|
+
end
|
186
|
+
|
187
|
+
it '#i returns the int value' do
|
188
|
+
expect(rt.i(num)).to eq(1)
|
189
|
+
end
|
190
|
+
|
191
|
+
it '#v returns num of visible fraction digits (with zeroes)' do
|
192
|
+
expect(rt.v(num)).to eq(3)
|
193
|
+
end
|
194
|
+
|
195
|
+
it '#w returns num of visible fraction digits (without zeroes)' do
|
196
|
+
expect(rt.w(num)).to eq(2)
|
197
|
+
end
|
198
|
+
|
199
|
+
it '#f returns visible fractional digits (with zeroes)' do
|
200
|
+
expect(rt.f(num)).to eq(230)
|
201
|
+
end
|
202
|
+
|
203
|
+
it '#t returns visible fractional digits (without zeroes)' do
|
204
|
+
expect(rt.t(num)).to eq(23)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cldr-plurals-runtime-js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cameron Dutro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: therubyracer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.12.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.12.0
|
27
|
+
description: Javascript runtime methods for CLDR plural rules (see camertron/cldr-plurals).
|
28
|
+
email:
|
29
|
+
- camertron@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/cldr-plurals/javascript-runtime/version.rb
|
35
|
+
- lib/cldr-plurals/javascript_runtime.js
|
36
|
+
- lib/cldr-plurals/javascript_runtime.rb
|
37
|
+
- spec/javascript_runtime_spec.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- Gemfile
|
40
|
+
- History.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- cldr-plurals-runtime-js.gemspec
|
44
|
+
homepage: http://github.com/camertron
|
45
|
+
licenses: []
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.14
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Javascript runtime methods for CLDR plural rules (see camertron/cldr-plurals).
|
67
|
+
test_files: []
|