Search code examples
regexunicodesassstring-matching

Test for unicode character in SASS


How can I test if a character is unicode in a SASS mixin?

I have a mixin that I'd like to accept either (1) a unicode character, or (2) a descriptor string as a parameter. If the the parameter is a unicode character then I'd like to just display it. If the parameter is a descriptor string I would look up the unicode character to go with it.

For example:

@include glyph('\f002');
@include glyph('search');

The mixin would be something akin to:

@mixin glyph($glyph) {
  @if type-of($glyph) == unicode {
    content: '$glyph';
  } @ else {
    content: get_glyph_for($glyph);
  }
}

type-of($glyph) returns string, as there is no "unicode" type.

I've not found regex capabilities in SASS, or a way to pull apart a single unicode character.

Is there a way I can run a regex with SASS, or pull the character apart?


Solution

  • You could use str-slice to detect if the first character is a "\". In order to transform the unicode character to a string, first you have to run it inside "inspect".

    str-slice(inspect(\f002),0,1)   => "\\"
    

    http://sass-lang.com/documentation/Sass/Script/Functions.html#str_slice-instance_method