1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const { execFile } = require('child_process'); 26 27// Does node think that i18n was enabled? 28let enablei18n = process.config.variables.v8_enable_i18n_support; 29if (enablei18n === undefined) { 30 enablei18n = 0; 31} 32 33// Returns true if no specific locale ids were configured (i.e. "all") 34// Else, returns true if loc is in the configured list 35// Else, returns false 36function haveLocale(loc) { 37 const locs = process.config.variables.icu_locales.split(','); 38 return locs.includes(loc); 39} 40 41// Always run these. They should always pass, even if the locale 42// param is ignored. 43assert.strictEqual('Ç'.toLocaleLowerCase('el'), 'ç'); 44assert.strictEqual('Ç'.toLocaleLowerCase('tr'), 'ç'); 45assert.strictEqual('Ç'.toLowerCase(), 'ç'); 46 47assert.strictEqual('ç'.toLocaleUpperCase('el'), 'Ç'); 48assert.strictEqual('ç'.toLocaleUpperCase('tr'), 'Ç'); 49assert.strictEqual('ç'.toUpperCase(), 'Ç'); 50 51if (!common.hasIntl) { 52 const erMsg = 53 `"Intl" object is NOT present but v8_enable_i18n_support is ${enablei18n}`; 54 assert.strictEqual(enablei18n, 0, erMsg); 55 common.skip('Intl tests because Intl object not present.'); 56} else { 57 const erMsg = 58 `"Intl" object is present but v8_enable_i18n_support is ${ 59 enablei18n}. Is this test out of date?`; 60 assert.strictEqual(enablei18n, 1, erMsg); 61 62 // Construct a new date at the beginning of Unix time 63 const date0 = new Date(0); 64 65 // Use the GMT time zone 66 const GMT = 'Etc/GMT'; 67 68 // Construct an English formatter. Should format to "Jan 70" 69 const dtf = new Intl.DateTimeFormat(['en'], { 70 timeZone: GMT, 71 month: 'short', 72 year: '2-digit' 73 }); 74 75 // If list is specified and doesn't contain 'en' then return. 76 if (process.config.variables.icu_locales && !haveLocale('en')) { 77 common.printSkipMessage( 78 'detailed Intl tests because English is not listed as supported.'); 79 // Smoke test. Does it format anything, or fail? 80 console.log(`Date(0) formatted to: ${dtf.format(date0)}`); 81 return; 82 } 83 84 // Check casing 85 { 86 assert.strictEqual('I'.toLocaleLowerCase('tr'), 'ı'); 87 } 88 89 // Check with toLocaleString 90 { 91 const localeString = dtf.format(date0); 92 assert.strictEqual(localeString, 'Jan 70'); 93 } 94 // Options to request GMT 95 const optsGMT = { timeZone: GMT }; 96 97 // Test format 98 { 99 const localeString = date0.toLocaleString(['en'], optsGMT); 100 assert.strictEqual(localeString, '1/1/1970, 12:00:00 AM'); 101 } 102 // number format 103 { 104 const numberFormat = new Intl.NumberFormat(['en']).format(12345.67890); 105 assert.strictEqual(numberFormat, '12,345.679'); 106 } 107 // If list is specified and doesn't contain 'en-US' then return. 108 if (process.config.variables.icu_locales && !haveLocale('en-US')) { 109 common.printSkipMessage('detailed Intl tests because American English is ' + 110 'not listed as supported.'); 111 return; 112 } 113 // Number format resolved options 114 { 115 const numberFormat = new Intl.NumberFormat('en-US', { style: 'percent' }); 116 const resolvedOptions = numberFormat.resolvedOptions(); 117 assert.strictEqual(resolvedOptions.locale, 'en-US'); 118 assert.strictEqual(resolvedOptions.style, 'percent'); 119 } 120 // Significant Digits 121 { 122 const loc = ['en-US']; 123 const opts = { maximumSignificantDigits: 4 }; 124 const num = 10.001; 125 const numberFormat = new Intl.NumberFormat(loc, opts).format(num); 126 assert.strictEqual(numberFormat, '10'); 127 } 128 129 const collOpts = { sensitivity: 'base', ignorePunctuation: true }; 130 const coll = new Intl.Collator(['en'], collOpts); 131 132 // Ignore punctuation 133 assert.strictEqual(coll.compare('blackbird', 'black-bird'), 0); 134 // Compare less 135 assert.strictEqual(coll.compare('blackbird', 'red-bird'), -1); 136 // Compare greater 137 assert.strictEqual(coll.compare('bluebird', 'blackbird'), 1); 138 // Ignore case 139 assert.strictEqual(coll.compare('Bluebird', 'bluebird'), 0); 140 // `ffi` ligature (contraction) 141 assert.strictEqual(coll.compare('\ufb03', 'ffi'), 0); 142 143 { 144 // Regression test for https://github.com/nodejs/node/issues/27379 145 const env = { ...process.env, LC_ALL: 'ja' }; 146 execFile( 147 process.execPath, ['-p', 'new Date().toLocaleString()'], 148 { env }, 149 common.mustSucceed() 150 ); 151 } 152 153 { 154 // Regression test for https://github.com/nodejs/node/issues/27418 155 const env = { ...process.env, LC_ALL: 'fr@EURO' }; 156 execFile( 157 process.execPath, 158 ['-p', 'new Intl.NumberFormat().resolvedOptions().locale'], 159 { env }, 160 common.mustSucceed() 161 ); 162 } 163} 164