1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Add a sass function unitless() that returns a boolean indicating if a number is unitless.

This commit is contained in:
Chris Eppstein 2010-04-10 22:40:36 -07:00
parent 4eacfa9c16
commit 8bf19c1c8a
3 changed files with 24 additions and 0 deletions

View file

@ -13,6 +13,7 @@ of obsolete APIs.
* `type-of` -- Returns the type of a value.
* `unit` -- Returns the units associated with a number.
* `unitless` -- Returns whether a number has units or not.
### `@warn`

View file

@ -113,6 +113,9 @@ module Sass::Script
# \{#unit}
# : Returns the units associated with a number.
#
# \{#unitless}
# : Returns whether a number has units or not.
#
# These functions are described in more detail below.
#
# ## Adding Custom Functions
@ -711,6 +714,20 @@ module Sass::Script
Sass::Script::String.new(number.unit_str, :string)
end
# Inspects the unit of the number, returning a boolean indicating if it is unitless.
# For example:
#
# unitless(100) => true
# unitless(100px) => false
#
# @param number [Literal] The number to inspect
# @return [Bool] indicating if the number is unitless
# @raise [ArgumentError] if `number` isn't a number
def unitless(number)
assert_type number, :Number
Sass::Script::Bool.new(number.unitless?)
end
# Converts a decimal number to a percentage.
# For example:
#

View file

@ -522,6 +522,12 @@ MSG
assert_error_message("#ff0000 is not a number for `unit'", "unit(#f00)")
end
def test_unitless
assert_equal(%Q{true}, evaluate("unitless(100)"))
assert_equal(%Q{false}, evaluate("unitless(100px)"))
assert_error_message("#ff0000 is not a number for `unitless'", "unitless(#f00)")
end
private
def evaluate(value)