From 40f1708393ac927b85d2ce6c44015745636bd2e5 Mon Sep 17 00:00:00 2001 From: ahnpnl Date: Sat, 29 Jun 2024 17:34:45 +0200 Subject: [PATCH] refactor: replace lodash deps with native js implementation --- package-lock.json | 14 -------------- package.json | 2 -- src/__mocks__/thing1.ts | 4 +--- src/__mocks__/thing2.ts | 6 ++++-- src/utils/backports.spec.ts | 22 +++++++++++++++++++++- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32ab730244..163e8129a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -64,8 +64,6 @@ "js-yaml": "latest", "json-schema-to-typescript": "^13.1.2", "lint-staged": "latest", - "lodash.camelcase": "^4.3.0", - "lodash.set": "^4.3.2", "prettier": "^2.8.8", "typescript": "~5.5.2" }, @@ -7441,12 +7439,6 @@ "integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==", "dev": true }, - "node_modules/lodash.set": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg==", - "dev": true - }, "node_modules/lodash.snakecase": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz", @@ -15421,12 +15413,6 @@ "integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==", "dev": true }, - "lodash.set": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg==", - "dev": true - }, "lodash.snakecase": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz", diff --git a/package.json b/package.json index 0ede439a6a..917e25003a 100644 --- a/package.json +++ b/package.json @@ -135,8 +135,6 @@ "js-yaml": "latest", "json-schema-to-typescript": "^13.1.2", "lint-staged": "latest", - "lodash.camelcase": "^4.3.0", - "lodash.set": "^4.3.2", "prettier": "^2.8.8", "typescript": "~5.5.2" }, diff --git a/src/__mocks__/thing1.ts b/src/__mocks__/thing1.ts index a15305ce0d..767fff6cff 100644 --- a/src/__mocks__/thing1.ts +++ b/src/__mocks__/thing1.ts @@ -1,6 +1,4 @@ -import camelCase from 'lodash.camelcase' - -import { getBar } from './thing2' +import { camelCase, getBar } from './thing2' export function getFoo(msg: string): string { return camelCase(msg) + getBar(msg) diff --git a/src/__mocks__/thing2.ts b/src/__mocks__/thing2.ts index 08b84b5570..034c556074 100644 --- a/src/__mocks__/thing2.ts +++ b/src/__mocks__/thing2.ts @@ -1,5 +1,7 @@ -import camelCase from 'lodash.camelcase' - export function getBar(msg: string): string { return camelCase(msg) + 'foo' } + +export function camelCase(str: string): string { + return str.replace(/[-_\s]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : '')) +} diff --git a/src/utils/backports.spec.ts b/src/utils/backports.spec.ts index 2824a6fd20..b99ca0da46 100644 --- a/src/utils/backports.spec.ts +++ b/src/utils/backports.spec.ts @@ -1,13 +1,33 @@ import { inspect } from 'util' import { testing } from 'bs-logger' -import set from 'lodash.set' import { backportJestConfig } from './backports' const logger = testing.createLoggerMock() const logTarget = logger.target +function set(obj: T, path: string | string[], value: unknown): T { + if (!path) return obj + + if (!Array.isArray(path)) { + path = path.toString().match(/[^.[\]]+/g) || [] + } + + path.reduce((acc, key, index) => { + if (index === path.length - 1) { + acc[key] = value + } else if (!acc[key]) { + acc[key] = {} + } + + return acc[key] + // eslint-disable-next-line @typescript-eslint/no-explicit-any + }, obj as Record) + + return obj +} + beforeEach(() => { logTarget.clear() })