Skip to content

Commit

Permalink
refactor: clean up lodash and use strict (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Nov 5, 2018
1 parent f7444da commit 8f79896
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 142 deletions.
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
"google-proto-files": "^0.17.0",
"grpc": "^1.15.1",
"is-stream-ended": "^0.1.4",
"lodash": "^4.17.10",
"lodash.at": "^4.6.0",
"lodash.flatten": "^4.4.0",
"lodash.has": "^4.5.2",
"protobufjs": "^6.8.8",
"retry-request": "^4.0.0",
"semver": "^5.5.1",
"through2": "^2.0.3"
"semver": "^5.5.1"
},
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/duplexify": "^3.5.0",
"@types/extend": "^3.0.0",
"@types/globby": "^8.0.0",
"@types/lodash": "^4.14.109",
"@types/lodash.at": "^4.6.4",
"@types/lodash.flatten": "^4.4.4",
"@types/lodash.has": "^4.5.4",
"@types/mocha": "^5.2.1",
"@types/node": "^10.3.2",
"@types/proxyquire": "^1.3.28",
Expand All @@ -54,7 +57,8 @@
"sinon": "^7.0.0",
"source-map-support": "^0.5.6",
"stream-events": "^1.0.4",
"typescript": "~3.1.0"
"through2": "^2.0.3",
"typescript": "~3.1.5"
},
"scripts": {
"codecov": "istanbul test ./node_modules/mocha/bin/_mocha -- build/test --reporter spec --slow 500 && ./node_modules/codecov/bin/codecov",
Expand Down
7 changes: 1 addition & 6 deletions src/api_callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@
* Provides function wrappers that implement page streaming and retrying.
*/

'use strict';

import * as util from 'util';
import {RetryOptions, CallSettings} from './gax';
import {Duplex} from 'stream';
import {CallSettings, RetryOptions} from './gax';
import {GoogleError} from './GoogleError';
import {BundleDescriptor, PageDescriptor} from '.';

export interface ArgumentFunction {
(argument: {}, callback: APICallback): void;
Expand Down
1 change: 0 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
'use strict';

import {GoogleError} from './GoogleError';

Expand Down
6 changes: 2 additions & 4 deletions src/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
* Provides behavior that supports request bundling.
*/

'use strict';

import * as _ from 'lodash';
import at = require('lodash.at');
import {NormalApiCaller, APICall, PromiseCanceller, APICallback} from './api_callable';
import {GoogleError} from './GoogleError';
import {CallSettings} from './gax';
Expand All @@ -62,7 +60,7 @@ export function computeBundleId(obj: {}, discriminatorFields: string[]) {
const ids: Array<{}|null> = [];
let hasIds = false;
for (let i = 0; i < discriminatorFields.length; ++i) {
const id = _.at(obj, discriminatorFields[i])[0];
const id = at(obj, discriminatorFields[i])[0];
if (id === undefined) {
ids.push(null);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/gax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
* Google API Extensions
*/

'use strict';

import {BundleOptions} from './bundling';

/**
Expand Down
10 changes: 5 additions & 5 deletions src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
'use strict';

import * as grpcProtoLoaderTypes from '@grpc/proto-loader'; // for types only
import * as fs from 'fs';
import * as globby from 'globby';
import * as grpcTypes from 'grpc'; // for types only
import * as grpcProtoLoaderTypes from '@grpc/proto-loader'; // for types only
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import * as grpcTypes from 'grpc'; // for types only
import {OutgoingHttpHeaders} from 'http';
import * as path from 'path';
import * as protobuf from 'protobufjs';
import * as semver from 'semver';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';

import * as gax from './gax';
import {OutgoingHttpHeaders} from 'http';

let googleProtoFilesDir = require('google-proto-files')('..');
googleProtoFilesDir = path.normalize(googleProtoFilesDir);
Expand Down
34 changes: 13 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,32 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

import * as extend from 'extend';

import {GrpcClient, GrpcClientOptions} from './grpc';
import * as operationsClient from './operations_client';
import * as routingHeader from './routing_header';
import {GrpcClient, GrpcClientOptions} from './grpc';

export {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
export {routingHeader};
export {constructSettings, CallOptions, ClientConfig} from './gax';
export {StreamType, StreamDescriptor} from './streaming';
export {LongrunningDescriptor, operation} from './longrunning';
export {BundleDescriptor, BundleExecutor} from './bundling';
export {PathTemplate} from './path_template';
export {PageDescriptor} from './paged_iteration';
export {createApiCall} from './api_callable';
export {BundleDescriptor, BundleExecutor} from './bundling';
export {CallOptions, ClientConfig, constructSettings} from './gax';
export {GoogleError} from './GoogleError';
export {ClientStub, ClientStubOptions, GrpcClient, GrpcClientOptions, GrpcModule, GrpcObject, GoogleProtoFilesRoot, Metadata, MetadataValue} from './grpc';
export {ClientStub, ClientStubOptions, GoogleProtoFilesRoot, GrpcClient, GrpcClientOptions, GrpcModule, GrpcObject, Metadata, MetadataValue} from './grpc';
export {LongrunningDescriptor, operation} from './longrunning';
export {PageDescriptor} from './paged_iteration';
export {PathTemplate} from './path_template';
export {StreamDescriptor, StreamType} from './streaming';
export {routingHeader};

function lro(options: GrpcClientOptions) {
options = extend(
{
// tslint:disable-next-line no-any
scopes: (lro as any).ALL_SCOPES,
},
options);
options = extend({scopes: lro.ALL_SCOPES}, options);
const gaxGrpc = new GrpcClient(options);
return new operationsClient.OperationsClientBuilder(gaxGrpc);
}
// tslint:disable-next-line no-any
(lro as any).SERVICE_ADDRESS = operationsClient.SERVICE_ADDRESS;
// tslint:disable-next-line no-any
(lro as any).ALL_SCOPES = operationsClient.ALL_SCOPES;

lro.SERVICE_ADDRESS = operationsClient.SERVICE_ADDRESS;
lro.ALL_SCOPES = operationsClient.ALL_SCOPES;

export {lro};
export const createByteLengthFunction = GrpcClient.createByteLengthFunction;
Expand Down
6 changes: 2 additions & 4 deletions src/operations_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
* The only allowed edits are to method and file documentation. A 3-way
* merge preserves those additions if the generated source changes.
*/
/* TODO: introduce line-wrapping so that it never exceeds the limit. */
/* jscs: disable maximumLineLength */
'use strict';

import * as extend from 'extend';

import * as apiCallable from './api_callable';
import * as gax from './gax';
import * as pathTemplate from './path_template';
import * as pagedIteration from './paged_iteration';
import * as pathTemplate from './path_template';

const configData = require('./operations_client_config');

Expand Down
8 changes: 3 additions & 5 deletions src/paged_iteration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';

import * as extend from 'extend';
import * as through2 from 'through2';
import * as ended from 'is-stream-ended';
import {Transform} from 'stream';
import {PassThrough, Transform} from 'stream';

import {NormalApiCaller, APICall, APICallback} from './api_callable';
import {APICall, APICallback, NormalApiCaller} from './api_callable';

export class PagedIteration extends NormalApiCaller {
pageDescriptor: PageDescriptor;
Expand Down Expand Up @@ -161,7 +159,7 @@ export class PageDescriptor {
* @return {Stream} - a new object Stream.
*/
createStream(apiCall, request, options): Transform {
const stream = through2.obj();
const stream = new PassThrough({objectMode: true});
options = extend({}, options, {autoPaginate: false});
const maxResults = 'maxResults' in options ? options.maxResults : -1;
let pushCount = 0;
Expand Down
7 changes: 1 addition & 6 deletions src/parser_extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
'use strict';

import * as util from 'util';
import * as _ from 'lodash';
import {Segment} from './path_template';

/* constants used in the pegjs parser */
Expand Down Expand Up @@ -73,10 +71,7 @@ function allowOnePathWildcard(segments: Segment[]) {
* @return {number} the number of terminal segments in the template
*/
function countTerminals(segments: Segment[]) {
const terms = _.filter(segments, x => {
return x.kind === TERMINAL;
});
return terms.length;
return segments.filter(x => x.kind === TERMINAL).length;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/path_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
'use strict';

/*
* Path template utility.
*/

import * as _ from 'lodash';
import has = require('lodash.has');
import * as util from 'util';
import * as extras from './parser_extras';
const parser = require('./path_template_parser');
Expand Down Expand Up @@ -134,7 +133,7 @@ export class PathTemplate {
let inABinding = false;
this.segments.forEach(segment => {
if (segment.kind === extras.BINDING) {
if (!_.has(bindings, segment.literal)) {
if (!has(bindings, segment.literal)) {
const msg = util.format(
'Value for key %s is not provided in %s', segment.literal,
bindings);
Expand Down
5 changes: 2 additions & 3 deletions src/path_template_parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
*/

{
const _ = require('lodash');
const util = require('util');
const flatten = require('lodash.flatten');
const extras = require('./parser_extras');
}

Expand All @@ -56,7 +55,7 @@ bound_segment

variable
= '{' l:literal '=' segments:unbound_segments '}' {
return _.flatten([
return flatten([
{ kind: extras.BINDING, literal: l },
segments,
{ kind: extras.END_BINDING, literal: '' }
Expand Down
8 changes: 2 additions & 6 deletions src/path_template_parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-disable */
module.exports = (() => {
'use strict';

/*
* Generated by PEG.js 0.9.0.
*
Expand Down Expand Up @@ -54,7 +51,7 @@ module.exports = (() => {
const peg$c9 = '}';
const peg$c10 = {type: 'literal', value: '}', description: '"}"'};
const peg$c11 = (l, segments) => {
return _.flatten([
return flatten([
{kind: extras.BINDING, literal: l},
segments,
{kind: extras.END_BINDING, literal: ''},
Expand Down Expand Up @@ -620,8 +617,7 @@ module.exports = (() => {
return s0;
}

const _ = require('lodash');
const util = require('util');
const flatten = require('lodash.flatten');
const extras = require('./parser_extras');

peg$result = peg$startRuleFunction();
Expand Down
2 changes: 0 additions & 2 deletions src/routing_header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
* Generally, these headers are specified as gRPC metadata.
*/

'use strict';

/**
* Constructs the routing header from the given params
*
Expand Down
6 changes: 3 additions & 3 deletions src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';

/* This file describes the gRPC-streaming. */

import * as Duplexify from 'duplexify';
import {Stream, Duplex, DuplexOptions} from 'stream';
import {APICallback, APICall} from './api_callable';
import {Duplex, DuplexOptions, Stream} from 'stream';

import {APICall, APICallback} from './api_callable';

const retryRequest = require('retry-request');

Expand Down
8 changes: 3 additions & 5 deletions test/api_callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* jshint expr: true*/

'use strict';

import {expect} from 'chai';
import * as sinon from 'sinon';
import {GoogleError} from '../src/GoogleError';

import * as gax from '../src/gax';
import {GoogleError} from '../src/GoogleError';

import * as utils from './utils';

const fail = utils.fail;
Expand Down
1 change: 0 additions & 1 deletion test/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
'use strict';

import {expect} from 'chai';
import * as sinon from 'sinon';
Expand Down
Loading

0 comments on commit 8f79896

Please sign in to comment.