Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose currency display names #73

Merged
merged 5 commits into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
pkg/
reports/
Gemfile.lock
.ruby-version

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ fmt.format(1337); // 1.337%
fmt.format(1337, {precision: 2}); // 1.337,00%
```

#### More on Currencies

If you're looking for a list of supported currencies, use the `Currencies` function:

```javascript
# all supported currency codes
TwitterCldr.Currencies.currency_codes() # ["ADP", "AED", "AFA", "AFN", ... ]

# data for a specific currency code
TwitterCldr.Currencies.for_code("CAD") # {currency: "CAD", name: "Canadian dollar", cldr_symbol: "CA$", symbol: "$", code_points: [36]}
```

### Plural Rules

Some languages, like English, have "countable" nouns. You probably know this concept better as "plural" and "singular", i.e. the difference between "strawberry" and "strawberries". Other languages, like Russian, have three plural forms: one (numbers ending in 1), few (numbers ending in 2, 3, or 4), and many (everything else). Still other languages like Japanese don't use countable nouns at all.
Expand Down
16 changes: 2 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,8 @@ def run_tests
puts " `npm install jasmine-node -g`"
exit 1
else
puts `jasmine-node #{File.dirname(__FILE__)} --junitreport`
doc_files = Dir.glob(File.join(File.dirname(__FILE__), "reports/**"))
failures = 0

doc_files.each do |doc_file|
doc = REXML::Document.new(File.read(doc_file))
failures = doc.elements.to_a("testsuites/testsuite").inject(0) do |sum, element|
sum + element.attributes["failures"].to_i
end
end

if failures > 0
exit 1
end
system "jasmine-node #{File.dirname(__FILE__)}"
exit $?.exitstatus
end
end

Expand Down
12 changes: 7 additions & 5 deletions lib/assets/javascripts/twitter_cldr/core.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2012 Twitter, Inc
# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

class TwitterCldr.Currencies
@currencies = `{{{currencies}}}`

@currency_codes: ->
@codes ||= (data.code for _, data of @currencies)
@codes ||= (code for code, _ of @currencies)

@for_code: (currency_code) ->
result = null
Expand All @@ -16,6 +16,8 @@ class TwitterCldr.Currencies
cldr_symbol: data.cldr_symbol
symbol: data.symbol
currency: data.currency
name: data.name
code_points: data.code_points
break

result
2 changes: 2 additions & 0 deletions spec/js/numbers/currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ describe("CurrencyFormatter", function() {
});

it("should use the cldr_symbol for the corresponding currency code if use_cldr_code is specified", function() {
var old_currencies = TwitterCldr.Currencies.currencies;
TwitterCldr.Currencies.currencies = {
"JPY": {symbol: "¥", cldr_symbol: "YEN", currency: "JPY", "name": "Japanese yen"}
};
expect(formatter.format(12, {currency: "JPY", use_cldr_symbol: true})).toEqual("YEN12");
TwitterCldr.Currencies.currencies = old_currencies;
});

it("overrides the default precision", function() {
Expand Down
4 changes: 1 addition & 3 deletions spec/js/parsers/number_parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ describe("NumberParser", function() {
});

it("re-raises any unexpected errors", function() {
expect(function() {
parser.try_parse({});
}).toThrow(new Error("Object #<Object> has no method 'split'"));
expect(function() { parser.try_parse({}) }).toThrow();
});

it("parses zero correctly", function() {
Expand Down
43 changes: 43 additions & 0 deletions spec/js/shared/currencies.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2012 Twitter, Inc
// http://www.apache.org/licenses/LICENSE-2.0

var TwitterCldr = require('../../../lib/assets/javascripts/twitter_cldr/core.js');
var data = require('../../../lib/assets/javascripts/twitter_cldr/en.js');
TwitterCldr.set_data(data);

describe("Currencies", function() {
describe(".currency_codes", function() {
it("returns a list of currency codes", function() {
var codes = TwitterCldr.Currencies.currency_codes();
expect(codes).toContain('USD');
expect(codes).toContain('JPY');
expect(codes).toContain('PEN');
});
});

describe(".for_code", function() {
it("looks up and returns information for USD currency", function() {
var usd = TwitterCldr.Currencies.for_code('USD');
expect(usd).toEqual({
country: 'USD',
cldr_symbol: '$',
symbol: '$',
currency: 'USD',
name: 'US dollar',
code_points: [36]
});
});

it("looks up and returns information for INR currency", function() {
var inr = TwitterCldr.Currencies.for_code('INR');
expect(inr).toEqual({
country: 'INR',
cldr_symbol: '₹',
symbol: '₨',
currency: 'INR',
name: 'Indian rupee',
code_points: [8360]
});
});
});
});