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

Commit 56aab5a

Browse files
committed
Add locale sensitive functions to Intl.__localeSensitiveProtos
Even if we're not overriding native locale sensitive prototype functions, we should still expose them. This change exposes them under a new property on the Intl/IntlPolyfill object.
1 parent 17ad3e9 commit 56aab5a

File tree

1 file changed

+124
-126
lines changed

1 file changed

+124
-126
lines changed

Intl.js

+124-126
Original file line numberDiff line numberDiff line change
@@ -2630,156 +2630,154 @@ function ToLocalTime(date, calendar, timeZone) {
26302630
// Sect 13 Locale Sensitive Functions of the ECMAScript Language Specification
26312631
// ===========================================================================
26322632

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

2658-
/**
2659-
* When the toLocaleString method is called with optional arguments locales and options,
2660-
* the following steps are taken:
2661-
*/
2662-
/* 13.3.1 */defineProperty(Date.prototype, 'toLocaleString', {
2663-
writable: true,
2664-
configurable: true,
2665-
value: function () {
2666-
// Satisfy test 13.3.0_1
2667-
if (Object.prototype.toString.call(this) !== '[object Date]')
2668-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleString()');
2638+
/**
2639+
* When the toLocaleString method is called with optional arguments locales and options,
2640+
* the following steps are taken:
2641+
*/
2642+
/* 13.2.1 */ls.Number.toLocaleString = function () {
2643+
// Satisfy test 13.2.1_1
2644+
if (Object.prototype.toString.call(this) !== '[object Number]')
2645+
throw new TypeError('`this` value must be a number for Number.prototype.toLocaleString()');
2646+
2647+
// 1. Let x be this Number value (as defined in ES5, 15.7.4).
2648+
// 2. If locales is not provided, then let locales be undefined.
2649+
// 3. If options is not provided, then let options be undefined.
2650+
// 4. Let numberFormat be the result of creating a new object as if by the
2651+
// expression new Intl.NumberFormat(locales, options) where
2652+
// Intl.NumberFormat is the standard built-in constructor defined in 11.1.3.
2653+
// 5. Return the result of calling the FormatNumber abstract operation
2654+
// (defined in 11.3.2) with arguments numberFormat and x.
2655+
return FormatNumber(new NumberFormatConstructor(arguments[0], arguments[1]), this);
2656+
};
26692657

2670-
var
2671-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2672-
x = +this;
2658+
/**
2659+
* When the toLocaleString method is called with optional arguments locales and options,
2660+
* the following steps are taken:
2661+
*/
2662+
/* 13.3.1 */ls.Date.toLocaleString = 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()');
26732666

2674-
// 2. If x is NaN, then return "Invalid Date".
2675-
if (isNaN(x))
2676-
return 'Invalid Date';
2667+
var
2668+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2669+
x = +this;
26772670

2678-
var
2679-
// 3. If locales is not provided, then let locales be undefined.
2680-
locales = arguments[0],
2671+
// 2. If x is NaN, then return "Invalid Date".
2672+
if (isNaN(x))
2673+
return 'Invalid Date';
26812674

2682-
// 4. If options is not provided, then let options be undefined.
2683-
options = arguments[1],
2675+
var
2676+
// 3. If locales is not provided, then let locales be undefined.
2677+
locales = arguments[0],
26842678

2685-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2686-
// operation (defined in 12.1.1) with arguments options, "any", and "all".
2687-
options = ToDateTimeOptions(options, 'any', 'all'),
2679+
// 4. If options is not provided, then let options be undefined.
2680+
options = arguments[1],
26882681

2689-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2690-
// expression new Intl.DateTimeFormat(locales, options) where
2691-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2692-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
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'),
26932685

2694-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2695-
// in 12.3.2) with arguments dateTimeFormat and x.
2696-
return FormatDateTime(dateTimeFormat, x);
2697-
}
2698-
});
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);
26992690

