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

Commit 9b58d0d

Browse files
committed
Expose and use a function to infect Number/Date.prototype
1 parent 56aab5a commit 9b58d0d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Intl.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313

1414
(function (global, factory) {
1515
var IntlPolyfill = factory();
16+
1617
// register in -all- the module systems (at once)
17-
if (typeof define === 'function' && define.amd) {
18+
if (typeof define === 'function' && define.amd)
1819
define(IntlPolyfill);
19-
}
20-
if (typeof exports === 'object') {
20+
21+
if (typeof exports === 'object')
2122
module.exports = IntlPolyfill;
22-
}
23+
2324
if (!global.Intl) {
2425
global.Intl = IntlPolyfill;
26+
IntlPolyfill.__applyLocaleSensitivePrototypes();
2527
}
28+
2629
global.IntlPolyfill = IntlPolyfill;
30+
2731
})(typeof global !== 'undefined' ? global : this, function() {
2832
"use strict";
2933
var
@@ -40,11 +44,6 @@ var
4044
// We use this a lot (and need it for proto-less objects)
4145
hop = Object.prototype.hasOwnProperty,
4246

43-
// Determine whether we need to override ECMA-262 locale sensitive functions
44-
overrideTLS = (function () {
45-
try { (0).toLocaleString(null); return true; } catch (e) { return false; }
46-
})(),
47-
4847
// Naive defineProperty for compatibility
4948
defineProperty = realDefineProp ? Object.defineProperty : function (obj, name, desc) {
5049
if ('get' in desc && obj.__defineGetter__)
@@ -2769,16 +2768,18 @@ var ls = Intl.__localeSensitiveProtos = {
27692768
return FormatDateTime(dateTimeFormat, x);
27702769
};
27712770

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 });
2771+
defineProperty(Intl, '__applyLocaleSensitivePrototypes', {
2772+
writable: true,
2773+
configurable: true,
2774+
value: function () {
2775+
defineProperty(Number.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Number.toLocaleString });
27762776

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] });
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+
}
27802781
}
2781-
}
2782+
});
27822783

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

0 commit comments

Comments
 (0)