1- import { isArray , mapKeys , mapValues , pickBy } from 'lodash'
2- import stepper from './stepper'
1+ import { isArray , isEmpty , mapKeys , mapValues , negate , pickBy , uniq } from 'lodash'
32import { combine } from './values'
3+ import interpolate from './interpolate'
44import { getAnimatableProps } from './parse'
55
66// spring presets. selected combinations of stiffness/damping.
@@ -21,13 +21,6 @@ const defaultOptions = {
2121 precision : 2 ,
2222}
2323
24- export const appendUnits = ( object , units ) =>
25- mapValues ( object , ( value , key ) => {
26- return isArray ( value )
27- ? value . map ( ( value , i ) => `${ value } ${ units [ key ] [ i ] } ` )
28- : `${ value } ${ units [ key ] } `
29- } )
30-
3124// @todo comment
3225const buildInterpolation = ( stiffness , damping ) => {
3326 return ( start , end ) => {
@@ -41,12 +34,12 @@ const buildInterpolation = (stiffness, damping) => {
4134 let something
4235 for ( let j = 0 ; j < start . length ; j += 1 ) {
4336 something = [ ] ;
44- [ value , velocity ] = stepper ( 0.01 , value , velocity , end [ j ] , stiffness , damping )
37+ [ value , velocity ] = interpolate ( 0.01 , value , velocity , end [ j ] , stiffness , damping )
4538 something . push ( value )
4639 }
4740 interpolated . push ( something )
4841 } else {
49- [ value , velocity ] = stepper ( 0.01 , value , velocity , end , stiffness , damping )
42+ [ value , velocity ] = interpolate ( 0.01 , value , velocity , end , stiffness , damping )
5043 interpolated . push ( value )
5144 }
5245 }
@@ -92,29 +85,59 @@ export const spring = (startProps, endProps, options = {}) => {
9285
9386 // iterate over the result object, combining values and identifying
9487 // equal frames to be able to eliminate duplicates in a later step
95- let prevFrame
96- const obsoleteFrames = [ ]
97- Object . keys ( result ) . forEach ( ( i ) => {
98- const currentFrame = JSON . stringify ( result [ i ] )
99- result [ i ] = mapValues ( result [ i ] , ( value , key ) => combine ( key , value ) )
100- if ( prevFrame === currentFrame ) {
101- obsoleteFrames . push ( i - 1 )
102- }
103- prevFrame = currentFrame
88+ // let prevFrame
89+ // const obsoleteFrames = []
90+ // Object.keys(result).forEach((i) => {
91+ // const currentFrame = JSON.stringify(result[i])
92+ // result[i] = mapValues(result[i], (value, key) => combine(key, value))
93+ // if (prevFrame === currentFrame) {
94+ // obsoleteFrames.push(i - 1)
95+ // }
96+ // prevFrame = currentFrame
97+ // })
98+
99+ const props = uniq ( data . map ( ( { prop } ) => prop ) )
100+ const obsoleteValues = [ ]
101+
102+ // iterate over the result object for each interpolated css property,
103+ // combining values and identifying equal ones to be able to eliminate
104+ // them in a later step
105+ props . forEach ( ( prop ) => {
106+ let prevValue
107+ Object . keys ( result ) . forEach ( ( i ) => {
108+ // get the value of the interpolated property in this frame
109+ const currentValue = JSON . stringify ( result [ i ] [ prop ] )
110+ obsoleteValues [ i ] = obsoleteValues [ i ] || [ ]
111+ result [ i ] = mapValues ( result [ i ] , ( value , key ) => combine ( key , value ) )
112+ if ( prevValue === currentValue && Number ( i ) !== 100 ) {
113+ obsoleteValues [ i ] . push ( prop )
114+ }
115+ prevValue = currentValue
116+ } )
104117 } )
105118
106- // remove obsolute frames to reduce size and add % to keys
119+ // remove obsolute properties and frames to reduce size and add % to keys
107120 // @todo might chain this - not using chain.
108121 // @see https://medium.com/making-internets/why-using-chain-is-a-mistake-9bc1f80d51ba
109- result = mapKeys (
110- pickBy ( result , ( value , key ) => obsoleteFrames . indexOf ( Number ( key ) ) < 0 ) ,
111- ( value , key ) => `${ key } %`
122+ result = pickBy (
123+ mapKeys (
124+ mapValues ( result , ( value , key ) => {
125+ return pickBy (
126+ value ,
127+ ( propValue , propName ) => obsoleteValues [ Number ( key ) ] . indexOf ( propName ) < 0
128+ )
129+ } ) ,
130+ ( value , key ) => `${ key } %`
131+ ) ,
132+ ( frame ) => negate ( isEmpty ) ( frame )
112133 )
113134
114- console . log ( result )
135+ // console.log(result)
136+
115137 return result
116138}
117139
140+ console . time ( 'interpolate' )
118141spring ( {
119142 left : '10px' ,
120143 right : '20px' ,
@@ -126,6 +149,7 @@ spring({
126149} , {
127150 preset : 'noWobble' ,
128151} )
152+ console . timeEnd ( 'interpolate' )
129153
130154export { default as toString } from './to-string'
131155export default spring
0 commit comments