When I write a jQuery plugin, I like to specify CSS style distance options. I wrote a function that returns CSS String as values ββin an object.
5px 10px returns top: 5px, right: 10px, bottom: 5px, left: 10px
Now I often use return values ββto perform some calculations, and itβs not very nice to retrieve a measurement unit every time ...
I suck written regular expressions, can someone help me fulfill this function:
this.cssMeasure = function(cssString, separateUnits){ if ( cssString ){ var values = {} }else{ return errorMsg } var spacing = cssString.split(' ') var errorMsg = 'please format your css values correctly dude' if( spacing[4] ) { return errorMsg } else if ( spacing[3] ) { values = {top: spacing[0], right:spacing[1], bottom:spacing[2], left:spacing[3]} } else if ( spacing[2] ) { values = {top: spacing[0], right:spacing[1], bottom:spacing[2], left:spacing[1]} } else if ( spacing[1] ) { values = {top: spacing[0], right:spacing[1], bottom:spacing[0], left:spacing[1]} } else { values = {top: spacing[0], right:spacing[0], bottom:spacing[0], left:spacing[0]} } if (separateUnits) { $.each(values, function(i, value){ }) } return values }
If you have an idea how to improve this feature, I am open to your comments.
source share