Skip to content

Commit

Permalink
Replace lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Nov 18, 2024
1 parent a964293 commit 64fd88c
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 73 deletions.
19 changes: 10 additions & 9 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"chalk": "^4.1.2",
"cross-spawn": "^7.0.5",
"dedent": "^0.7.0",
"deepmerge-ts": "^7.1.3",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^18.0.0",
Expand All @@ -70,7 +71,6 @@
"lint-staged": "15.2.10",
"listr": "^0.14.3",
"listr-inquirer": "^0.1.0",
"lodash": "^4.17.21",
"prettier": "^3.3.3",
"read-pkg-up": "^7.0.1",
"semver": "^7.6.3",
Expand All @@ -89,7 +89,6 @@
"@types/inquirer": "^8.2.10",
"@types/is-ci": "^3.0.4",
"@types/listr": "^0.14.9",
"@types/lodash": "^4.17.13",
"@types/node": "^18.19.0",
"@types/prettier": "^3.0.0",
"@types/semver": "^7.5.8",
Expand Down
13 changes: 8 additions & 5 deletions src/cli/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
* limitations under the License.
*/

import { isArray, isEmpty, mapValues } from 'lodash/fp';

import { getOptions } from '../lib/options';
import * as logger from '../lib/logger';
import { isArray } from '../lib/type-check';
import { isEmpty } from '../lib/helpers';

