Skip to content

Instantly share code, notes, and snippets.

@TexRx
Created January 30, 2015 22:41
Show Gist options
  • Save TexRx/9cd0dd87099a7c11ee23 to your computer and use it in GitHub Desktop.
Save TexRx/9cd0dd87099a7c11ee23 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.9)
// Compass (v1.0.1)
// ----
/// Trim `$string`
/// @param {String} $string - String to trim
/// @return {String}
@function trim($string) {
@return str-slice(
$string,
first-index($string, 'left'),
first-index($string, 'right')
);
}
/// Find first char which is not a space
/// @param {String} $string - String
/// @param {String} $direction ['left'] - Either `left` or `right`
/// @return {Number}
@function first-index($string, $direction: 'left') {
@for $i from 1 through str-length($string) {
$index: if('left' == $direction, $i, -$i);
@if ' ' != str-slice($string, $index, $index) {
@return $index;
}
}
@return 0;
}
.foo {
content: trim(' ');
content: trim('no spaces');
content: trim(' 3 spaces in front');
content: trim(' 3 spaces in front and back ');
content: trim(' lots and lots of spaces');
}
.foo {
content: "";
content: "no spaces";
content: "3 spaces in front";
content: "3 spaces in front and back";
content: "lots and lots of spaces";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment