Skip to content

Commit

Permalink
refactor: remove lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
jlopezcur committed Oct 29, 2024
1 parent ead2549 commit 5e72dd8
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 31 deletions.
2 changes: 0 additions & 2 deletions package-lock.json

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

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"happy-dom": "^15.7.4",
"identity-obj-proxy": "^3.0.0",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"mermaid": "^11.2.1",
"oxlint": "^0.9.8",
"polished": "^4.3.1",
Expand Down Expand Up @@ -96,8 +95,5 @@
"engines": {
"node": ">=18",
"npm": ">=7"
},
"overrides": {
"@types/lodash": "4.14.197"
}
}
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@dnd-kit/sortable": "7.x.x",
"@popperjs/core": "2.x.x",
"ahooks": "3.x.x",
"lodash": "4.x.x",
"polished": "4.x.x",
"react": "16.x || 17.x || 18.x",
"react-dock": "0.x.x",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import type { IAvatarStyled } from '../../declarations';

import { getAvatarSizeConfig } from '../../utils';

import { typoMixin } from '../../../../styled/';
import { getTokenKeyFromColorScheme } from '../../../../helpers';

export interface StyledAvatarContainerProps
extends Pick<
Expand All @@ -30,7 +30,7 @@ export const StyledAvatarContainer = styled.span<StyledAvatarContainerProps>`
theme,
$variant,
}) => {
const colorSchemeForTokens = camelCase($colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme($colorScheme);
const cmpTokens = theme.cmp.avatar;
const bgColor = cmpTokens.color.background[colorSchemeForTokens];
const color = cmpTokens.color.text[colorSchemeForTokens];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import { isValidColor, getAccTextColor } from '../../../../helpers';
import {
isValidColor,
getAccTextColor,
getTokenKeyFromColorScheme,
} from '../../../../helpers';
import { getBadgeInverseModeColor } from '../../helpers';

import type { IBadgeContainer } from './declarations';
Expand All @@ -28,7 +31,7 @@ export const StyledBadgeContainer = styled.span<StyledBadgeContainerProps>`
$size = 'md',
theme,
}) => {
const colorSchemeForTokens = camelCase($colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme($colorScheme);
const contentType = $hasContent ? 'hasContent' : 'isEmpty';
const cmpTokens = theme.cmp.badge;
const squareSize = cmpTokens.size.square[contentType][$size];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import {
typoMixin,
Expand All @@ -8,6 +7,7 @@ import {
} from '../../../../styled/mixins';
import { loadingAnimationMixin } from '../../helpers';
import type { IButtonContainerStyled } from './declarations';
import { getTokenKeyFromColorScheme } from '../../../../helpers';

export interface StyledButtonContainerProps extends IButtonContainerStyled {}

Expand All @@ -25,7 +25,7 @@ export const StyledButtonContainer = styled.button<StyledButtonContainerProps>`
theme,
}) => {
const isIconButtonDropdown = $icon && $squared && $hasDropdown;
const colorSchemeForTokens = camelCase($colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme($colorScheme);
const buttonTokens = theme.cmp.button;
const IconButtonDropdownTokens = theme.cmp.iconButtonDropdown;
const boxShadowTokens = buttonTokens.elevation.boxShadow;
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/components/Divider/StyledDivider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import type { IDividerStyled } from './declarations';
import { getSpacingPropCss } from '../../helpers';
import { getSpacingPropCss, getTokenKeyFromColorScheme } from '../../helpers';

export interface StyledDividerProps extends IDividerStyled {}

Expand All @@ -21,7 +20,7 @@ export const StyledDivider = styled.hr<StyledDividerProps>`
$width,
}) => {
const cmpTokens = theme.cmp.divider;
const colorSchemeForTokens = camelCase($colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme($colorScheme);
const defaultSpacing =
cmpTokens.space.margin[$vertical ? 'vertical' : 'base'];
const spacing = $margin
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/components/Panel/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { css, DefaultTheme } from 'styled-components';

import type { BoxProps } from '../Box';
import { pseudoElementOverlayMixin } from '../../styled';
import { camelCase } from 'lodash';
import { getTokenKeyFromColorScheme } from '../../helpers';

export interface PanelMixinProps
extends Pick<BoxProps, 'display' | 'elevation'> {
Expand All @@ -19,7 +19,7 @@ export interface PanelMixinProps
export const panelMixin =
(theme: DefaultTheme) =>
({ bordered, colorScheme, display, elevation }: PanelMixinProps) => {
const elevationForTokens = camelCase(elevation);
const elevationForTokens = getTokenKeyFromColorScheme(elevation);
const tokens = theme.cmp.panel;
const borderColor = tokens.color.border[elevationForTokens];
const borderWidth = tokens.shape.borderSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import type { IPartitionsItem, TPartitionsSize } from '../../declarations';
import { isValidColor } from '../../../../helpers';
import { getTokenKeyFromColorScheme, isValidColor } from '../../../../helpers';

export interface StyledPartitionsItemProps {
/** The size of the partition item. */
Expand All @@ -16,7 +15,7 @@ export interface StyledPartitionsItemProps {
export const StyledPartitionsItem = styled.li<StyledPartitionsItemProps>`
transition: all 0.15s ease;
background-color: ${({ $color, theme }) => {
const colorSchemeForTokens = camelCase($color);
const colorSchemeForTokens = getTokenKeyFromColorScheme($color);
return isValidColor($color)
? $color
: theme.cmp.partitions.color.background[colorSchemeForTokens];
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/components/SelectControl/SelectControl.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { get } from 'lodash';
import {
CSSObjectWithLabel,
GroupBase,
Expand Down Expand Up @@ -134,7 +133,7 @@ export const SelectControl = <
// react-select will be constantly changing the state and re-rendering
// all the selects, even the closed ones.
ev != null &&
get(ev, 'srcElement.classList') != null &&
ev?.srcElement?.classList != null &&
ev.srcElement.getElementsByClassName(
`${rest.classNamePrefix}__control--menu-is-open`,
).length &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import styled, { css } from 'styled-components';
import { camelCase } from 'lodash';

import { typoMixin } from '../../../../styled';
import { getAccTextColor, isValidColor } from '../../../../helpers';
import {
getAccTextColor,
getTokenKeyFromColorScheme,
isValidColor,
} from '../../../../helpers';
import type { ITag } from '../../declarations';

export interface StyledTagContainerProps {
Expand All @@ -22,7 +25,7 @@ export const StyledTagContainer = styled.span<StyledTagContainerProps>`
$size = 'md',
theme,
}) => {
const colorSchemeForTokens = camelCase($colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme($colorScheme);
const isBlendColorScheme =
$colorScheme === 'blend-base' || $colorScheme === 'blend-inverse';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/helpers/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './accessibility';
export * from './colors';
export * from './getters';
export * from './tokens';
export * from './validation';
18 changes: 18 additions & 0 deletions packages/core/src/helpers/color/tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, test } from 'vitest';

import { getTokenKeyFromColorScheme } from './tokens';

describe('helpers', () => {
describe('color', () => {
describe('tokens', () => {
const cases: [string, string, string][] = [
['test', 'test', 'test'],
['test-test', 'test-test', 'testTest'],
];

test.each(cases)('%s', (_title, colorScheme, expected) => {
expect(getTokenKeyFromColorScheme(colorScheme)).toBe(expected);
});
});
});
});
9 changes: 9 additions & 0 deletions packages/core/src/helpers/color/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// colorScheme could be 'blend-base', 'data-green', ...
// and in tokens the colorSchemes are 'blendBase', 'dataGreen', ...
export const getTokenKeyFromColorScheme = (colorScheme: string) =>
colorScheme
.split('-')
.map((chunk, index) =>
index === 0 ? chunk : `${chunk[0].toUpperCase()}${chunk.slice(1)}`,
)
.join('');
4 changes: 2 additions & 2 deletions packages/core/src/helpers/icon/color.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DefaultTheme } from 'styled-components';
import { camelCase } from 'lodash';

import { isValidColor } from '../color';
import type { TAllColorScheme } from '../../declarations';
import { getTokenKeyFromColorScheme } from '../color/tokens';

export const getIconColorSchemeToken = ({
colorScheme,
Expand All @@ -11,7 +11,7 @@ export const getIconColorSchemeToken = ({
colorScheme: TAllColorScheme;
theme: DefaultTheme;
}) => {
const colorSchemeForTokens = camelCase(colorScheme);
const colorSchemeForTokens = getTokenKeyFromColorScheme(colorScheme);
return theme.cmp.icon.color.fill[colorSchemeForTokens];
};

Expand Down
1 change: 0 additions & 1 deletion packages/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default defineConfig({
'react-dom/server',
'react/jsx-runtime',
'react-window',
'lodash',
'react-select',
'react-select/creatable',
'@dnd-kit/core',
Expand Down
1 change: 0 additions & 1 deletion packages/table/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default defineConfig({
'react-dom',
'react/jsx-runtime',
'styled-components',
'lodash',
'date-fns',
'date-fns-tz',
'date-fns/locale',
Expand Down

0 comments on commit 5e72dd8

Please sign in to comment.