Skip to content

Commit 76743d8

Browse files
committed
Add extend_
1 parent cbba1fd commit 76743d8

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/Service.gs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Service_.prototype.getAuthorizationUrl = function() {
268268
redirect_uri: redirectUri,
269269
state: state
270270
};
271-
params = _.extend(params, this.params_);
271+
params = extend_(params, this.params_);
272272
return buildUrl_(this.authorizationBaseUrl_, params);
273273
};
274274

@@ -298,7 +298,7 @@ Service_.prototype.handleCallback = function(callbackRequest) {
298298
'Accept': this.tokenFormat_
299299
};
300300
if (this.tokenHeaders_) {
301-
headers = _.extend(headers, this.tokenHeaders_);
301+
headers = extend_(headers, this.tokenHeaders_);
302302
}
303303
var tokenPayload = {
304304
code: code,
@@ -471,7 +471,7 @@ Service_.prototype.refresh = function() {
471471
'Accept': this.tokenFormat_
472472
};
473473
if (this.tokenHeaders_) {
474-
headers = _.extend(headers, this.tokenHeaders_);
474+
headers = extend_(headers, this.tokenHeaders_);
475475
}
476476
var tokenPayload = {
477477
refresh_token: token.refresh_token,
@@ -593,7 +593,7 @@ Service_.prototype.exchangeJwt_ = function() {
593593
'Accept': this.tokenFormat_
594594
};
595595
if (this.tokenHeaders_) {
596-
headers = _.extend(headers, this.tokenHeaders_);
596+
headers = extend_(headers, this.tokenHeaders_);
597597
}
598598
var response = UrlFetchApp.fetch(this.tokenUrl_, {
599599
method: 'post',

src/Utilities.gs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@ function validate_(params) {
5454
function getTimeInSeconds_(date) {
5555
return Math.floor(date.getTime() / 1000);
5656
}
57+
58+
/**
59+
* Copy all of the properties in the source objects over to the
60+
* destination object, and return the destination object.
61+
* @param {Object} destination The combined object.
62+
* @param {Object} source The object who's properties are copied to the destination.
63+
* @returns {Object} A combined object with the desination and source properties.
64+
* @see http://underscorejs.org/#extend
65+
*/
66+
function extend_(destination, source) {
67+
var keys = Object.keys(source);
68+
for (var i = 0; i < keys.length; ++i) {
69+
destination[keys[i]] = source[keys[i]];
70+
}
71+
return destination;
72+
}

test/test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var options = {
1717
};
1818
var OAuth2 = gas.require('./src', mocks, options);
1919

20-
2120
describe('Service', function() {
2221
describe('#getToken()', function() {
2322
it('should load from the in-memory store', function() {
@@ -110,3 +109,22 @@ describe('Service', function() {
110109
});
111110
});
112111
});
112+
113+
describe('Utilities', function() {
114+
describe('#extend_()', function() {
115+
var extend_ = OAuth2.extend_;
116+
var baseObj = {foo: [3]}; // An object with a non-primitive key-value
117+
it('should extend (left) an object', function() {
118+
var o = extend_(baseObj, {bar: 2});
119+
assert.deepEqual(o, {foo: [3], bar: 2});
120+
});
121+
it('should extend (right) an object', function() {
122+
var o = extend_({bar: 2}, baseObj);
123+
assert.deepEqual(o, {foo: [3], bar: 2});
124+
});
125+
it('should extend (merge) an object', function() {
126+
var o = extend_(baseObj, {foo: [100], bar: 2, baz: {}});
127+
assert.deepEqual(o, {foo: [100], bar: 2, baz: {}});
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)