Skip to content

Commit

Permalink
refactor: reduce usage of extend (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed May 14, 2020
1 parent cf8f58f commit 0ec8052
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 44 deletions.
7 changes: 3 additions & 4 deletions src/bigquery.ts
Expand Up @@ -1295,8 +1295,7 @@ export class BigQuery extends common.Service {
options: JobOptions,
callback?: JobCallback
): void | Promise<JobResponse> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reqOpts: any = extend({}, options);
const reqOpts = Object.assign({}, options);
let jobId = reqOpts.jobId || uuid.v4();

if (reqOpts.jobId) {
Expand Down Expand Up @@ -1447,7 +1446,7 @@ export class BigQuery extends common.Service {
let nextQuery: GetDatasetsOptions | null = null;

if (resp.nextPageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.nextPageToken,
});
}
Expand Down Expand Up @@ -1551,7 +1550,7 @@ export class BigQuery extends common.Service {
}
let nextQuery: {} | null = null;
if (resp.nextPageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.nextPageToken,
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/dataset.ts
Expand Up @@ -772,7 +772,7 @@ class Dataset extends ServiceObject {

let nextQuery: {} | null = null;
if (resp.nextPageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.nextPageToken,
});
}
Expand Down Expand Up @@ -868,7 +868,7 @@ class Dataset extends ServiceObject {

let nextQuery: {} | null = null;
if (resp.nextPageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.nextPageToken,
});
}
Expand Down Expand Up @@ -957,7 +957,7 @@ class Dataset extends ServiceObject {

let nextQuery: {} | null = null;
if (resp.nextPageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.nextPageToken,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/job.ts
Expand Up @@ -426,10 +426,10 @@ class Job extends Operation {
let nextQuery: {} | null = null;
if (resp.jobComplete === false) {
// Query is still running.
nextQuery = extend({}, options);
nextQuery = Object.assign({}, options);
} else if (resp.pageToken) {
// More results exist.
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.pageToken,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/table.ts
Expand Up @@ -1693,7 +1693,7 @@ class Table extends common.ServiceObject {
}
let nextQuery: GetRowsOptions | null = null;
if (resp.pageToken) {
nextQuery = extend({}, options, {
nextQuery = Object.assign({}, options, {
pageToken: resp.pageToken,
});
}
Expand Down
20 changes: 10 additions & 10 deletions test/bigquery.ts
Expand Up @@ -57,7 +57,7 @@ interface CalledWithService extends Service {
}

let promisified = false;
const fakePfy = extend({}, pfy, {
const fakePfy = Object.assign({}, pfy, {
promisifyAll: (c: Function, options: pfy.PromisifyAllOptions) => {
if (c.name !== 'BigQuery') {
return;
Expand All @@ -74,7 +74,7 @@ const fakePfy = extend({}, pfy, {
]);
},
});
const fakeUtil = extend({}, util, {
const fakeUtil = Object.assign({}, util, {
ApiError: FakeApiError,
});
const originalFakeUtil = extend(true, {}, fakeUtil);
Expand Down Expand Up @@ -160,12 +160,12 @@ describe('BigQuery', () => {
'@google-cloud/paginator': fakePaginator,
'@google-cloud/promisify': fakePfy,
}).BigQuery;
BigQueryCached = extend({}, BigQuery);
BigQueryCached = Object.assign({}, BigQuery);
});

beforeEach(() => {
extend(fakeUtil, originalFakeUtil);
BigQuery = extend(BigQuery, BigQueryCached);
Object.assign(fakeUtil, originalFakeUtil);
BigQuery = Object.assign(BigQuery, BigQueryCached);
bq = new BigQuery({projectId: PROJECT_ID});
});

Expand Down Expand Up @@ -603,7 +603,7 @@ describe('BigQuery', () => {
});

it('should not include fractional digits if not provided', () => {
const input = extend({}, INPUT_OBJ);
const input = Object.assign({}, INPUT_OBJ);
delete input.fractional;

const time = bq.time(input);
Expand Down Expand Up @@ -1103,7 +1103,7 @@ describe('BigQuery', () => {
c: 'd',
};

const originalOptions = extend({}, options);
const originalOptions = Object.assign({}, options);

bq.request = (reqOpts: DecorateRequestOptions) => {
assert.notStrictEqual(reqOpts.json, options);
Expand Down Expand Up @@ -1197,7 +1197,7 @@ describe('BigQuery', () => {
a: 'b',
};

const expectedOptions = extend({}, fakeOptions, {
const expectedOptions = Object.assign({}, fakeOptions, {
jobReference: {
projectId: bq.projectId,
jobId: fakeJobId,
Expand Down Expand Up @@ -1737,7 +1737,7 @@ describe('BigQuery', () => {
});

const options = {a: 'b'};
const expectedOptions = extend({location: LOCATION}, options);
const expectedOptions = Object.assign({location: LOCATION}, options);

const ds = bq.dataset(DATASET_ID, options);
const args = ds.calledWith_;
Expand Down Expand Up @@ -2043,7 +2043,7 @@ describe('BigQuery', () => {
});

const options = {a: 'b'};
const expectedOptions = extend({location: LOCATION}, options);
const expectedOptions = Object.assign({location: LOCATION}, options);

const job = bq.job(JOB_ID, options);
const args = job.calledWith_;
Expand Down
8 changes: 4 additions & 4 deletions test/dataset.ts
Expand Up @@ -39,7 +39,7 @@ interface CalledWithDataset extends ServiceObject {
}

let promisified = false;
const fakePfy = extend({}, pfy, {
const fakePfy = Object.assign({}, pfy, {
promisifyAll: (c: Function, options: pfy.PromisifyAllOptions) => {
if (c.name !== 'Dataset') {
return;
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('BigQuery/Dataset', () => {
},
};

const expectedHeaders = extend({}, fakeReqOpts.headers, {
const expectedHeaders = Object.assign({}, fakeReqOpts.headers, {
'If-Match': FAKE_ETAG,
});

Expand Down Expand Up @@ -521,7 +521,7 @@ describe('BigQuery/Dataset', () => {
});

it('should pass the location to the Table', done => {
const response = extend({location: LOCATION}, API_RESPONSE);
const response = Object.assign({location: LOCATION}, API_RESPONSE);

ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
callback(null, response);
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('BigQuery/Dataset', () => {
});

it('should assign metadata to the Table object', done => {
const apiResponse = extend(
const apiResponse = Object.assign(
{
a: 'b',
c: 'd',
Expand Down
5 changes: 2 additions & 3 deletions test/job.ts
Expand Up @@ -17,7 +17,6 @@ import * as pfy from '@google-cloud/promisify';
import arrify = require('arrify');
import * as assert from 'assert';
import {describe, it, beforeEach, afterEach, before} from 'mocha';
import * as extend from 'extend';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';

Expand Down Expand Up @@ -46,7 +45,7 @@ interface CalledWithJob extends FakeOperation {
}

let promisified = false;
const fakePfy = extend({}, pfy, {
const fakePfy = Object.assign({}, pfy, {
promisifyAll: (c: Function) => {
if (c.name === 'Job') {
promisified = true;
Expand Down Expand Up @@ -213,7 +212,7 @@ describe('BigQuery/Job', () => {

it('should optionally accept options', done => {
const options = {a: 'b'};
const expectedOptions = extend({location: undefined}, options);
const expectedOptions = Object.assign({location: undefined}, options);

BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
assert.deepStrictEqual(reqOpts.qs, expectedOptions);
Expand Down
1 change: 0 additions & 1 deletion test/model.ts
Expand Up @@ -29,7 +29,6 @@ describe('BigQuery/Model', () => {
const MODEL_ID = 'my_model';
const DATASET = {id: 'my_dataset'} as Dataset;

// tslint:disable-next-line variable-name
let Model: typeof m.Model;
let model: m.Model;

Expand Down
25 changes: 9 additions & 16 deletions test/table.ts
Expand Up @@ -57,7 +57,7 @@ interface CalledWithTable extends ServiceObject {
let promisified = false;
let makeWritableStreamOverride: Function | null;
let isCustomTypeOverride: Function | null;
const fakeUtil = extend({}, util, {
const fakeUtil = Object.assign({}, util, {
isCustomType: (...args: Array<{}>) => {
return (isCustomTypeOverride || util.isCustomType)(...args);
},
Expand All @@ -66,7 +66,7 @@ const fakeUtil = extend({}, util, {
},
noop: () => {},
});
const fakePfy = extend({}, pfy, {
const fakePfy = Object.assign({}, pfy, {
promisifyAll: (c: Function) => {
if (c.name === 'Table') {
promisified = true;
Expand Down Expand Up @@ -94,8 +94,7 @@ const fakePaginator = {
},
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let fakeUuid: any = extend(true, {}, uuid);
let fakeUuid = extend(true, {}, uuid);

class FakeServiceObject extends ServiceObject {
calledWith_: IArguments;
Expand Down Expand Up @@ -143,7 +142,7 @@ describe('BigQuery/Table', () => {
});

beforeEach(() => {
fakeUuid = extend(fakeUuid, uuid);
fakeUuid = Object.assign(fakeUuid, uuid);
isCustomTypeOverride = null;
makeWritableStreamOverride = null;
tableOverrides = {};
Expand Down Expand Up @@ -214,7 +213,7 @@ describe('BigQuery/Table', () => {
});

it('should inherit from ServiceObject', done => {
const datasetInstance = extend({}, DATASET, {
const datasetInstance = Object.assign({}, DATASET, {
createTable: {
bind: (context: {}) => {
assert.strictEqual(context, datasetInstance);
Expand Down Expand Up @@ -277,7 +276,7 @@ describe('BigQuery/Table', () => {
},
};

const expectedHeaders = extend({}, fakeReqOpts.headers, {
const expectedHeaders = Object.assign({}, fakeReqOpts.headers, {
'If-Match': FAKE_ETAG,
});

Expand Down Expand Up @@ -1513,17 +1512,13 @@ describe('BigQuery/Table', () => {
});

describe('writable stream', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let fakeJob: any;
let fakeJob: EventEmitter;
let fakeJobId: string;

beforeEach(() => {
fakeJob = new EventEmitter();
fakeJobId = uuid.v4();

fakeUuid.v4 = () => {
return fakeJobId;
};
sandbox.stub(fakeUuid, 'v4').returns(fakeJobId);
});

it('should make a writable stream when written to', done => {
Expand Down Expand Up @@ -2045,9 +2040,7 @@ describe('BigQuery/Table', () => {
beforeEach(() => {
insertSpy = sinon.spy(table, '_insert');
requestStub = sinon.stub(table, 'request').resolves([{}]);
fakeUuid.v4 = () => {
return fakeInsertId;
};
sandbox.stub(fakeUuid, 'v4').returns(fakeInsertId);
});

afterEach(() => {
Expand Down

0 comments on commit 0ec8052

Please sign in to comment.