| 14 | import { placeholderText } from './util'; |
| 15 | |
| 16 | const substitutePlaceholders = ( |
| 17 | stringWithPlaceholders: string, |
| 18 | expressions: string[] |
| 19 | ) => { |
| 20 | if (!stringWithPlaceholders.includes(placeholderText) || !expressions) { |
| 21 | return stringWithPlaceholders; |
| 22 | } |
| 23 | |
| 24 | const values = stringWithPlaceholders.split(' '); |
| 25 | const temp: string[] = []; |
| 26 | values.forEach((val) => { |
| 27 | let [prefix, expressionIndexString] = val.split(placeholderText); |
| 28 | prefix = prefix.replace(/(\.|--|\/\*)$/, ''); |
| 29 | // if the val is 'pcss-lin10px', need to remove the px to get the placeholder number |
| 30 | let suffix = ''; |
| 31 | while ( |
| 32 | Number.isNaN(Number(expressionIndexString)) && |
| 33 | expressionIndexString && |
| 34 | expressionIndexString.length > 0 |
| 35 | ) { |
| 36 | suffix = expressionIndexString[expressionIndexString.length - 1] + suffix; |
| 37 | expressionIndexString = expressionIndexString.slice( |
| 38 | 0, |
| 39 | expressionIndexString.length - 1 |
| 40 | ); |
| 41 | } |
| 42 | const expressionIndex = Number(expressionIndexString); |
| 43 | const expression = |
| 44 | expressions && |
| 45 | !Number.isNaN(expressionIndex) && |
| 46 | expressions[expressionIndex]; |
| 47 | if (expression) { |
| 48 | temp.push(prefix + expression + suffix); |
| 49 | } else { |
| 50 | temp.push(val); |
| 51 | } |
| 52 | }); |
| 53 | return temp.join(' '); |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * Stringifies PostCSS nodes while taking interpolated expressions |