Skip to content

Commit

Permalink
zip($list1, $list2, ...) returns a new combined list of lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed May 29, 2011
1 parent c4e10c7 commit 6d0893f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions lib/sass/script/functions.rb
Expand Up @@ -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.
#
Expand Down
5 changes: 5 additions & 0 deletions test/sass/functions_test.rb
Expand Up @@ -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)"))
Expand Down

0 comments on commit 6d0893f

Please sign in to comment.