2700-
/**
2701-
* When the toLocaleDateString method is called with optional arguments locales and
2702-
* options, the following steps are taken:
2703-
*/
2704-
/* 13.3.2 */defineProperty(Date.prototype, 'toLocaleDateString', {
2705-
writable: true,
2706-
configurable: true,
2707-
value: function () {
2708-
// Satisfy test 13.3.0_1
2709-
if (Object.prototype.toString.call(this) !== '[object Date]')
2710-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleDateString()');
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+
};
27112695

2712-
var
2713-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2714-
x = +this;
2696+
/**
2697+
* When the toLocaleDateString method is called with optional arguments locales and
2698+
* options, the following steps are taken:
2699+
*/
2700+
/* 13.3.2 */ls.Date.toLocaleDateString = 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()');
27152704

2716-
// 2. If x is NaN, then return "Invalid Date".
2717-
if (isNaN(x))
2718-
return 'Invalid Date';
2705+
var
2706+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2707+
x = +this;
27192708

2720-
var
2721-
// 3. If locales is not provided, then let locales be undefined.
2722-
locales = arguments[0],
2709+
// 2. If x is NaN, then return "Invalid Date".
2710+
if (isNaN(x))
2711+
return 'Invalid Date';
27232712

2724-
// 4. If options is not provided, then let options be undefined.
2725-
options = arguments[1],
2713+
var
2714+
// 3. If locales is not provided, then let locales be undefined.
2715+
locales = arguments[0],
27262716

2727-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2728-
// operation (defined in 12.1.1) with arguments options, "date", and "date".
2729-
options = ToDateTimeOptions(options, 'date', 'date'),
2717+
// 4. If options is not provided, then let options be undefined.
2718+
options = arguments[1],
27302719

2731-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2732-
// expression new Intl.DateTimeFormat(locales, options) where
2733-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2734-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
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'),
27352723

2736-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2737-
// in 12.3.2) with arguments dateTimeFormat and x.
2738-
return FormatDateTime(dateTimeFormat, x);
2739-
}
2740-
});
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);
27412728

2742-
/**
2743-
* When the toLocaleTimeString method is called with optional arguments locales and
2744-
* options, the following steps are taken:
2745-
*/
2746-
/* 13.3.3 */defineProperty(Date.prototype, 'toLocaleTimeString', {
2747-
writable: true,
2748-
configurable: true,
2749-
value: function () {
2750-
// Satisfy test 13.3.0_1
2751-
if (Object.prototype.toString.call(this) !== '[object Date]')
2752-
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleTimeString()');
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+
};
27532733

2754-
var
2755-
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2756-
x = +this;
2734+
/**
2735+
* When the toLocaleTimeString method is called with optional arguments locales and
2736+
* options, the following steps are taken:
2737+
*/
2738+
/* 13.3.3 */ls.Date.toLocaleTimeString = function () {
2739+
// Satisfy test 13.3.0_1
2740+
if (Object.prototype.toString.call(this) !== '[object Date]')
2741+
throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleTimeString()');
27572742

2758-
// 2. If x is NaN, then return "Invalid Date".
2759-
if (isNaN(x))
2760-
return 'Invalid Date';
2743+
var
2744+
// 1. Let x be this time value (as defined in ES5, 15.9.5).
2745+
x = +this;
27612746

2762-
var
2763-
// 3. If locales is not provided, then let locales be undefined.
2764-
locales = arguments[0],
2747+
// 2. If x is NaN, then return "Invalid Date".
2748+
if (isNaN(x))
2749+
return 'Invalid Date';
2750+
2751+
var
2752+
// 3. If locales is not provided, then let locales be undefined.
2753+
locales = arguments[0],
27652754

2766-
// 4. If options is not provided, then let options be undefined.
2767-
options = arguments[1],
2755+
// 4. If options is not provided, then let options be undefined.
2756+
options = arguments[1],
27682757

2769-
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2770-
// operation (defined in 12.1.1) with arguments options, "time", and "time".
2771-
options = ToDateTimeOptions(options, 'time', 'time'),
2758+
// 5. Let options be the result of calling the ToDateTimeOptions abstract
2759+
// operation (defined in 12.1.1) with arguments options, "time", and "time".
2760+
options = ToDateTimeOptions(options, 'time', 'time'),
27722761

2773-
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2774-
// expression new Intl.DateTimeFormat(locales, options) where
2775-
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2776-
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
2762+
// 6. Let dateTimeFormat be the result of creating a new object as if by the
2763+
// expression new Intl.DateTimeFormat(locales, options) where
2764+
// Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
2765+
dateTimeFormat = new DateTimeFormatConstructor(locales, options);
27772766

2778-
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2779-
// in 12.3.2) with arguments dateTimeFormat and x.
2780-
return FormatDateTime(dateTimeFormat, x);
2781-
}
2782-
});
2767+
// 7. Return the result of calling the FormatDateTime abstract operation (defined
2768+
// in 12.3.2) with arguments dateTimeFormat and x.
2769+
return FormatDateTime(dateTimeFormat, x);
2770+
};
2771+
2772+
// Where native locale-sensitive functions do not follow ECMA-402 section 13,
2773+
// Intl.js will override them automatically
2774+
if (overrideTLS) {
2775+
defineProperty(Number.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Number.toLocaleString });
2776+
2777+
for (var k in ls.Date) {
2778+
if (hop.call(ls.Date, k))
2779+
defineProperty(Date.prototype, k, { writable: true, configurable: true, value: ls.Date[k] });
2780+
}
27832781
}
27842782

27852783
/**

0 commit comments

Comments
 (0)