Skip to content

Commit e5ea652

Browse files
committed
Adds str-trim and parenthesis functions.
1 parent 422e043 commit e5ea652

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"css"
1111
],
1212
"homepage": "https://www.github.com/deviscoding/base",
13-
"version": "2.0.2",
13+
"version": "2.0.3",
1414
"replace": {
1515
"strapless/base": "^2.0"
1616
},

scss/functions/_functions.scss

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,48 @@
4444
@return $string;
4545
}
4646

47+
// Remove empty spaces from ends of string
48+
//
49+
// @author Hugo Giraudel (Original), Aaron M Jones (modified to work with quoted strings)
50+
//
51+
// @param {String} $string - The string to trim previously quoted strings.
52+
@function str-trim($string) {
53+
$unquote: unquote(quote($string));
54+
$start: 1;
55+
$end: str-length($unquote);
56+
57+
@for $i from 1 through str-length($unquote) {
58+
$first: str-slice($unquote, $i, $i);
59+
$last: str-slice($unquote, -$i, -$i);
60+
61+
@if $first == " " and $i + 1 == $start + 1 {
62+
$start: $i + 1;
63+
}
64+
65+
@if $last == " " and str-length($unquote) - $i == $end - 1 {
66+
$end: str-length($unquote) - $i;
67+
}
68+
}
69+
70+
$trimmed: str-slice($unquote, $start, $end);
71+
72+
@return if(($unquote != $string), quote($trimmed), $trimmed);
73+
}
74+
75+
// Surround String with Parenthesis. If doNotQuote property is true, quoted strings are unquoted.
76+
//
77+
// @param {String} $string - The string to enclose in parenthesis.
78+
// @param {Boolean} $doNotQuote - TRUE: Leave the string unquoted, FALSE: Return quotes to previously quoted strings.
79+
@function parenthesis($string,$doNotQuote: false) {
80+
$unquote: str-trim(unquote(quote($string)));
81+
$left: unquote("(");
82+
$right: unquote(")");
83+
$tItem: if(str-slice($unquote,1,1) == $left,$unquote,#{$left}#{$unquote});
84+
$tItem: if(str-slice($tItem,-1,-1) == $right,$tItem,#{$tItem}#{$right});
85+
86+
@return if(($unquote != $string and $doNotQuote != true), quote($tItem), $tItem);
87+
}
88+
4789
// Explodes a string into a SASS list by the given delimiter
4890
// @author Hugo Giraudel (modified from original)
4991
@function _str-explode($string, $delimiter: "") {

0 commit comments

Comments
 (0)