(
token: string | number,
defaultUnit?: string,
colors?: Record<string, Record<string, string>>,
values?: Record<string, Record<string, string>>,
rootSize?: number
)
| 4 | const VAR_START = 'var(--'; |
| 5 | |
| 6 | export function parseValue( |
| 7 | token: string | number, |
| 8 | defaultUnit?: string, |
| 9 | colors?: Record<string, Record<string, string>>, |
| 10 | values?: Record<string, Record<string, string>>, |
| 11 | rootSize?: number |
| 12 | ) { |
| 13 | let value: any = values ? values[token] : ''; |
| 14 | let unit: string = ''; |
| 15 | let unitToken: string = ''; |
| 16 | if (value) { |
| 17 | return { value, unit, unitToken } |
| 18 | } else if (typeof token === 'number') { |
| 19 | value = token; |
| 20 | unit = defaultUnit || ''; |
| 21 | } else { |
| 22 | if (colors) { |
| 23 | const colorNames = Object.keys(colors);; |
| 24 | token = token.replace( |
| 25 | new RegExp(`(^|,| |\\()(${colorNames.join('|')})(?:-([0-9]+))?(?:\\/(\\.?[0-9]+))?(?=(\\)|\\}|,| |$))`, 'gm'), |
| 26 | (origin, prefix, colorName, level, opacityStr, end) => { |
| 27 | const hexColor = colors[colorName][level || '']; |
| 28 | if (hexColor) { |
| 29 | let newValue = '#' + hexColor; |
| 30 | if (opacityStr) { |
| 31 | let opacity = +opacityStr; |
| 32 | opacity = isNaN(opacity) |
| 33 | ? 1 |
| 34 | : Math.min(Math.max(opacity, 0), 1); |
| 35 | |
| 36 | newValue += Math.round(opacity * 255).toString(16).toUpperCase().padStart(2, '0'); |
| 37 | } |
| 38 | |
| 39 | return prefix + newValue; |
| 40 | } |
| 41 | |
| 42 | return origin; |
| 43 | }); |
| 44 | } |
| 45 | if (defaultUnit) { |
| 46 | const matches = token.match(UNIT_VALUE_PATTERN); |
| 47 | // ['0.5deg', '0.5', 'deg', index: 0, input: '0.5deg', groups: undefined] |
| 48 | if (matches) { |
| 49 | if (token.includes('/')) { |
| 50 | // w:1/2 -> width: 50% |
| 51 | const splits = token.split('/'); |
| 52 | return { value: (+splits[0] / +splits[1]) * 100 + '%', unit, unitToken } |
| 53 | } else { |
| 54 | value = +matches[1]; |
| 55 | unit = unitToken = matches[3] || ''; |
| 56 | /** |
| 57 | * 當無單位值且 defaultUnit === 'rem', |
| 58 | * 將 pxValue / 16 轉為 remValue |
| 59 | */ |
| 60 | if (!unit) { |
| 61 | if (defaultUnit === 'rem' || defaultUnit === 'em') { |
| 62 | value = value / rootSize; |
| 63 | } |
no test coverage detected
searching dependent graphs…