index.js
35 lines
| 1 | import isNumeric from '../is-numeric'; |
| 2 | |
| 3 | function value( num, unit ) { |
| 4 | return isNumeric( num ) && unit |
| 5 | ? parseFloat( num ) + unit |
| 6 | : num; |
| 7 | } |
| 8 | |
| 9 | export default function shorthandCSS( top, right, bottom, left, unit ) { |
| 10 | if ( '' === top && '' === right && '' === bottom && '' === left ) { |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | top = ( top != 0 && '' !== top ) ? value( top, unit ) + ' ' : '0 '; // eslint-disable-line eqeqeq |
| 15 | right = ( right != 0 && '' !== right ) ? value( right, unit ) + ' ' : '0 '; // eslint-disable-line eqeqeq |
| 16 | bottom = ( bottom != 0 && '' !== bottom ) ? value( bottom, unit ) + ' ' : '0 '; // eslint-disable-line eqeqeq |
| 17 | left = ( left != 0 && '' !== left ) ? value( left, unit ) + ' ' : '0 '; // eslint-disable-line eqeqeq |
| 18 | |
| 19 | if ( right === left ) { |
| 20 | left = ''; |
| 21 | |
| 22 | if ( top === bottom ) { |
| 23 | bottom = ''; |
| 24 | |
| 25 | if ( top === right ) { |
| 26 | right = ''; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | const output = top + right + bottom + left; |
| 32 | |
| 33 | return output.trim(); |
| 34 | } |
| 35 |