* @param {string} a * @param {string} b * @param {number} p
(a, b, p)
| 168 | * @param {number} p |
| 169 | */ |
| 170 | function interpolate(a, b, p) { |
| 171 | if (a === b) return a; |
| 172 | |
| 173 | const fallback = p < 0.5 ? a : b; |
| 174 | |
| 175 | const a_match = a.match(/[\d.]+|[^\d.]+/g); |
| 176 | const b_match = b.match(/[\d.]+|[^\d.]+/g); |
| 177 | |
| 178 | if (!a_match || !b_match) return fallback; |
| 179 | if (a_match.length !== b_match.length) return fallback; |
| 180 | |
| 181 | let result = ''; |
| 182 | |
| 183 | for (let i = 0; i < a_match.length; i += 2) { |
| 184 | const a_num = parseFloat(a_match[i]); |
| 185 | const b_num = parseFloat(b_match[i]); |
| 186 | result += a_num + (b_num - a_num) * p; |
| 187 | |
| 188 | if (a_match[i + 1] !== b_match[i + 1]) { |
| 189 | // bail |
| 190 | return fallback; |
| 191 | } |
| 192 | |
| 193 | result += a_match[i + 1] ?? ''; |
| 194 | } |
| 195 | |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param {Keyframe[]} keyframes |
no test coverage detected