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 * @private |
| 234 * @constructor |
| 235 */ |
| 236 v8Locale.__NumberFormat = function(locale, settings) { |
| 237 native function NativeJSNumberFormat(); |
| 238 |
| 239 settings = v8Locale.__createSettingsOrDefault(settings, {}); |
| 240 |
| 241 var cleanSettings = {}; |
| 242 if (settings.hasOwnProperty('skeleton')) { |
| 243 cleanSettings['skeleton'] = settings['skeleton']; |
| 244 } else if (settings.hasOwnProperty('pattern')) { |
| 245 cleanSettings['pattern'] = settings['pattern']; |
| 246 } else { |
| 247 cleanSettings = {}; |
| 248 if (settings.hasOwnProperty('style')) { |
| 249 var style = settings['style']; |
| 250 if (!/^decimal|currency|percent|scientific$/.test(style)) { |
| 251 style = 'decimal'; |
| 252 } |
| 253 cleanSettings['style'] = style; |
| 254 } |
| 255 } |
| 256 |
| 257 // Default is to show decimal style. |
| 258 if (!cleanSettings.hasOwnProperty('skeleton') && |
| 259 !cleanSettings.hasOwnProperty('pattern') && |
| 260 !cleanSettings.hasOwnProperty('style')) { |
| 261 cleanSettings = {'style': 'decimal'}; |
| 262 } |
| 263 |
| 264 locale = v8Locale.__createLocaleOrDefault(locale); |
| 265 // Pass in region ID for proper currency detection. Use ZZ if region is empty. |
| 266 var region = locale.options.regionID !== '' ? locale.options.regionID : 'ZZ'; |
| 267 var formatter = NativeJSNumberFormat( |
| 268 locale.__icuLocaleID, 'und_' + region, cleanSettings); |
| 269 |
| 270 formatter.options = {}; |
| 271 for (key in cleanSettings) { |
| 272 formatter.options[key] = cleanSettings[key]; |
| 273 } |
| 274 |
| 275 /** |
| 276 * Clones existing number format with possible overrides for some |
| 277 * of the options. |
| 278 * @param {!Object} overrideSettings - overrides for current format settings. |
| 279 * @returns {Object} - new or cached NumberFormat object. |
| 280 * @public |
| 281 */ |
| 282 formatter.derive = function(overrideSettings) { |
| 283 // To remove a setting user can specify undefined as its value. We'll remove |
| 284 // it from the map in that case. |
| 285 for (var prop in overrideSettings) { |
| 286 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) { |
| 287 delete settings[prop]; |
| 288 } |
| 289 } |
| 290 return new v8Locale.__NumberFormat( |
| 291 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings)); |
| 292 }; |
| 293 |
| 294 return formatter; |
| 295 }; |
| 296 |
| 297 /** |
| 298 * Creates new NumberFormat based on current locale. |
| 299 * @param {Object} - formatting flags. See constructor. |
| 300 * @returns {Object} - new or cached NumberFormat object. |
| 301 */ |
| 302 v8Locale.prototype.createNumberFormat = function(settings) { |
| 303 return new v8Locale.__NumberFormat(this, settings); |
| 304 }; |
| 305 |
| 306 /** |
226 * Merges user settings and defaults. | 307 * Merges user settings and defaults. |
227 * Settings that are not of object type are rejected. | 308 * Settings that are not of object type are rejected. |
228 * Actual property values are not validated, but whitespace is trimmed if they | 309 * Actual property values are not validated, but whitespace is trimmed if they |
229 * are strings. | 310 * are strings. |
230 * @param {!Object} settings - user provided settings. | 311 * @param {!Object} settings - user provided settings. |
231 * @param {!Object} defaults - default values for this type of settings. | 312 * @param {!Object} defaults - default values for this type of settings. |
232 * @returns {Object} - valid settings object. | 313 * @returns {Object} - valid settings object. |
233 * @private | 314 * @private |
234 */ | 315 */ |
235 v8Locale.__createSettingsOrDefault = function(settings, defaults) { | 316 v8Locale.__createSettingsOrDefault = function(settings, defaults) { |
(...skipping 28 matching lines...) Expand all Loading... |
264 * @returns {Object} - v8Locale object. | 345 * @returns {Object} - v8Locale object. |
265 * @private | 346 * @private |
266 */ | 347 */ |
267 v8Locale.__createLocaleOrDefault = function(locale) { | 348 v8Locale.__createLocaleOrDefault = function(locale) { |
268 if (!locale || !(locale instanceof v8Locale)) { | 349 if (!locale || !(locale instanceof v8Locale)) { |
269 return new v8Locale(); | 350 return new v8Locale(); |
270 } else { | 351 } else { |
271 return locale; | 352 return locale; |
272 } | 353 } |
273 }; | 354 }; |
OLD | NEW |