|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +if (!common.hasIntl) |
| 7 | + common.skip('Intl not present.'); |
| 8 | +{ |
| 9 | + const { child } = spawnSyncAndExitWithoutError( |
| 10 | + process.execPath, |
| 11 | + [ |
| 12 | + '--icu-locale=fr-FR', |
| 13 | + '-p', |
| 14 | + 'Intl?.Collator().resolvedOptions().locale', |
| 15 | + ]); |
| 16 | + assert.strictEqual(child.stdout.toString(), 'fr-FR\n'); |
| 17 | +} |
| 18 | + |
| 19 | +{ |
| 20 | + const { child } = spawnSyncAndExitWithoutError( |
| 21 | + process.execPath, |
| 22 | + [ |
| 23 | + '--icu-locale=en-GB', |
| 24 | + '-p', |
| 25 | + 'Intl?.Collator().resolvedOptions().locale', |
| 26 | + ]); |
| 27 | + assert.strictEqual(child.stdout.toString(), 'en-GB\n'); |
| 28 | +} |
| 29 | + |
| 30 | +{ |
| 31 | + const { child } = spawnSyncAndExitWithoutError( |
| 32 | + process.execPath, |
| 33 | + [ |
| 34 | + '--icu-locale=en_GB', |
| 35 | + '-p', |
| 36 | + 'Intl?.Collator().resolvedOptions().locale', |
| 37 | + ]); |
| 38 | + assert.strictEqual(child.stdout.toString(), 'en-GB\n'); |
| 39 | +} |
| 40 | + |
| 41 | +{ |
| 42 | + const { child } = spawnSyncAndExitWithoutError( |
| 43 | + process.execPath, |
| 44 | + [ |
| 45 | + '--icu-locale=en', |
| 46 | + '-p', |
| 47 | + 'Intl?.Collator().resolvedOptions().locale', |
| 48 | + ]); |
| 49 | + assert.strictEqual(child.stdout.toString(), 'en\n'); |
| 50 | +} |
| 51 | + |
| 52 | +{ |
| 53 | + const { child } = spawnSyncAndExitWithoutError( |
| 54 | + process.execPath, |
| 55 | + [ |
| 56 | + '--icu-locale=de-DE', |
| 57 | + '-p', |
| 58 | + 'Intl?.Collator().resolvedOptions().locale', |
| 59 | + ]); |
| 60 | + assert.strictEqual(child.stdout.toString(), 'de-DE\n'); |
| 61 | +} |
| 62 | + |
| 63 | +{ |
| 64 | + const { child } = spawnSyncAndExitWithoutError( |
| 65 | + process.execPath, |
| 66 | + [ |
| 67 | + '--icu-locale=invalid', |
| 68 | + '-p', |
| 69 | + 'Intl?.Collator().resolvedOptions().locale', |
| 70 | + ]); |
| 71 | + assert.strictEqual(child.stdout.toString(), 'en-US\n'); |
| 72 | +} |
| 73 | + |
| 74 | +// NumberFormat |
| 75 | +{ |
| 76 | + const { child } = spawnSyncAndExitWithoutError( |
| 77 | + process.execPath, |
| 78 | + ['--icu-locale=de-DE', '-p', '(123456.789).toLocaleString(undefined, { style: "currency", currency: "EUR" })'] |
| 79 | + ); |
| 80 | + assert.strictEqual(child.stdout.toString(), '123.456,79\xa0€\n'); |
| 81 | +} |
| 82 | + |
| 83 | +{ |
| 84 | + const { child } = spawnSyncAndExitWithoutError( |
| 85 | + process.execPath, |
| 86 | + [ |
| 87 | + '--icu-locale=ja-JP', |
| 88 | + '-p', |
| 89 | + '(123456.789).toLocaleString(undefined, { style: "currency", currency: "JPY" })', |
| 90 | + ]); |
| 91 | + assert.strictEqual(child.stdout.toString(), '¥123,457\n'); |
| 92 | +} |
| 93 | + |
| 94 | +{ |
| 95 | + const { child } = spawnSyncAndExitWithoutError( |
| 96 | + process.execPath, |
| 97 | + [ |
| 98 | + '--icu-locale=en-IN', |
| 99 | + '-p', |
| 100 | + '(123456.789).toLocaleString(undefined, { maximumSignificantDigits: 3 })', |
| 101 | + ]); |
| 102 | + assert.strictEqual(child.stdout.toString(), '1,23,000\n'); |
| 103 | +} |
| 104 | +// DateTimeFormat |
| 105 | +{ |
| 106 | + const { child } = spawnSyncAndExitWithoutError( |
| 107 | + process.execPath, |
| 108 | + [ |
| 109 | + '--icu-locale=en-GB', |
| 110 | + '-p', |
| 111 | + 'new Date(Date.UTC(2012, 11, 20, 3, 0, 0)).toLocaleString(undefined, { timeZone: "UTC" })', |
| 112 | + ]); |
| 113 | + assert.strictEqual(child.stdout.toString(), '20/12/2012, 03:00:00\n'); |
| 114 | +} |
| 115 | + |
| 116 | +{ |
| 117 | + const { child } = spawnSyncAndExitWithoutError( |
| 118 | + process.execPath, |
| 119 | + [ |
| 120 | + '--icu-locale=ko-KR', |
| 121 | + '-p', |
| 122 | + 'new Date(Date.UTC(2012, 11, 20, 3, 0, 0)).toLocaleString(undefined, { timeZone: "UTC" })', |
| 123 | + ]); |
| 124 | + assert.strictEqual(child.stdout.toString(), '2012. 12. 20. 오전 3:00:00\n'); |
| 125 | +} |
| 126 | + |
| 127 | +// ListFormat |
| 128 | +{ |
| 129 | + const { child } = spawnSyncAndExitWithoutError( |
| 130 | + process.execPath, |
| 131 | + [ |
| 132 | + '--icu-locale=en-US', |
| 133 | + '-p', |
| 134 | + 'new Intl.ListFormat(undefined, {style: "long", type:"conjunction"}).format(["a", "b", "c"])', |
| 135 | + ]); |
| 136 | + assert.strictEqual(child.stdout.toString(), 'a, b, and c\n'); |
| 137 | +} |
| 138 | + |
| 139 | +{ |
| 140 | + const { child } = spawnSyncAndExitWithoutError( |
| 141 | + process.execPath, |
| 142 | + [ |
| 143 | + '--icu-locale=de-DE', |
| 144 | + '-p', |
| 145 | + 'new Intl.ListFormat(undefined, {style: "long", type:"conjunction"}).format(["a", "b", "c"])', |
| 146 | + ]); |
| 147 | + assert.strictEqual(child.stdout.toString(), 'a, b und c\n'); |
| 148 | +} |
| 149 | + |
| 150 | +// RelativeTimeFormat |
| 151 | +{ |
| 152 | + const { child } = spawnSyncAndExitWithoutError( |
| 153 | + process.execPath, |
| 154 | + [ |
| 155 | + '--icu-locale=en', |
| 156 | + '-p', |
| 157 | + 'new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }).format(3, "quarter")', |
| 158 | + ]); |
| 159 | + assert.strictEqual(child.stdout.toString(), 'in 3 quarters\n'); |
| 160 | +} |
| 161 | +{ |
| 162 | + const { child } = spawnSyncAndExitWithoutError( |
| 163 | + process.execPath, |
| 164 | + [ |
| 165 | + '--icu-locale=es', |
| 166 | + '-p', |
| 167 | + 'new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }).format(2, "day")', |
| 168 | + ]); |
| 169 | + assert.strictEqual(child.stdout.toString(), 'pasado mañana\n'); |
| 170 | +} |
| 171 | + |
| 172 | +// Collator |
| 173 | +{ |
| 174 | + const { child } = spawnSyncAndExitWithoutError( |
| 175 | + process.execPath, |
| 176 | + [ |
| 177 | + '--icu-locale=de', |
| 178 | + '-p', |
| 179 | + '["Z", "a", "z", "ä"].sort(new Intl.Collator().compare).join(",")', |
| 180 | + ]); |
| 181 | + assert.strictEqual(child.stdout.toString(), 'a,ä,z,Z\n'); |
| 182 | +} |
| 183 | +{ |
| 184 | + const { child } = spawnSyncAndExitWithoutError( |
| 185 | + process.execPath, |
| 186 | + [ |
| 187 | + '--icu-locale=sv', |
| 188 | + '-p', |
| 189 | + '["Z", "a", "z", "ä"].sort(new Intl.Collator().compare).join(",")', |
| 190 | + ]); |
| 191 | + assert.strictEqual(child.stdout.toString(), 'a,z,Z,ä\n'); |
| 192 | +} |
| 193 | + |
| 194 | +{ |
| 195 | + const { child } = spawnSyncAndExitWithoutError( |
| 196 | + process.execPath, |
| 197 | + [ |
| 198 | + '--icu-locale=de', |
| 199 | + '-p', |
| 200 | + '["Z", "a", "z", "ä"].sort(new Intl.Collator(undefined, { caseFirst: "upper" }).compare).join(",")', |
| 201 | + ]); |
| 202 | + assert.strictEqual(child.stdout.toString(), 'a,ä,Z,z\n'); |
| 203 | +} |
0 commit comments