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 // Number format resolved options 108 { 109 const numberFormat = new Intl.NumberFormat('en-US', { style: 'percent' }); 110 const resolvedOptions = numberFormat.resolvedOptions(); 111 assert.strictEqual(resolvedOptions.locale, 'en-US'); 112 assert.strictEqual(resolvedOptions.style, 'percent'); 113 } 114 // Significant Digits 115 { 116 const loc = ['en-US']; 117 const opts = { maximumSignificantDigits: 4 }; 118 const num = 10.001; 119 const numberFormat = new Intl.NumberFormat(loc, opts).format(num); 120 assert.strictEqual(numberFormat, '10'); 121 } 122 123 const collOpts = { sensitivity: 'base', ignorePunctuation: true }; 124 const coll = new Intl.Collator(['en'], collOpts); 125 126 // Ignore punctuation 127 assert.strictEqual(coll.compare('blackbird', 'black-bird'), 0); 128 // Compare less 129 assert.strictEqual(coll.compare('blackbird', 'red-bird'), -1); 130 // Compare greater 131 assert.strictEqual(coll.compare('bluebird', 'blackbird'), 1); 132 // Ignore case 133 assert.strictEqual(coll.compare('Bluebird', 'bluebird'), 0); 134 // `ffi` ligature (contraction) 135 assert.strictEqual(coll.compare('\ufb03', 'ffi'), 0); 136 137 { 138 // Regression test for https://github.com/nodejs/node/issues/27379 139 const env = { ...process.env, LC_ALL: 'ja' }; 140 execFile( 141 process.execPath, ['-p', 'new Date().toLocaleString()'], 142 { env }, 143 common.mustSucceed() 144 ); 145 } 146 147 { 148 // Regression test for https://github.com/nodejs/node/issues/27418 149 const env = { ...process.env, LC_ALL: 'fr@EURO' }; 150 execFile( 151 process.execPath, 152 ['-p', 'new Intl.NumberFormat().resolvedOptions().locale'], 153 { env }, 154 common.mustSucceed() 155 ); 156 } 157} 158