Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Totally remove lodash #1247

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"axios": "^1.7.2",
"axios-ntlm": "^1.4.2",
"debug": "^4.3.5",
"deepmerge-ts": "^7.1.0",
"formidable": "^3.5.1",
"get-stream": "^6.0.1",
"lodash": "^4.17.21",
"sax": "^1.4.1",
"strip-bom": "^3.0.0",
"whatwg-mimetype": "4.0.0",
Expand Down
11 changes: 8 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { randomUUID } from 'crypto';
import debugBuilder from 'debug';
import { EventEmitter } from 'events';
import getStream = require('get-stream');
import * as _ from 'lodash';
import { HttpClient } from './http';
import { IHeaders, IHttpClient, IMTOMAttachments, IOptions, ISecurity, SoapMethod, SoapMethodAsync } from './types';
import { findPrefix } from './utils';
Expand Down Expand Up @@ -368,7 +367,7 @@ export class Client extends EventEmitter {
if (!output || !output.$lookupTypes) {
debug('Response element is not present. Unable to convert response xml to json.');
// If the response is JSON then return it as-is.
const json = _.isObject(body) ? body : tryJSONparse(body);
const json = body !== null && typeof body === 'object' ? body : tryJSONparse(body);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body can be undefined, should be something like

const json = body && typeof body === 'object' ? body : tryJSONparse(body);

if (json) {
return callback(null, response, json, undefined, xml);
}
Expand Down Expand Up @@ -482,7 +481,13 @@ export class Client extends EventEmitter {
};

if (this.streamAllowed && typeof this.httpClient.requestStream === 'function') {
callback = _.once(callback);
callback = (cb) => {
let lastResult;
return (...args) => {
if (lastResult === undefined) { lastResult = cb(...args); }
return lastResult;
};
};
const startTime = Date.now();
const onError = (err) => {
this.lastResponse = null;
Expand Down
6 changes: 3 additions & 3 deletions src/security/BasicAuthSecurity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import * as _ from 'lodash';
import { deepmergeInto } from 'deepmerge-ts';
import { IHeaders, ISecurity } from '../types';

export class BasicAuthSecurity implements ISecurity {
Expand All @@ -11,7 +11,7 @@ export class BasicAuthSecurity implements ISecurity {
this._username = username;
this._password = password;
this.defaults = {};
_.merge(this.defaults, defaults);
deepmergeInto(this.defaults, defaults);
}

public addHeaders(headers: IHeaders): void {
Expand All @@ -23,6 +23,6 @@ export class BasicAuthSecurity implements ISecurity {
}

public addOptions(options: any): void {
_.merge(options, this.defaults);
deepmergeInto(options, this.defaults);
}
}
6 changes: 3 additions & 3 deletions src/security/BearerSecurity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import * as _ from 'lodash';
import { deepmergeInto } from 'deepmerge-ts';
import { IHeaders, ISecurity } from '../types';

export class BearerSecurity implements ISecurity {
Expand All @@ -9,7 +9,7 @@ export class BearerSecurity implements ISecurity {
constructor(token: string, defaults?: any) {
this._token = token;
this.defaults = {};
_.merge(this.defaults, defaults);
deepmergeInto(this.defaults, defaults);
}

public addHeaders(headers: IHeaders): void {
Expand All @@ -21,6 +21,6 @@ export class BearerSecurity implements ISecurity {
}

public addOptions(options: any): void {
_.merge(options, this.defaults);
deepmergeInto(options, this.defaults);
}
}
6 changes: 3 additions & 3 deletions src/security/ClientSSLSecurity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { deepmergeInto } from 'deepmerge-ts';
import * as fs from 'fs';
import * as https from 'https';
import * as _ from 'lodash';
import { ISecurity } from '../types';

/**
Expand Down Expand Up @@ -53,7 +53,7 @@ export class ClientSSLSecurity implements ISecurity {
}

this.defaults = {};
_.merge(this.defaults, defaults);
deepmergeInto(this.defaults, defaults);

this.agent = null;
}
Expand All @@ -68,7 +68,7 @@ export class ClientSSLSecurity implements ISecurity {
options.key = this.key;
options.cert = this.cert;
options.ca = this.ca;
_.merge(options, this.defaults);
deepmergeInto(options, this.defaults);

if (!!options.forever) {
if (!this.agent) {
Expand Down
6 changes: 3 additions & 3 deletions src/security/ClientSSLSecurityPFX.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { deepmergeInto } from 'deepmerge-ts';
import * as fs from 'fs';
import * as https from 'https';
import * as _ from 'lodash';
import { ISecurity } from '../types';

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ export class ClientSSLSecurityPFX implements ISecurity {
}
}
this.defaults = {};
_.merge(this.defaults, defaults);
deepmergeInto(this.defaults, defaults);
}

public toXML(): string {
Expand All @@ -51,7 +51,7 @@ export class ClientSSLSecurityPFX implements ISecurity {
if (this.passphrase) {
options.passphrase = this.passphrase;
}
_.merge(options, this.defaults);
deepmergeInto(options, this.defaults);
options.httpsAgent = new https.Agent(options);
}
}
4 changes: 2 additions & 2 deletions src/security/NTLMSecurity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import * as _ from 'lodash';
import { deepmergeInto } from 'deepmerge-ts';
import { IHeaders, ISecurity } from '../types';

export class NTLMSecurity implements ISecurity {
Expand Down Expand Up @@ -30,6 +30,6 @@ export class NTLMSecurity implements ISecurity {
}

public addOptions(options: any): void {
_.merge(options, this.defaults);
deepmergeInto(options, this.defaults);
}
}
28 changes: 17 additions & 11 deletions src/wsdl/elements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { ok as assert } from 'assert';
import debugBuilder from 'debug';
import * as _ from 'lodash';
import { deepmergeInto, deepmergeIntoCustom } from 'deepmerge-ts';
import { IWsdlBaseOptions } from '../types';
import { splitQName, TNS_PREFIX } from '../utils';

Expand Down Expand Up @@ -146,7 +146,13 @@ export class Element {
}
const parent = stack[stack.length - 2];
if (this !== stack[0]) {
_.defaultsDeep(stack[0].xmlns, this.xmlns);
const defaultsDeep = deepmergeIntoCustom({
mergeOthers: (target, values) => {
// Multiple values means value was already present ⇒ keeping original
if (values.length > 1) { target.value = values[0]; }
},
});
defaultsDeep(stack[0].xmlns, this.xmlns);
// delete this.xmlns;
parent.children.push(this);
parent.addChild(this);
Expand Down Expand Up @@ -444,7 +450,7 @@ export class ExtensionElement extends Element {
);
if (typeElement) {
const base = typeElement.description(definitions, schema.xmlns);
desc = typeof base === 'string' ? base : _.defaults(base, desc);
desc = typeof base === 'string' ? base : Object.assign(base, desc);
}
}
}
Expand Down Expand Up @@ -791,15 +797,15 @@ export class SchemaElement extends Element {
public merge(source: SchemaElement) {
assert(source instanceof SchemaElement);

_.merge(this.complexTypes, source.complexTypes);
_.merge(this.types, source.types);
_.merge(this.elements, source.elements);
_.merge(this.xmlns, source.xmlns);
deepmergeInto(this.complexTypes, source.complexTypes);
deepmergeInto(this.types, source.types);
deepmergeInto(this.elements, source.elements);
deepmergeInto(this.xmlns, source.xmlns);

// Merge attributes from source without overwriting our's
_.merge(this, _.pickBy(source, (value, key) => {
return key.startsWith('$') && !this.hasOwnProperty(key);
}));
deepmergeInto(this,
Object.fromEntries(Object.entries(source).filter(([key]) => key.startsWith('$') && !this.hasOwnProperty(key))),
);

return this;
}
Expand Down Expand Up @@ -1115,7 +1121,7 @@ export class DefinitionsElement extends Element {
public addChild(child) {
if (child instanceof TypesElement) {
// Merge types.schemas into definitions.schemas
_.merge(this.schemas, child.schemas);
deepmergeInto(this.schemas, child.schemas);
} else if (child instanceof MessageElement) {
this.messages[child.$name] = child;
} else if (child.name === 'import') {
Expand Down
17 changes: 7 additions & 10 deletions src/wsdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import { ok as assert } from 'assert';
import debugBuilder from 'debug';
import { deepmergeCustom, deepmergeIntoCustom } from 'deepmerge-ts';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as path from 'path';
import * as sax from 'sax';
import * as stripBom from 'strip-bom';
Expand All @@ -27,12 +27,6 @@ export function trim(text) {
return text.trim();
}

function deepMerge<A, B>(destination: A, source: B): A & B {
return _.mergeWith(destination, source, (a, b) => {
return Array.isArray(a) ? a.concat(b) : undefined;
});
}

function appendColon(ns: string): string {
return (ns && ns.charAt(ns.length - 1) !== ':') ? ns + ':' : ns;
}
Expand Down Expand Up @@ -366,7 +360,7 @@ export class WSDL {
}
}

if (_.isPlainObject(obj) && !Object.keys(obj).length) {
if (obj !== undefined && typeof obj === 'object' && !Object.keys(obj).length) {
obj = null;
}

Expand Down Expand Up @@ -1226,9 +1220,12 @@ export class WSDL {
this._includesWsdl.push(wsdl);

if (wsdl.definitions instanceof elements.DefinitionsElement) {
_.mergeWith(this.definitions, wsdl.definitions, (a, b) => {
return (a instanceof elements.SchemaElement) ? a.merge(b) : undefined;
const mergeFunction = deepmergeCustom({
mergeOthers: (values, utils) => {
return (values[0] instanceof elements.SchemaElement) ? utils.defaultMergeFunctions.mergeOthers(values) : undefined;
},
});
this.definitions = mergeFunction(this.definitions, wsdl.definitions);
} else {
return callback(new Error('wsdl.defintions is not an instance of elements.DefinitionsElement'));
}
Expand Down
Loading
Loading