Allow ysMap to map to anoter type

This commit is contained in:
2023-10-17 22:01:18 +02:00
parent ca7882ba73
commit e1ada31468
5 changed files with 20 additions and 18 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "yoshiki",
"version": "1.2.8",
"version": "1.2.9",
"author": "Zoe Roux <zoe.roux@sdg.moe> (https://github.com/zoriya)",
"license": "MIT",
"keywords": [
+1
View File
@@ -34,3 +34,4 @@ export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: Re
};
export * from "../type";
export * from "../utils";
-16
View File
@@ -79,19 +79,3 @@ export const assignChilds = <Style>(
}
return target;
};
export const ysMap = <Property>(
value: YoshikiStyle<Property>,
f: (p: Property) => Property,
): YoshikiStyle<Property> => {
if (typeof value === "function") {
// @ts-ignore too hard to type right
return (theme) => ysMap(value(theme), f);
}
if (isBreakpoints(value)) {
return Object.fromEntries(
Object.entries(value).map(([bp, bpValue]) => [bp, ysMap(bpValue, f)]),
);
}
return f(value);
};
+17 -1
View File
@@ -4,7 +4,7 @@
//
import { breakpoints } from "./theme";
import { Breakpoints } from "./type";
import { Breakpoints, YoshikiStyle } from "./type";
export const isBreakpoints = <T>(value: unknown): value is Breakpoints<T> => {
if (typeof value !== "object" || !value) return false;
@@ -26,3 +26,19 @@ export const forceBreakpoint = <T extends Record<string, unknown>>(
Object.entries(value).map(([key, value]) => [key, { [breakpoint]: value }]),
) as any;
};
export const ysMap = <Property, Mapped>(
value: YoshikiStyle<Property>,
f: (p: Property) => Mapped,
): YoshikiStyle<Mapped> => {
if (typeof value === "function") {
// @ts-ignore too hard to type right
return (theme) => ysMap(value(theme), f);
}
if (isBreakpoints(value)) {
return Object.fromEntries(
Object.entries(value).map(([bp, bpValue]) => [bp, ysMap(bpValue, f)]),
);
}
return f(value);
};
+1
View File
@@ -27,3 +27,4 @@ export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: Re
createElement(ThemeContext.Provider, { value: theme }, [children]);
export * from "../type";
export * from "../utils";