Skip to content

Instantly share code, notes, and snippets.

@alademann
Created April 25, 2016 15:24
Show Gist options
  • Select an option

  • Save alademann/18e0955e2d7a213c85397ad066d2ef0a to your computer and use it in GitHub Desktop.

Select an option

Save alademann/18e0955e2d7a213c85397ad066d2ef0a to your computer and use it in GitHub Desktop.
An exponent / power function for sassy css
///
/// Raises `$number` to the power of `$exponent`.
/// ---
/// @since 1.5.0
/// ---
/// @param {Number} $number
/// @param {Number} $exponent
/// ---
/// @returns {Number} - `$number` raised to the power of `$exponent`
///
@function pow($number, $exponent) {
// ----- BEGIN ERROR CHECKING ----- //
@if not type-of($number) == 'number' {
@error "pow() function expecting `$number` param value to be a number. Was #{$number} (#{type-of($number)}).";
}
@if not type-of($exponent) == 'number' {
@error "pow() function expecting `$exponent` param value to be a number. Was #{$number} (#{type-of($number)}).";
}
// ----- END ERROR CHECKING ----- //
@if $exponent == 0 {
@return 1;
}
// Initialize the value that we will return
$value: $number;
// If the provided number is negative, the return value should also be negative
$preserve-negative: if($value < 0, -1, 1);
// Multiply when `$exponent` is positive
@if $exponent > 1 {
@for $i from 2 through $exponent {
$value: $value * $number;
}
$value: abs($value) * $preserve-negative;
}
// Divide when `$exponent` is negative
@if $exponent < 1 {
@for $i from 0 through -$exponent {
$value: $value / $number;
}
$value: abs($value) * $preserve-negative;
}
@return $value;
}
// Tests for pow() (using bootcamp)
@include describe('[pow]') {
@include describe('when [number] is a positive number') {
@include describe('and [exponent] is a positive number greater than one') {
@include it('should return the correct value') {
$actual: pow(10, 2);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( 100 )));
}
}
@include describe('and [exponent] is one') {
@include it('should return the correct value') {
$actual: pow(10, 1);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( 10 )));
}
}
@include describe('and [exponent] is zero') {
@include it('should return the correct value') {
$actual: pow(10, 0);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( 1 )));
}
}
@include describe('and [exponent] is negative one') {
@include it('should return the correct value') {
$actual: pow(10, -1);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( .1 )));
}
}
@include describe('and [exponent] is a negative number greater than one') {
@include it('should return the correct value') {
$actual: pow(10, -2);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( .01 )));
}
}
}
@include describe('when [number] is a negative number') {
@include describe('and [exponent] is a positive number greater than one') {
@include it('should return the correct value') {
$actual: pow(-10, 2);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( -100 )));
}
}
@include describe('and [exponent] is one') {
@include it('should return the correct value') {
$actual: pow(-10, 1);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( -10 )));
}
}
@include describe('and [exponent] is zero') {
@include it('should return the correct value') {
$actual: pow(-10, 0);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( 1 )));
}
}
@include describe('and [exponent] is negative one') {
@include it('should return the correct value') {
$actual: pow(-10, -1);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( -.1 )));
}
}
@include describe('and [exponent] is a negative number greater than one') {
@include it('should return the correct value') {
$actual: pow(-10, -2);
@include should(expect( $actual ), have-type-of('number'));
@include should(expect( $actual ), to(be( -.01 )));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment