Download chakra-ui/packages/react/src/styled-system/serialize.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/serialize.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/serialize.ts
-
curl -L -o serialize.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/serialize.ts
1.44 kB
| import { type Dict, isObject, walkObject } from "../utils" | |
| import type { SystemContext } from "./types" | |
| export function createSerializeFn( | |
| options: Pick<SystemContext, "conditions" | "isValidProperty">, | |
| ) { | |
| const { conditions, isValidProperty } = options | |
| return function serialize(styles: Dict) { | |
| return walkObject(styles, (value) => value, { | |
| getKey: (prop, value) => { | |
| if (!isObject(value)) return prop | |
| if (!conditions.has(prop) && !isValidProperty(prop)) { | |
| return parseSelectors(prop) | |
| .map((s) => "&" + s) | |
| .join(", ") | |
| } | |
| return prop | |
| }, | |
| }) | |
| } | |
| } | |
| function parseSelectors(selector: string): string[] { | |
| const result = [] as string[] | |
| let parenCount = 0 | |
| let currentSelector = "" | |
| let inEscape = false | |
| for (let i = 0; i < selector.length; i++) { | |
| const char = selector[i] | |
| if (char === "\\" && !inEscape) { | |
| inEscape = true | |
| currentSelector += char | |
| continue | |
| } | |
| if (inEscape) { | |
| inEscape = false | |
| currentSelector += char | |
| continue | |
| } | |
| if (char === "(") { | |
| parenCount++ | |
| } else if (char === ")") { | |
| parenCount-- | |
| } | |
| if (char === "," && parenCount === 0) { | |
| result.push(currentSelector.trim()) | |
| currentSelector = "" | |
| } else { | |
| currentSelector += char | |
| } | |
| } | |
| if (currentSelector) { | |
| result.push(currentSelector.trim()) | |
| } | |
| return result | |
| } | |