export function debug(): void {
const options = getOptions();

const stringifiedOptions = mapValues(
(value) => (isArray(value) && !isEmpty(value) ? value.join(', ') : value),
options,
const stringifiedOptions = Object.entries(options).reduce(
(acc, [key, value]) => {
acc[key] = isArray(value) && !isEmpty(value) ? value.join(', ') : value;
return acc;
},
{} as Record<string, unknown>,
);

logger.empty();
Expand Down
3 changes: 1 addition & 2 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import { dirname, resolve, join, relative } from 'node:path';
import { access, readFile } from 'node:fs';
import { promisify } from 'node:util';

import { isString } from 'lodash/fp';

import type { PackageJson } from '../types/shared';
import { spawn } from '../lib/spawn';
import * as logger from '../lib/logger';
import { isString } from '../lib/type-check';

const readFileAsync = promisify(readFile);
const accessAsync = promisify(access);
Expand Down
43 changes: 19 additions & 24 deletions src/configs/eslint/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { cwd } from 'node:process';
import path from 'node:path';

import { flow, mergeWith, isArray, isObject, isEmpty, uniq } from 'lodash/fp';
import { deepmergeCustom } from 'deepmerge-ts';

import {
Language,
Expand All @@ -27,30 +27,25 @@ import {
} from '../../types/shared';
import * as logger from '../../lib/logger';
import { getOptions } from '../../lib/options';
import { isEmpty, flow, uniq } from '../../lib/helpers';

// NOTE: Using the Linter.Config interface from ESLint causes errors
// and I couldn't figure out how to fix them. — @connor_baer
type ESLintConfig = unknown;

export const customizeConfig = mergeWith(customizer);

function isArrayTypeGuard(array: unknown): array is unknown[] {
return isArray(array);
}

function customizer(
objValue: unknown,
srcValue: unknown,
key: string,
): unknown {
if (isArrayTypeGuard(objValue) && isArrayTypeGuard(srcValue)) {
return uniq([...objValue, ...srcValue]);
}
if (isObject(objValue) && isObject(srcValue)) {
return key === 'rules' ? { ...objValue, ...srcValue } : undefined;
}
return undefined;
}
export const customizeConfig = deepmergeCustom({
mergeArrays: (values) => {
const [baseValue, sourceValue] = values;
return uniq([...baseValue, ...sourceValue]);
},
mergeRecords: (values, utils, meta) => {
const [baseValue, sourceValue] = values;
if (meta?.key === 'rules') {
return { ...baseValue, ...sourceValue };
}
return utils.actions.defaultMerge;
},
});

export function getFileGlobsForWorkspaces(
workspaces: Workspaces,
Expand Down Expand Up @@ -452,7 +447,7 @@ function customizeLanguage(language: Language, useBiome: boolean) {
return config;
}
const overrides = languageMap[language];
return customizeConfig(config, overrides);
return overrides ? customizeConfig(config, overrides) : config;
};
}

Expand Down Expand Up @@ -516,7 +511,7 @@ function customizeEnvironments(environments: Environment[]) {
}
return environments.reduce((acc, environment: Environment) => {
const overrides = environmentMap[environment];
return customizeConfig(acc, overrides);
return overrides ? customizeConfig(acc, overrides) : acc;
}, config);
};
}
Expand Down Expand Up @@ -614,7 +609,7 @@ function customizeFramework(frameworks: Framework[]) {

return frameworks.reduce((acc, framework: Framework) => {
const overrides = frameworkMap[framework];
return customizeConfig(acc, overrides);
return overrides ? customizeConfig(acc, overrides) : acc;
}, config);
};
}
Expand Down Expand Up @@ -709,7 +704,7 @@ function customizePlugin(plugins: Plugin[], workspaces: Workspaces) {

return plugins.reduce((acc, plugin: Plugin) => {
const overrides = pluginMap[plugin];
return customizeConfig(acc, overrides);
return overrides ? customizeConfig(acc, overrides) : acc;
}, config);
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/configs/husky/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { merge } from 'lodash/fp';
import { deepmerge } from 'deepmerge-ts';

interface HuskyConfig {
skipCI?: boolean;
Expand All @@ -27,5 +27,5 @@ export const base: HuskyConfig = {
};

export function config(overrides: HuskyConfig = {}): HuskyConfig {
return merge(base, overrides);
return deepmerge(base, overrides);
}
37 changes: 16 additions & 21 deletions src/configs/stylelint/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@
*/

import type { Config as StylelintConfig } from 'stylelint';
import { flow, mergeWith, isArray, isObject, uniq, isEmpty } from 'lodash/fp';
import { deepmergeCustom } from 'deepmerge-ts';

import { getOptions } from '../../lib/options';
import { isEmpty, flow, uniq } from '../../lib/helpers';
import { Plugin } from '../../types/shared';

export const customizeConfig = mergeWith(customizer);

function isArrayTypeGuard(array: unknown): array is unknown[] {
return isArray(array);
}

function customizer(
objValue: unknown,
srcValue: unknown,
key: string,
): unknown {
if (isArrayTypeGuard(objValue) && isArrayTypeGuard(srcValue)) {
return uniq([...objValue, ...srcValue]);
}
if (isObject(objValue) && isObject(srcValue)) {
return key === 'rules' ? { ...objValue, ...srcValue } : undefined;
}
return undefined;
}
export const customizeConfig = deepmergeCustom({
mergeArrays: (values) => {
const [baseValue, sourceValue] = values;
return uniq([...baseValue, ...sourceValue]);
},
mergeRecords: (values, utils, meta) => {
const [baseValue, sourceValue] = values;
if (meta?.key === 'rules') {
return { ...baseValue, ...sourceValue };
}
return utils.actions.defaultMerge;
},
});

const base: StylelintConfig = {
extends: ['stylelint-config-standard', 'stylelint-config-recess-order'],
Expand Down Expand Up @@ -87,7 +82,7 @@ function customizePlugin(plugins: Plugin[]) {

return plugins.reduce((acc, plugin: Plugin) => {
const overrides = pluginMap[plugin];
return customizeConfig(acc, overrides);
return overrides ? customizeConfig(acc, overrides) : config;
}, config);
};
}
Expand Down
10 changes: 3 additions & 7 deletions src/lib/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,24 @@
* limitations under the License.
*/

import { isArray } from 'lodash/fp';
import { isArray } from './type-check';

type Enum = { [key: string]: string };
type Choices = { [key: string]: Enum | Enum[] };
type Combination = { [key: string]: string | string[] };

function isArrayTypeGuard(array: unknown): array is unknown[] {
return isArray(array);
}

export function getAllChoiceCombinations(
possibleChoices: Choices,
): Combination[] {
return Object.entries(possibleChoices).reduce(
(acc, [optionName, choices]) => {
const choiceEnum = isArrayTypeGuard(choices) ? choices[0] : choices;
const choiceEnum = isArray(choices) ? choices[0] : choices;
const choicesForOption = Object.values(choiceEnum);
const allCombinations: Combination[] = [];

acc.forEach((combination: Combination) => {
choicesForOption.forEach((value) => {
const choice = isArrayTypeGuard(choices) ? [value] : value;
const choice = isArray(choices) ? [value] : value;
allCombinations.push({
...combination,
[optionName]: choice,
Expand Down
56 changes: 56 additions & 0 deletions src/lib/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it, vi } from 'vitest';

import { flow, uniq } from './helpers.js';

describe('helpers', () => {
describe('flow', () => {
it('should call each function', () => {
const fn1 = vi.fn((value: string) => value);
const fn2 = vi.fn((value: string) => value);
const fn3 = vi.fn((value: string) => value);
const value = 'foo';
const actual = flow(fn1, fn2, fn3)(value);

expect(fn1).toHaveBeenCalledWith(value);
expect(fn2).toHaveBeenCalledWith(value);
expect(fn3).toHaveBeenCalledWith(value);
expect(actual).toBe(value);
});

it('should call the next function with the previous return value', () => {
const fn1 = vi.fn((value: number) => value + 1);
const fn2 = vi.fn((value: number) => value + 2);
const fn3 = vi.fn((value: number) => value + 3);
const value = 0;
const actual = flow(fn1, fn2, fn3)(value);

expect(fn1).toHaveBeenCalledWith(0);
expect(fn2).toHaveBeenCalledWith(1);
expect(fn3).toHaveBeenCalledWith(3);
expect(actual).toBe(6);
});
});

describe('uniq', () => {
it('should return an array of unique values', () => {
const values = [1, 3, 3, 4, 5, 1, 'foo', 'bar', 'foo'];
const actual = uniq(values);
expect(actual).toEqual([1, 3, 4, 5, 'foo', 'bar']);
});
});
});
Loading

0 comments on commit 64fd88c

Please sign in to comment.