(value: string, getUnit: (method: string) => string)
| 1 | import { REM } from '../constants/css-property-keyword'; |
| 2 | |
| 3 | export function parseValueUnit(value: string, getUnit: (method: string) => string): string { |
| 4 | let result = ''; |
| 5 | |
| 6 | let i = 0; |
| 7 | (function analyze(end?, method?) { |
| 8 | let current = ''; |
| 9 | const unit = method |
| 10 | ? getUnit(method) |
| 11 | : ''; |
| 12 | |
| 13 | const pushCurrent = () => { |
| 14 | if (current) { |
| 15 | result += (!unit || Number.isNaN(+current)) |
| 16 | ? current |
| 17 | : (+current / (unit === REM ? 16 : 1)) + unit; |
| 18 | |
| 19 | current = ''; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | for (; i < value.length; i++) { |
| 24 | const val = value[i]; |
| 25 | |
| 26 | if (val === end && (end !== '\'' || value[i + 1] === ')')) { |
| 27 | pushCurrent(); |
| 28 | result += val; |
| 29 | break; |
| 30 | } else if (val === ',' || val === ' ') { |
| 31 | pushCurrent(); |
| 32 | result += val; |
| 33 | } else if (!current && val === '\'') { |
| 34 | result += val; |
| 35 | |
| 36 | i++; |
| 37 | analyze(val); |
| 38 | current = ''; |
| 39 | } else if (current && val === '(') { |
| 40 | result += current + val; |
| 41 | |
| 42 | i++; |
| 43 | analyze(')', current); |
| 44 | current = ''; |
| 45 | } else { |
| 46 | current += val; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pushCurrent(); |
| 51 | })(); |
| 52 | |
| 53 | return result; |
| 54 | } |
no test coverage detected