3.2 KiB
string.split()
: Draft 1.2
This proposal adds string.split()
to the sass:string
module.
Table of Contents
Background
This section is non-normative.
The sass:string
module contains several functions for manipulating and finding
out information about strings. Currently, though, there is no built-in function
that splits one string into a list of substrings, and authors have been creating
their own versions of functions that achieve this functionality.
Summary
This section is non-normative.
This proposal adds the string.split()
function to the sass:string
module.
The function takes a string, splits it based on a provided separator, and
returns a bracketed, comma-separated list of substrings.
This could be used to take a string and repurpose parts of it for some other use. For example, fonts contained in a font stack list could be split into segments and then used as keys in a new map.
Examples:
$fonts: "Helvetica Neue, Helvetica, Arial";
string.split($fonts, ', '); // ["Helvetica Neue", "Helvetica", "Arial"]
A third argument can set a limit to the the number of splits performed on the string:
string.split($fonts, ', ', 1); // ["Helvetica Neue", "Helvetica, Arial"]
An empty $separator
returns all Unicode code points in the original string:
$font: "Helvetica"
string.split($font, ''); // ["H", "e", "l", "v", "e", "t", "i", "c", "a"]
Semantics
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 ornull
, 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
isnull
, set$limit
to the value of callingstring.length($string)
. -
Let
split-counter
equal 0. -
While
split-counter <= $limit
andstring.length($string) > 0
:-
If
split-counter == $limit
:-
Append
$string
tosplit-list
. -
Set
$string
to an empty string.
-
-
Otherwise:
-
If
$separator
is an empty string:-
Let
code-point
be the value of callingstring.slice($string, 1, 1)
. -
Append
code-point
tosplit-list
. -
Set
$string
tostring.slice($string, 2)
. -
Increase
split-counter
by 1.
-
-
Otherwise:
-
Let
index
be the result of callingstring.index($string, $separator)
. -
If
index
isnull
, append$string
tosplit-list
and set$string
to an empty string. -
Otherwise:
-
Let
current-substring
be the result of callingstring.slice($string, 1, index - 1)
. -
Append
current-substring
tosplit-list
. -
Set
$string
tostring.slice($string, index + string.length($separator))
. -
Increase
split-counter
by 1.
-
-
-
-
-
Return
split-list
as a bracketed, comma-separated list.