Last active
November 23, 2020 12:43
-
-
Save solovets/0021d71ba4871411481fb91f9a086103 to your computer and use it in GitHub Desktop.
str.scss — set of functions that helps to manipulate strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// str.scss | v. 1.2.0 | MIT license | |
// str.scss - set of functions that helps to manipulate strings. | |
// | |
// Homepage: https://github.com/solovets/str.scss#readme | |
// Report an issue: https://github.com/solovets/str.scss/issues | |
// Author: Denis Solovets | |
// This file is always up to date. | |
$str-scss-strong-type-check: false !default; | |
@if type-of($str-scss-strong-type-check) != bool { | |
@error('[str.scss] Global variable `$str-scss-strong-type-check` must be of type bool'); | |
} | |
/// #_check-type($input[, $type, $check-if-list]) => bool | |
/// Checks if provided $input has required type | |
/// @access private | |
/// @param {any} $input - any value to check it's type | |
/// @param {string | list} $type ['string'] - allowed types of $input | |
/// @param {bool} $check-if-list [false] - checks that provided $input is a list | |
/// @return {bool} | |
@function _check-type($input, $type: string, $check-if-list: false) { | |
$inputs: (); | |
$types: (); | |
$inputs: if(type-of($input) != list, append($inputs, $input), $input); | |
$types: if(type-of($type) != list, append($types, $type), $type); | |
@if $check-if-list and type-of($input) != list { | |
@if index($types, type-of($input)) == null { | |
@return false; | |
} | |
} | |
@each $item in $inputs { | |
@if index($types, type-of($item)) == null { | |
@return false; | |
} | |
} | |
@return true; | |
} | |
/// #_return($correct, $result, $error) => @return | @error | |
/// Returns @return or @error directive for function | |
/// @access private | |
/// @param {bool} $correct - boolean value that shows if parent function has an error | |
/// @param {any} $result - result of parent function to be returned | |
/// @param {string} $error - description of errors in parent function | |
/// @return {@return | @error} | |
@function _return($correct, $result, $error) { | |
@if $correct { | |
@return $result; | |
} @else if not $str-scss-strong-type-check and not $correct { | |
@warn $error; | |
@return $result; | |
} @else { | |
@error $error; | |
} | |
} | |
/// #_error($func, $args...) => string | |
/// Returns a string with error description to be thrown as an error | |
/// @access private | |
/// @param {string} $func - name of function that requires error description | |
/// @param {list} $args... - lists of parent function variables to be checked: ($variable-value, $variable-required-type) | |
/// @return {string} | |
@function _error($func, $args...) { | |
$description: ''; | |
@each $arg in $args { | |
$arg-value: nth($arg, 1); | |
$arg-expected-type: nth($arg, 2); | |
$argument-is-correct: _check-type($arg-value, $arg-expected-type); | |
@if $argument-is-correct == false { | |
@if type-of($arg-value) == list { | |
$description: $description + ' One or more items of `#{$arg-value}` have wrong type. Expected types: `#{$arg-expected-type}`.'; | |
} @else { | |
$description: $description + ' `#{$arg-value}` must be of type `#{$arg-expected-type}`, got `#{type-of($arg-value)}`.'; | |
} | |
} | |
} | |
@return '[str.scss] [#{$func}]' + $description; | |
} | |
/// #_substring-info($input-string, $substring) => map | |
/// Returns map with information about $substring in $input-string: quantity, first index and last index | |
/// @access private | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @return {map} | |
@function _substring-info($input-string, $substring) { | |
$count: 0; | |
$first-index: null; | |
$last-index: null; | |
$string: $input-string; | |
$include: not not str-index($input-string, $substring); | |
$has-substring: $include; | |
@if ($has-substring) { | |
$first-index: str-index($string, $substring); | |
$last-index: 0; | |
} | |
@while($has-substring != false) { | |
$count: $count + 1; | |
$current-index: str-index($input-string, $substring) + if($count == 1, 0, str-length($substring) - 1); | |
$last-index: $last-index + $current-index; | |
$input-string: str-slice($input-string, str-index($input-string, $substring) + str-length($substring)); | |
$has-substring: not not str-index($input-string, $substring); | |
} | |
@return ( | |
include: $include, | |
count: $count, | |
first-index: $first-index, | |
last-index: $last-index | |
); | |
} | |
/// #_str-get-not-skipped-char-index($input-string[, $search-from, $trim-chars]) => number | |
/// Returns an index of first character that doesn't match any characters from $trim-chars argument | |
/// @access private | |
/// @param {string} $input-string | |
/// @param {'left' | 'right'} $search-from ['left'] | |
/// @param {string} $skip-char [' '] | |
/// @return {number} | |
@function _str-get-not-skipped-char-index($input-string, $search-from: 'left', $trim-chars: ' ') { | |
@if (type-of($input-string) != string) { | |
@error("$input-string should be a string"); | |
} | |
@if (index(('left', 'right'), $search-from) == null) { | |
@error("$search-from should be defined as 'left' or 'right'"); | |
} | |
$exclude: (); | |
@for $i from 1 through str-length($trim-chars) { | |
$exclude: append($exclude, str-slice($trim-chars, $i, $i)); | |
} | |
@for $i from 1 through str-length($input-string) { | |
$index: if($search-from == 'left', $i, -$i); | |
$char: str-slice($input-string, $index, $index); | |
@if (index($exclude, $char) == null) { | |
@return $index; | |
} | |
} | |
@return 0; | |
} | |
/// #str-chars($input-string) => list | |
/// Returns SCSS list with all string characters. | |
/// @param {string} $input-string - input string | |
/// @return {list} $input-string ## () | |
/// @example | |
/// @debug str-chars('Hello world'); | |
/// // => ("H" "e" "l" "l" "o" " " "w" "o" "r" "l" "d") | |
@function str-chars($input-string) { | |
$result: (); | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
@for $i from 1 through str-length($input-string) { | |
$result: append($result, str-slice($input-string, $i, $i)); | |
} | |
} @else { | |
$error: _error('str-chars', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-char-at($input-string, $index) => string | |
/// Returns character from input string at provided index | |
/// @param {string} $input-string - input string | |
/// @param {number} $index - index of required char | |
/// @return {string} $input-string, $index ## '' | |
/// @example | |
/// @debug str-char-at('Hello World', 2); | |
/// // => "e" | |
/// @debug str-char-at('Hello World', -4); | |
/// // => "o" | |
@function str-char-at($input-string, $index) { | |
$result: ''; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($index, number); | |
@if $argument-is-correct { | |
$result: str-slice($input-string, $index, $index); | |
} @else { | |
$error: _error('str-char-at', ($input-string, string), ($index, number)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-split($input-string[, $separator]) => list | |
/// Returns an array of strings by separating the string into substrings | |
/// @param {string} $input-string - input string | |
/// @param {string} $separator [' '] - separator to split on | |
/// @return {list} $input-string, $separator ## () | |
/// @example | |
/// @debug str-split('Hello World'); | |
/// // => ("Hello" "World") | |
/// @debug str-split('Hello World, Hello World', ', '); | |
/// // => ("Hello World" "Hello World") | |
@function str-split($input-string, $separator: ' ') { | |
$result: (); | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($separator); | |
@if $argument-is-correct { | |
$index: str-index($input-string, $separator); | |
$indent: str-length($separator); | |
@while ($index != null) { | |
$item: str-slice($input-string, 1, $index - 1); | |
$input-string: str-slice($input-string, $index + $indent); | |
$index: str-index($input-string, $separator); | |
@if (str-length($item) != 0) { | |
$result: append($result, $item); | |
} | |
} | |
$result: append($result, $input-string); | |
} @else { | |
$error: _error('str-split', ($input-string, string), ($separator, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-join($input-list[, $separator]) => string | |
/// Returns input list converted to a string | |
/// @param {list} $input-list | |
/// @param {string} $separator [''] | |
/// @return {string} $input-list, $separator ## '' | |
/// @example | |
/// @debug str-join((1, '. ', 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')); | |
/// // => "1. Hello World" | |
/// @debug str-join(('a', 'b', 'c'), '*'); | |
/// // => "a*b*c" | |
@function str-join($input-list, $separator: '') { | |
$result: ''; | |
$error: ''; | |
$argument-is-correct: _check-type($input-list, (string, number), true) and _check-type($separator); | |
@if $argument-is-correct { | |
@each $item in $input-list { | |
$result: $result + if(index($input-list, $item) != 1, $separator, '') + $item; | |
} | |
} @else { | |
$error: _error('str-join', ($input-list, (string, number)), ($separator, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
}; | |
/// #str-to-swapcase($input-string) => string | |
/// Returns a copy of the string in which all the case-based characters have had their case swapped. | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-to-swapcase('hELLO wORLD'); | |
/// // => "Hello World" | |
@function str-to-swapcase($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: ''; | |
$chars: str-chars($input-string); | |
@each $char in $chars { | |
@if ($char == to-upper-case($char)) { | |
$result: $result + to-lower-case($char); | |
} @else { | |
$result: $result + to-upper-case($char); | |
} | |
} | |
} @else { | |
$error: _error('str-to-swapcase', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-replace($input-string, $substring[, $replace, $g]) => string | |
/// Returns copy of input string where defined substring replaced by $replace argument | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @param {string} $replace [''] | |
/// @param {boolean} $g [true] | |
/// @return {string} $input-string, $substring, $replace ## $input-string | |
/// @example | |
/// @debug str-replace('Hello world', 'l'); | |
/// // => "Heo word" | |
/// @debug str-replace('Hello world', 'l', $g: false); | |
/// // => "Helo world" | |
/// @debug str-replace('Hello world', 'Hello', 'Privet'); | |
/// // => "Privet world" | |
@function str-replace($input-string, $substring, $replace: '', $g: true, $is-bulk: false) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring) and _check-type($replace, (string, number)); | |
@if $argument-is-correct { | |
$index: str-index($result, $substring); | |
@if $index { | |
@if $g { | |
$result: str-slice($result, 1, $index - 1) + $replace + str-replace(str-slice($result, $index + str-length($substring)), $substring, $replace); | |
} @else { | |
$result: str-slice($result, 1, $index - 1) + $replace + str-slice($result, $index + str-length($substring)); | |
} | |
} | |
} @else { | |
$error: _error('str#{if($is-bulk,'-bulk','')}-replace', ($input-string, string), ($substring, string), ($replace, (string, number))); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-bulk-replace($input-string, $substrings[, $replace, $g]) => string | |
/// Returns copy of input string where defined substrings replaced by $replace argument | |
/// @param {string} $input-string - input string | |
/// @param {list} $substrings - substrings to search in input string | |
/// @param {string} $replace [''] - string to be inserted instead of substrings | |
/// @param {boolean} $g [true] - global flag | |
/// @return {string} $input-string, $substrings, $replace ## $input-string | |
/// @example | |
/// @debug str-bulk-replace('Hello world', ('l', 'o'), '*'); | |
/// // => "He*** w*r*d" | |
/// @debug str-bulk-replace('Hello world', ('l', 'o'), $g: false); | |
/// // => "Hel world" | |
/// @debug str-bulk-replace('Hello To The mir', ('Hello', 'To The'), 'Privet'); | |
/// // => "Privet Privet mir" | |
@function str-bulk-replace($input-string, $substrings, $replace: '', $g: true) { | |
@each $substring in $substrings { | |
$input-string: str-replace($input-string, $substring, $replace, $g, true); | |
} | |
@return $input-string; | |
} | |
/// #str-include($input-string, $substring) => boolean | |
/// Returns boolean result of check if string contains a substring. | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @return {boolean} $input-string, $substring ## null | |
/// @example | |
/// @debug str-include('Hello World', 'lo'); | |
/// // => true | |
@function str-include($input-string, $substring) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
$substring-info: _substring-info($input-string, $substring); | |
$result: map-get($substring-info, include); | |
} @else { | |
$error: _error('str-include', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-count($input-string, $substring) => number | |
/// Returns number of occurrences of substring in string. | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @return {number} $input-string, $substring ## 0 | |
/// @example | |
/// @debug str-count('Hello World', 'z'); | |
/// // => 0 | |
/// @debug str-count('Hello World', 'l'); | |
/// // => 3 | |
/// @debug str-count('Hello World', 'ello'); | |
/// // => 1 | |
@function str-count($input-string, $substring) { | |
$result: 0; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
$substring-info: _substring-info($input-string, $substring); | |
$result: map-get($substring-info, count); | |
} @else { | |
$error: _error('str-count', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-first-index($input-string, $substring) => number | |
/// Returns first index of substring in provided string | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @return {number} $input-string, $substring ## null | |
/// @example | |
/// @debug str-first-index('Hello World', 'l'); | |
/// // => 3 | |
/// @debug str-first-index('Hello World', 'z'); | |
/// // => null | |
@function str-first-index($input-string, $substring) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
$substring-info: _substring-info($input-string, $substring); | |
$result: map-get($substring-info, first-index); | |
} @else { | |
$error: _error('str-first-index', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-last-index($input-string, $substring) => number | |
/// Returns last index of substring in provided string | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @return {number} $input-string, $substring ## null | |
/// @example | |
/// @debug str-last-index('Hello World', 'l'); | |
/// // => 10 | |
/// @debug str-last-index('Hello World', 'z'); | |
/// // => null | |
@function str-last-index($input-string, $substring) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
$substring-info: _substring-info($input-string, $substring); | |
$result: map-get($substring-info, last-index); | |
} @else { | |
$error: _error('str-last-index', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-capitalize($input-string[, $lowercase-rest]) => string | |
/// Returns string with capitalized first letter | |
/// @param {string} $input-string - input string | |
/// @param {boolean} $lowercase-rest [false] | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-capitalize('hello Wold'); | |
/// // => "Hello Wold" | |
/// @debug str-capitalize('hELLO WORLD', true); | |
/// // => "Hello world" | |
@function str-capitalize($input-string, $lowercase-rest: false) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$first-item: to-upper-case( str-slice($input-string, 1, 1) ); | |
$rest: str-slice($input-string, 2); | |
$result: $first-item + if($lowercase-rest == true, to-lower-case($rest), $rest); | |
} @else { | |
$error: _error('str-capitalize', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-decapitalize($input-string) => string | |
/// Returns string with decapitalized first letter | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-decapitalize('Hello World'); | |
/// // => "hello World" | |
@function str-decapitalize($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$first-item: to-lower-case( str-slice($input-string, 1, 1) ); | |
$result: $first-item + str-slice($input-string, 2); | |
} @else { | |
$error: _error('str-decapitalize', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-reverse($input-string) => string | |
/// Returns reversed string. | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-reverse('Hello World'); | |
/// // => "dlroW olleH" | |
@function str-reverse($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: ''; | |
$chars: str-chars($input-string); | |
@for $i from length($chars) through 1 { | |
$result: $result + nth($chars, $i); | |
} | |
} @else { | |
$error: _error('str-reverse', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-trim($input-string[, $trim-chars]) => string | |
/// Returns trimmed string | |
/// @param {string} $input-string | |
/// @param {string} $trim-chars [' '] | |
/// @return {string} $input-string, $trim-chars ## $input-string | |
/// @example | |
/// @debug str-trim(' Hello World '); | |
/// // => "Hello World" | |
/// @debug str-trim(' -_ Helllo World _- ', '- _'); | |
/// // => "Hello World" | |
@function str-trim($input-string, $trim-chars: ' ') { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($trim-chars); | |
@if $argument-is-correct { | |
$result: str-slice( | |
$input-string, | |
_str-get-not-skipped-char-index($input-string, 'left', $trim-chars), | |
_str-get-not-skipped-char-index($input-string, 'right', $trim-chars) | |
); | |
} @else { | |
$error: _error('str-trim', ($input-string, string), ($trim-chars, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-ltrim($input-string[, $trim-chars]) => string | |
/// Returns string with removed leading characters. | |
/// @param {string} $input-string | |
/// @param {string} $trim-chars [' '] | |
/// @return {string} $input-string, $trim-chars ## $input-string | |
/// @example | |
/// @debug str-ltrim(' Hello World '); | |
/// // => "Hello World " | |
/// @debug str-ltrim(' -_ Helllo World _- ', '- _'); | |
/// // => "Helllo World _- " | |
@function str-ltrim($input-string, $trim-chars: ' ') { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($trim-chars); | |
@if $argument-is-correct { | |
$result: str-slice( | |
$input-string, | |
_str-get-not-skipped-char-index($input-string, 'left', $trim-chars) | |
); | |
} @else { | |
$error: _error('str-ltrim', ($input-string, string), ($trim-chars, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-rtrim($input-string[, $trim-chars]) => string | |
/// Returns string with removed trailing characters. | |
/// @param {string} $input-string | |
/// @param {string} $trim-chars [' '] | |
/// @return {string} $input-string, $trim-chars ## $input-string | |
/// @example | |
/// @debug str-rtrim(' Hello World '); | |
/// // => " Hello World" | |
/// @debug str-rtrim(' -_ Helllo World _- ', '- _'); | |
/// // => " -_ Helllo World" | |
@function str-rtrim($input-string, $trim-chars: ' ') { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($trim-chars); | |
@if $argument-is-correct { | |
$result: str-slice( | |
$input-string, | |
1, | |
_str-get-not-skipped-char-index($input-string, 'right', $trim-chars) | |
); | |
} @else { | |
$error: _error('str-rtrim', ($input-string, string), ($trim-chars, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-clean($input-string) => string | |
/// Returns trimmed string with multiply spaces replaced with single space | |
/// @param {string} $input-string - input string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-clean(' Hello World '); | |
/// // => "Hello World" | |
@function str-clean($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: ''; | |
$input-string: str-trim($input-string); | |
$items: str-split($input-string); | |
@for $i from 1 through length($items) { | |
$result: $result + if($i == 1, '', ' ') + nth($items, $i); | |
} | |
} @else { | |
$error: _error('str-clean', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-is-blank($input-string) => boolean | |
/// Returns true if string is empty or contains whitespaces only | |
/// @param {string} $input-string | |
/// @return {boolean} $input-string ## null | |
/// @example | |
/// @debug str-is-blank(''); | |
/// // => true | |
/// @debug str-is-blank(' '); | |
/// // => true | |
/// @debug str-is-blank('Hello World'); | |
/// // => false | |
@function str-is-blank($input-string) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$input-string: str-trim($input-string); | |
$result: if(str-length($input-string) > 0, false, true); | |
} @else { | |
$error: _error('str-is-blank', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-starts-with($input-string, $substring[, $ignore-case]) => boolean | |
/// Returns true if string starts with provided substring | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @param {boolean} $ignore-case | |
/// @return {boolean} $input-string, $substring ## null | |
/// @example | |
/// @debug str-starts-with('Hello World', 'Hel'); | |
/// // => true | |
/// @debug str-starts-with('Hello World', 'hel'); | |
/// // => false | |
/// @debug str-starts-with('Hello World', 'hel', true); | |
/// // => true | |
@function str-starts-with($input-string, $substring, $ignore-case: false) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
@if ($ignore-case) { | |
$input-string: to-lower-case($input-string); | |
$substring: to-lower-case($substring); | |
} | |
$result: if(str-slice($input-string, 1, str-length($substring)) == $substring, true, false); | |
} @else { | |
$error: _error('str-starts-with', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-ends-with($input-string, $substring[, $ignore-case]) => boolean | |
/// Returns true if string ends with provided substring | |
/// @param {string} $input-string | |
/// @param {string} $substring | |
/// @param {boolean} $ignore-case [false] | |
/// @return {boolean} $input-string, $substring ## null | |
/// @example | |
/// @debug str-ends-with('Hello World', 'rld'); | |
/// // => true | |
/// @debug str-ends-with('Hello World', 'RLD'); | |
/// // => false | |
/// @debug str-ends-with('Hello World', 'RLD', true); | |
/// // => true | |
@function str-ends-with($input-string, $substring, $ignore-case: false) { | |
$result: null; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($substring); | |
@if $argument-is-correct { | |
@if ($ignore-case) { | |
$input-string: to-lower-case($input-string); | |
$substring: to-lower-case($substring); | |
} | |
$start-index: str-length($substring) * -1; | |
$result: if(str-slice($input-string, $start-index, -1) == $substring, true, false); | |
} @else { | |
$error: _error('str-ends-with', ($input-string, string), ($substring, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-repeat($input-string[, $times, $separator]) => string | |
/// Returns input string repeated provided number of times | |
/// @param {string} $input-string | |
/// @param {number} $times [1] | |
/// @param {string} $separate-with [''] | |
/// @return {string} $input-string, $times, $separator ## $input-string | |
/// @example | |
/// @debug str-repeat('Hello'); | |
/// // => "Hello" | |
/// @debug str-repeat('Hello', 2); | |
/// // => "HelloHello" | |
/// @debug str-repeat('Hello', 2, ', '); | |
/// // => "Hello, Hello" | |
@function str-repeat($input-string, $times: 1, $separator: '') { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string) and _check-type($times, number) and _check-type($separator, (string, number)); | |
@if $argument-is-correct { | |
$result: ''; | |
@for $i from 1 through $times { | |
$result: $result + if($i == 1, '', $separator) + $input-string; | |
} | |
} @else { | |
$error: _error('str-repeat', ($input-string, string), ($times, number), ($separator, (string, number))); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-to-upper-case($input-string) => string | |
/// Returns the calling string value converted to uppercase | |
/// Alias for to-upper-case String SASS built-in function | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-to-upper-case('hello world'); | |
/// // => "HELLO WORLD" | |
@function str-to-upper-case($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: to-upper-case($input-string); | |
} @else { | |
$error: _error('str-to-upper-case', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-to-lower-case($input-string) => string | |
/// Returns the calling string value converted to lowercase | |
/// Alias for to-lower-case String SASS built-in function | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-to-lower-case('Hello World'); | |
/// // => "hello world" | |
@function str-to-lower-case($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: to-lower-case($input-string); | |
} @else { | |
$error: _error('str-to-lower-case', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-quote($input-string) => string | |
/// Returns $input-string as quoted string | |
/// Alias for quote String SASS built-in function | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-quote(Hello); | |
/// // => "Hello" | |
@function str-quote($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: quote($input-string); | |
} @else { | |
$error: _error('str-quote', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} | |
/// #str-unique-id() => string | |
/// Returns a randomly-generated unquoted string | |
/// Alias for unique-id String SASS built-in function | |
/// @return {string} | |
/// @example | |
/// @debug str-unique-id(); | |
/// // => e.g. "ufl6i52" | |
@function str-unique-id() { | |
@return unique-id(); | |
} | |
/// #str-unquote($input-string) => string | |
/// Returns $input-string as unquoted string | |
/// Alias for unquote String SASS built-in function | |
/// @param {string} $input-string | |
/// @return {string} $input-string ## $input-string | |
/// @example | |
/// @debug str-unquote('.link:hover'); | |
/// // => .link:hover | |
@function str-unquote($input-string) { | |
$result: $input-string; | |
$error: ''; | |
$argument-is-correct: _check-type($input-string); | |
@if $argument-is-correct { | |
$result: unquote($input-string); | |
} @else { | |
$error: _error('str-unquote', ($input-string, string)); | |
} | |
@return _return($argument-is-correct, $result, $error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment