OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 the v8-i18n authors. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 // ECMAScript 402 API implementation is broken into separate files for |
| 16 // each service. The build system combines them together into one |
| 17 // Intl namespace. |
| 18 |
| 19 |
| 20 // Save references to Intl objects and methods we use, for added security. |
| 21 var collator = Intl.Collator; |
| 22 var numberFormat = Intl.NumberFormat; |
| 23 var dateFormat = Intl.DateTimeFormat; |
| 24 |
| 25 |
| 26 /** |
| 27 * Compares this and that, and returns less than 0, 0 or greater than 0 value. |
| 28 * Overrides the built-in method. |
| 29 */ |
| 30 Object.defineProperty( |
| 31 String.prototype, 'localeCompare', |
| 32 {value: function(that, locales, options) { |
| 33 // Call internal method. |
| 34 return compare(new collator(locales, options), this, that); |
| 35 }, |
| 36 writable: true, |
| 37 configurable: true, |
| 38 enumerable: false |
| 39 } |
| 40 ); |
| 41 |
| 42 |
| 43 /** |
| 44 * Formats a Number object (this) using locale and options values. |
| 45 * If locale or options are omitted, defaults are used. |
| 46 */ |
| 47 Object.defineProperty( |
| 48 Number.prototype, 'toLocaleString', |
| 49 {value: function(locales, options) { |
| 50 // Call internal method. |
| 51 return formatNumber(new numberFormat(locales, options), this); |
| 52 }, |
| 53 writable: true, |
| 54 configurable: true, |
| 55 enumerable: false |
| 56 } |
| 57 ); |
| 58 |
| 59 |
| 60 /** |
| 61 * Returns actual formatted date. |
| 62 */ |
| 63 function toLocaleDateTime(date, locales, options, required, defaults) { |
| 64 if (!(date instanceof Date)) { |
| 65 throw new TypeError('Method invoked on an object that is not Date.'); |
| 66 } |
| 67 |
| 68 if (isNaN(date)) { |
| 69 return 'Invalid Date'; |
| 70 } |
| 71 |
| 72 var internalOptions = toDateTimeOptions(options, required, defaults); |
| 73 // Call internal method. |
| 74 return formatDate(new dateFormat(locales, internalOptions), date); |
| 75 } |
| 76 |
| 77 |
| 78 /** |
| 79 * Formats a Date object (this) using locale and options values. |
| 80 * If locale or options are omitted, defaults are used - both date and time are |
| 81 * present in the output. |
| 82 */ |
| 83 Object.defineProperty( |
| 84 Date.prototype, 'toLocaleString', |
| 85 {value: function(locales, options) { |
| 86 return toLocaleDateTime(this, locales, options, 'any', 'all'); |
| 87 }, |
| 88 writable: true, |
| 89 configurable: true, |
| 90 enumerable: false |
| 91 } |
| 92 ); |
| 93 |
| 94 |
| 95 /** |
| 96 * Formats a Date object (this) using locale and options values. |
| 97 * If locale or options are omitted, defaults are used - only date is present |
| 98 * in the output. |
| 99 */ |
| 100 Object.defineProperty( |
| 101 Date.prototype, 'toLocaleDateString', |
| 102 {value: function(locales, options) { |
| 103 return toLocaleDateTime(this, locales, options, 'date', 'date'); |
| 104 }, |
| 105 writable: true, |
| 106 configurable: true, |
| 107 enumerable: false |
| 108 } |
| 109 ); |
| 110 |
| 111 |
| 112 /** |
| 113 * Formats a Date object (this) using locale and options values. |
| 114 * If locale or options are omitted, defaults are used - only time is present |
| 115 * in the output. |
| 116 */ |
| 117 Object.defineProperty( |
| 118 Date.prototype, 'toLocaleTimeString', |
| 119 {value: function(locales, options) { |
| 120 return toLocaleDateTime(this, locales, options, 'time', 'time'); |
| 121 }, |
| 122 writable: true, |
| 123 configurable: true, |
| 124 enumerable: false |
| 125 } |
| 126 ); |
OLD | NEW |