* Supports string shapes by extracting numbers so new values can be computed, * and recombines those values into new strings of the same shape. Supports * things like: * * rgba(123, 42, 99, 0.36) // colors * -45deg // values with units
( config: InterpolationConfigType, )
| 184 | * -45deg // values with units |
| 185 | */ |
| 186 | function createInterpolationFromStringOutputRange( |
| 187 | config: InterpolationConfigType, |
| 188 | ): (input: number) => string { |
| 189 | var outputRange: Array<string> = (config.outputRange: any); |
| 190 | invariant(outputRange.length >= 2, 'Bad output range'); |
| 191 | outputRange = outputRange.map(colorToRgba); |
| 192 | checkPattern(outputRange); |
| 193 | |
| 194 | // ['rgba(0, 100, 200, 0)', 'rgba(50, 150, 250, 0.5)'] |
| 195 | // -> |
| 196 | // [ |
| 197 | // [0, 50], |
| 198 | // [100, 150], |
| 199 | // [200, 250], |
| 200 | // [0, 0.5], |
| 201 | // ] |
| 202 | /* $FlowFixMe(>=0.18.0): `outputRange[0].match()` can return `null`. Need to |
| 203 | * guard against this possibility. |
| 204 | */ |
| 205 | var outputRanges = outputRange[0].match(stringShapeRegex).map(() => []); |
| 206 | outputRange.forEach(value => { |
| 207 | /* $FlowFixMe(>=0.18.0): `value.match()` can return `null`. Need to guard |
| 208 | * against this possibility. |
| 209 | */ |
| 210 | value.match(stringShapeRegex).forEach((number, i) => { |
| 211 | outputRanges[i].push(+number); |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | /* $FlowFixMe(>=0.18.0): `outputRange[0].match()` can return `null`. Need to |
| 216 | * guard against this possibility. |
| 217 | */ |
| 218 | var interpolations = outputRange[0].match(stringShapeRegex).map((value, i) => { |
| 219 | return Interpolation.create({ |
| 220 | ...config, |
| 221 | outputRange: outputRanges[i], |
| 222 | }); |
| 223 | }); |
| 224 | |
| 225 | // rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to |
| 226 | // round the opacity (4th column). |
| 227 | const shouldRound = (/^rgb/).test(outputRange[0]); |
| 228 | |
| 229 | return (input) => { |
| 230 | var i = 0; |
| 231 | // 'rgba(0, 100, 200, 0)' |
| 232 | // -> |
| 233 | // 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...' |
| 234 | return outputRange[0].replace(stringShapeRegex, () => { |
| 235 | const val = interpolations[i++](input); |
| 236 | return String(shouldRound && i < 4 ? Math.round(val) : val); |
| 237 | }); |
| 238 | }; |
| 239 | } |
| 240 | |
| 241 | function checkPattern(arr: Array<string>) { |
| 242 | var pattern = arr[0].replace(stringShapeRegex, ''); |
no test coverage detected
searching dependent graphs…