Skip to content

Commit

Permalink
fix: use UnknownMap type for better type safety in utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaic4o committed Jun 9, 2024
1 parent 5c83e9c commit 83ae7f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-buttons-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/theme": patch
---

Used `@ts-ignore` to bypass type checking, and used `Object`, leading to potential type problems and limited flexibility. The function now uses the `StringMap` type for better type safety and flexibility.
17 changes: 7 additions & 10 deletions packages/core/theme/src/utils/object.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import flatten from "flat";

export function swapColorValues<T extends Object>(colors: T) {
const swappedColors = {};
type StringMap = Record<string, string>;

export function swapColorValues<T extends StringMap>(colors: T) {
const swappedColors: StringMap = {};
const keys = Object.keys(colors);
const length = keys.length;

for (let i = 0; i < length / 2; i++) {
const key1 = keys[i];
const key2 = keys[length - 1 - i];

// @ts-ignore
swappedColors[key1] = colors[key2];
// @ts-ignore
swappedColors[key2] = colors[key1];
}
if (length % 2 !== 0) {
const middleKey = keys[Math.floor(length / 2)];

// @ts-ignore
swappedColors[middleKey] = colors[middleKey];
}

return swappedColors;
}

export function removeDefaultKeys<T extends Object>(obj: T) {
const newObj = {};
export function removeDefaultKeys<T extends StringMap>(obj: T): StringMap {
const newObj: StringMap = {};

for (const key in obj) {
if (key.endsWith("-DEFAULT")) {
// @ts-ignore
newObj[key.replace("-DEFAULT", "")] = obj[key];
continue;
}
// @ts-ignore
newObj[key] = obj[key];
}

Expand All @@ -52,5 +49,5 @@ export const flattenThemeObject = <TTarget>(obj: TTarget) =>
flatten(obj, {
safe: true,
delimiter: "-",
}) as Object,
}) as StringMap,
);

0 comments on commit 83ae7f3

Please sign in to comment.