Skip to content

Commit

Permalink
refactor: replace lodash for native alternative (#2242)
Browse files Browse the repository at this point in the history
- startsWith
- noop
- isFunction
- isString
- find

Refs #2187
  • Loading branch information
jimmywarting authored Sep 15, 2021
1 parent 2ea42d2 commit 1556f58
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import isObject from 'lodash/isObject';
import startsWith from 'lodash/startsWith';

const toLower = (str) => String.prototype.toLowerCase.call(str);
const escapeString = (str) => str.replace(/[^\w]/gi, '_');
Expand All @@ -11,7 +10,7 @@ export function isOAS3(spec) {
return false;
}

return startsWith(oasVersion, '3');
return oasVersion.startsWith('3');
}

export function isSwagger2(spec) {
Expand All @@ -20,7 +19,7 @@ export function isSwagger2(spec) {
return false;
}

return startsWith(swaggerVersion, '2');
return swaggerVersion.startsWith('2');
}

// Strategy for determining operationId
Expand Down
3 changes: 1 addition & 2 deletions src/http/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'cross-fetch/polyfill'; /* global fetch */
import qs from 'qs';
import jsYaml from 'js-yaml';
import isFunction from 'lodash/isFunction';
import { Buffer } from 'buffer';
import { FormData, File, Blob } from 'formdata-node';

Expand Down Expand Up @@ -142,7 +141,7 @@ function serializeHeaderValue(value) {
// Cookie: two
// = { Cookie: [ "one", "two" ]
export function serializeHeaders(headers = {}) {
if (!isFunction(headers.entries)) return {};
if (typeof headers.entries !== 'function') return {};

return Array.from(headers.entries()).reduce((acc, [header, value]) => {
acc[header] = serializeHeaderValue(value);
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import startsWith from 'lodash/startsWith';
import Url from 'url';

import Http, { makeHttp, serializeRes, serializeHeaders } from './http';
Expand Down Expand Up @@ -88,7 +87,7 @@ Swagger.prototype.applyDefaults = function applyDefaults() {
const { spec } = this;
const specUrl = this.url;
// TODO: OAS3: support servers here
if (specUrl && startsWith(specUrl, 'http')) {
if (specUrl && specUrl.startsWith('http')) {
const parsed = Url.parse(specUrl);
if (!spec.host) {
spec.host = parsed.host;
Expand Down
3 changes: 1 addition & 2 deletions src/specmap/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import traverse from 'traverse';
import URL from 'url';
import isString from 'lodash/isString';

// This will match if the direct parent's key exactly matches an item.
const freelyNamedKeyParents = ['properties'];
Expand Down Expand Up @@ -59,7 +58,7 @@ export function generateAbsoluteRefPatches(
const patches = [];

traverse(obj).forEach(function callback() {
if (targetKeys.includes(this.key) && isString(this.node)) {
if (targetKeys.includes(this.key) && typeof this.node === 'string') {
const nodePath = this.path; // this node's path, relative to `obj`
const fullPath = basePath.concat(this.path);

Expand Down
7 changes: 2 additions & 5 deletions src/specmap/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import find from 'lodash/find';
import noop from 'lodash/noop';

import lib from './lib';
import refs from './lib/refs';
import allOf from './lib/all-of';
Expand All @@ -9,6 +6,7 @@ import properties from './lib/properties';
import ContextTree from './lib/context-tree';

const HARD_LIMIT = 100;
const noop = () => {};

class SpecMap {
static getPluginName(plugin) {
Expand Down Expand Up @@ -154,8 +152,7 @@ class SpecMap {
}

nextPlugin() {
// Array.prototype.find doesn't work in IE 11 :(
return find(this.wrappedPlugins, (plugin) => {
return this.wrappedPlugins.find((plugin) => {
const mutations = this.getMutationsForPlugin(plugin);
return mutations.length > 0;
});
Expand Down

0 comments on commit 1556f58

Please sign in to comment.