(style: object | string)
| 43 | } |
| 44 | |
| 45 | export function styleToString(style: object | string): string { |
| 46 | // Faster escaping process that only looks for the " character. |
| 47 | // As we use the " character to wrap the style string, we need to escape it. |
| 48 | if (typeof style === "string") { |
| 49 | let end = style.indexOf('"'); |
| 50 | |
| 51 | // This is a optimization to avoid having to look twice for the " character. |
| 52 | // And make the loop already start in the middle |
| 53 | if (end === -1) { |
| 54 | return style; |
| 55 | } |
| 56 | |
| 57 | const length = style.length; |
| 58 | |
| 59 | let escaped = ""; |
| 60 | let start = 0; |
| 61 | |
| 62 | // Faster than using regex |
| 63 | // https://jsperf.app/kakihu |
| 64 | for (; end < length; end++) { |
| 65 | if (style[end] === '"') { |
| 66 | escaped += style.slice(start, end) + """; |
| 67 | start = end + 1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Appends the remaining string. |
| 72 | escaped += style.slice(start, end); |
| 73 | |
| 74 | return escaped; |
| 75 | } |
| 76 | |
| 77 | const keys = Object.keys(style); |
| 78 | const length = keys.length; |
| 79 | |
| 80 | let key; |
| 81 | let value; |
| 82 | let index = 0; |
| 83 | let result = ""; |
| 84 | |
| 85 | for (; index < length; index++) { |
| 86 | key = keys[index]; |
| 87 | // @ts-expect-error - this indexing is safe. |
| 88 | value = style[key]; |
| 89 | |
| 90 | if (value === null || value === undefined) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | // @ts-expect-error - this indexing is safe. |
| 95 | result += toKebabCase(key) + ":"; |
| 96 | |
| 97 | // Only needs escaping when the value is a string. |
| 98 | if (typeof value !== "string") { |
| 99 | result += value.toString() + ";"; |
| 100 | continue; |
| 101 | } |
| 102 |
no test coverage detected