-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
_string-ext.scss
128 lines (115 loc) · 4.15 KB
/
_string-ext.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright 2021 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
@use 'sass:string';
/// Checks if a string starts with a given prefix.
///
/// @example - scss
/// @debug has-prefix('var(--foo)', 'var('); // true
///
/// @param {String} $string - The string to test.
/// @param {String} $prefix - The prefix to check.
/// @return {Boolean} True if the string starts with the given prefix.
@function has-prefix($string, $prefix) {
@return string.slice($string, 1, string.length($prefix)) == $prefix;
}
/// Checks if a string ends with a given suffix.
///
/// @example - scss
/// @debug has-suffix('var(--foo)', ')'); // true
///
/// @param {String} $string - The string to test.
/// @param {String} $suffix - The suffix to check.
/// @return {Boolean} True if the string ends with the given suffix.
@function has-suffix($string, $suffix) {
@return string.slice($string, -1 * string.length($suffix)) == $suffix;
}
/// Trims a repeating prefix from the start of a string.
///
/// @example - scss
/// @debug trim-repeating-prefix(' foo bar ', ' '); // "foo bar "
///
/// @param {String} $string - The string to trim.
/// @param {String} $prefix - The repeating prefix string to trim.
/// @return {String} The string with the prefix trimmed from the start.
@function trim-repeating-prefix($string, $prefix) {
@while has-prefix($string, $prefix) {
$string: trim-prefix($string, $prefix);
}
@return $string;
}
/// Trims a prefix from the start of a string.
///
/// @example - scss
/// @debug trim-prefix('var(--foo)', 'var('); // "--foo)"
///
/// @param {String} $string - The string to trim.
/// @param {String} $prefix - The prefix string to trim.
/// @return {String} The string with the prefix trimmed from the start.
@function trim-prefix($string, $prefix) {
@if has-prefix($string, $prefix) {
$string: string.slice($string, string.length($prefix) + 1);
}
@return $string;
}
/// Trims a repeating suffix from the end of a string.
///
/// @example - scss
/// @debug trim-repeating-suffix(' foo bar ', ' '); // " foo bar"
/// @debug trim-repeating-suffix('var(--foo)', ')'); // "var(--foo"
///
/// @param {String} $string - The string to trim.
/// @param {String} $suffix - The repeating suffix string to trim.
/// @return {String} The string with the suffix trimmed from the end.
@function trim-repeating-suffix($string, $suffix) {
@while has-suffix($string, $suffix) {
$string: trim-suffix($string, $suffix);
}
@return $string;
}
/// Trims a suffix from the end of a string.
///
/// @example - scss
/// @debug trim-suffix('var(--foo)', ')'); // "var(--foo"
///
/// @param {String} $string - The string to trim.
/// @param {String} $suffix - The suffix string to trim.
/// @return {String} The string with the suffix trimmed from the end.
@function trim-suffix($string, $suffix) {
@if has-suffix($string, $suffix) {
$string: string.slice($string, 1, -1 * string.length($suffix) - 1);
}
@return $string;
}
/// Trims a repeating prefix and suffix from the start and end of a string.
///
/// If a suffix is not provided, the prefix is used as the suffix to trim.
///
/// @example - scss
/// @debug trim-repeating(' foo bar ', ' '); // "foo bar"
///
/// @param {String} $string - The string to trim.
/// @param {String} $prefix - The repeating prefix string to trim.
/// @param {String} $suffix [$prefix] - The repeating suffix string to trim.
/// @return {String} The string with the prefix and suffix trimmed.
@function trim-repeating($string, $prefix, $suffix: $prefix) {
@return trim-repeating-prefix(
trim-repeating-suffix($string, $suffix),
$prefix
);
}
/// Trims a prefix and suffix from the start and end of a string.
///
/// If a suffix is not provided, the prefix is used as the suffix to trim.
///
/// @example - scss
/// @debug trim('var(--foo)', 'var(', ')'); // "--foo"
///
/// @param {String} $string - The string to trim.
/// @param {String} $prefix - The prefix string to trim.
/// @param {String} $suffix [$prefix] - The suffix string to trim.
/// @return {String} The string with the prefix and suffix trimmed.
@function trim($string, $prefix, $suffix: $prefix) {
@return trim-prefix(trim-suffix($string, $suffix), $prefix);
}