sassIndexToListIndex method

int sassIndexToListIndex(
  1. Value sassIndex, [
  2. String? name
])

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

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

Throws a SassScriptException if sassIndex isn't a number, if that number isn't an integer, or if that integer isn't a valid index for asList. If sassIndex came from a function argument, name is the argument name (without the $). It's used for error reporting.

Implementation

int sassIndexToListIndex(Value sassIndex, [String? name]) {
  var indexValue = sassIndex.assertNumber(name);
  if (indexValue.hasUnits) {
    warnForDeprecation(
        "\$$name: Passing a number with unit ${indexValue.unitString} is "
        "deprecated.\n"
        "\n"
        "To preserve current behavior: "
        "${indexValue.unitSuggestion(name ?? 'index')}\n"
        "\n"
        "More info: https://sass-lang.com/d/function-units",
        Deprecation.functionUnits);
  }

  var index = indexValue.assertInt(name);
  if (index == 0) throw SassScriptException("List index may not be 0.", name);
  if (index.abs() > lengthAsList) {
    throw SassScriptException(
        "Invalid index $sassIndex for a list with $lengthAsList elements.",
        name);
  }

  return index < 0 ? lengthAsList + index : index - 1;
}