import { isDefined , isNil } from "./utility" ; import { Option , Some , None } from "@architus/lib/option" ; const baseDimensionUnits = [ "cm" , "mm" , "Q" , "in" , "pc" , "pt" , "px" , "em" , "ex" , "ch" , "rem" , "lh" , "vw" , "vh" , "vmin" , "vmax" , ] as const ; remarks The units come from the  https export type DimensionUnit = typeof baseDimensionUnits [ number ] ; remarks The units come from the  https export const dimensionUnits = baseDimensionUnits as readonly string [ ] ; /**
 * Represents a CSS dimension, with possible units derived from DimensionUnit
 */ export interface Dimension { amount : number ; unit : DimensionUnit ; } export type RawDimension = string | number | Dimension ; export function isDimension ( o ) { return ( typeof o === "object" && isDefined ( o ) && isDefined ( ( o as Dimension ) . amount ) && isDefined ( ( o as Dimension ) . unit ) && typeof ( o as Dimension ) . amount === "number" && typeof ( o as Dimension ) . unit === "string" ) ; } const DIMENSION_REGEX = /^([0-9]*\.?[0-9]*)([A-Za-z%]+)$/ ; param rawDimension export function parseDimension ( rawDimension ) { if ( typeof rawDimension === "object" ) { if ( isDimension ( rawDimension ) ) { return Some ( rawDimension ) ; } return None ; } if ( typeof rawDimension === "number" ) return Some ( { unit : "px" , amount : rawDimension } ) ; const parsed = Number ( rawDimension ) ; if ( isNaN ( parsed ) ) { const matches = DIMENSION_REGEX . exec ( rawDimension ) ; if ( isNil ( matches ) ) { return None ; } const amount = Number . parseFloat ( matches [ 1 ] ) ; const unit = matches [ 2 ] ; if ( dimensionUnits . includes ( unit ) ) { return Some ( { unit : unit as DimensionUnit , amount } ) ; } return None ; } return Some ( { unit : "px" , amount : parsed } ) ; } param dimension param precision export function formatDimension ( dimension , precision = 3 ) { return `${ dimension . amount . toFixed ( precision ) }${ dimension . unit }` ; } param dimension param scalar export function multiplyDimension ( dimension , scalar ) { return { amount : dimension . amount * scalar , unit : dimension . unit , } ; }