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

Implement string.split() #1839

Merged
merged 2 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
@@ -1,4 +1,7 @@
## 1.56.3
## 1.57.0

* Add a `split($string, $separator, $limit: null)` function to `sass:string`
that splits a string into separate substrings based on a separator string.

### JavaScript API

Expand Down
39 changes: 39 additions & 0 deletions lib/src/functions/string.dart
Expand Up @@ -8,6 +8,7 @@ import 'dart:math' as math;
import 'package:collection/collection.dart';

import '../callable.dart';
import '../exception.dart';
import '../module/built_in.dart';
import '../util/character.dart';
import '../utils.dart';
Expand All @@ -32,6 +33,44 @@ final global = UnmodifiableListView([
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);
}),
]);

final _unquote = _function("unquote", r"$string", (arguments) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sass_api/CHANGELOG.md
@@ -1,3 +1,7 @@
## 4.2.0

* No user-visible changes.

## 4.1.2

* No user-visible changes.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Expand Up @@ -2,15 +2,15 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 4.1.2
version: 4.2.0
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: ">=2.17.0 <3.0.0"

dependencies:
sass: 1.56.2
sass: 1.57.0

dev_dependencies:
dartdoc: ^5.0.0
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: sass
version: 1.56.3-dev
version: 1.57.0
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down