SassString

Sass's string type.

Hierarchy

Constructors

  • Creates a new string.

    Parameters

    • text: string

      The contents of the string. For quoted strings, this is the semantic content—any escape sequences that were been written in the source text are resolved to their Unicode values. For unquoted strings, though, escape sequences are preserved as literal backslashes.

    • Optional options: {
          quotes?: boolean;
      }
      • Optional quotes?: boolean

        Whether the string is quoted. Defaults to true.

    Returns SassString

  • Creates an empty string.

    Parameters

    • Optional options: {
          quotes?: boolean;
      }
      • Optional quotes?: boolean

        Whether the string is quoted. Defaults to true.

    Returns SassString

Accessors

  • get hasBrackets(): boolean
  • Whether this value as a list has brackets.

    All SassScript values can be used as lists. Maps count as lists of pairs, and all other values count as single-value lists.

    Returns boolean

  • get isTruthy(): boolean
  • Whether the value counts as true in an @if statement and other contexts.

    Returns boolean

  • get sassLength(): number
  • Sass's notion of this string's length.

    Sass treats strings as a series of Unicode code points while JavaScript treats them as a series of UTF-16 code units. For example, the character U+1F60A SMILING FACE WITH SMILING EYES is a single Unicode code point but is represented in UTF-16 as two code units (0xD83D and 0xDE0A). So in JavaScript, "n😊b".length returns 4, whereas in Sass string.length("n😊b") returns 3.

    Returns number

  • get separator(): ListSeparator
  • The separator for this value as a list.

    All SassScript values can be used as lists. Maps count as lists of pairs, and all other values count as single-value lists.

    Returns ListSeparator

  • get text(): string
  • The contents of the string.

    For quoted strings, this is the semantic content—any escape sequences that were been written in the source text are resolved to their Unicode values. For unquoted strings, though, escape sequences are preserved as literal backslashes.

    This difference allows us to distinguish between identifiers with escapes, such as url\u28 http://example.com\u29, and unquoted strings that contain characters that aren't valid in identifiers, such as url(http://example.com). Unfortunately, it also means that we don't consider foo and f\6F\6F the same string.

    Returns string

Methods

  • Returns the value at index index in this value as a list, or undefined if index isn't valid for this list.

    All SassScript values can be used as lists. Maps count as lists of pairs, and all other values count as single-value lists.

    This is a shorthand for this.asList.get(index), although it may be more efficient in some cases.

    ⚠️ Heads up!

    This method uses the same indexing conventions as the immutable package: unlike Sass the index of the first element is 0, but like Sass negative numbers index from the end of the list.

    Parameters

    • index: number

    Returns undefined | Value

  • Converts sassIndex into a JavaScript-style index into the list returned by asList.

    Sass indexes are one-based, while JavaScript indexes are zero-based. Sass indexes may also be negative in order to index from the end of the list.

    Throws

    Error If sassIndex isn't a number, if that number isn't an integer, or if that integer isn't a valid index for asList.

    Parameters

    • sassIndex: Value

      The Sass-style index into this as a list.

    • Optional name: string

      The name of the function argument sassIndex came from (without the $) if it came from an argument. Used for error reporting.

    Returns number

  • Converts sassIndex to a JavaScript index into text.

    Sass indices are one-based, while JavaScript indices are zero-based. Sass indices may also be negative in order to index from the end of the string.

    In addition, Sass indices refer to Unicode code points while JavaScript string indices refer to UTF-16 code units. For example, the character U+1F60A SMILING FACE WITH SMILING EYES is a single Unicode code point but is represented in UTF-16 as two code units (0xD83D and 0xDE0A). So in JavaScript, "n😊b".charCodeAt(1) returns 0xD83D, whereas in Sass string.slice("n😊b", 1, 1) returns "😊".

    This function converts Sass's code point indices to JavaScript's code unit indices. This means it's O(n) in the length of text.

    Throws

    Error - If sassIndex isn't a number, if that number isn't an integer, or if that integer isn't a valid index for this string.

    Parameters

    • sassIndex: Value
    • Optional name: string

    Returns number