sassIndexToListIndex method Null safety

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 index = sassIndex.assertNumber(name).assertInt(name);
  if (index == 0) throw _exception("List index may not be 0.", name);
  if (index.abs() > lengthAsList) {
    throw _exception(
        "Invalid index $sassIndex for a list with $lengthAsList elements.",
        name);
  }

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