OLD | NEW |
1 // Copyright 2006-2011 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 /** | 216 /** |
217 * Creates new DateTimeFormat based on current locale. | 217 * Creates new DateTimeFormat based on current locale. |
218 * @param {Object} - formatting flags. See constructor. | 218 * @param {Object} - formatting flags. See constructor. |
219 * @returns {Object} - new DateTimeFormat object. | 219 * @returns {Object} - new DateTimeFormat object. |
220 */ | 220 */ |
221 v8Locale.prototype.createDateTimeFormat = function(settings) { | 221 v8Locale.prototype.createDateTimeFormat = function(settings) { |
222 return new v8Locale.__DateTimeFormat(this, settings); | 222 return new v8Locale.__DateTimeFormat(this, settings); |
223 }; | 223 }; |
224 | 224 |
225 /** | 225 /** |
| 226 * NumberFormat class implements locale-aware number formatting. |
| 227 * Constructor is not part of public API. |
| 228 * @param {Object} locale - locale object to pass to formatter. |
| 229 * @param {Object} settings - formatting flags: |
| 230 * - skeleton |
| 231 * - pattern |
| 232 * - style - decimal, currency, percent or scientific |
| 233 * - currencyCode - ISO 4217 3-letter currency code |
| 234 * @private |
| 235 * @constructor |
| 236 */ |
| 237 v8Locale.__NumberFormat = function(locale, settings) { |
| 238 native function NativeJSNumberFormat(); |
| 239 |
| 240 settings = v8Locale.__createSettingsOrDefault(settings, {}); |
| 241 |
| 242 var cleanSettings = {}; |
| 243 if (settings.hasOwnProperty('skeleton')) { |
| 244 // Assign skeleton to cleanSettings and fix invalid currency pattern |
| 245 // if present - 'ooxo' becomes 'o'. |
| 246 cleanSettings['skeleton'] = |
| 247 settings['skeleton'].replace(/\u00a4+[^\u00a4]+\u00a4+/g, '\u00a4'); |
| 248 } else if (settings.hasOwnProperty('pattern')) { |
| 249 cleanSettings['pattern'] = settings['pattern']; |
| 250 } else if (settings.hasOwnProperty('style')) { |
| 251 var style = settings['style']; |
| 252 if (!/^decimal|currency|percent|scientific$/.test(style)) { |
| 253 style = 'decimal'; |
| 254 } |
| 255 cleanSettings['style'] = style; |
| 256 } |
| 257 |
| 258 // Default is to show decimal style. |
| 259 if (!cleanSettings.hasOwnProperty('skeleton') && |
| 260 !cleanSettings.hasOwnProperty('pattern') && |
| 261 !cleanSettings.hasOwnProperty('style')) { |
| 262 cleanSettings = {'style': 'decimal'}; |
| 263 } |
| 264 |
| 265 // Add currency code if available and valid (3-letter ASCII code). |
| 266 if (settings.hasOwnProperty('currencyCode') && |
| 267 /^[a-zA-Z]{3}$/.test(settings['currencyCode'])) { |
| 268 cleanSettings['currencyCode'] = settings['currencyCode'].toUpperCase(); |
| 269 } |
| 270 |
| 271 locale = v8Locale.__createLocaleOrDefault(locale); |
| 272 // Pass in region ID for proper currency detection. Use ZZ if region is empty. |
| 273 var region = locale.options.regionID !== '' ? locale.options.regionID : 'ZZ'; |
| 274 var formatter = NativeJSNumberFormat( |
| 275 locale.__icuLocaleID, 'und_' + region, cleanSettings); |
| 276 |
| 277 // ICU doesn't always uppercase the currency code. |
| 278 if (formatter.options.hasOwnProperty('currencyCode')) { |
| 279 formatter.options['currencyCode'] = |
| 280 formatter.options['currencyCode'].toUpperCase(); |
| 281 } |
| 282 |
| 283 for (key in cleanSettings) { |
| 284 // Don't overwrite keys that are alredy in. |
| 285 if (formatter.options.hasOwnProperty(key)) continue; |
| 286 |
| 287 formatter.options[key] = cleanSettings[key]; |
| 288 } |
| 289 |
| 290 /** |
| 291 * Clones existing number format with possible overrides for some |
| 292 * of the options. |
| 293 * @param {!Object} overrideSettings - overrides for current format settings. |
| 294 * @returns {Object} - new or cached NumberFormat object. |
| 295 * @public |
| 296 */ |
| 297 formatter.derive = function(overrideSettings) { |
| 298 // To remove a setting user can specify undefined as its value. We'll remove |
| 299 // it from the map in that case. |
| 300 for (var prop in overrideSettings) { |
| 301 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) { |
| 302 delete settings[prop]; |
| 303 } |
| 304 } |
| 305 return new v8Locale.__NumberFormat( |
| 306 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings)); |
| 307 }; |
| 308 |
| 309 return formatter; |
| 310 }; |
| 311 |
| 312 /** |
| 313 * Creates new NumberFormat based on current locale. |
| 314 * @param {Object} - formatting flags. See constructor. |
| 315 * @returns {Object} - new or cached NumberFormat object. |
| 316 */ |
| 317 v8Locale.prototype.createNumberFormat = function(settings) { |
| 318 return new v8Locale.__NumberFormat(this, settings); |
| 319 }; |
| 320 |
| 321 /** |
226 * Merges user settings and defaults. | 322 * Merges user settings and defaults. |
227 * Settings that are not of object type are rejected. | 323 * Settings that are not of object type are rejected. |
228 * Actual property values are not validated, but whitespace is trimmed if they | 324 * Actual property values are not validated, but whitespace is trimmed if they |
229 * are strings. | 325 * are strings. |
230 * @param {!Object} settings - user provided settings. | 326 * @param {!Object} settings - user provided settings. |
231 * @param {!Object} defaults - default values for this type of settings. | 327 * @param {!Object} defaults - default values for this type of settings. |
232 * @returns {Object} - valid settings object. | 328 * @returns {Object} - valid settings object. |
233 * @private | 329 * @private |
234 */ | 330 */ |
235 v8Locale.__createSettingsOrDefault = function(settings, defaults) { | 331 v8Locale.__createSettingsOrDefault = function(settings, defaults) { |
(...skipping 28 matching lines...) Expand all Loading... |
264 * @returns {Object} - v8Locale object. | 360 * @returns {Object} - v8Locale object. |
265 * @private | 361 * @private |
266 */ | 362 */ |
267 v8Locale.__createLocaleOrDefault = function(locale) { | 363 v8Locale.__createLocaleOrDefault = function(locale) { |
268 if (!locale || !(locale instanceof v8Locale)) { | 364 if (!locale || !(locale instanceof v8Locale)) { |
269 return new v8Locale(); | 365 return new v8Locale(); |
270 } else { | 366 } else { |
271 return locale; | 367 return locale; |
272 } | 368 } |
273 }; | 369 }; |
OLD | NEW |