Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string.split missing but present in dart-sass 1.57.0 #95

Closed
xpe opened this issue May 4, 2024 · 2 comments
Closed

string.split missing but present in dart-sass 1.57.0 #95

xpe opened this issue May 4, 2024 · 2 comments

Comments

@xpe
Copy link
Contributor

xpe commented May 4, 2024

grass doesn't yet support string.split, which has been available in dart-sass since 1.57.0.

https://sass-lang.com/documentation/modules/string/#split

string.split($string, $separator, $limit: null) //=> list

Compatibility:

  • Dart Sass since 1.57.0
  • LibSass: ✗
  • Ruby Sass: ✗

Returns a bracketed, comma-separated list of substrings of $string that are separated by $separator. The $separators aren’t included in these substrings.

If $limit is a number 1 or higher, this splits on at most that many $separators (and so returns at most $limit + 1 strings). The last substring contains the rest of the string, including any remaining $separators.

@xpe xpe changed the title string.split string.split missing but present in dart-sass 1.57.0 May 4, 2024
@xpe
Copy link
Contributor Author

xpe commented May 5, 2024

Here is the current source code for split from dart-sass

/// The Sass string module.
final module = BuiltInModule("string", functions: <Callable>[
  _unquote, _quote, _toUpperCase, _toLowerCase, _length, _insert, _index, //
  _slice, _uniqueId,

  _function("split", r"$string, $separator, $limit: null", (arguments) {
    var string = arguments[0].assertString("string");
    var separator = arguments[1].assertString("separator");
    var limit = arguments[2].realNull?.assertNumber("limit").assertInt("limit");

    if (limit != null && limit < 1) {
      throw SassScriptException("\$limit: Must be 1 or greater, was $limit.");
    }

    if (string.text.isEmpty) {
      return const SassList.empty(
          separator: ListSeparator.comma, brackets: true);
    } else if (separator.text.isEmpty) {
      return SassList(
          string.text.runes.map((rune) =>
              SassString(String.fromCharCode(rune), quotes: string.hasQuotes)),
          ListSeparator.comma,
          brackets: true);
    }

    var i = 0;
    var lastEnd = 0;
    var chunks = <String>[];
    for (var match in separator.text.allMatches(string.text)) {
      chunks.add(string.text.substring(lastEnd, match.start));
      lastEnd = match.end;

      i++;
      if (i == limit) break;
    }
    chunks.add(string.text.substring(lastEnd));

    return SassList(
        chunks.map((chunk) => SassString(chunk, quotes: string.hasQuotes)),
        ListSeparator.comma,
        brackets: true);
  }),
]);

xpe pushed a commit to xpe/grass that referenced this issue May 5, 2024
connorskees pushed a commit that referenced this issue May 11, 2024
@connorskees
Copy link
Owner

closed by #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants