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 cleanSettings['skeleton'] = settings['skeleton']; | |
245 } else if (settings.hasOwnProperty('pattern')) { | |
246 cleanSettings['pattern'] = settings['pattern']; | |
247 } else { | |
248 cleanSettings = {}; | |
249 if (settings.hasOwnProperty('style')) { | |
250 var style = settings['style']; | |
251 if (!/^decimal|currency|percent|scientific$/.test(style)) { | |
252 style = 'decimal'; | |
253 } | |
254 cleanSettings['style'] = style; | |
255 } | |
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'])) { | |
jungshik at Google
2011/06/16 18:08:19
Is it your intent to allow mixed-case 3-letter cod
Nebojša Ćirić
2011/06/17 17:48:45
I don't think it's a problem to allow that in this
| |
268 cleanSettings['currencyCode'] = settings['currencyCode'].toUpperCase(); | |
jungshik at Google
2011/06/16 18:08:19
It looks like it's assumed that v8's toUpperCase w
Nebojša Ćirić
2011/06/17 17:48:45
It's locale independent. There is a locale depende
| |
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 for (key in cleanSettings) { | |
278 // Don't overwrite keys that are alredy in. | |
279 if (formatter.hasOwnProperty(key)) continue; | |
280 | |
281 formatter.options[key] = cleanSettings[key]; | |
282 } | |
283 | |
284 /** | |
285 * Clones existing number format with possible overrides for some | |
286 * of the options. | |
287 * @param {!Object} overrideSettings - overrides for current format settings. | |
288 * @returns {Object} - new or cached NumberFormat object. | |
289 * @public | |
290 */ | |
291 formatter.derive = function(overrideSettings) { | |
292 // To remove a setting user can specify undefined as its value. We'll remove | |
293 // it from the map in that case. | |
294 for (var prop in overrideSettings) { | |
295 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) { | |
296 delete settings[prop]; | |
297 } | |
298 } | |
299 return new v8Locale.__NumberFormat( | |
300 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings)); | |
301 }; | |
302 | |
303 return formatter; | |
304 }; | |
305 | |
306 /** | |
307 * Creates new NumberFormat based on current locale. | |
308 * @param {Object} - formatting flags. See constructor. | |
309 * @returns {Object} - new or cached NumberFormat object. | |
310 */ | |
311 v8Locale.prototype.createNumberFormat = function(settings) { | |
312 return new v8Locale.__NumberFormat(this, settings); | |
313 }; | |
314 | |
315 /** | |
226 * Merges user settings and defaults. | 316 * Merges user settings and defaults. |
227 * Settings that are not of object type are rejected. | 317 * Settings that are not of object type are rejected. |
228 * Actual property values are not validated, but whitespace is trimmed if they | 318 * Actual property values are not validated, but whitespace is trimmed if they |
229 * are strings. | 319 * are strings. |
230 * @param {!Object} settings - user provided settings. | 320 * @param {!Object} settings - user provided settings. |
231 * @param {!Object} defaults - default values for this type of settings. | 321 * @param {!Object} defaults - default values for this type of settings. |
232 * @returns {Object} - valid settings object. | 322 * @returns {Object} - valid settings object. |
233 * @private | 323 * @private |
234 */ | 324 */ |
235 v8Locale.__createSettingsOrDefault = function(settings, defaults) { | 325 v8Locale.__createSettingsOrDefault = function(settings, defaults) { |
(...skipping 28 matching lines...) Expand all Loading... | |
264 * @returns {Object} - v8Locale object. | 354 * @returns {Object} - v8Locale object. |
265 * @private | 355 * @private |
266 */ | 356 */ |
267 v8Locale.__createLocaleOrDefault = function(locale) { | 357 v8Locale.__createLocaleOrDefault = function(locale) { |
268 if (!locale || !(locale instanceof v8Locale)) { | 358 if (!locale || !(locale instanceof v8Locale)) { |
269 return new v8Locale(); | 359 return new v8Locale(); |
270 } else { | 360 } else { |
271 return locale; | 361 return locale; |
272 } | 362 } |
273 }; | 363 }; |
OLD | NEW |