MCPcopy
hub / github.com/sveltejs/svelte / interpolate

Function interpolate

packages/svelte/tests/animation-helpers.js:170–197  ·  view source on GitHub ↗

* @param {string} a * @param {string} b * @param {number} p

(a, b, p)

Source from the content-addressed store, hash-verified

168 * @param {number} p
169 */
170function 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

Callers 3

setFunction · 0.85
setMethod · 0.85
#apply_keyframeMethod · 0.85

Calls 1

matchMethod · 0.80

Tested by

no test coverage detected