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 a06422f commit 7ce3a5b
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
---

WHAT: Used `@ts-ignore` to bypass type checking, Functions used `Record<string, string>` or `Object`, causing potential type issues and limited flexibility. Functions now use `UnknownMap` 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 UnknownMap = Record<string, unknown>;

export function swapColorValues<T extends UnknownMap>(colors: T) {
const swappedColors: UnknownMap = {};
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 UnknownMap>(obj: T): UnknownMap {
const newObj: UnknownMap = {};

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 UnknownMap,
);

0 comments on commit 7ce3a5b

Please sign in to comment.