Sha256: 8fbdcc20ff446ab5fb6d9fbe5f1a63bdd66b075042c12c76b957b12d54a6c26f

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

//
// Replace `$search` with `$replace` in `$string`
// @author Hugo Giraudel
//
// @param {String} $string - Initial string
// @param {String} $search - Substring to replace
// @param {String} $replace ('') - New value
//
// @return {String} - Updated string
//
@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}

//
// String to number converter
// @author Hugo Giraudel
// @access private
//
// @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);
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mcgriddle-0.5.0 _sass/mcgriddle/functions/_helpers.scss
mcgriddle-0.4.2 _sass/mcgriddle/functions/_helpers.scss
mcgriddle-0.4.1 _sass/mcgriddle/functions/_helpers.scss
mcgriddle-0.4.0 _sass/mcgriddle/functions/_helpers.scss
mcgriddle-0.3.1 _sass/mcgriddle/functions/_helpers.scss