Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Replace lodash.clonedeep with rfdc (#1321)

sarayourfriend2315ecd45df1
diff --git a/package.json b/package.json
index 52aab3971..8d22dec0b 100644
--- a/package.json
+++ b/package.json
@@ -81,7 +81,6 @@
"focus-trap-vue": "1.1.1",
"focus-visible": "^5.2.0",
"glob": "^7.1.6",
- "lodash.clonedeep": "^4.5.0",
"lodash.isempty": "^4.4.0",
"lodash.sortby": "^4.7.0",
"node-html-parser": "^5.3.3",
@@ -89,6 +88,7 @@
"pinia": "^2.0.13",
"postcss-focus-visible": "^6.0.4",
"reakit-utils": "^0.15.2",
+ "rfdc": "^1.3.0",
"seeded-rand": "^2.0.1",
"throttle-debounce": "^4.0.1",
"uuid": "^8.3.2",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index aa3fabb38..e6c368567 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -64,7 +64,6 @@ specifiers:
jest: ^26.6.3
jest-transform-stub: ^2.0.0
lint-staged: ^12.3.7
- lodash.clonedeep: ^4.5.0
lodash.isempty: ^4.4.0
lodash.sortby: ^4.7.0
module-alias: ^2.2.2
@@ -76,6 +75,7 @@ specifiers:
postcss-focus-visible: ^6.0.4
prettier: ^2.2.1
reakit-utils: ^0.15.2
+ rfdc: ^1.3.0
rimraf: ^3.0.2
sass: ^1.49.9
sass-loader: ^10.2.1
@@ -120,7 +120,6 @@ dependencies:
focus-trap-vue: [email protected]
focus-visible: 5.2.0
glob: 7.2.0
- lodash.clonedeep: 4.5.0
lodash.isempty: 4.4.0
lodash.sortby: 4.7.0
node-html-parser: 5.3.3
@@ -128,6 +127,7 @@ dependencies:
postcss-focus-visible: [email protected]
reakit-utils: 0.15.2
+ rfdc: 1.3.0
seeded-rand: 2.0.1
throttle-debounce: 4.0.1
uuid: 8.3.2
@@ -12688,10 +12688,6 @@ packages:
resolution: {integrity: sha1-wCUTUV4wna3dTCTGDP3c9ZdtkRU=}
dev: false
- /lodash.clonedeep/4.5.0:
- resolution: {integrity: sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=}
- dev: false
-
/lodash.debounce/4.0.8:
resolution: {integrity: sha1-gteb/zCmfEAF/9XiUVMArZyk168=}
@@ -16139,7 +16135,6 @@ packages:
/rfdc/1.3.0:
resolution: {integrity: sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==}
- dev: true
/rgb-regex/1.0.1:
resolution: {integrity: sha1-wODWiC3w4jviVKR16O3UGRX+rrE=}
diff --git a/src/constants/filters.ts b/src/constants/filters.ts
index 9bc3c06c3..9c21da01a 100644
--- a/src/constants/filters.ts
+++ b/src/constants/filters.ts
@@ -134,7 +134,7 @@ export const initFilters = () =>
checked: false,
})),
}),
- {} as Record<FilterCategory, FilterItem>
+ {} as Filters
)
export const filterData = deepFreeze(initFilters())
diff --git a/src/stores/search.ts b/src/stores/search.ts
index 2beab8737..bda0c40a9 100644
--- a/src/stores/search.ts
+++ b/src/stores/search.ts
@@ -1,10 +1,7 @@
import { defineStore } from 'pinia'
-// We plan to remove this dependency, so don't need to add types for it:
-// https://github.com/WordPress/openverse-frontend/issues/1103
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment
-// @ts-ignore
-import clonedeep from 'lodash.clonedeep'
+import { deepClone } from '~/utils/clone'
+import type { DeepWriteable } from '~/types/utils'
import {
ALL_MEDIA,
@@ -60,7 +57,7 @@ export const useSearchStore = defineStore('search', {
state: (): SearchState => ({
searchType: ALL_MEDIA,
searchTerm: '',
- filters: clonedeep(filterData),
+ filters: deepClone(filterData as DeepWriteable<typeof filterData>),
}),
getters: {
filterCategories(state) {
@@ -139,7 +136,7 @@ export const useSearchStore = defineStore('search', {
}))
}
return {
- ...clonedeep(filterData),
+ ...(deepClone(filterData) as DeepWriteable<typeof filterData>),
audioProviders: resetProviders(AUDIO),
imageProviders: resetProviders(IMAGE),
}
diff --git a/src/types/utils.ts b/src/types/utils.ts
new file mode 100644
index 000000000..7ccdecc26
--- /dev/null
+++ b/src/types/utils.ts
@@ -0,0 +1 @@
+export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> }
diff --git a/src/utils/clone.ts b/src/utils/clone.ts
new file mode 100644
index 000000000..48a0c0cb2
--- /dev/null
+++ b/src/utils/clone.ts
@@ -0,0 +1,3 @@
+import rfdc from 'rfdc'
+
+export const deepClone = rfdc()
diff --git a/src/utils/search-query-transform.ts b/src/utils/search-query-transform.ts
index 59b9aff31..85ac63a81 100644
--- a/src/utils/search-query-transform.ts
+++ b/src/utils/search-query-transform.ts
@@ -1,9 +1,3 @@
-// We plan to remove this dependency, so don't need to add types for it:
-// https://github.com/WordPress/openverse-frontend/issues/1103
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment
-// @ts-ignore
-import clonedeep from 'lodash.clonedeep'
-
import {
FilterCategory,
FilterItem,
@@ -11,8 +5,8 @@ import {
mediaFilterKeys,
} from '~/constants/filters'
import { ALL_MEDIA, SupportedSearchType } from '~/constants/media'
-
import { getParameterByName } from '~/utils/url-params'
+import { deepClone } from '~/utils/clone'
export interface ApiQueryParams {
q?: string
@@ -177,7 +171,7 @@ export const queryToFilterData = ({
}) => {
// The default filterData object from search store doesn't contain provider filters,
// so we can't use it.
- const filters = clonedeep(defaultFilters) as Filters
+ const filters = deepClone(defaultFilters) as Filters
const filterTypes = getMediaFilterTypes(searchType)
const differentFiltersWithSameApiParams = [
'audioProviders',
diff --git a/test/unit/specs/utils/search-query-transform.spec.js b/test/unit/specs/utils/search-query-transform.spec.js
index c15f95d32..9e3693af3 100644
--- a/test/unit/specs/utils/search-query-transform.spec.js
+++ b/test/unit/specs/utils/search-query-transform.spec.js
@@ -1,5 +1,4 @@
-import clonedeep from 'lodash.clonedeep'
-
+import { deepClone } from '~/utils/clone'
import {
filtersToQueryData,
queryToFilterData,
@@ -258,7 +257,7 @@ describe('searchQueryTransform', () => {
searchBy: 'creator',
mature: 'true',
}
- const testFilters = clonedeep(filters)
+ const testFilters = deepClone(filters)
testFilters.audioProviders = [
{ code: 'jamendo', checked: true },
{ code: 'wikimedia', checked: true },
@@ -300,7 +299,7 @@ describe('searchQueryTransform', () => {
searchBy: 'creator',
mature: 'true',
}
- const expectedFilters = clonedeep(filters)
+ const expectedFilters = deepClone(filters)
const setChecked = (code, filterCategory) => {
const idx = expectedFilters[filterCategory].findIndex(
(item) => item.code === code