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

Commit 5357dd6

Browse files
author
Eric Koleda
committed
Use a better Object.assign polyfill
1 parent 41e698e commit 5357dd6

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/Service.gs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,14 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token,
431431
switch (this.paramLocation_) {
432432
case 'auth-header':
433433
params.headers =
434-
extend_(params.headers || {}, signer.toHeader(oauthParams));
434+
assign_({}, params.headers, signer.toHeader(oauthParams));
435435
break;
436436
case 'uri-query':
437437
url = buildUrl_(url, oauthParams);
438438
break;
439439
case 'post-body':
440-
params.payload =
441-
extend_(params.payload || {}, oauthParams);
440+
// Clone the payload.
441+
params.payload = assign_({}, params.payload, oauthParams);
442442
break;
443443
default:
444444
throw 'Unknown param location: ' + this.paramLocation_;

src/Utilities.gs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,38 @@ function validate_(params) {
4646
}
4747

4848
/**
49-
* Copy all of the properties in the source objects over to the
50-
* destination object, and return the destination object.
51-
* @param {Object} destination The combined object.
52-
* @param {Object} source The object who's properties are copied to the
53-
* destination.
54-
* @returns {Object} A combined object with the desination and source
55-
* properties.
56-
* @private
57-
* @see http://underscorejs.org/#extend
49+
* Polyfill for Object.assign, which isn't available on the legacy runtime.
50+
* Not assigning to Object to avoid overwriting behavior in the parent
51+
* script(s).
52+
* @param {Object} target The target object to apply the sources’ properties to,
53+
* which is returned after it is modified.
54+
* @param {...Object} sources The source object(s) containing the properties you
55+
* want to apply.
56+
* @returns {Object} The target object.
57+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill}
58+
* @license Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
5859
*/
59-
function extend_(destination, source) {
60-
// Use Object.assign from the v8 engine, if available.
61-
if (Object.assign) {
62-
return Object.assign(destination, source);
60+
function assign_(target, varArgs) {
61+
if (typeof Object.assign === 'function') {
62+
return Object.assign.apply(null, arguments);
63+
}
64+
if (target === null || target === undefined) {
65+
throw new TypeError('Cannot convert undefined or null to object');
6366
}
64-
var keys = Object.keys(source);
65-
for (var i = 0; i < keys.length; ++i) {
66-
destination[keys[i]] = source[keys[i]];
67+
var to = Object(target);
68+
for (var index = 1; index < arguments.length; index++) {
69+
var nextSource = arguments[index];
70+
71+
if (nextSource !== null && nextSource !== undefined) {
72+
for (var nextKey in nextSource) {
73+
// Avoid bugs when hasOwnProperty is shadowed
74+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
75+
to[nextKey] = nextSource[nextKey];
76+
}
77+
}
78+
}
6779
}
68-
return destination;
80+
return to;
6981
}
7082

7183
/**

0 commit comments

Comments
 (0)