|
| 1 | +// Sass. |
| 2 | +@use 'sass:list'; |
| 3 | +@use 'sass:meta'; |
| 4 | +@use 'sass:string'; |
| 5 | + |
| 6 | +// Completed |
| 7 | +// The `string.index()` function returns index of required `$substring` and additional indexes of `$substrings`. |
| 8 | +// @param `$string` String to check whether it contains required `$substring` and additional `$substrings`. |
| 9 | +// @param `$substring` A substring to find in `$string`. |
| 10 | +// @arbitrary `$substrings...` Additional substrings to find in `$string`. |
| 11 | +// @returns The returned value is index of `$substring` and additional indexes of `$substrings`. |
| 12 | +@function index($string, $substring, $substrings...) { |
| 13 | + @if not (meta.type-of($string) == string) { |
| 14 | + @error "$string: #{$string} is not string"; |
| 15 | + } |
| 16 | + $result: (); |
| 17 | + @each $substring in list.join($substring, $substrings, comma) { |
| 18 | + $result: list.append( |
| 19 | + $result, |
| 20 | + meta.type-of($substring) == string |
| 21 | + and string.index($string, $substring) |
| 22 | + or null, |
| 23 | + comma |
| 24 | + ); |
| 25 | + } |
| 26 | + @if list.length($result) > 0 { |
| 27 | + @return list.length($result) > 1 |
| 28 | + and $result |
| 29 | + or list.nth($result, 1); |
| 30 | + } |
| 31 | + @return null; |
| 32 | +} |
| 33 | + |
| 34 | +// Examples. |
| 35 | +// Single |
| 36 | +// @debug index("Helvetica Neue", "Helvetica"); // 1 |
| 37 | +// @debug index("Helvetica Neue", "Neue"); // 11 |
| 38 | + |
| 39 | +// Multiple |
| 40 | +// @debug index("Helvetica Neue", "Helvetica", "Neue"); // 1, 11 |
| 41 | +// @debug index("Helvetica Neue", "Helvetica", "Neue", "Wrong"); // 1, 11, null |
| 42 | +// @debug index("Helvetica Neue", "Helvetica", "Neue", "Wrong", true); // 1, 11, null, null |
| 43 | + |
| 44 | +// null |
| 45 | +// @debug index("Helvetica Neue", "d"); // null |
| 46 | + |
| 47 | +// Different type of the `$string` |
| 48 | +// @debug index(true, "Helvetica"); // error |
0 commit comments