Skip to content

Instantly share code, notes, and snippets.

@brettsmason
Created December 16, 2022 11:04
Show Gist options
  • Save brettsmason/da20ece00644d8099013d6caedb3b892 to your computer and use it in GitHub Desktop.
Save brettsmason/da20ece00644d8099013d6caedb3b892 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
/// Check if a string contains a number.
@function string-is-number($string) {
// Matrices
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
@for $i from 1 through str-length($string) {
$character: str-slice($string, $i, $i);
$index: index($numbers, $character);
@if $index {
@return true;
}
}
@return false;
}
/// Checks a string for the presence of a number.
/// If it contains a number, append a separator to it.
@function split-string($string, $separator) {
@for $i from 1 through str-length($string) {
$character: str-slice($string, $i, $i);
$next-character: str-slice($string, $i + 1, $i + 1);
$previous-character: if($i == 1, false, str-slice($string, $i - 1, $i - 1));
@if string-is-number($character) and not string-is-number($next-character) {
$string: str-insert($string, $separator, $i + 1);
}
}
@return $string;
}
/// Testing
body {
margin-top: split-string('20xl', '-');
}
body {
margin-top: "20-xl";
}
{
"sass": {
"compiler": "dart-sass/1.32.12",
"extensions": {},
"syntax": "SCSS",
"outputStyle": "expanded"
},
"autoprefixer": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment