Skip to content

Commit 6db0cf9

Browse files
committed
feat: sort out Lodash imports
1 parent d70e397 commit 6db0cf9

File tree

7 files changed

+61
-49
lines changed

7 files changed

+61
-49
lines changed

src/JsonSchemaViewer.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as React from 'react';
33
import { safeParse } from '@stoplight/json';
44
import { Dictionary, ISchema } from '@stoplight/types';
55
import { Box } from '@stoplight/ui-kit';
6-
import * as _ from 'lodash';
6+
import dropRight = require('lodash/dropRight');
7+
import isEmpty = require('lodash/isEmpty');
78

89
import { MutedText } from './common/MutedText';
910
import { Row } from './common/Row';
@@ -68,7 +69,7 @@ export class JsonSchemaViewer extends React.Component<IJsonSchemaViewer, IJsonSc
6869
}
6970

7071
try {
71-
if (!dereferencedSchema || _.isEmpty(dereferencedSchema)) {
72+
if (!dereferencedSchema || isEmpty(dereferencedSchema)) {
7273
parsed = dereferenceSchema(parsed, { definitions: schemas }, hideInheritedFrom);
7374
}
7475
} catch (e) {
@@ -122,11 +123,11 @@ export class JsonSchemaViewer extends React.Component<IJsonSchemaViewer, IJsonSc
122123

123124
if (limitPropertyCount) {
124125
if (!showExtra && propOverflowCount > 0) {
125-
rowElems = _.dropRight(rowElems, propOverflowCount);
126+
rowElems = dropRight(rowElems, propOverflowCount);
126127
}
127128
}
128129

129-
if (_.isEmpty(rowElems)) {
130+
if (isEmpty(rowElems)) {
130131
return emptyElem;
131132
}
132133

src/dereferenceSchema.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import * as _ from 'lodash';
1+
import cloneDeep = require('lodash/cloneDeep');
2+
import get = require('lodash/get');
3+
import isArray = require('lodash/isArray');
4+
import isEmpty = require('lodash/isEmpty');
5+
import map = require('lodash/map');
6+
import mapValues = require('lodash/mapValues');
27
import { IResolvedProp } from './types';
38

49
const _dereferenceSchema = (options: any = {}) => {
@@ -18,11 +23,11 @@ const _dereferenceSchema = (options: any = {}) => {
1823
let isCombiner;
1924
let isAllOf;
2025

21-
if (_.isEmpty(schemas)) {
26+
if (isEmpty(schemas)) {
2227
return schema;
2328
}
2429

25-
if (_.get(schema, '$ref')) {
30+
if (get(schema, '$ref')) {
2631
schema = _dereferenceSchemaRef({
2732
target: schema,
2833
schemas,
@@ -34,9 +39,9 @@ const _dereferenceSchema = (options: any = {}) => {
3439
hideInheritedFrom,
3540
isRoot,
3641
});
37-
} else if (_.get(schema, 'properties')) {
42+
} else if (get(schema, 'properties')) {
3843
properties = schema.properties;
39-
} else if (_.get(schema, 'items')) {
44+
} else if (get(schema, 'items')) {
4045
if (schema.items.$ref) {
4146
schema.items = _dereferenceSchemaRef({
4247
target: schema.items,
@@ -60,21 +65,21 @@ const _dereferenceSchema = (options: any = {}) => {
6065
properties = schema.items.anyOf;
6166
isCombiner = true;
6267
}
63-
} else if (_.get(schema, 'allOf')) {
68+
} else if (get(schema, 'allOf')) {
6469
properties = schema.allOf;
6570
isCombiner = true;
6671
isAllOf = true;
67-
} else if (_.get(schema, 'oneOf')) {
72+
} else if (get(schema, 'oneOf')) {
6873
properties = schema.oneOf;
6974
isCombiner = true;
70-
} else if (_.get(schema, 'anyOf')) {
75+
} else if (get(schema, 'anyOf')) {
7176
properties = schema.anyOf;
7277
isCombiner = true;
73-
} else if (_.get(schema, 'additionalProperties')) {
78+
} else if (get(schema, 'additionalProperties')) {
7479
properties = schema.additionalProperties;
75-
} else if (_.get(schema, 'patternProperties')) {
80+
} else if (get(schema, 'patternProperties')) {
7681
properties = schema.patternProperties;
77-
} else if (_.get(schema, 'dependencies')) {
82+
} else if (get(schema, 'dependencies')) {
7883
properties = schema.dependencies;
7984
}
8085

@@ -131,7 +136,7 @@ const _dereferenceSchemaRef = (options: any = {}) => {
131136

132137
// defensive clone to protect against mutations :(
133138
const refPath = ref.replace('#/', '').split('/');
134-
const schema = _.cloneDeep(_.get(schemas, refPath.concat('schema')) || _.get(schemas, refPath));
139+
const schema = cloneDeep(get(schemas, refPath.concat('schema')) || get(schemas, refPath));
135140

136141
if (schema) {
137142
let propsInherited: boolean | string = false;
@@ -161,12 +166,12 @@ const _dereferenceSchemaRef = (options: any = {}) => {
161166
}
162167

163168
if (propsInherited) {
164-
const iterFunc = _.isArray(schema[propsInherited]) ? _.map : _.mapValues;
169+
const iterFunc = isArray(schema[propsInherited]) ? map : mapValues;
165170

166171
// @ts-ignore
167172
schema[propsInherited] = iterFunc(schema[propsInherited], item => {
168173
if (item.properties) {
169-
item.properties = _.mapValues(item.properties, val => {
174+
item.properties = mapValues(item.properties, val => {
170175
return {
171176
...val,
172177
// Don't show hideInheritedFrom for combiners anymore.
@@ -219,7 +224,7 @@ const _dereferenceSchemaRef = (options: any = {}) => {
219224
/**
220225
* Replace all $ref's with their respective JSON Schema objects
221226
* @param {Object} target - Schema to dereference
222-
* @param {Object} schemas - Schemas that the target may reference. For example a Swagger or {definitions: {}}. This function will use _.get and .split('/') to find the reference path. #/responses/foo will be in {responses: {foo: {}}}
227+
* @param {Object} schemas - Schemas that the target may reference. For example a Swagger or {definitions: {}}. This function will use get and .split('/') to find the reference path. #/responses/foo will be in {responses: {foo: {}}}
223228
* @param {Boolean} hideInheritedFrom - Don't add the __inheritedFrom property to the dereferenced schema
224229
* @return {Object} - An object with no $refs
225230
*/
@@ -228,7 +233,7 @@ export const dereferenceSchema = (target: object, schemas: object, hideInherited
228233
return {};
229234
}
230235

231-
const schema = _.cloneDeep(target);
236+
const schema = cloneDeep(target);
232237

233238
return _dereferenceSchema({ target: schema, schemas, hideInheritedFrom, isRoot: true });
234239
};

src/renderers/renderCombiner.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Dictionary } from '@stoplight/types';
2-
import * as _ from 'lodash';
2+
import has = require('lodash/has');
3+
import set = require('lodash/set');
34
import { ICommonProps, IProp, IResolvedProp } from '../types';
45
import { renderProp } from './renderProp';
56
import { renderRowDivider } from './renderRowDivider';
@@ -25,8 +26,8 @@ export const renderCombiner = ({
2526
jsonPath,
2627
}: IRenderCombinerProp) => {
2728
for (const [e, elem] of Object.entries(props)) {
28-
if (!_.has(elem, 'type') && defaultType) {
29-
_.set(elem, 'type', defaultType);
29+
if (!has(elem, 'type') && defaultType) {
30+
set(elem, 'type', defaultType);
3031
}
3132

3233
const key = `${parentName}-c-${level}-${e}`;

src/renderers/renderProp.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* @jsx jsx */
22
import { jsx } from '@emotion/core';
33
import { Box, Flex, Textarea } from '@stoplight/ui-kit';
4-
5-
import * as _ from 'lodash';
4+
import has = require('lodash/has');
5+
import isEmpty = require('lodash/isEmpty');
6+
import isString = require('lodash/isString');
67
import { ReactNode, ReactNodeArray } from 'react';
8+
79
import { MutedText } from '../common/MutedText';
810
import { Row } from '../common/Row';
911
import { RowType } from '../common/RowType';
@@ -52,9 +54,7 @@ export const renderProp = ({
5254
let childPropType: 'object' | 'anyOf' | 'oneOf' | 'array';
5355
let isBasic = false;
5456
let expandable = false;
55-
const expanded = _.has(expandedRows, rowKey)
56-
? expandedRows[rowKey]
57-
: expandedRows.all || level <= defaultExpandedDepth;
57+
const expanded = has(expandedRows, rowKey) ? expandedRows[rowKey] : expandedRows.all || level <= defaultExpandedDepth;
5858

5959
if (prop.items) {
6060
if (prop.items.allOf) {
@@ -81,13 +81,13 @@ export const renderProp = ({
8181
}
8282
} else if (prop.oneOf) {
8383
propType = 'oneOf';
84-
expandable = !_.isEmpty(prop.oneOf);
84+
expandable = !isEmpty(prop.oneOf);
8585
} else if (prop.anyOf) {
8686
propType = 'anyOf';
87-
expandable = !_.isEmpty(prop.anyOf);
87+
expandable = !isEmpty(prop.anyOf);
8888
} else if (prop.allOf) {
8989
propType = 'object';
90-
expandable = !_.isEmpty(prop.allOf);
90+
expandable = !isEmpty(prop.allOf);
9191
} else {
9292
propType = prop.type;
9393
isBasic = !!(prop.properties || prop.patternProperties || propType === 'object');
@@ -100,14 +100,14 @@ export const renderProp = ({
100100
if (jsonPath === 'root') expandable = false;
101101

102102
let types: string[] = [];
103-
if (_.isString(propType)) {
103+
if (isString(propType)) {
104104
types = [propType];
105105
} else {
106106
types = propType;
107107
}
108108

109109
let typeElems: ReactNodeArray = [];
110-
if (!_.isEmpty(types)) {
110+
if (!isEmpty(types)) {
111111
typeElems = types.reduce((acc: ReactNode[], type: string, i) => {
112112
acc.push(
113113
<span key={i} className={`sl--${type}`}>
@@ -158,7 +158,7 @@ export const renderProp = ({
158158
);
159159
}
160160

161-
const showInheritedFrom = !hideInheritedFrom && !_.isEmpty(prop.__inheritedFrom);
161+
const showInheritedFrom = !hideInheritedFrom && !isEmpty(prop.__inheritedFrom);
162162

163163
if (!(hideRoot && jsonPath === 'root')) {
164164
rowElems.push(
@@ -185,10 +185,10 @@ export const renderProp = ({
185185
<Flex alignItems="baseline">
186186
{name && name !== 'root' ? <Box mr={3}>{name}</Box> : null}
187187

188-
{!_.isEmpty(typeElems) && <RowType name={name}>{typeElems}</RowType>}
188+
{!isEmpty(typeElems) && <RowType name={name}>{typeElems}</RowType>}
189189
</Flex>
190190

191-
{!_.isEmpty(prop.description) ? <Textarea className="text-muted text-sm" value={prop.description} /> : null}
191+
{!isEmpty(prop.description) ? <Textarea className="text-muted text-sm" value={prop.description} /> : null}
192192
</Box>
193193

194194
{requiredElem || showInheritedFrom || expanded ? (

src/renderers/renderSchema.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as _ from 'lodash';
2-
1+
import includes = require('lodash/includes');
2+
import isEmpty = require('lodash/isEmpty');
3+
import pick = require('lodash/pick');
34
import { ICommonProps } from '../types';
45
import { renderAllOf } from './renderAllOf';
56
import { renderCombiner } from './renderCombiner';
@@ -23,7 +24,7 @@ export const renderSchema = ({
2324
jsonPath,
2425
hideRoot,
2526
}: IRenderSchemaProps) => {
26-
if (!schema || _.isEmpty(schema)) {
27+
if (!schema || isEmpty(schema)) {
2728
return rowElems;
2829
}
2930

@@ -36,7 +37,7 @@ export const renderSchema = ({
3637
defaultExpandedDepth,
3738
parentName: name,
3839
propName: name,
39-
required: _.includes(schema.required || [], name),
40+
required: includes(schema.required || [], name),
4041
hideInheritedFrom,
4142
jsonPath,
4243
hideRoot,
@@ -50,7 +51,7 @@ export const renderSchema = ({
5051
};
5152

5253
if (!hideInheritedFrom && schema.__inheritedFrom) {
53-
Object.assign(prop, _.pick(schema, '__inheritedFrom'));
54+
Object.assign(prop, pick(schema, '__inheritedFrom'));
5455
}
5556

5657
rowElems = renderProp({

src/util/buildAllOfSchema.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as _ from 'lodash';
1+
import isArray = require('lodash/isArray');
2+
import merge = require('lodash/merge');
3+
import union = require('lodash/union');
24

35
export const buildAllOfSchema = ({ elems, schema = {} }: any) => {
46
for (const e in elems) {
@@ -13,10 +15,10 @@ export const buildAllOfSchema = ({ elems, schema = {} }: any) => {
1315
buildAllOfSchema({ elems: targetElems.allOf, schema });
1416
} else {
1517
for (const key in targetElems) {
16-
if (_.isArray(targetElems[key])) {
17-
schema[key] = _.union(schema[key], targetElems[key]);
18+
if (isArray(targetElems[key])) {
19+
schema[key] = union(schema[key], targetElems[key]);
1820
} else if (typeof targetElems[key] === 'object') {
19-
schema[key] = _.merge(schema[key], targetElems[key]);
21+
schema[key] = merge(schema[key], targetElems[key]);
2022
} else {
2123
schema[key] = targetElems[key];
2224
}

src/util/isSchemaViewerEmpty.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import * as _ from 'lodash';
1+
import get = require('lodash/get');
2+
import isEmpty = require('lodash/isEmpty');
3+
import keys = require('lodash/keys');
24

35
const combinerTypes = ['allOf', 'oneOf', 'anyOf'];
46

57
export const isSchemaViewerEmpty = (schema: object | string) => {
6-
const keys = _.keys(schema);
8+
const objectKeys = keys(schema);
79

8-
if (keys.length === 1 && combinerTypes.includes(keys[0])) {
9-
return _.isEmpty(_.get(schema, keys[0], []));
10+
if (objectKeys.length === 1 && combinerTypes.includes(objectKeys[0])) {
11+
return isEmpty(get(schema, objectKeys[0], []));
1012
}
1113

1214
return false;

0 commit comments

Comments
 (0)