(definition)
| 25 | const cache = {}; |
| 26 | |
| 27 | export default function createAnimation(definition) { |
| 28 | const cacheKey = JSON.stringify(definition); |
| 29 | if (cache[cacheKey]) { |
| 30 | return cache[cacheKey]; |
| 31 | } |
| 32 | |
| 33 | const positions = Object.keys(definition).map(parsePosition).filter(notNull); |
| 34 | positions.sort(compareNumbers); |
| 35 | |
| 36 | if (positions.length < 2) { |
| 37 | throw new Error('Animation definitions must have at least two values.'); |
| 38 | } |
| 39 | |
| 40 | const compiled = {}; |
| 41 | if (definition.easing) { |
| 42 | compiled.easing = definition.easing; |
| 43 | } |
| 44 | if (definition.style) { |
| 45 | compiled.style = definition.style; |
| 46 | } |
| 47 | |
| 48 | for (let i = 0; i < positions.length; i += 1) { |
| 49 | const position = positions[i]; |
| 50 | let keyframe = definition[position]; |
| 51 | if (!keyframe) { |
| 52 | if (position === 0) { |
| 53 | keyframe = definition.from; |
| 54 | } else if (position === 1) { |
| 55 | keyframe = definition.to; |
| 56 | } |
| 57 | } |
| 58 | if (!keyframe) { |
| 59 | throw new Error('Missing animation keyframe, this should not happen'); |
| 60 | } |
| 61 | |
| 62 | keyframe = flattenStyle(keyframe); |
| 63 | Object.keys(keyframe).forEach((key) => { |
| 64 | if (!(key in compiled)) { |
| 65 | compiled[key] = { |
| 66 | inputRange: [], |
| 67 | outputRange: [], |
| 68 | }; |
| 69 | } |
| 70 | compiled[key].inputRange.push(position); |
| 71 | compiled[key].outputRange.push(keyframe[key]); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | cache[cacheKey] = compiled; |
| 76 | |
| 77 | return compiled; |
| 78 | } |
no test coverage detected
searching dependent graphs…