Skip to content

Commit 100f76f

Browse files
authored
Implement string.split() (#1839)
Closes #1821 See sass/sass#1950
1 parent 236b83f commit 100f76f

5 files changed

Lines changed: 50 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
## 1.56.3
1+
## 1.57.0
2+
3+
* Add a `split($string, $separator, $limit: null)` function to `sass:string`
4+
that splits a string into separate substrings based on a separator string.
25

36
### JavaScript API
47

lib/src/functions/string.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:math' as math;
88
import 'package:collection/collection.dart';
99

1010
import '../callable.dart';
11+
import '../exception.dart';
1112
import '../module/built_in.dart';
1213
import '../util/character.dart';
1314
import '../utils.dart';
@@ -32,6 +33,44 @@ final global = UnmodifiableListView([
3233
final module = BuiltInModule("string", functions: <Callable>[
3334
_unquote, _quote, _toUpperCase, _toLowerCase, _length, _insert, _index, //
3435
_slice, _uniqueId,
36+
37+
_function("split", r"$string, $separator, $limit: null", (arguments) {
38+
var string = arguments[0].assertString("string");
39+
var separator = arguments[1].assertString("separator");
40+
var limit = arguments[2].realNull?.assertNumber("limit").assertInt("limit");
41+
42+
if (limit != null && limit < 1) {
43+
throw SassScriptException("\$limit: Must be 1 or greater, was $limit.");
44+
}
45+
46+
if (string.text.isEmpty) {
47+
return const SassList.empty(
48+
separator: ListSeparator.comma, brackets: true);
49+
} else if (separator.text.isEmpty) {
50+
return SassList(
51+
string.text.runes.map((rune) =>
52+
SassString(String.fromCharCode(rune), quotes: string.hasQuotes)),
53+
ListSeparator.comma,
54+
brackets: true);
55+
}
56+
57+
var i = 0;
58+
var lastEnd = 0;
59+
var chunks = <String>[];
60+
for (var match in separator.text.allMatches(string.text)) {
61+
chunks.add(string.text.substring(lastEnd, match.start));
62+
lastEnd = match.end;
63+
64+
i++;
65+
if (i == limit) break;
66+
}
67+
chunks.add(string.text.substring(lastEnd));
68+
69+
return SassList(
70+
chunks.map((chunk) => SassString(chunk, quotes: string.hasQuotes)),
71+
ListSeparator.comma,
72+
brackets: true);
73+
}),
3574
]);
3675

3776
final _unquote = _function("unquote", r"$string", (arguments) {

pkg/sass_api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.0
2+
3+
* No user-visible changes.
4+
15
## 4.1.2
26

37
* No user-visible changes.

pkg/sass_api/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ name: sass_api
22
# Note: Every time we add a new Sass AST node, we need to bump the *major*
33
# version because it's a breaking change for anyone who's implementing the
44
# visitor interface(s).
5-
version: 4.1.2
5+
version: 4.2.0
66
description: Additional APIs for Dart Sass.
77
homepage: https://github.com/sass/dart-sass
88

99
environment:
1010
sdk: ">=2.17.0 <3.0.0"
1111

1212
dependencies:
13-
sass: 1.56.2
13+
sass: 1.57.0
1414

1515
dev_dependencies:
1616
dartdoc: ^5.0.0

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.56.3-dev
2+
version: 1.57.0
33
description: A Sass implementation in Dart.
44
homepage: https://github.com/sass/dart-sass
55

0 commit comments

Comments
 (0)