Skip to content

Commit

Permalink
Merge pull request #22 from ianrose/gh21-floating-point-warning
Browse files Browse the repository at this point in the history
Adds floating point warn
  • Loading branch information
ianrose committed Nov 9, 2014
2 parents 832689d + d87ac8a commit b59bcaf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
14 changes: 14 additions & 0 deletions _typesettings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ $ms-up2: $ms-up1 * $ms-ratio; // [5]
$ms-up3: $ms-up2 * $ms-ratio; // [6]
$ms-up4: $ms-up3 * $ms-ratio; // [7]

// Warning text for when an integer is not used in rhythm functions
$not-integer-txt: ' value breaks vertical rhythm. Use an interger (whole number) for $lines.';

//==============================================================================
// Helper function(s)
//==============================================================================

// Check if value is an integer and returns true or false
@function is-int($number) {
@return type-of($number) == number and floor(abs($number)) == abs($number);
}

// Returns a number without a unit. For example 16px becomes 16
@function stripUnits($number) {
@return $number / ($number * 0 + 1);
Expand All @@ -98,12 +106,18 @@ $ms-up4: $ms-up3 * $ms-ratio; // [7]
// Returns an EM value that is a multiple of our defined base vertical unit. For
// example 3 becomes 1.125em
@function emRhythm($lines: $base-vertical-unit, $font-size: $base-em-font-size) {
@if is-int($lines) == false {
@warn #{$lines} + $not-integer-txt;
}
@return (stripUnits($lines) * stripUnits($base-vertical-unit)) / (stripUnits($font-size) * stripUnits($base-font-size)) + 0em;
}

// Returns a unitless number that is a multiple of our defined base vertical unit.
// For example 3 becomes 1.125
@function unitlessRhythm($lines: $base-line-multi, $font-size: $base-em-font-size) {
@if is-int($lines) == false {
@warn #{$lines} + $not-integer-txt;
}
@return ($lines * stripUnits($base-vertical-unit)) / (stripUnits($font-size) * stripUnits($base-font-size));
}

Expand Down
3 changes: 3 additions & 0 deletions test/test.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ hr {
border-top-style: solid;
border-top-width: 1px;
padding-top: 1.4375em; }

.div {
margin-bottom: 0.45em; }
6 changes: 5 additions & 1 deletion test/test.scss
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
@import "../typesettings";
@import "../typesettings";

.div {
margin-bottom: emRhythm(1.2);
}

0 comments on commit b59bcaf

Please sign in to comment.