mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
[Sass] Add the type-of() sass function to inspect the data type of values.
This commit is contained in:
parent
c42d9d93c3
commit
e427ab302e
3 changed files with 37 additions and 0 deletions
|
@ -5,6 +5,14 @@
|
|||
|
||||
## 3.0.0.beta.2 (Unreleased)
|
||||
|
||||
### New Sass Functions for Introspection
|
||||
|
||||
Several new functions were added to make it easier to have
|
||||
more flexible arguments to mixins and to enable deprecation
|
||||
of obsolete APIs.
|
||||
|
||||
* `type-of` -- Returns the type of a value.
|
||||
|
||||
### `@warn`
|
||||
|
||||
A new directive `@warn` has been added that allows sass libraries to emit warnings. This can be used to issue deprecation warnings, discourage sloppy coding, etc.
|
||||
|
|
|
@ -105,6 +105,11 @@ module Sass::Script
|
|||
# \{#abs}
|
||||
# : Returns the absolute value of a number.
|
||||
#
|
||||
# ## Introspection Functions
|
||||
#
|
||||
# \{#type_of}
|
||||
# : Returns the type of a value.
|
||||
#
|
||||
# These functions are described in more detail below.
|
||||
#
|
||||
# ## Adding Custom Functions
|
||||
|
@ -669,6 +674,22 @@ module Sass::Script
|
|||
Sass::Script::String.new(str.value, :string)
|
||||
end
|
||||
|
||||
# Inspects the type of the argument, returning it as an unquoted string.
|
||||
# For example:
|
||||
#
|
||||
# type-of(100px) => number
|
||||
# type-of(asdf) => string
|
||||
# type-of("asdf") => string
|
||||
# type-of(true) => bool
|
||||
# type-of(#fff) => color
|
||||
# type-of(blue) => color
|
||||
#
|
||||
# @param obj [Literal] The object to inspect
|
||||
# @return [String] The unquoted string value of the literal's type
|
||||
def type_of(obj)
|
||||
Sass::Script::String.new(obj.class.name.gsub(/Sass::Script::/,'').downcase)
|
||||
end
|
||||
|
||||
# Converts a decimal number to a percentage.
|
||||
# For example:
|
||||
#
|
||||
|
|
|
@ -505,6 +505,14 @@ The #options attribute is not set on this Sass::Script::String.
|
|||
MSG
|
||||
end
|
||||
|
||||
def test_type_of
|
||||
assert_equal("string", evaluate("type-of(\"asdf\")"))
|
||||
assert_equal("string", evaluate("type-of(asdf)"))
|
||||
assert_equal("number", evaluate("type-of(1px)"))
|
||||
assert_equal("bool", evaluate("type-of(true)"))
|
||||
assert_equal("color", evaluate("type-of(#fff)"))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def evaluate(value)
|
||||
|
|
Loading…
Add table
Reference in a new issue