////////////////////////////////////////////////////////////////////////////////
/// Number                                                            #is-number
////////////////////////////////////////////////////////////////////////////////
///
/// Checks to see if value is a number. This includes units (except for calc)
///
/// @author Hugo Giraudel
///
/// @link https://css-tricks.com/snippets/sass/advanced-type-checking/
///

///
/// @group helpers-checks
///
/// @return {bool}

@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use './modules/to';

@function number($value, $check-strings:true, $allow-units:true) {

  @if $check-strings and meta.type-of($value) == 'string' {

    // Early check to dismiss this as a number if any spaces are found in the string.
    @if string.index($value, ' ') != null {
      $value : string.replace($value, ' ', '');
    }

    // If a '-' symbol exists but it's not the first character, return false
    @if string.index($value, '-') != null and string.slice($value, 1, 1) != '-' {
      @return false;
    }

    // Remove percentage symbol.
    @if ( meta.type-of($value) == 'number' and math.unit($value) == '%' ) {
      $value : string.replace($value, '%', '');
    }

    // Remove units from the value string. It had to be done this way, because
    // you can't strip units from a string.
    @if $allow-units and not math.is-unitless(to.number($value)) {
      $unit : null;

      @each $u in $units {
        @if string.slice($value, 1, string.length($u)) == $u {
          $unit : $u;
        }
      }
      $value : string.replace($value, $unit, '');
    }

    $exploaded-value : string.explode($value, '');
    $allowed-characters : "-" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9";

    // Return false if any of the chacters are not in the list of allowed numbers
    @each $character in $exploaded-value {
      @if (list.index($allowed-characters, $character) != null) == false {
        @return false;
      }
    }

    @return true;

  } @else {

    @return number($value);

  }
}
