Skip to content

Commit 2262746

Browse files
committed
Replace some usage of lodash with native JS alternatives
Change-type: patch
1 parent 522f93b commit 2262746

File tree

16 files changed

+92
-74
lines changed

16 files changed

+92
-74
lines changed

src/components/DownloadImageDialog/ApplicationInstructions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import has from 'lodash/has';
21
import { interpolateMustache } from './utils';
32
import { Box, List, Tab, Tabs, Typography } from '@mui/material';
43
import { memo, useEffect, useState } from 'react';
@@ -133,7 +132,8 @@ interface InstructionsListProps {
133132
}
134133

135134
const InstructionsItem = ({ node, index }: InstructionsItemProps) => {
136-
const hasChildren = has(node, 'children');
135+
const hasChildren =
136+
node != null && Object.hasOwnProperty.call(node, 'children');
137137
let text = null;
138138

139139
if (typeof node === 'string') {

src/components/DownloadImageDialog/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import { DropDownButton } from '../DropDownButton';
2020

2121
import pickBy from 'lodash/pickBy';
2222
import debounce from 'lodash/debounce';
23-
import isEmpty from 'lodash/isEmpty';
2423
import type { DeviceType, Dictionary, OsVersionsByDeviceType } from './models';
2524
import { OsTypesEnum } from './models';
26-
import uniq from 'lodash/uniq';
2725
import { enqueueSnackbar } from 'notistack';
2826
import { DialogWithCloseButton } from '../DialogWithCloseButton';
2927
import type { CalloutProps } from '../Callout';
@@ -62,14 +60,14 @@ const getUniqueOsTypes = (
6260
deviceTypeSlug: string | undefined,
6361
) => {
6462
if (
65-
isEmpty(osVersions) ||
63+
Object.keys(osVersions).length === 0 ||
6664
!deviceTypeSlug ||
67-
isEmpty(osVersions[deviceTypeSlug])
65+
osVersions[deviceTypeSlug].length === 0
6866
) {
6967
return [];
7068
}
7169

72-
return uniq(osVersions[deviceTypeSlug].map((x) => x.osType));
70+
return Array.from(new Set(osVersions[deviceTypeSlug].map((x) => x.osType)));
7371
};
7472

7573
const generateImageUrl = (
@@ -230,7 +228,9 @@ export const DownloadImageDialog = ({
230228
}
231229
: {},
232230
);
233-
const [isFetching, setIsFetching] = useState(isEmpty(osVersions));
231+
const [isFetching, setIsFetching] = useState(
232+
Object.keys(osVersions).length === 0,
233+
);
234234
const [downloadSize, setDownloadSize] = useState<string | null>(null);
235235
const [isValidatingUrl, setIsValidatingUrl] = useState(false);
236236

@@ -469,7 +469,7 @@ export const DownloadImageDialog = ({
469469
<Spinner />
470470
) : (
471471
<>
472-
{isEmpty(osVersions) && (
472+
{Object.keys(osVersions).length === 0 && (
473473
<Callout severity="warning">
474474
No OS versions available for download
475475
</Callout>

src/components/DownloadImageDialog/version.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import uniq from 'lodash/uniq';
21
import partition from 'lodash/partition';
32
import type { Dictionary, OsVersion } from './models';
43

@@ -76,7 +75,7 @@ export const getPreferredVersionOpts = (
7675

7776
const opts = supportedVersions.length ? supportedVersions : legacyVersions;
7877

79-
const lines = uniq(opts.map((option) => option.line));
78+
const lines = Array.from(new Set(opts.map((option) => option.line)));
8079
const hasMultipleLines = lines.length > 1;
8180

8281
if (hasMultipleLines) {

src/components/DropDownButton/index.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Button, ButtonGroup, MenuItem, Menu } from '@mui/material';
99
import { ButtonWithTracking } from '../ButtonWithTracking';
1010
import { useAnalyticsContext } from '../../contexts/AnalyticsContext';
1111
import groupBy from 'lodash/groupBy';
12-
import flatMap from 'lodash/flatMap';
1312
import { Tooltip } from '../Tooltip';
1413
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
1514
import {
@@ -59,16 +58,18 @@ export const DropDownButton = <T extends unknown>({
5958
return items;
6059
}
6160
const grouped = groupBy(items, (item) => item[groupByProp]);
62-
const keys = Object.keys(grouped);
63-
const lastKey = keys[keys.length - 1];
61+
const entries = Object.entries(grouped);
62+
const lastKey = entries[entries.length - 1][0];
6463

65-
return flatMap(grouped, (value, key) => [
66-
...value.map((v, index) =>
67-
key !== lastKey && index === value.length - 1
68-
? { ...v, divider: true }
69-
: v,
70-
),
71-
]).filter((item) => item);
64+
return entries
65+
.flatMap(([key, value]) =>
66+
value.map((v, index) =>
67+
key !== lastKey && index === value.length - 1
68+
? { ...v, divider: true }
69+
: v,
70+
),
71+
)
72+
.filter(Boolean);
7273
}, [items, groupByProp]);
7374

7475
const handleClick = (

src/components/Form/Widgets/FileWidget.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
faUpload,
3030
} from '@fortawesome/free-solid-svg-icons';
3131
import { useRandomUUID } from '../../../hooks/useRandomUUID';
32-
import uniq from 'lodash/uniq';
3332
import { token } from '../../../utils/token';
3433

3534
const restingStyle: SxProps = {
@@ -306,12 +305,14 @@ export const FileWidget = ({
306305
<Typography color={token('color.text.subtle')}>
307306
{accept == null
308307
? ''
309-
: `Supported formats: ${uniq(
310-
Object.values(accept)
311-
// The values of accept are arrays of file types, take them and flatten them
312-
.flat()
313-
// Remove the dot from the file type and capitalize it
314-
.map((fileType) => fileType.slice(1).toUpperCase()),
308+
: `Supported formats: ${Array.from(
309+
new Set(
310+
Object.values(accept)
311+
// The values of accept are arrays of file types, take them and flatten them
312+
.flat()
313+
// Remove the dot from the file type and capitalize it
314+
.map((fileType) => fileType.slice(1).toUpperCase()),
315+
),
315316
).join(', ')}`}
316317
</Typography>
317318
{maxSize != null && (

src/components/RJST/DataTypes/object.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { JSONSchema7 as JSONSchema } from 'json-schema';
2-
import find from 'lodash/find';
32
import type { CreateFilter, KeysOfUnion } from './utils';
43
import { getDataTypeSchema, regexEscape } from './utils';
54
import type { FormData } from '../components/Filters/SchemaSieve';
@@ -8,23 +7,43 @@ import {
87
createModelFilter,
98
} from '../components/Filters/SchemaSieve';
109
import { isJSONSchema, getRefSchema } from '../schemaOps';
11-
import findKey from 'lodash/findKey';
1210
import pick from 'lodash/pick';
1311
import mapValues from 'lodash/mapValues';
1412

13+
const findValueByDescription = (
14+
obj: object | undefined,
15+
description: string,
16+
) => {
17+
return Object.values(obj ?? {}).find(
18+
(property) =>
19+
typeof property === 'object' &&
20+
'description' in property &&
21+
property.description === description,
22+
) as JSONSchema | undefined;
23+
};
24+
25+
const findKeyByDescription = (obj: object | undefined, description: string) => {
26+
return Object.entries(obj ?? {}).find(
27+
([, value]) =>
28+
typeof value === 'object' &&
29+
'description' in value &&
30+
value.description === description,
31+
)?.[0];
32+
};
33+
1534
const getKeyLabel = (schema: JSONSchema) => {
16-
const s = find(schema.properties, { description: 'key' }) as JSONSchema;
35+
const s = findValueByDescription(schema.properties, 'key');
1736
return s?.title ?? 'key';
1837
};
1938

2039
const getValueLabel = (schema: JSONSchema) => {
21-
const s = find(schema.properties, { description: 'value' }) as JSONSchema;
40+
const s = findValueByDescription(schema.properties, 'value');
2241
return s?.title ?? 'value';
2342
};
2443

2544
export const isKeyValueObj = (schema: JSONSchema) =>
26-
!!find(schema.properties, { description: 'key' }) ||
27-
!!find(schema.properties, { description: 'value' });
45+
!!findValueByDescription(schema.properties, 'key') ||
46+
!!findValueByDescription(schema.properties, 'value');
2847

2948
export const operators = (s: JSONSchema) => {
3049
return {
@@ -94,7 +113,7 @@ const getValueForOperation = (
94113
? 'value'
95114
: null;
96115
const schemaProperty = schemaField
97-
? findKey(schema.properties, { description: schemaField })
116+
? findKeyByDescription(schema.properties, schemaField)
98117
: null;
99118

100119
// Return the appropriate value format based on the operation type
@@ -191,8 +210,8 @@ export const createFilter: CreateFilter<OperatorSlug> = (
191210

192211
// TODO: this case does not cover complex objects for FULL_TEXT_SLUG
193212
if (operator === FULL_TEXT_SLUG && schema.properties) {
194-
const schemaKey = findKey(schema.properties, { description: 'key' });
195-
const schemaValue = findKey(schema.properties, { description: 'value' });
213+
const schemaKey = findKeyByDescription(schema.properties, 'key');
214+
const schemaValue = findKeyByDescription(schema.properties, 'value');
196215
const properties = [schemaKey, schemaValue]
197216
.map((key) =>
198217
key

src/components/RJST/Filters/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
parseDescription,
1111
parseDescriptionProperty,
1212
} from '../schemaOps';
13-
import isEmpty from 'lodash/isEmpty';
1413
import get from 'lodash/get';
1514

1615
const X_FOREIGN_KEY_SCHEMA_SEPARATOR = '___ref_scheme_separator_';
@@ -69,12 +68,12 @@ export const removeFieldsWithNoFilter = (schema: JSONSchema): JSONSchema => {
6968
}
7069

7170
const hasEmptyProperties =
72-
newValue.properties && isEmpty(newValue.properties);
71+
newValue.properties && Object.keys(newValue.properties).length === 0;
7372

7473
const hasEmptyItemsProperties =
7574
isJSONSchema(newValue.items) &&
7675
'properties' in newValue.items &&
77-
isEmpty(newValue.items.properties);
76+
Object.keys(newValue.items.properties ?? {}).length === 0;
7877

7978
if (hasEmptyProperties || hasEmptyItemsProperties) {
8079
continue;

src/components/RJST/Lenses/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as types from './types';
22
import type { IconProp } from '@fortawesome/fontawesome-svg-core';
3-
import uniq from 'lodash/uniq';
43

54
export interface LensTemplate<T extends { id: number } = any> {
65
slug: string;
@@ -32,7 +31,7 @@ export const getLenses = <T extends { id: number }>(
3231
);
3332

3433
const slugs = concatenatedLenses.map((lens) => lens.slug);
35-
if (slugs.length > uniq(slugs).length) {
34+
if (slugs.length > new Set(slugs).size) {
3635
throw new Error('Lenses must have unique slugs');
3736
}
3837

src/components/RJST/components/Table/useTagActions.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useMemo, useState } from 'react';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
33
import { faPlus } from '@fortawesome/free-solid-svg-icons/faPlus';
4-
import uniq from 'lodash/uniq';
54
import type { RJSTContext } from '../../schemaOps';
65
import { useQuery } from '@tanstack/react-query';
76
import { Stack } from '@mui/material';
@@ -28,7 +27,7 @@ export function useTagActions<T extends object>(
2827
) as Array<{
2928
tag_key: string;
3029
}>;
31-
return uniq(tags.map((tag) => tag.tag_key)) ?? null;
30+
return Array.from(new Set(tags.map((tag) => tag.tag_key))) ?? null;
3231
},
3332
});
3433

src/components/RJST/components/Widget/Formats/TxtWidget.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import get from 'lodash/get';
22
import invokeMap from 'lodash/invokeMap';
3-
import isArray from 'lodash/isArray';
43
import type { UiSchema, Value } from '../utils';
54
import { UiOption, JsonTypes, widgetFactory, formatTimestamp } from '../utils';
65
import { Truncate } from '../../../../Truncate';
@@ -45,7 +44,7 @@ const TxtWidget = widgetFactory(
4544
variant: UiOption.string,
4645
},
4746
)(({ value, schema, uiSchema }) => {
48-
let displayValue = isArray(value)
47+
let displayValue = Array.isArray(value)
4948
? getArrayValue(value as Array<Exclude<typeof value, any[]>>, uiSchema)
5049
: value?.toString();
5150
if (DATE_TIME_FORMATS.includes(schema?.format ?? '')) {

0 commit comments

Comments
 (0)