This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
_string-to-list.scss
executable file
·93 lines (78 loc) · 2.63 KB
/
_string-to-list.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Turns string into a flat list
*
* @access private
*
* @param {String} $string - string to perform on
* @param {String} $find (" ") - item to find which separates substrings
* @param {String} $ignore (",") - removes remaining string beyond item
*
* @return {List | False}
*/
@function flint-string-to-list($string, $find: " ", $ignore: ",") {
@if flint-is-string($string) {
// Use Ruby function if available
@if $flint__use-ruby-functions {
@return flint_ruby_string_to_list($string, $find, $ignore);
} @else {
$string-list: ();
$space-indexes: ();
$ignore-remainder: ();
$length: str-length($string);
// Find ignore separator, and flint-remove remainder of string beyond that point
@for $i from 1 through $length {
$slice: str-slice($string, $i, $i);
@if $slice == $ignore {
$ignore-remainder: append($ignore-remainder, $i - 1, "comma");
}
}
// Redefine string and length
@if length($ignore-remainder) >= 1 {
$string: str-slice($string, 1, nth($ignore-remainder, 1));
$length: str-length($string);
}
// Find all spaces and their indices by looking over each character in string
@for $i from 1 through $length {
$slice: str-slice($string, $i, $i);
@if $slice == $find {
$space-indexes: append($space-indexes, $i, "comma");
}
}
// Check if there are any spaces
@if length($space-indexes) >= 1 {
// Keep a count of number of spaces
$count: 1;
// Loop through each space
@each $space in $space-indexes {
// If is initial count, grab first substring and store in list
@if $count == 1 {
$matched-string: str-slice($string, 0, ($space - 1));
@if $matched-string != "" {
$string-list: append($string-list, $matched-string, "comma");
}
// Else, add a little math to make up for the spaces to do the same
} @else {
$matched-string: str-slice($string, (nth($space-indexes, ($count - 1)) + 1), ($space - 1));
@if $matched-string != "" {
$string-list: append($string-list, $matched-string, "comma");
}
}
// Increase count
$count: $count + 1;
}
// Now grab that last selector
$flint-last-space: nth($space-indexes, length($space-indexes));
$matched-string: str-slice($string, ($flint-last-space + 1), $length);
$string-list: append($string-list, $matched-string, "comma");
// Finally, return comma separated list of selectors
@return $string-list;
} @else {
// Else, just return the string as a single item list
$string-list: append($string-list, $string);
@return $string-list;
}
}
} @else {
@return false;
}
}