Skip to content

Commit

Permalink
feat: Add core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arkarlov committed Sep 14, 2024
1 parent 5336153 commit 920a8bb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/formatters/camelToKebab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function camelToKebab(str: string) {
return str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
}
1 change: 1 addition & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./camelToKebab";
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./stringifyCSSProperties";
export * from "./stringifyStyles";

export * from "./types";
14 changes: 14 additions & 0 deletions src/stringifyCSSProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type CSSProperties } from "react";
import { camelToKebab } from "./formatters";

export function stringifyCSSProperties(
cssProperties: CSSProperties,
important: boolean = false
) {
return Object.entries(cssProperties)
.map(
([key, value]) =>
`${camelToKebab(key)}:${value}${important ? "!important" : ""}`
)
.join("; ");
}
13 changes: 13 additions & 0 deletions src/stringifyStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type StyleObject } from "./types";
import { stringifyCSSProperties } from "./stringifyCSSProperties";

export function stringifyStyles(
styleObject: StyleObject,
important: boolean = false
) {
return Object.entries(styleObject)
.map(
([key, value]) => `${key}{${stringifyCSSProperties(value, important)}}`
)
.join(" ");
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type CSSProperties } from "react";

export type CSSSelector = string;

export type StyleObject = Record<CSSSelector, CSSProperties>;

0 comments on commit 920a8bb

Please sign in to comment.