(...styles: (StyleString | null | undefined)[])
| 51 | * // merged has `padding: 16` and `color: heading`. |
| 52 | */ |
| 53 | export function mergeStyles(...styles: (StyleString | null | undefined)[]): StyleString { |
| 54 | let definedStyles = styles.filter(Boolean) as StyleString[]; |
| 55 | if (definedStyles.length === 1) { |
| 56 | let first = definedStyles[0]; |
| 57 | if (typeof first !== 'string') { |
| 58 | // static macro has a toString method so that we generate the style macro map for the entry |
| 59 | // it's automatically called in other places, but for our merging, we have to call it ourselves |
| 60 | return (first as StyleString).toString() as StyleString; |
| 61 | } |
| 62 | return first; |
| 63 | } |
| 64 | |
| 65 | let map = new Map<string, string>(); |
| 66 | |
| 67 | for (let style of definedStyles) { |
| 68 | // must call toString here for the static macro |
| 69 | let str = style.toString(); |
| 70 | |
| 71 | for (let [k, v] of parse(str)) { |
| 72 | map.set(k, v); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | let res = ''; |
| 77 | for (let value of map.values()) { |
| 78 | res += value; |
| 79 | } |
| 80 | |
| 81 | return res as StyleString; |
| 82 | } |
| 83 | |
| 84 | function parse(s: string) { |
| 85 | let properties = new Map<string, string>(); |
no test coverage detected