Skip to content

Commit b9a9e14

Browse files
committed
New sass function: if
1 parent 9819c42 commit b9a9e14

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc-src/SASS_CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
## 3.2.0 (Unreleased)
77

88
* Add an {Sass::Script::Functions#invert `invert` function} that takes the inverse of colors.
9+
* A new sass function called `if` can be used to emit one of two values
10+
based on the truth value of the first argument. E.g. `if(true, 1px, 2px)`
11+
returns `1px` and `if(false, 1px, 2px)` returns `2px`
912

1013
### Backwards Incompatibilities -- Must Read!
1114

lib/sass/script/functions.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,22 @@ def abs(value)
825825
numeric_transformation(value) {|n| n.abs}
826826
end
827827

828+
# returns one of two values based on the truth value of the first argument.
829+
#
830+
# @example
831+
# if(true, 1px, 2px) => 1px
832+
# if(false, 1px, 2px) => 2px
833+
# @param truth [Bool] the expression to be evaluated for truth
834+
# @param if_true will be returned if the truth value is true.
835+
# @param if_false will be returned if the truth value is false.
836+
def if(truth, if_true, if_false)
837+
if truth.to_bool
838+
if_true
839+
else
840+
if_false
841+
end
842+
end
843+
828844
private
829845

830846
# This method implements the pattern of transforming a numeric value into

test/sass/functions_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ def test_comparable
545545
assert_error_message("#ff0000 is not a number for `comparable'", "comparable(1px, #f00)")
546546
end
547547

548+
def test_if
549+
assert_equal("1px", evaluate("if(true, 1px, 2px)"))
550+
assert_equal("2px", evaluate("if(false, 1px, 2px)"))
551+
end
552+
548553
private
549554

550555
def evaluate(value)

0 commit comments

Comments
 (0)