Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 28a5799

Browse files
committed
Don't override native locale sensetive functions (fixes #58)
1 parent 9cc1533 commit 28a5799

File tree

1 file changed

+130
-125
lines changed

1 file changed

+130
-125
lines changed

Intl.js

+130-125
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ var
3939
// We use this a lot (and need it for proto-less objects)
4040
hop = Object.prototype.hasOwnProperty,
4141

42+
// Does the browser already support Intl .toLocaleString() functions?
43+
tls = (1.23).toLocaleString(undefined, {style:'currency', currency: 'ZZZ'}).indexOf('ZZZ') > -1,
44+
4245
// Naive defineProperty for compatibility
4346
defineProperty = realDefineProp ? Object.defineProperty : function (obj, name, desc) {
4447
if ('get' in desc && obj.__defineGetter__)
@@ -2624,155 +2627,157 @@ function ToLocalTime(date, calendar, timeZone) {
26242627
// Sect 13 Locale Sensitive Functions of the ECMAScript Language Specification
26252628
// ===========================================================================
26262629

2627-
/**
2628-
* When the toLocaleString method is called with optional arguments locales and options,
2629-
* the following steps are taken:
2630-
*/
2631-
/* 13.2.1 */defineProperty(Number.prototype, 'toLocaleString', {
2632-
writable: true,
2633-
configurable: true,
2634-
value: function () {
2635-
// Satisfy test 13.2.1_1
2636-
if (Object.prototype.toString.call(this) !== '[object Number]')
2637-
throw new TypeError('`this` value must be a number for Number.prototype.toLocaleString()');
2638-
2639-
// 1. Let x be this Number value (as defined in ES5, 15.7.4).
2640-
// 2. If locales is not provided, then let locales be undefined.
2641-
// 3. If options is not provided, then let options be undefined.
2642-
// 4. Let numberFormat be the result of creating a new object as if by the
2643-
// expression new Intl.NumberFormat(locales, options) where
2644-
// Intl.NumberFormat is the standard built-in constructor defined in 11.1.3.
2645-
// 5. Return the result of calling the FormatNumber abstract operation
2646-
// (defined in 11.3.2) with arguments numberFormat and x.
2647-
return FormatNumber(new NumberFormatConstructor(arguments[0], arguments[1]), this);
2648-
}
2649-
});
2630+
if (tls) {
2631+
/**
2632+
* When the toLocaleString method is called with optional arguments locales and options,
2633+
* the following steps are taken:
2634+
*/
2635+
/* 13.2.1 */defineProperty(Number.prototype, 'toLocaleString', {
2636+
writable: true,
2637+
configurable: true,
2638+
value: function () {
2639+
// Satisfy test 13.2.1_1
2640+
if (Object.prototype.toString.call(this) !== '[object Number]')
2641+
throw new TypeError('`this` value must be a number for Number.prototype.toLocaleString()');
2642+
2643+
// 1. Let x be this Number value (as defined in ES5, 15.7.4).
2644+
// 2. If locales is not provided, then let locales be undefined.
2645+
// 3. If options is not provided, then let options be undefined.
2646+
// 4. Let numberFormat be the result of creating a new object as if by the
2647+
// expression new Intl.NumberFormat(locales, options) where
2648+
// Intl.NumberFormat is the standard built-in constructor defined in 11.1.3.
2649+
// 5. Return the result of calling the FormatNumber abstract operation
2650+
// (defined in 11.3.2) with arguments numberFormat and x.
2651+
return FormatNumber(new NumberFormatConstructor(arguments[0], arguments[1]), this);
2652+
}
2653+
});
26502654

2651-
/**
2652-
* When the toLocaleString method is called with optional arguments locales and options,
2653-
* the following steps are taken:
2654-
*/
2655-
/* 13.3.1 */defineProperty(Date.prototype, 'toLocaleString', {
2656-
writable: true,
2657-
configurable: true,
2658-
value: function () {
2659-
// Satisfy test 13.3.0_1
2660-
if (Object.prototype.toString.call(this) !== '[object Date]')
2661-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleString()');
2655+
/**
2656+
* When the toLocaleString method is called with optional arguments locales and options,
2657+
* the following steps are taken:
2658+
*/
2659+
/* 13.3.1 */defineProperty(Date.prototype, 'toLocaleString', {
2660+
writable: true,
2661+
configurable: true,
2662+
value: function () {
2663+
// Satisfy test 13.3.0_1
2664+
if (Object.prototype.toString.call(this) !== '[object Date]')
2665+
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleString()');
26622666

2663-
var
2664-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2665-
x = +this;
2667+
var
2668+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2669+
x = +this;
26662670

2667-
// 2. If x is NaN, then return "Invalid Date".
2668-
if (isNaN(x))
2669-
return 'Invalid Date';
2671+
// 2. If x is NaN, then return "Invalid Date".
2672+
if (isNaN(x))
2673+
return 'Invalid Date';
26702674

2671-
var
2672-
// 3. If locales is not provided, then let locales be undefined.
2673-
locales = arguments[0],
2675+
var
2676+
// 3. If locales is not provided, then let locales be undefined.
2677+
locales = arguments[0],
26742678

2675-
// 4. If options is not provided, then let options be undefined.
2676-
options = arguments[1],
2679+
// 4. If options is not provided, then let options be undefined.
2680+
options = arguments[1],
26772681

2678-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2679-
// operation (defined in 12.1.1) with arguments options, "any", and "all".
2680-
options = ToDateTimeOptions(options, 'any', 'all'),
2682+
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2683+
// operation (defined in 12.1.1) with arguments options, "any", and "all".
2684+
options = ToDateTimeOptions(options, 'any', 'all'),
26812685

2682-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2683-
// expression new Intl.DateTimeFormat(locales, options) where
2684-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2685-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
2686+
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2687+
// expression new Intl.DateTimeFormat(locales, options) where
2688+
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2689+
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
26862690

2687-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2688-
// in 12.3.2) with arguments dateTimeFormat and x.
2689-
return FormatDateTime(dateTimeFormat, x);
2690-
}
2691-
});
2691+
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2692+
// in 12.3.2) with arguments dateTimeFormat and x.
2693+
return FormatDateTime(dateTimeFormat, x);
2694+
}
2695+
});
26922696

2693-
/**
2694-
* When the toLocaleDateString method is called with optional arguments locales and
2695-
* options, the following steps are taken:
2696-
*/
2697-
/* 13.3.2 */defineProperty(Date.prototype, 'toLocaleDateString', {
2698-
writable: true,
2699-
configurable: true,
2700-
value: function () {
2701-
// Satisfy test 13.3.0_1
2702-
if (Object.prototype.toString.call(this) !== '[object Date]')
2703-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleDateString()');
2697+
/**
2698+
* When the toLocaleDateString method is called with optional arguments locales and
2699+
* options, the following steps are taken:
2700+
*/
2701+
/* 13.3.2 */defineProperty(Date.prototype, 'toLocaleDateString', {
2702+
writable: true,
2703+
configurable: true,
2704+
value: function () {
2705+
// Satisfy test 13.3.0_1
2706+
if (Object.prototype.toString.call(this) !== '[object Date]')
2707+
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleDateString()');
27042708

2705-
var
2706-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2707-
x = +this;
2709+
var
2710+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2711+
x = +this;
27082712

2709-
// 2. If x is NaN, then return "Invalid Date".
2710-
if (isNaN(x))
2711-
return 'Invalid Date';
2713+
// 2. If x is NaN, then return "Invalid Date".
2714+
if (isNaN(x))
2715+
return 'Invalid Date';
27122716

2713-
var
2714-
// 3. If locales is not provided, then let locales be undefined.
2715-
locales = arguments[0],
2717+
var
2718+
// 3. If locales is not provided, then let locales be undefined.
2719+
locales = arguments[0],
27162720

2717-
// 4. If options is not provided, then let options be undefined.
2718-
options = arguments[1],
2721+
// 4. If options is not provided, then let options be undefined.
2722+
options = arguments[1],
27192723

2720-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2721-
// operation (defined in 12.1.1) with arguments options, "date", and "date".
2722-
options = ToDateTimeOptions(options, 'date', 'date'),
2724+
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2725+
// operation (defined in 12.1.1) with arguments options, "date", and "date".
2726+
options = ToDateTimeOptions(options, 'date', 'date'),
27232727

2724-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2725-
// expression new Intl.DateTimeFormat(locales, options) where
2726-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2727-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
2728+
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2729+
// expression new Intl.DateTimeFormat(locales, options) where
2730+
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2731+
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
27282732

2729-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2730-
// in 12.3.2) with arguments dateTimeFormat and x.
2731-
return FormatDateTime(dateTimeFormat, x);
2732-
}
2733-
});
2733+
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2734+
// in 12.3.2) with arguments dateTimeFormat and x.
2735+
return FormatDateTime(dateTimeFormat, x);
2736+
}
2737+
});
27342738

2735-
/**
2736-
* When the toLocaleTimeString method is called with optional arguments locales and
2737-
* options, the following steps are taken:
2738-
*/
2739-
/* 13.3.3 */defineProperty(Date.prototype, 'toLocaleTimeString', {
2740-
writable: true,
2741-
configurable: true,
2742-
value: function () {
2743-
// Satisfy test 13.3.0_1
2744-
if (Object.prototype.toString.call(this) !== '[object Date]')
2745-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleTimeString()');
2739+
/**
2740+
* When the toLocaleTimeString method is called with optional arguments locales and
2741+
* options, the following steps are taken:
2742+
*/
2743+
/* 13.3.3 */defineProperty(Date.prototype, 'toLocaleTimeString', {
2744+
writable: true,
2745+
configurable: true,
2746+
value: function () {
2747+
// Satisfy test 13.3.0_1
2748+
if (Object.prototype.toString.call(this) !== '[object Date]')
2749+
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleTimeString()');
27462750

2747-
var
2748-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2749-
x = +this;
2751+
var
2752+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2753+
x = +this;
27502754

2751-
// 2. If x is NaN, then return "Invalid Date".
2752-
if (isNaN(x))
2753-
return 'Invalid Date';
2755+
// 2. If x is NaN, then return "Invalid Date".
2756+
if (isNaN(x))
2757+
return 'Invalid Date';
27542758

2755-
var
2756-
// 3. If locales is not provided, then let locales be undefined.
2757-
locales = arguments[0],
2759+
var
2760+
// 3. If locales is not provided, then let locales be undefined.
2761+
locales = arguments[0],
27582762

2759-
// 4. If options is not provided, then let options be undefined.
2760-
options = arguments[1],
2763+
// 4. If options is not provided, then let options be undefined.
2764+
options = arguments[1],
27612765

2762-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2763-
// operation (defined in 12.1.1) with arguments options, "time", and "time".
2764-
options = ToDateTimeOptions(options, 'time', 'time'),
2766+
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2767+
// operation (defined in 12.1.1) with arguments options, "time", and "time".
2768+
options = ToDateTimeOptions(options, 'time', 'time'),
27652769

2766-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2767-
// expression new Intl.DateTimeFormat(locales, options) where
2768-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2769-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
2770+
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2771+
// expression new Intl.DateTimeFormat(locales, options) where
2772+
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2773+
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
27702774

2771-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2772-
// in 12.3.2) with arguments dateTimeFormat and x.
2773-
return FormatDateTime(dateTimeFormat, x);
2774-
}
2775-
});
2775+
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2776+
// in 12.3.2) with arguments dateTimeFormat and x.
2777+
return FormatDateTime(dateTimeFormat, x);
2778+
}
2779+
});
2780+
}
27762781

27772782
/**
27782783
* Can't really ship a single script with data for hundreds of locales, so we provide

0 commit comments

Comments
 (0)