Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 845e37b

Browse files
committed
fix: extend correctly native classes
1 parent 1501228 commit 845e37b

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

src/code/polyfill.js

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@ function _get(object, property, receiver) {
3434
return getter.call(receiver);
3535
}
3636

37+
function _getPrototypeOf(Class) {
38+
return Object.getPrototypeOf
39+
? Object.getPrototypeOf(Class)
40+
: Class.__proto__;
41+
}
42+
43+
function _setPrototypeOf(subClass, superClass) {
44+
if (Object.setPrototypeOf) {
45+
return Object.setPrototypeOf(subClass, superClass);
46+
}
47+
subClass.__proto__ = superClass;
48+
return subClass;
49+
}
50+
51+
function _wrapNativeSuper(Class) {
52+
function Wrapper() {
53+
const prototypeConstructor = _getPrototypeOf(this).constructor;
54+
const args = [null];
55+
args.push.apply(args, arguments);
56+
const Constructor = Function.bind.apply(Class, args);
57+
const instance = new Constructor();
58+
if (prototypeConstructor) {
59+
_setPrototypeOf(instance, prototypeConstructor.prototype);
60+
}
61+
return instance;
62+
}
63+
Wrapper.prototype = Object.create(Class.prototype, {
64+
constructor: {
65+
value: Wrapper,
66+
enumerable: false,
67+
writable: true,
68+
configurable: true
69+
}
70+
});
71+
return _setPrototypeOf(Wrapper, Class);
72+
}
73+
3774
// Also from Babel, minimal subclassing utility function
3875
function _inherits(subClass, superClass) {
3976
if (typeof superClass !== "function" && superClass !== null) {
@@ -48,9 +85,7 @@ function _inherits(subClass, superClass) {
4885
}
4986
});
5087
if (superClass) {
51-
Object.setPrototypeOf
52-
? Object.setPrototypeOf(subClass, superClass)
53-
: subClass.__proto__ = superClass;
88+
_setPrototypeOf(subClass, superClass);
5489
}
5590
}
5691

@@ -83,29 +118,31 @@ export default function polyfill(globalSpace) {
83118
return new DateTimeFormatPolyfill(locale, options);
84119
}
85120

121+
let _this;
122+
86123
const timeZone = (options && options.timeZone) || 'UTC';
87124

88125
if (options === undefined) {
89126
// options is not provided. this means
90127
// we don't need to format arbitrary timezone
91-
_DateTimeFormat.call(this, locale, options);
128+
_this = _getPrototypeOf(DateTimeFormatPolyfill).call(this, locale, options);
92129

93-
if (this.formatToParts) {
94-
this._nativeObject = new _DateTimeFormat(locale, options);
130+
if (_this.formatToParts) {
131+
_this._nativeObject = new _DateTimeFormat(locale, options);
95132
}
96133

97-
return;
134+
return _this;
98135
}
99136

100137
if (checkTimeZoneSupport(timeZone)) {
101138
// native method has support for timezone. no polyfill logic needed.
102-
_DateTimeFormat.call(this, locale, options);
139+
_this = _getPrototypeOf(DateTimeFormatPolyfill).call(this, locale, options);
103140

104-
if (this.formatToParts) {
105-
this._nativeObject = new _DateTimeFormat(locale, options);
141+
if (_this.formatToParts) {
142+
_this._nativeObject = new _DateTimeFormat(locale, options);
106143
}
107144

108-
return;
145+
return _this;
109146
}
110147

111148
const timeZoneData = gIntl._timeZoneData.get(timeZone);
@@ -118,16 +155,15 @@ export default function polyfill(globalSpace) {
118155
// Do a timeshift to UTC to avoid explosion due to unsupprted timezone.
119156
const tsOption = jsonClone(options);
120157
tsOption.timeZone = 'UTC';
121-
_DateTimeFormat.call(this, locale, tsOption);
158+
_this = _getPrototypeOf(DateTimeFormatPolyfill).call(this, locale, tsOption);
122159

123160
const _resolvedOptions = _get(
124-
DateTimeFormatPolyfill.prototype.__proto__ ||
125-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
161+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
126162
'resolvedOptions',
127-
this
163+
_this
128164
);
129165

130-
const resolvedLocale = _resolvedOptions.call(this).locale;
166+
const resolvedLocale = _resolvedOptions.call(_this).locale;
131167

132168
if (options.timeZoneName !== undefined) {
133169
// We need to include timeZoneName in date format.
@@ -139,26 +175,26 @@ export default function polyfill(globalSpace) {
139175
}
140176

141177
// to minimize pollution everything we need to perform polyfill is wrapped under one object.
142-
this._dateTimeFormatPolyfill = {
178+
_this._dateTimeFormatPolyfill = {
143179
optionTimeZone: timeZone,
144180
optionTimeZoneName: options.timeZoneName,
145181
timeZoneData: timeZoneData
146182
};
183+
184+
return _this;
147185
}
148-
_inherits(DateTimeFormatPolyfill, _DateTimeFormat);
186+
_inherits(DateTimeFormatPolyfill, _wrapNativeSuper(_DateTimeFormat));
149187

150188
Object.defineProperty(DateTimeFormatPolyfill.prototype, 'format', {
151189
configurable: true,
152190
value: function(date) {
153191
const _format = _get(
154-
DateTimeFormatPolyfill.prototype.__proto__ ||
155-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
192+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
156193
'format',
157194
this
158195
);
159196
const _resolvedOptions = _get(
160-
DateTimeFormatPolyfill.prototype.__proto__ ||
161-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
197+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
162198
'resolvedOptions',
163199
this
164200
);
@@ -219,8 +255,7 @@ export default function polyfill(globalSpace) {
219255
});
220256

221257
const _formatToParts = _get(
222-
DateTimeFormatPolyfill.prototype.__proto__ ||
223-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
258+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
224259
'formatToParts',
225260
this
226261
);
@@ -229,13 +264,11 @@ export default function polyfill(globalSpace) {
229264
Object.defineProperty(DateTimeFormatPolyfill.prototype, 'formatToParts', {
230265
configurable: true,
231266
value: function(date) {
232-
233267
const _resolvedOptions = _get(
234-
DateTimeFormatPolyfill.prototype.__proto__ ||
235-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
236-
'resolvedOptions',
237-
this
238-
);
268+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
269+
'resolvedOptions',
270+
this
271+
);
239272

240273
if (!this._dateTimeFormatPolyfill && this._nativeObject) {
241274
return this._nativeObject.formatToParts(date);
@@ -288,8 +321,7 @@ export default function polyfill(globalSpace) {
288321
configurable: true,
289322
value: function() {
290323
const _resolvedOptions = _get(
291-
DateTimeFormatPolyfill.prototype.__proto__ ||
292-
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
324+
_getPrototypeOf(DateTimeFormatPolyfill.prototype),
293325
'resolvedOptions',
294326
this
295327
);

0 commit comments

Comments
 (0)