Skip to content

Commit

Permalink
refactor: replaced lodash clonedeep by structuredclone
Browse files Browse the repository at this point in the history
  • Loading branch information
chloe-renaud committed Dec 16, 2024
1 parent f001d11 commit 21f4ff8
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 53 deletions.
5 changes: 2 additions & 3 deletions src/actions/component-insert.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import sortBy from 'lodash/sortby';

import { COMPONENT_TYPE } from '../constants/pogues-constants';
Expand Down Expand Up @@ -291,7 +290,7 @@ export function duplicate(activesComponents, idComponent) {
const id = uuid();
const component = {
[id]: {
...cloneDeep(activesComponents[idComponent]),
...structuredClone(activesComponents[idComponent]),
id,
weight: activesComponents[idComponent].weight + 1,
},
Expand Down Expand Up @@ -326,7 +325,7 @@ export function duplicateComponentAndVars(
activesComponents[idComponent].responseFormat.type;

const duplicatedComponent = {
...cloneDeep(activesComponents[idComponent]),
...structuredClone(activesComponents[idComponent]),
id: uuid(),
weight: activesComponents[idComponent].weight + 1,
redirections: {},
Expand Down
8 changes: 3 additions & 5 deletions src/forms/validation-rules.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import cloneDeep from 'lodash/clonedeep';

import {
CODES_LIST_INPUT_ENUM,
DATATYPE_NAME,
Expand Down Expand Up @@ -307,7 +305,7 @@ export function validateDuplicatesCalculated(
value,
{ form: { calculatedVariables: values }, state: { selectedItemIndex } },
) {
const listItems = cloneDeep(values.calculatedVariables);
const listItems = structuredClone(values.calculatedVariables);

// We need to remove the element from the list
if (selectedItemIndex !== undefined) {
Expand All @@ -327,7 +325,7 @@ export function validateDuplicatesExternal(
value,
{ form: { externalVariables: values }, state: { selectedItemIndex } },
) {
const listItems = cloneDeep(values.externalVariables);
const listItems = structuredClone(values.externalVariables);

// We need to remove the element from the list
if (selectedItemIndex !== undefined) {
Expand All @@ -341,7 +339,7 @@ export function validateDuplicatesCollected(
value,
{ form: { collectedVariables: values }, state: { selectedItemIndex } },
) {
const listItems = cloneDeep(values.collectedVariables);
const listItems = structuredClone(values.collectedVariables);

// We need to remove the element from the list
if (selectedItemIndex !== undefined) {
Expand Down
7 changes: 3 additions & 4 deletions src/model/formToState/codes-lists/codes-list.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { uuid } from '../../../utils/utils';
Expand Down Expand Up @@ -65,7 +64,7 @@ export function stateComponentToForm({
urn,
suggesterParameters,
}
: merge(cloneDeep(defaultForm), {
: merge(structuredClone(defaultForm), {
id,
label,
codes: Object.keys(codes).map((key) => codes[key]),
Expand All @@ -75,11 +74,11 @@ export function stateComponentToForm({
export const Factory = (currentState = {}, codesListsStore) => {
if (codesListsStore && currentState.id) {
currentState = merge(
cloneDeep(defaultState),
structuredClone(defaultState),
codesListsStore[currentState.id],
);
} else {
currentState = merge(cloneDeep(defaultState), { id: uuid() });
currentState = merge(structuredClone(defaultState), { id: uuid() });
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { Factory as CodesListFactory } from '../..';
Expand All @@ -15,27 +14,27 @@ const { CHECKBOX } = DATATYPE_VIS_HINT;

export const defaultState = {
[PRIMARY]: {
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultState),
},
[MEASURE]: {
type: BOOL,
[BOOL]: {},
[CODES_LIST]: {
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultState),
visHint: CHECKBOX,
},
},
};

export const defaultForm = {
[PRIMARY]: {
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultForm),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultForm),
},
[MEASURE]: {
type: BOOL,
[BOOL]: {},
[CODES_LIST]: {
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultForm),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultForm),
visHint: CHECKBOX,
},
},
Expand Down Expand Up @@ -102,14 +101,14 @@ export function stateToForm(currentState, transformers) {
}

const Factory = (initialState = {}, codesListsStore) => {
let currentState = merge(cloneDeep(defaultState), initialState);
let currentState = merge(structuredClone(defaultState), initialState);
const transformers = {
codesListPrimary: CodesListFactory(
cloneDeep(currentState[PRIMARY][DEFAULT_CODES_LIST_SELECTOR_PATH]),
structuredClone(currentState[PRIMARY][DEFAULT_CODES_LIST_SELECTOR_PATH]),
codesListsStore,
),
codesListMeasure: CodesListFactory(
cloneDeep(
structuredClone(
currentState[MEASURE][CODES_LIST][DEFAULT_CODES_LIST_SELECTOR_PATH],
),
codesListsStore,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { Factory as CodesListFactory } from '../..';
Expand All @@ -19,7 +18,7 @@ export const defaultState = {
specialUiBehaviour: UI_BEHAVIOUR.FIRST_INTENTION,
specialFollowUpMessage: '',
visHint: DROPDOWN,
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultState),
};

export const defaultForm = {
Expand All @@ -31,7 +30,7 @@ export const defaultForm = {
specialUiBehaviour: UI_BEHAVIOUR.FIRST_INTENTION,
specialFollowUpMessage: '',
visHint: DROPDOWN,
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultForm),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultForm),
};

export function formToState(form, transformers) {
Expand Down Expand Up @@ -94,17 +93,17 @@ export function stateToForm(currentState, transformers) {
}

export const Factory = (initialState = {}, codesListsStore) => {
let currentState = merge(cloneDeep(defaultState), initialState);
let currentState = merge(structuredClone(defaultState), initialState);
const transformers = {
codesList: CodesListFactory(
cloneDeep(currentState[DEFAULT_CODES_LIST_SELECTOR_PATH]),
structuredClone(currentState[DEFAULT_CODES_LIST_SELECTOR_PATH]),
codesListsStore,
),
};
return {
formToState: (form) => {
const state = formToState(form, transformers);
currentState = merge(cloneDeep(currentState), state);
currentState = merge(structuredClone(currentState), state);
return state;
},
stateToForm: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { DATATYPE_NAME } from '../../../constants/pogues-constants';
Expand Down Expand Up @@ -56,7 +55,7 @@ export function formToState(form) {
export function stateToForm(currentState) {
const { mandatory, type, [type]: simpleState, id } = currentState;

return merge(cloneDeep(defaultForm), {
return merge(structuredClone(defaultForm), {
id,
mandatory,
type,
Expand All @@ -67,12 +66,12 @@ export function stateToForm(currentState) {
}

const Factory = (initialState = {}) => {
let currentState = merge(cloneDeep(defaultForm), initialState);
let currentState = merge(structuredClone(defaultForm), initialState);

return {
formToState: (form) => {
const state = formToState(form);
currentState = merge(cloneDeep(currentState), state);
currentState = merge(structuredClone(currentState), state);
return state;
},
stateToForm: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { Factory as CodesListFactory } from '../..';
Expand All @@ -18,7 +17,7 @@ export const defaultState = {
specialUiBehaviour: UI_BEHAVIOUR.FIRST_INTENTION,
specialFollowUpMessage: '',
visHint: RADIO,
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultState),
};

export const defaultForm = {
Expand All @@ -29,7 +28,7 @@ export const defaultForm = {
specialUiBehaviour: UI_BEHAVIOUR.FIRST_INTENTION,
specialFollowUpMessage: '',
visHint: RADIO,
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultForm),
// [DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultForm),
};

export function formToState(form, transformers) {
Expand Down Expand Up @@ -88,17 +87,17 @@ export function stateToForm(currentState, transformers) {
}

export const Factory = (initialState = {}, codesListsStore) => {
let currentState = merge(cloneDeep(defaultState), initialState);
let currentState = merge(structuredClone(defaultState), initialState);
const transformers = {
codesList: CodesListFactory(
cloneDeep(currentState[DEFAULT_CODES_LIST_SELECTOR_PATH]),
structuredClone(currentState[DEFAULT_CODES_LIST_SELECTOR_PATH]),
codesListsStore,
),
};
return {
formToState: (form) => {
const state = formToState(form, transformers);
currentState = merge(cloneDeep(currentState), state);
currentState = merge(structuredClone(currentState), state);
return state;
},
stateToForm: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import {
Expand Down Expand Up @@ -64,7 +63,7 @@ export const defaultMeasureState = {
[SIMPLE]: defaultMeasureSimpleState,
[SINGLE_CHOICE]: {
[DEFAULT_CODES_LIST_SELECTOR_PATH]: merge(
cloneDeep(CodesListDefaultState),
structuredClone(CodesListDefaultState),
{ id: uuid() },
),
visHint: RADIO,
Expand All @@ -81,7 +80,7 @@ export const defaultMeasureForm = {
specialCode: '',
specialUiBehaviour: UI_BEHAVIOUR.FIRST_INTENTION,
specialFollowUpMessage: '',
[DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultForm),
[DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultForm),
visHint: RADIO,
},
};
Expand All @@ -102,11 +101,13 @@ export const defaultState = {
type: LIST,
[LIST]: defaultPrimaryListState,
[CODES_LIST]: {
[DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
[DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(
CodesListDefaultState,
),
},
},
[SECONDARY]: {
[DEFAULT_CODES_LIST_SELECTOR_PATH]: cloneDeep(CodesListDefaultState),
[DEFAULT_CODES_LIST_SELECTOR_PATH]: structuredClone(CodesListDefaultState),
showSecondaryAxis: false,
},
[LIST_MEASURE]: {
Expand Down Expand Up @@ -361,7 +362,7 @@ export function getNormalizedMeasureValues(measure) {

const Factory = (initialState = {}, codesListsStore) => {
const { [LIST_MEASURE]: measures, ...otherState } = initialState;
let currentState = merge(cloneDeep(defaultState), {
let currentState = merge(structuredClone(defaultState), {
...otherState,
[LIST_MEASURE]: { measures },
});
Expand All @@ -383,7 +384,7 @@ const Factory = (initialState = {}, codesListsStore) => {
state[SIMPLE] = measureState;
}

return merge(cloneDeep(defaultState[MEASURE]), state);
return merge(structuredClone(defaultState[MEASURE]), state);
},
);

Expand Down
7 changes: 3 additions & 4 deletions src/model/formToState/component-new-edit/response-format.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

import { QUESTION_TYPE_ENUM } from '../../../constants/pogues-constants';
Expand Down Expand Up @@ -52,7 +51,7 @@ export function formToState(form, transformers) {
export function stateToForm(currentState, transformers) {
const { type } = currentState;

return merge({}, cloneDeep(defaultForm), {
return merge({}, structuredClone(defaultForm), {
type,
[SIMPLE]: transformers.simple.stateToForm(),
[SINGLE_CHOICE]: transformers.single.stateToForm(),
Expand All @@ -63,7 +62,7 @@ export function stateToForm(currentState, transformers) {
}

const Factory = (initialState = {}, codesListsStore) => {
let currentState = merge(cloneDeep(defaultForm), initialState);
let currentState = merge(structuredClone(defaultForm), initialState);

const transformers = {
simple: Simple(currentState[SIMPLE]),
Expand All @@ -77,7 +76,7 @@ const Factory = (initialState = {}, codesListsStore) => {
formToState: (form) => {
if (form) {
const state = formToState(form, transformers);
currentState = merge(cloneDeep(currentState), state);
currentState = merge(structuredClone(currentState), state);
return state;
}
return currentState;
Expand Down
3 changes: 1 addition & 2 deletions src/utils/integrity/combine-checkers.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import merge from 'lodash/merge';

/**
Expand All @@ -17,7 +16,7 @@ import merge from 'lodash/merge';
export default function combineCheckers(...checkers) {
return function (state) {
return checkers.reduce((errorsByComponent, checker) => {
return merge(cloneDeep(errorsByComponent), checker(state) || {});
return merge(structuredClone(errorsByComponent), checker(state) || {});
}, {});
};
}
5 changes: 3 additions & 2 deletions src/utils/integrity/integrity-checker.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from 'lodash/clonedeep';
import uniq from 'lodash/uniq';

/**
Expand All @@ -20,7 +19,9 @@ function integrityChecker(reducer, checkers) {
if (!checker) return newState;

const checkerResult = checker(newState);
const currentErrorsIntegrity = cloneDeep(newState.errors.errorsIntegrity);
const currentErrorsIntegrity = structuredClone(
newState.errors.errorsIntegrity,
);
const componentsIds = uniq([
...Object.keys(currentErrorsIntegrity),
...Object.keys(checkerResult),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Children, cloneElement, useEffect, useState } from 'react';

import cloneDeep from 'lodash/clonedeep';
import PropTypes from 'prop-types';
import ReactModal from 'react-modal';
import { FieldArray } from 'redux-form';
Expand All @@ -27,7 +26,7 @@ const {
// Utils

function getFormValuesToValidate(formValues, item, selectorPath, name) {
const formValuesToValidate = cloneDeep(formValues);
const formValuesToValidate = structuredClone(formValues);
let pointer = formValuesToValidate;
const pathStack = selectorPath.split('.');

Expand Down

0 comments on commit 21f4ff8

Please sign in to comment.