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

Add string.split() to the canonical spec #3502

Merged
merged 3 commits into from
Mar 3, 2023
Merged
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
67 changes: 67 additions & 0 deletions spec/built-in-modules/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This built-in module is available from the URL `sass:string`.
* [`length()`](#length)
* [`quote()`](#quote)
* [`slice()`](#slice)
* [`split()`](#split)
* [`to-lower-case()`](#to-lower-case)
* [`to-upper-case()`](#to-upper-case)
* [`unique-id()`](#unique-id)
Expand Down Expand Up @@ -58,6 +59,72 @@ slice($string, $start-at, $end-at: -1)

This function is also available as a global function named `str-slice()`.

### `split()`

```
split($string, $separator, $limit: null)
```

* If `$string` is not a string, throw an error.

* If `$separator` is not a string, throw an error.

* If `$limit` is a value other than an integer or `null`, throw an error.

* If `$limit` is less than 1, throw an error.

* If `$string` is an empty string, return a list with `$string` as the only
item.

* Let `split-list` be an empty list.

* If `$limit` is `null`, set `$limit` to the value of calling
`string.length($string)`.

* Let `split-counter` equal 0.

* While `split-counter <= $limit` and `string.length($string) > 0`:

* If `split-counter == $limit`:

* Append `$string` to `split-list`.

* Set `$string` to an empty string.

* Otherwise:

* If `$separator` is an empty string:

* Let `code-point` be the value of calling `string.slice($string, 1, 1)`.

* Append `code-point` to `split-list`.

* Set `$string` to `string.slice($string, 2)`.

* Increase `split-counter` by 1.

* Otherwise:

* Let `index` be the result of calling
`string.index($string, $separator)`.

* If `index` is `null`, append `$string` to `split-list` and set `$string`
to an empty string.

* Otherwise:

* Let `current-substring` be the result of calling
`string.slice($string, 1, index - 1)`.

* Append `current-substring` to `split-list`.

* Set `$string` to
`string.slice($string, index + string.length($separator))`.

* Increase `split-counter` by 1.

* Return `split-list` as a bracketed, comma-separated list.

### `to-lower-case()`

```
Expand Down