@charset 'UTF-8';

////
/// Flavor SCSS Types Number
/// @group types-number
/// @author blackmirror1980
////

/// Checks if something is a number
///
/// @access public
/// @param {any} $n - the value to check
/// @return {boolean} - true if is a number
@function is-number($n) {
  @return is-defined($n) and type-of($n)==number;
}

/// Checks if something is an integer number
///
/// @access public
/// @param {any} $i - the value to check
/// @return {boolean} - true if is an integer number
@function is-integer($i) {
  @return is-number($i) and round($i)==$i;
}

/// Checks if something is a floating number
///
/// @access public
/// @param {any} $f - the value to check
/// @return {boolean} - true if is a float number
@function is-float($f) {
  @return is-number($f) and floor($f)!=$f;
}

/// Removes the unit of something
///
/// @access public
/// @param {number} $n - the something ;-)
/// @return {number} - the something unitstripped ;-)
@function unitstrip($n) {
  @if is-number($n) and not unitless($n) {
    @return $n / ($n * 0 + 1);
  }

  @return $n;
}

/// Checks if a number is between two numbers, with an include bounds options defaulted to true
///
/// @access public
/// @param {number} $n - the value to check
/// @param {number} $f - the from value
/// @param {number} $t - the to value
/// @param {boolean} $include-bounds [true] - specifies to execute either >= & =< or > & <
/// @return {boolean} - true if the number is inside the bounds
@function is-number-between($n, $from, $to, $include-bounds: true) {
  @if (is-number($n) and is-number($from) and is-number($to)) {
    @return if($include-bounds, ($n >=$from) and ($n <=$to), ($n > $from) and ($n < $to));
  }

  @warn '`#{$n}, #{$from} and #{$to}` are not valid numbers';
  @return false;
}

/// Checks if something is a percentage number
///
/// @access public
/// @param {any} $p - the value to check
/// @return {boolean} - true if is percentage number
@function is-percentage($p) {
  @return is-number($p) and unit($p)=='%';
}

/// Checks if something is a percentage floating number (between 0 and 1)
///
/// @access public
/// @param {any} $pn - the value to check
/// @return {boolean} - true if is percentage floating number
@function is-percentage-number($pn) {
  @return is-number($pn) and is-number-between(unitstrip($pn), 0, 1);
}

///
/// Casts a string into a number
///
/// @param {String | Number} $value - Value to be parsed
///
/// @return {Number}
///
@function to-number($value) {
  @if type-of($value) == 'number' {
    @return $value;
  } @else if type-of($value) != 'string' {
    $_: log('Value for `to-number` should be a number or a string.');
  }

  $result: 0;
  $digits: 0;
  $minus: str-slice($value, 1, 1) == '-';
  $numbers: ('0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9);

  @for $i from if($minus, 2, 1) through str-length($value) {
    $character: str-slice($value, $i, $i);

    @if not (index(map-keys($numbers), $character) or $character == '.') {
      @return to-length(if($minus, -$result, $result), str-slice($value, $i));
    }

    @if $character == '.' {
      $digits: 1;
    } @else if $digits == 0 {
      $result: $result * 10 + map-get($numbers, $character);
    } @else {
      $digits: $digits * 10;
      $result: $result + map-get($numbers, $character) / $digits;
    }
  }

  @return if($minus, -$result, $result);;
}


///
/// Add `$unit` to `$value`
///
/// @param {Number} $value - Value to add unit to
/// @param {String} $unit - String representation of the unit
///
/// @return {Number} - `$value` expressed in `$unit`
///
@function to-length($value, $unit) {
  $units: ('px': 1px, 'cm': 1cm, 'mm': 1mm, '%': 1%, 'ch': 1ch, 'pc': 1pc, 'in': 1in, 'em': 1em, 'rem': 1rem, 'pt': 1pt, 'ex': 1ex, 'vw': 1vw, 'vh': 1vh, 'vmin': 1vmin, 'vmax': 1vmax);

  @if not index(map-keys($units), $unit) {
    $_: log('Invalid unit `#{$unit}`.');
  }

  @return $value * map-get($units, $unit);
}
