Skip to content

Commit

Permalink
Implement string.split() (#1839)
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 authored Dec 16, 2022
1 parent 236b83f commit 100f76f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 100f76f

Please sign in to comment.