Skip to content

Commit

Permalink
refactor: use dlv or pure JS instead of lodash/get
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Feb 19, 2025
1 parent af9dfac commit 5f0e1ef
Show file tree
Hide file tree
Showing 37 changed files with 156 additions and 179 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default [
{
name: 'lodash',
message:
"Use a deep import instead, like for example 'lodash/get.js'",
"Use a deep import instead, like for example 'lodash/merge.js'",
},
{
name: '@simbathesailor/use-what-changed',
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"clipboard-polyfill": "^4.1.1",
"convert-to-jcamp": "^5.4.11",
"d3": "^7.9.0",
"dlv": "^1.1.3",
"eventemitter3": "^5.0.1",
"fifo-logger": "^1.0.0",
"file-saver": "^2.0.5",
Expand Down Expand Up @@ -123,6 +124,7 @@
"@playwright/test": "^1.50.1",
"@simbathesailor/use-what-changed": "^2.0.0",
"@types/d3": "^7.4.3",
"@types/dlv": "^1.1.5",
"@types/lodash": "^4.17.15",
"@types/node": "^22.13.4",
"@types/papaparse": "^5.3.15",
Expand Down
4 changes: 2 additions & 2 deletions src/component/1d-2d/components/SpectrumInfoBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import type { Spectrum } from 'nmr-load-save';
import type { CSSProperties } from 'react';
import { useState } from 'react';
Expand Down Expand Up @@ -42,7 +42,7 @@ function getInfoValue(
field: { jpath: string[]; format: string },
) {
const { jpath, format } = field;
const value = lodashGet(spectrum, jpath, '');
const value = dlv(spectrum, jpath, '');

switch (typeof value) {
case 'number':
Expand Down
6 changes: 3 additions & 3 deletions src/component/1d/SpectraLegends.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import { xFindClosestIndex } from 'ml-spectra-processing';
import type {
JpathLegendField,
Expand All @@ -9,7 +9,7 @@ import type {
} from 'nmr-load-save';
import type { CSSProperties } from 'react';

import { get1DDataXY } from '../../data/data1d/Spectrum1D/get1DDataXY.js';
import { get1DDataXY } from '../../data/data1d/Spectrum1D/index.js';
import { useMouseTracker } from '../EventsTrackers/MouseTracker.js';
import { useChartData } from '../context/ChartContext.js';
import { useScale } from '../context/ScaleContext.js';
Expand Down Expand Up @@ -99,7 +99,7 @@ function InnerSpectraLegends({
);
default: {
const jpath = (field as JpathLegendField).jpath;
const value = lodashGet(spectrum, jpath, '');
const value = dlv(spectrum, jpath, '');
return (
<text
alignmentBaseline="middle"
Expand Down
4 changes: 2 additions & 2 deletions src/component/1d/multiplicityTree/generateTreeNodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get.js';
import type { Spectrum1D } from 'nmr-load-save';
import type { Jcoupling, Range } from 'nmr-processing';

Expand All @@ -25,7 +24,8 @@ export interface TreeNodes {
}

export function generateTreeNodes(range: Range, spectrumData: Spectrum1D) {
const frequency = lodashGet(spectrumData, 'info.originFrequency');
// TODO: make sure spectrumData is not a lie and remove the optional chaining.

Check warning on line 27 in src/component/1d/multiplicityTree/generateTreeNodes.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: make sure spectrumData is not a...'
const frequency = spectrumData?.info?.originFrequency;

const result: TreeNodes[] = [];
let nodes: TreeNode[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/component/2d/ft/Contours.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debounce from 'lodash/debounce.js';
import get from 'lodash/get.js';
import type { Spectrum2D } from 'nmr-load-save';
import { memo, useMemo, useRef } from 'react';

Expand Down Expand Up @@ -93,7 +92,8 @@ function ContoursPaths({
const opacity =
activeSpectrum === null || spectrumID === activeSpectrum.id
? '1'
: get(preferences.current, 'general.dimmedSpectraOpacity', 0.1);
: // TODO: make sure preferences are not a lie and remove the optional chaining.

Check warning on line 95 in src/component/2d/ft/Contours.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: make sure preferences are not a...'
(preferences?.current?.general?.dimmedSpectraOpacity ?? 0.1);

return (
<path
Expand Down
6 changes: 3 additions & 3 deletions src/component/context/SortSpectraContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import type { Spectrum } from 'nmr-load-save';
import type { ReactNode } from 'react';
import {
Expand Down Expand Up @@ -81,7 +81,7 @@ function sortSpectraByPath(
) {
sortedSpectra.push({
spectrum,
sortValue: lodashGet(spectrum, path),
sortValue: dlv(spectrum, path),
});
originIndexes.push(index);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ function sortSpectraByReferences(
if (spectraReferenceMap.size > 0 && spectraReferenceMap.has(spectrum.id)) {
sortedSpectra.push({
spectrum,
sortValue: lodashGet(spectraReferenceMap.get(spectrum.id), path),
sortValue: dlv(spectraReferenceMap.get(spectrum.id), path),
});
originIndexes.push(index);
}
Expand Down
6 changes: 3 additions & 3 deletions src/component/hooks/useActiveSpectrumStyleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from 'lodash/get.js';
import { useMemo } from 'react';

import { usePreferences } from '../context/PreferencesContext.js';
Expand Down Expand Up @@ -26,10 +25,11 @@ export default function useActiveSpectrumStyleOptions(
false;

const isActive =
!!(activeSpectra?.length === 0 || index !== -1) || isNoneSelected;
activeSpectra?.length === 0 || index !== -1 || isNoneSelected;
const opacity = isActive
? 1
: get(preferences.current, 'general.dimmedSpectraOpacity', 0.1);
: // TODO: make sure preferences are not a lie and remove the optional chaining.

Check warning on line 31 in src/component/hooks/useActiveSpectrumStyleOptions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: make sure preferences are not a...'
(preferences?.current?.general?.dimmedSpectraOpacity ?? 0.1);
return { isActive, opacity };
}, [activeSpectra, id, preferences]);
}
9 changes: 3 additions & 6 deletions src/component/hooks/useCheckToolsVisibility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get.js';
import type { Info1D, Info2D } from 'nmr-processing';
import { useCallback } from 'react';

Expand Down Expand Up @@ -37,11 +36,9 @@ export function useCheckToolsVisibility(): (

const { spectraOptions, mode, isExperimental } = options[toolKey];

const flag = lodashGet(
preferences.current,
`display.toolBarButtons.${toolKey}`,
false,
);
// TODO: make sure preferences are not a lie and remove the optional chaining.

Check warning on line 39 in src/component/hooks/useCheckToolsVisibility.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: make sure preferences are not a...'
const flag =
preferences?.current?.display?.toolBarButtons?.[toolKey] ?? false;

const modeFlag =
!checkMode || (checkMode && (!mode || displayerMode === mode));
Expand Down
6 changes: 4 additions & 2 deletions src/component/hooks/useFormatNumberByNucleus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get.js';
import { useMemo } from 'react';

import { usePreferences } from '../context/PreferencesContext.js';
Expand All @@ -11,11 +10,14 @@ export type ReturnFunction = (
suffix?: string,
) => string;

const defaultNuclei = [];

export function useFormatNumberByNucleus(nucleus?: string[]): ReturnFunction[];
export function useFormatNumberByNucleus(nucleus?: string): ReturnFunction;
export function useFormatNumberByNucleus(nucleus?: string | string[]) {
const preferences = usePreferences();
const nucleiPreferences = lodashGet(preferences.current, `nuclei`, []);
// TODO: make sure preferences are not a lie and remove the optional chaining.
const nucleiPreferences = preferences?.current?.nuclei ?? defaultNuclei;

return useMemo(() => {
function formatFun(n: string) {
Expand Down
10 changes: 3 additions & 7 deletions src/component/hooks/useTableSortBy.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import { useCallback, useMemo, useState } from 'react';

export enum SortType {
Expand All @@ -19,13 +19,9 @@ export default function useTableSortBy(items, config = null) {
if (sortConfig !== null) {
sortableItems.sort((a, b) => {
if (sortConfig.direction === SortType.ASCENDING) {
return (
lodashGet(a, sortConfig.key, 0) - lodashGet(b, sortConfig.key, 0)
);
return dlv(a, sortConfig.key, 0) - dlv(b, sortConfig.key, 0);
} else if (sortConfig.direction === SortType.DESCENDING) {
return (
lodashGet(b, sortConfig.key, 0) - lodashGet(a, sortConfig.key, 0)
);
return dlv(b, sortConfig.key, 0) - dlv(a, sortConfig.key, 0);
}
return 0;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, Callout, Classes } from '@blueprintjs/core';
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import type { Jcoupling, Peak1D } from 'nmr-processing';
import { translateMultiplet } from 'nmr-processing';
import type { CSSProperties } from 'react';
Expand Down Expand Up @@ -46,7 +46,7 @@ function getJCouplingKey(
}

function getCouplingMinErrorMessage(errors, index) {
return lodashGet(errors, `signals.${index}.js.root.message`);
return dlv(errors, `signals.${index}.js.root.message`);
}

export function SignalJCouplingsTable(props: SignalJCouplingsTableProps) {
Expand Down
6 changes: 2 additions & 4 deletions src/component/panels/IntegralsPanel/IntegralPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SvgNmrIntegrate } from 'cheminfo-font';
import lodashGet from 'lodash/get.js';
import type { Spectrum1D } from 'nmr-load-save';
import type { Info1D, Integrals } from 'nmr-processing';
import { memo, useCallback, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -70,9 +69,8 @@ function IntegralPanelInner({
[dispatch],
);

const currentSum = useMemo(() => {
return lodashGet(integrals, 'options.sum', null);
}, [integrals]);
// TODO: make sure currentSum is not a lie and remove the optional chaining.
const currentSum = integrals?.options?.sum ?? null;

const settingsPanelHandler = useCallback(() => {
setFlipStatus(!isFlipped);
Expand Down
6 changes: 3 additions & 3 deletions src/component/panels/IntegralsPanel/IntegralTable.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import type { Info1D, Integral } from 'nmr-processing';
import { checkIntegralKind } from 'nmr-processing';
import { memo, useCallback, useMemo } from 'react';
import { FaRegTrashAlt } from 'react-icons/fa';

import { SIGNAL_KINDS } from '../../../data/constants/signalsKinds.js';
import { checkIntegralKind } from '../../../data/data1d/Spectrum1D/index.js';
import { useDispatch } from '../../context/DispatchContext.js';
import { EditableColumn } from '../../elements/EditableColumn.js';
import { EmptyText } from '../../elements/EmptyText.js';
Expand Down Expand Up @@ -167,7 +167,7 @@ function IntegralTable({ activeTab, data, info }: IntegralTableProps) {
const columns: Array<CustomColumn<Integral>> = [];
for (const col of COLUMNS) {
const { showWhen, ...colParams } = col;
if (lodashGet(integralsPreferences, showWhen)) {
if (dlv(integralsPreferences, showWhen)) {
addCustomColumn(columns, colParams);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/component/panels/PeaksPanel/PeaksTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodashGet from 'lodash/get.js';
import dlv from 'dlv';
import type { Info1D, Peak1D } from 'nmr-processing';
import { memo, useCallback, useMemo, useState } from 'react';
import { FaEdit, FaRegTrashAlt } from 'react-icons/fa';
Expand Down Expand Up @@ -190,7 +190,7 @@ function PeaksTable({ activeTab, data, info }: PeaksTableProps) {
const columns: Array<ControlCustomColumn<PeakRecord>> = [];
for (const col of COLUMNS) {
const { showWhen, ...colParams } = col;
if (lodashGet(peaksPreferences, showWhen)) {
if (dlv(peaksPreferences, showWhen)) {
addCustomColumn(columns, colParams);
}
}
Expand Down
37 changes: 25 additions & 12 deletions src/component/panels/RangesPanel/RangesHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
SvgNmrPeaksTopLabels,
} from 'cheminfo-font';
import fileSaver from 'file-saver';
import lodashGet from 'lodash/get.js';
import type { RangesViewState } from 'nmr-load-save';
import type { Info1D, Ranges } from 'nmr-processing';
import { rangesToACS, rangesToTSV } from 'nmr-processing';
import { useState } from 'react';
import {
Expand Down Expand Up @@ -58,22 +58,35 @@ const EXPORT_MENU: ExportItem[] = [
},
] as const;

function RangesHeader({
ranges,
info,
onUnlink,
onFilterActivated,
onSettingClick,
isFilterActive,
filterCounter,
activeTab,
}) {
interface RangesHeaderProps {
ranges: Ranges;
info: Info1D;
activeTab: string;
isFilterActive: boolean;
onUnlink: () => void;
onFilterActivated: () => void;
onSettingClick: () => void;
filterCounter: number;
}

function RangesHeader(props: RangesHeaderProps) {
const {
ranges,
info,
activeTab,
isFilterActive,
onUnlink,
onFilterActivated,
onSettingClick,
filterCounter,
} = props;
const dispatch = useDispatch();
const alert = useAlert();
const toaster = useToaster();
const assignmentData = useAssignmentData();

const currentSum = lodashGet(ranges, 'options.sum', null);
// TODO: make sure ranges are not a lie and remove the optional chaining.
const currentSum = ranges?.options?.sum ?? null;
const rangesPreferences = usePanelPreferences('ranges', activeTab);
const [acs, setACS] = useState<string>();

Expand Down
Loading

0 comments on commit 5f0e1ef

Please sign in to comment.