// =============================================================================
// =ALEKSI - STR-TRIM
// =============================================================================
////
//// @group aleksi-strings
//// @author [Yoannis Jamar](https://yoannis.com)
//// @author [Hugo Giraudel](https://hugogiraudel.com)
//// @link https://github.com/HugoGiraudel/SassyStrings/tree/master/stylesheets/public

@import "aleksi/general/throw";
@import "aleksi/lists/is-list";

// =str-ltrim( $str[, $chars ])
// -----------------------------------------------------------------------------
/// Removes all occurences of given character from the beginning of a string.
/// If a list of characters is passed as second argument, all of those
/// characters will be removed.
///
/// @param {string} $str - The string to transform
/// @param {string|list} $chars [' '] - The characters to remove
///
/// @return {list} - Transformed string
///
/// @access public
/// @since 0.3.7

@function str-ltrim( $str, $chars: ' ')
{
  @if type-of($str) != 'string' {
    @return throw-error('str-ltrim(): `$str` must be a string – was #{inspect($str)}');
  }

  // allow empty string
  @if $str == '' {
    @return $str;
  }

  // transform single character into a list
  @if type-of($chars) == 'string' {
    $chars: append((), $chars);
  }

  @else if not is-list($chars) {
    @return throw-error('str-ltrim(): `$chars` must be a string or a list – was #{inspect($str)}');
  }

  // remove leading chars
  @while index($chars, str-slice($str, 1, 1)) {
    $str: str-slice($str, 2);
  }

  @return $str;
}

// =str-rtrim( $str[, $chars ])
// -----------------------------------------------------------------------------
/// Removes all occurences of given character from the ending of a string. If a
/// list of characters is passed as second argument, all of those characters
/// will be removed.
///
/// @param {string} $str - The string to transform
/// @param {string|list} $chars [' '] - The characters to remove
///
/// @return {list} - Transformed string
///
/// @access public
/// @since 0.3.7

@function str-rtrim( $str, $chars: ' ')
{
  @if type-of($str) != 'string' {
    @return throw-error('str-rtrim(): `$str` must be a string – was #{inspect($str)}');
  }

  // allow empty string
  @if $str == '' {
    @return $str;
  }

  // transform single character into a list
  @if type-of($chars) == 'string' {
    $chars: append((), $chars);
  }

  @else if not is-list($chars) {
    @return throw-error('str-rtrim(): `$chars` must be a string or a list – was #{inspect($str)}');
  }

  // remove trailing chars
  @while index($chars, str-slice($str, -1)) {
    $str: str-slice($str, 1, -2);
  }

  @return $str;
}

// =str-trim( $str[, $chars ])
// -----------------------------------------------------------------------------
/// Removes all occurences of given character from both ends of a string. If a
/// list of characters is passed as second argument, all of those characters
/// will be removed.
///
/// @param {string} $str - The string to transform
/// @param {string|list} $chars [' '] - The characters to remove
///
/// @return {list} - A list containing the separated parts of `$str`.
///
/// @access public
/// @since 0.3.7

@function str-trim( $str, $chars: ' ')
{
  @if type-of($str) != 'string' {
    @return throw-error('str-trim(): `$str` must be a string – was #{inspect($str)}');
  }

  // allow empty string
  @if $str == '' {
    @return $str;
  }

  // transform single character into a list
  @if type-of($chars) == 'string' {
    $chars: append((), $chars);
  }

  @else if not is-list($chars) {
    @return throw-error('str-trim(): `$chars` must be a string or a list – was #{inspect($str)}');
  }

  // remove leading and trailing occurences of chars and return result
  @return str-rtrim(str-ltrim($str, $chars), $chars);
}
