|
33 | 33 | /// |
34 | 34 | /// @example - scss |
35 | 35 | /// @debug trim-prefix(' foo bar ', ' '); // "foo bar " |
36 | | -/// @debug trim-prefix('var(--foo)', 'var('); // "--foo)" |
37 | 36 | /// |
38 | 37 | /// @param {String} $string - The string to trim. |
39 | 38 | /// @param {String} $prefix - The repeating prefix string to trim. |
40 | 39 | /// @return {String} The string with the prefix trimmed from the start. |
41 | 40 | @function trim-prefix($string, $prefix) { |
42 | 41 | @while has-prefix($string, $prefix) { |
| 42 | + $string: trim-prefix-once($string, $prefix); |
| 43 | + } |
| 44 | + |
| 45 | + @return $string; |
| 46 | +} |
| 47 | + |
| 48 | +/// Trims a prefix from the start of a string. |
| 49 | +/// |
| 50 | +/// @example - scss |
| 51 | +/// @debug trim-prefix('var(--foo)', 'var('); // "--foo)" |
| 52 | +/// |
| 53 | +/// @param {String} $string - The string to trim. |
| 54 | +/// @param {String} $prefix - The prefix string to trim. |
| 55 | +/// @return {String} The string with the prefix trimmed from the start. |
| 56 | +@function trim-prefix-once($string, $prefix) { |
| 57 | + @if has-prefix($string, $prefix) { |
43 | 58 | $string: string.slice($string, string.length($prefix) + 1); |
44 | 59 | } |
45 | 60 |
|
|
57 | 72 | /// @return {String} The string with the suffix trimmed from the end. |
58 | 73 | @function trim-suffix($string, $suffix) { |
59 | 74 | @while has-suffix($string, $suffix) { |
| 75 | + $string: trim-suffix-once($string, $suffix); |
| 76 | + } |
| 77 | + |
| 78 | + @return $string; |
| 79 | +} |
| 80 | + |
| 81 | +/// Trims a suffix from the end of a string. |
| 82 | +/// |
| 83 | +/// @example - scss |
| 84 | +/// @debug trim-suffix('var(--foo)', ')'); // "var(--foo" |
| 85 | +/// |
| 86 | +/// @param {String} $string - The string to trim. |
| 87 | +/// @param {String} $suffix - The suffix string to trim. |
| 88 | +/// @return {String} The string with the suffix trimmed from the end. |
| 89 | +@function trim-suffix-once($string, $suffix) { |
| 90 | + @if has-suffix($string, $suffix) { |
60 | 91 | $string: string.slice($string, 1, -1 * string.length($suffix) - 1); |
61 | 92 | } |
62 | 93 |
|
|
69 | 100 | /// |
70 | 101 | /// @example - scss |
71 | 102 | /// @debug trim(' foo bar ', ' '); // "foo bar" |
72 | | -/// @debug trim('var(--foo)', 'var(', ')'); // "--foo" |
73 | 103 | /// |
74 | 104 | /// @param {String} $string - The string to trim. |
75 | 105 | /// @param {String} $prefix - The repeating prefix string to trim. |
|
78 | 108 | @function trim($string, $prefix, $suffix: $prefix) { |
79 | 109 | @return trim-prefix(trim-suffix($string, $suffix), $prefix); |
80 | 110 | } |
| 111 | + |
| 112 | +/// Trims a prefix and suffix from the start and end of a string. |
| 113 | +/// |
| 114 | +/// If a suffix is not provided, the prefix is used as the suffix to trim. |
| 115 | +/// |
| 116 | +/// @example - scss |
| 117 | +/// @debug trim('var(--foo)', 'var(', ')'); // "--foo" |
| 118 | +/// |
| 119 | +/// @param {String} $string - The string to trim. |
| 120 | +/// @param {String} $prefix - The prefix string to trim. |
| 121 | +/// @param {String} $suffix [$prefix] - The suffix string to trim. |
| 122 | +/// @return {String} The string with the prefix and suffix trimmed. |
| 123 | +@function trim-once($string, $prefix, $suffix: $prefix) { |
| 124 | + @return trim-prefix-once(trim-suffix-once($string, $suffix), $prefix); |
| 125 | +} |
0 commit comments