From e427ab302eef4ea5a62e0212a8c80bf70f996f50 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 10 Apr 2010 22:36:54 -0700 Subject: [PATCH] [Sass] Add the type-of() sass function to inspect the data type of values. --- doc-src/SASS_CHANGELOG.md | 8 ++++++++ lib/sass/script/functions.rb | 21 +++++++++++++++++++++ test/sass/functions_test.rb | 8 ++++++++ 3 files changed, 37 insertions(+) diff --git a/doc-src/SASS_CHANGELOG.md b/doc-src/SASS_CHANGELOG.md index 8cf914b9..7edca11f 100644 --- a/doc-src/SASS_CHANGELOG.md +++ b/doc-src/SASS_CHANGELOG.md @@ -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. diff --git a/lib/sass/script/functions.rb b/lib/sass/script/functions.rb index 24458d9d..cb437e3a 100644 --- a/lib/sass/script/functions.rb +++ b/lib/sass/script/functions.rb @@ -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: # diff --git a/test/sass/functions_test.rb b/test/sass/functions_test.rb index d33bd229..e2a198eb 100755 --- a/test/sass/functions_test.rb +++ b/test/sass/functions_test.rb @@ -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)