Skip to content

Commit

Permalink
New sass function: if
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Sep 22, 2010
1 parent 9819c42 commit b9a9e14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@
## 3.2.0 (Unreleased)

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

### Backwards Incompatibilities -- Must Read!

Expand Down
16 changes: 16 additions & 0 deletions lib/sass/script/functions.rb
Expand Up @@ -825,6 +825,22 @@ def abs(value)
numeric_transformation(value) {|n| n.abs}
end

# returns one of two values based on the truth value of the first argument.
#
# @example
# if(true, 1px, 2px) => 1px
# if(false, 1px, 2px) => 2px
# @param truth [Bool] the expression to be evaluated for truth
# @param if_true will be returned if the truth value is true.
# @param if_false will be returned if the truth value is false.
def if(truth, if_true, if_false)
if truth.to_bool
if_true
else
if_false
end
end

private

# This method implements the pattern of transforming a numeric value into
Expand Down
5 changes: 5 additions & 0 deletions test/sass/functions_test.rb
Expand Up @@ -545,6 +545,11 @@ def test_comparable
assert_error_message("#ff0000 is not a number for `comparable'", "comparable(1px, #f00)")
end

def test_if
assert_equal("1px", evaluate("if(true, 1px, 2px)"))
assert_equal("2px", evaluate("if(false, 1px, 2px)"))
end

private

def evaluate(value)
Expand Down

0 comments on commit b9a9e14

Please sign in to comment.