Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

zip($list1, $list2, ...) returns a new combined list of lists.

  • Loading branch information...
commit 6d0893fe3ce573f337a638b7189f8b5e97e1e223 1 parent c4e10c7
@chriseppstein chriseppstein authored
View
4 doc-src/SASS_CHANGELOG.md
@@ -7,6 +7,10 @@
* Fix some issues that were breaking Sass when running within Rubinius.
* Fix some issues that were affecting Rails 3.1 integration.
+* New function `zip` allows several lists to be combined into one
+ list of lists. For example:
+ `zip(1px 1px 3px, solid dashed solid, red green blue)` becomes
+ `1px solid red, 1px dashed green, 3px solid blue`
* 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.
View
26 lib/sass/script/functions.rb
@@ -1299,6 +1299,32 @@ def append(list, val, separator = Sass::Script::String.new("auto"))
declare :append, [:list, :val]
declare :append, [:list, :val, :separator]
+ # Combines several lists into a single comma separated list
+ # space separated lists.
+ #
+ # The length of the resulting list is the length of the
+ # shortest list.
+ #
+ # @example
+ # zip(1px 1px 3px, solid dashed solid, red green blue)
+ # => 1px solid red, 1px dashed green, 3px solid blue
+ def zip(*lists)
+ length = nil
+ values = []
+ lists.each do |list|
+ assert_type list, :List
+ values << list.value.dup
+ length = length.nil? ? list.value.length : [length, list.value.length].min
+ end
+ values.each do |value|
+ value.slice!(length)
+ end
+ new_list_value = values.first.zip(*values[1..-1])
+ List.new(new_list_value.map{|list| List.new(list, :space)}, :comma)
+ end
+ declare :zip, [], :var_args => true
+
+
# Returns the position of the given value within the given
# list. If not found, returns false.
#
View
5 test/sass/functions_test.rb
@@ -965,6 +965,11 @@ def test_append
assert_error_message("Separator name must be space, comma, or auto for `append'", "append(1, 2, baboon)")
end
+ def test_zip
+ assert_equal("1 3 5, 2 4 6", evaluate("zip(1 2, 3 4, 5 6)"))
+ assert_equal("1 4 7, 2 5 8", evaluate("zip(1 2 3, 4 5 6, 7 8)"))
+ end
+
def test_index
assert_equal("1", evaluate("index(1px solid blue, 1px)"))
assert_equal("2", evaluate("index(1px solid blue, solid)"))
Please sign in to comment.
Something went wrong with that request. Please try again.