From 493ca2fec4e7cc32e074b2271817a4e02e345fd8 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 31 Oct 2018 16:57:01 -0400 Subject: [PATCH] grpc-js-core: remove simple uses of lodash This commit removes lodash as a production dependency. It remains as a devDependency because it's used in tests, but the uses in the src/ directory were easily replaced with vanilla JavaScript. --- packages/grpc-js-core/package.json | 2 +- packages/grpc-js-core/src/call-credentials.ts | 4 +--- packages/grpc-js-core/src/filter-stack.ts | 4 +--- packages/grpc-js-core/src/make-client.ts | 22 +++++++++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/grpc-js-core/package.json b/packages/grpc-js-core/package.json index b573a6fb..166c649d 100644 --- a/packages/grpc-js-core/package.json +++ b/packages/grpc-js-core/package.json @@ -20,6 +20,7 @@ "@types/node": "^10.5.4", "clang-format": "^1.0.55", "gts": "^0.5.1", + "lodash": "^4.17.4", "typescript": "~2.7.0" }, "contributors": [ @@ -41,7 +42,6 @@ "posttest": "npm run check" }, "dependencies": { - "lodash": "^4.17.4", "semver": "^5.5.0" }, "files": [ diff --git a/packages/grpc-js-core/src/call-credentials.ts b/packages/grpc-js-core/src/call-credentials.ts index 2a9d8195..212d49b2 100644 --- a/packages/grpc-js-core/src/call-credentials.ts +++ b/packages/grpc-js-core/src/call-credentials.ts @@ -1,5 +1,3 @@ -import {map} from 'lodash'; - import {Metadata} from './metadata'; export type CallMetadataOptions = { @@ -53,7 +51,7 @@ class ComposedCallCredentials extends CallCredentials { async generateMetadata(options: CallMetadataOptions): Promise { const base: Metadata = new Metadata(); const generated: Metadata[] = await Promise.all( - map(this.creds, (cred) => cred.generateMetadata(options))); + this.creds.map((cred) => cred.generateMetadata(options))); for (const gen of generated) { base.merge(gen); } diff --git a/packages/grpc-js-core/src/filter-stack.ts b/packages/grpc-js-core/src/filter-stack.ts index f9cd7d61..92232f4c 100644 --- a/packages/grpc-js-core/src/filter-stack.ts +++ b/packages/grpc-js-core/src/filter-stack.ts @@ -1,5 +1,3 @@ -import {map} from 'lodash'; - import {Call, StatusObject, WriteObject} from './call-stream'; import {Filter, FilterFactory} from './filter'; import {Metadata} from './metadata'; @@ -63,6 +61,6 @@ export class FilterStackFactory implements FilterFactory { createFilter(callStream: Call): FilterStack { return new FilterStack( - map(this.factories, (factory) => factory.createFilter(callStream))); + this.factories.map((factory) => factory.createFilter(callStream))); } } diff --git a/packages/grpc-js-core/src/make-client.ts b/packages/grpc-js-core/src/make-client.ts index 669723d0..3b03255f 100644 --- a/packages/grpc-js-core/src/make-client.ts +++ b/packages/grpc-js-core/src/make-client.ts @@ -1,5 +1,3 @@ -import * as _ from 'lodash'; - import {ChannelCredentials} from './channel-credentials'; import {ChannelOptions} from './channel-options'; import {Client} from './client'; @@ -73,10 +71,11 @@ export function makeClientConstructor( [methodName: string]: Function; } - _.each(methods, (attrs, name) => { + Object.keys(methods).forEach((name) => { + const attrs = methods[name]; let methodType: keyof typeof requesterFuncs; // TODO(murgatroid99): Verify that we don't need this anymore - if (_.startsWith(name, '$')) { + if (typeof name === 'string' && name.charAt(0) === '$') { throw new Error('Method names cannot start with $'); } if (attrs.requestStream) { @@ -94,11 +93,11 @@ export function makeClientConstructor( } const serialize = attrs.requestSerialize; const deserialize = attrs.responseDeserialize; - const methodFunc = _.partial( - requesterFuncs[methodType], attrs.path, serialize, deserialize); + const methodFunc = + partial(requesterFuncs[methodType], attrs.path, serialize, deserialize); ServiceClientImpl.prototype[name] = methodFunc; // Associate all provided attributes with the method - _.assign(ServiceClientImpl.prototype[name], attrs); + Object.assign(ServiceClientImpl.prototype[name], attrs); if (attrs.originalName) { ServiceClientImpl.prototype[attrs.originalName] = ServiceClientImpl.prototype[name]; @@ -110,6 +109,15 @@ export function makeClientConstructor( return ServiceClientImpl; } +function partial( + fn: Function, path: string, serialize: Function, + deserialize: Function): Function { + // tslint:disable-next-line:no-any + return function(this: any, ...args: any[]) { + return fn.call(this, path, serialize, deserialize, ...args); + }; +} + export type GrpcObject = { [index: string]: GrpcObject|ServiceClientConstructor; };