Permalink
Browse files

index($list, $value) returns the index of $value within $list or fals…

…e if not found.
  • Loading branch information...
1 parent d0ee2ee commit c4e10c7bed0c2760ab8ba5076cac875173077c7c @chriseppstein chriseppstein committed May 29, 2011
Showing with 28 additions and 0 deletions.
  1. +3 −0 doc-src/SASS_CHANGELOG.md
  2. +17 −0 lib/sass/script/functions.rb
  3. +8 −0 test/sass/functions_test.rb
@@ -7,6 +7,9 @@
* Fix some issues that were breaking Sass when running within Rubinius.
* Fix some issues that were affecting Rails 3.1 integration.
+* New function `index` returns the list index of a value
+ within a list. For example: `index(1px solid red, solid)`
+ returns `2`. When the value is not found `false` is returned.
## 3.1.1
@@ -1299,6 +1299,23 @@ def append(list, val, separator = Sass::Script::String.new("auto"))
declare :append, [:list, :val]
declare :append, [:list, :val, :separator]
+ # Returns the position of the given value within the given
+ # list. If not found, returns false.
+ #
+ # @example
+ # index(1px solid red, solid) => 2
+ # index(1px solid red, dashed) => false
+ def index(list, value)
+ assert_type list, :List
+ index = list.value.index {|e| e.eq(value).to_bool }
+ if index
+ Number.new(index + 1)
+ else
+ Bool.new(false)
+ end
+ end
+ declare :index, [:list, :value]
+
# Returns one of two values based on the truth value of the first argument.
#
# @example
@@ -965,6 +965,14 @@ def test_append
assert_error_message("Separator name must be space, comma, or auto for `append'", "append(1, 2, baboon)")
end
+ def test_index
+ assert_equal("1", evaluate("index(1px solid blue, 1px)"))
+ assert_equal("2", evaluate("index(1px solid blue, solid)"))
+ assert_equal("3", evaluate("index(1px solid blue, #00f)"))
+ assert_equal("false", evaluate("index(1px solid blue, 1em)"))
+ assert_equal("false", evaluate("index(1px solid blue, notfound)"))
+ end
+
def test_if
assert_equal("1px", evaluate("if(true, 1px, 2px)"))
assert_equal("2px", evaluate("if(false, 1px, 2px)"))

0 comments on commit c4e10c7

Please sign in to comment.