Download chakra-ui/packages/react/src/styled-system/unit-conversion.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/unit-conversion.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/unit-conversion.ts
-
curl -L -o unit-conversion.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/styled-system/unit-conversion.ts
1.48 kB
| const BASE_FONT_SIZE = 16 | |
| const UNIT_PX = "px" | |
| const UNIT_EM = "em" | |
| const UNIT_REM = "rem" | |
| export function getUnit(value = ""): string | undefined { | |
| const DIGIT_REGEX = new RegExp(String.raw`-?\d+(?:\.\d+|\d*)`) | |
| const UNIT_REGEX = new RegExp(`${UNIT_PX}|${UNIT_EM}|${UNIT_REM}`) | |
| const unit = value.match( | |
| new RegExp(`${DIGIT_REGEX.source}(${UNIT_REGEX.source})`), | |
| ) | |
| return unit?.[1] | |
| } | |
| export function toPx(value: string | number = ""): string | undefined { | |
| if (typeof value === "number") { | |
| return `${value}px` | |
| } | |
| const unit = getUnit(value) | |
| if (!unit) return value | |
| if (unit === UNIT_PX) { | |
| return value | |
| } | |
| if (unit === UNIT_EM || unit === UNIT_REM) { | |
| return `${parseFloat(value) * BASE_FONT_SIZE}${UNIT_PX}` | |
| } | |
| } | |
| export function toEm( | |
| value = "", | |
| fontSize = BASE_FONT_SIZE, | |
| ): string | undefined { | |
| const unit = getUnit(value) | |
| if (!unit) return value | |
| if (unit === UNIT_EM) { | |
| return value | |
| } | |
| if (unit === UNIT_PX) { | |
| return `${parseFloat(value) / fontSize}${UNIT_EM}` | |
| } | |
| if (unit === UNIT_REM) { | |
| return `${(parseFloat(value) * BASE_FONT_SIZE) / fontSize}${UNIT_EM}` | |
| } | |
| } | |
| export function toRem(value = ""): string | undefined { | |
| const unit = getUnit(value) | |
| if (!unit) return value | |
| if (unit === UNIT_REM) { | |
| return value | |
| } | |
| if (unit === UNIT_EM) { | |
| return `${parseFloat(value)}${UNIT_REM}` | |
| } | |
| if (unit === UNIT_PX) { | |
| return `${parseFloat(value) / BASE_FONT_SIZE}${UNIT_REM}` | |
| } | |
| } | |