_string-ext.scss 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright 2021 Google LLC
  3. // SPDX-License-Identifier: Apache-2.0
  4. //
  5. @use 'sass:string';
  6. /// Checks if a string starts with a given prefix.
  7. ///
  8. /// @example - scss
  9. /// @debug has-prefix('var(--foo)', 'var('); // true
  10. ///
  11. /// @param {String} $string - The string to test.
  12. /// @param {String} $prefix - The prefix to check.
  13. /// @return {Boolean} True if the string starts with the given prefix.
  14. @function has-prefix($string, $prefix) {
  15. @return string.slice($string, 1, string.length($prefix)) == $prefix;
  16. }
  17. /// Checks if a string ends with a given suffix.
  18. ///
  19. /// @example - scss
  20. /// @debug has-suffix('var(--foo)', ')'); // true
  21. ///
  22. /// @param {String} $string - The string to test.
  23. /// @param {String} $suffix - The suffix to check.
  24. /// @return {Boolean} True if the string ends with the given suffix.
  25. @function has-suffix($string, $suffix) {
  26. @return string.slice($string, -1 * string.length($suffix)) == $suffix;
  27. }
  28. /// Trims a repeating prefix from the start of a string.
  29. ///
  30. /// @example - scss
  31. /// @debug trim-repeating-prefix(' foo bar ', ' '); // "foo bar "
  32. ///
  33. /// @param {String} $string - The string to trim.
  34. /// @param {String} $prefix - The repeating prefix string to trim.
  35. /// @return {String} The string with the prefix trimmed from the start.
  36. @function trim-repeating-prefix($string, $prefix) {
  37. @while has-prefix($string, $prefix) {
  38. $string: trim-prefix($string, $prefix);
  39. }
  40. @return $string;
  41. }
  42. /// Trims a prefix from the start of a string.
  43. ///
  44. /// @example - scss
  45. /// @debug trim-prefix('var(--foo)', 'var('); // "--foo)"
  46. ///
  47. /// @param {String} $string - The string to trim.
  48. /// @param {String} $prefix - The prefix string to trim.
  49. /// @return {String} The string with the prefix trimmed from the start.
  50. @function trim-prefix($string, $prefix) {
  51. @if has-prefix($string, $prefix) {
  52. $string: string.slice($string, string.length($prefix) + 1);
  53. }
  54. @return $string;
  55. }
  56. /// Trims a repeating suffix from the end of a string.
  57. ///
  58. /// @example - scss
  59. /// @debug trim-repeating-suffix(' foo bar ', ' '); // " foo bar"
  60. /// @debug trim-repeating-suffix('var(--foo)', ')'); // "var(--foo"
  61. ///
  62. /// @param {String} $string - The string to trim.
  63. /// @param {String} $suffix - The repeating suffix string to trim.
  64. /// @return {String} The string with the suffix trimmed from the end.
  65. @function trim-repeating-suffix($string, $suffix) {
  66. @while has-suffix($string, $suffix) {
  67. $string: trim-suffix($string, $suffix);
  68. }
  69. @return $string;
  70. }
  71. /// Trims a suffix from the end of a string.
  72. ///
  73. /// @example - scss
  74. /// @debug trim-suffix('var(--foo)', ')'); // "var(--foo"
  75. ///
  76. /// @param {String} $string - The string to trim.
  77. /// @param {String} $suffix - The suffix string to trim.
  78. /// @return {String} The string with the suffix trimmed from the end.
  79. @function trim-suffix($string, $suffix) {
  80. @if has-suffix($string, $suffix) {
  81. $string: string.slice($string, 1, -1 * string.length($suffix) - 1);
  82. }
  83. @return $string;
  84. }
  85. /// Trims a repeating prefix and suffix from the start and end of a string.
  86. ///
  87. /// If a suffix is not provided, the prefix is used as the suffix to trim.
  88. ///
  89. /// @example - scss
  90. /// @debug trim-repeating(' foo bar ', ' '); // "foo bar"
  91. ///
  92. /// @param {String} $string - The string to trim.
  93. /// @param {String} $prefix - The repeating prefix string to trim.
  94. /// @param {String} $suffix [$prefix] - The repeating suffix string to trim.
  95. /// @return {String} The string with the prefix and suffix trimmed.
  96. @function trim-repeating($string, $prefix, $suffix: $prefix) {
  97. @return trim-repeating-prefix(
  98. trim-repeating-suffix($string, $suffix),
  99. $prefix
  100. );
  101. }
  102. /// Trims a prefix and suffix from the start and end of a string.
  103. ///
  104. /// If a suffix is not provided, the prefix is used as the suffix to trim.
  105. ///
  106. /// @example - scss
  107. /// @debug trim('var(--foo)', 'var(', ')'); // "--foo"
  108. ///
  109. /// @param {String} $string - The string to trim.
  110. /// @param {String} $prefix - The prefix string to trim.
  111. /// @param {String} $suffix [$prefix] - The suffix string to trim.
  112. /// @return {String} The string with the prefix and suffix trimmed.
  113. @function trim($string, $prefix, $suffix: $prefix) {
  114. @return trim-prefix(trim-suffix($string, $suffix), $prefix);
  115. }