1- const ignoredKeys = [ 'scale' , 'scale3d' , 'scaleX' , 'scaleY' , 'scaleZ' ] ;
1+ export interface TransformObject {
2+ [ key : string ] : number | string | ( string | number ) [ ] | boolean ;
3+ }
24
3- const parse = ( transformString : string ) : Record < string , any > => {
4- const transforms = transformString . split ( / \) | \) / ) ;
5+ // List of transforms for which the value has to be numeral. Default px substitution for these
6+ // transforms will be skipped.
7+ const transformsWithNumeralValues = [ 'scale' , 'scale3d' , 'scaleX' , 'scaleY' , 'scaleZ' ] ;
8+
9+ /**
10+ * Returns a object representation of the transform string supplied. It parses the string and
11+ * converts them to an object.
12+ * @param transformString String containing the transforms
13+ */
14+ const parse = ( transformString : string ) : TransformObject => {
15+ const transforms = transformString . trim ( ) . split ( / \) | \) / ) ;
16+ // Handle "initial", "inherit", "unset".
517 if ( transforms . length === 1 ) {
618 return {
719 [ transforms [ 0 ] ] : true ,
820 } ;
921 }
1022
23+ /**
24+ * Converts string values to number when the unit is pixel or parsable. Returns a string when the
25+ * unit is not pixel or not parsable.
26+ * @param value Value of a transform
27+ */
1128 const parsePixelValues = ( value : string ) : string | number => {
1229 if ( value . endsWith ( 'px' ) ) return parseFloat ( value ) ;
1330 if ( ! Number . isNaN ( Number ( value ) ) ) return Number ( value ) ;
@@ -28,9 +45,13 @@ const parse = (transformString: string): Record<string, any> => {
2845 } , { } ) ;
2946} ;
3047
31- const stringify = ( transformObject : Record < string , number | string | boolean | ( string | number ) [ ] > ) : string => {
48+ /**
49+ * Returns the transform string constructed from the transform object supplied.
50+ * @param transformObject Object containing the transforms
51+ */
52+ const stringify = ( transformObject : TransformObject ) : string => {
3253 const resolveUnits = ( key : string , val : number | string ) : string => {
33- if ( typeof val === 'number' && ! ignoredKeys . includes ( key ) ) {
54+ if ( typeof val === 'number' && ! transformsWithNumeralValues . includes ( key ) ) {
3455 return `${ val } px` ;
3556 }
3657 return val . toString ( ) ;
@@ -41,10 +62,11 @@ const stringify = (transformObject: Record<string, number | string | boolean | (
4162 if ( value === true ) return key ;
4263 if ( value === false ) return acc ;
4364 if ( Array . isArray ( value ) ) {
44- return `${ acc } ${ key } (${ value . map ( ( val ) => resolveUnits ( key , val ) ) . join ( ', ' ) } )` . trim ( ) ;
65+ return `${ acc } ${ key } (${ value . map ( ( val ) => resolveUnits ( key , val ) ) . join ( ', ' ) } )` ;
4566 }
46- return `${ acc } ${ key } (${ resolveUnits ( key , value ) } )` . trim ( ) ;
47- } , '' ) ;
67+ return `${ acc } ${ key } (${ resolveUnits ( key , value ) } )` ;
68+ } , '' )
69+ . trim ( ) ;
4870} ;
4971
5072export { parse , stringify } ;
0 commit comments