format-as-code-or-name.js (2671B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 //----------------------------------------------------------------------------- 7 var BUGNUMBER = 1093421; 8 var summary = 9 "new Intl.NumberFormat(..., { style: 'currency', currency: '...', " + 10 "currencyDisplay: 'name' or 'code' }) should have behavior other than " + 11 "throwing"; 12 13 print(BUGNUMBER + ": " + summary); 14 15 //----------------------------------------------------------------------------- 16 17 // Test that currencyDisplay: "code" behaves correctly and doesn't throw. 18 19 var usdCodeOptions = 20 { 21 style: "currency", 22 currency: "USD", 23 currencyDisplay: "code", 24 minimumFractionDigits: 0, 25 maximumFractionDigits: 0, 26 }; 27 var usDollarsCode = new Intl.NumberFormat("en-US", usdCodeOptions); 28 assertEq(/USD/.test(usDollarsCode.format(25)), true); 29 30 // ISO 4217 currency codes are formed from an ISO 3166-1 alpha-2 country code 31 // followed by a third letter. ISO 3166 guarantees that no country code 32 // starting with "X" will ever be assigned. Stepping carefully around a few 33 // 4217-designated special "currencies", XQQ will never have a representation. 34 // Thus, yes: this really is specified to work, as unrecognized or unsupported 35 // codes pass into the string unmodified. 36 var xqqCodeOptions = 37 { 38 style: "currency", 39 currency: "XQQ", 40 currencyDisplay: "code", 41 minimumFractionDigits: 0, 42 maximumFractionDigits: 0, 43 }; 44 var xqqMoneyCode = new Intl.NumberFormat("en-US", xqqCodeOptions); 45 assertEq(/XQQ/.test(xqqMoneyCode.format(25)), true); 46 47 // Test that currencyDisplay: "name" behaves without throwing. (Unlike the two 48 // above tests, the results here aren't guaranteed as the name is 49 // implementation-defined.) 50 var usdNameOptions = 51 { 52 style: "currency", 53 currency: "USD", 54 currencyDisplay: "name", 55 minimumFractionDigits: 0, 56 maximumFractionDigits: 0, 57 }; 58 var usDollarsName = new Intl.NumberFormat("en-US", usdNameOptions); 59 assertEq(usDollarsName.format(25), "25 US dollars"); 60 61 // But if the implementation doesn't recognize the currency, the provided code 62 // is used in place of a proper name, unmolested. 63 var xqqNameOptions = 64 { 65 style: "currency", 66 currency: "XQQ", 67 currencyDisplay: "name", 68 minimumFractionDigits: 0, 69 maximumFractionDigits: 0, 70 }; 71 var xqqMoneyName = new Intl.NumberFormat("en-US", xqqNameOptions); 72 assertEq(/XQQ/.test(xqqMoneyName.format(25)), true); 73 74 if (typeof reportCompare === "function") 75 reportCompare(true, true);