Permalink
Browse files

Add a join function.

  • Loading branch information...
1 parent aa64eda commit 83857899673a0f2a92fff73cf9e0a969fbcf992e @nex3 nex3 committed Nov 10, 2010
Showing with 56 additions and 1 deletion.
  1. +38 −1 lib/sass/script/functions.rb
  2. +18 −0 test/sass/functions_test.rb
@@ -705,7 +705,7 @@ def adjust_hue(color, degrees)
# mix(#f00, #00f) => #7f007f
# mix(#f00, #00f, 25%) => #3f00bf
# mix(rgba(255, 0, 0, 0.5), #00f) => rgba(63, 0, 191, 0.75)
- # @overload mix(color1, color2, weight = 50%)
+ # @overload mix(color1, color2, weight: 50%)
# @param color1 [Color]
# @param color2 [Color]
# @param weight [Number] between 0% and 100%
@@ -995,6 +995,43 @@ def nth(list, n)
end
declare :nth, [:list, :n]
+ # Joins together two lists into a new list.
+ # Since individual values are the same as one-item lists,
+ # this can also join items onto the beginning or end of a list.
+ #
+ # Unless the `$separator` argument is passed,
+ # if one list is comma-separated and one is space-separated,
+ # the first parameter's separator is used for the resulting list.
+ # If the lists have only one item each, spaces are used for the resulting list.
+ #
+ # @example
+ # join(10px 20px, 30px 40px) => 10px 20px 30px 40px
+ # join((blue, red), (#abc, #def)) => blue, red, #abc, #def
+ # join(10px, 20px) => 10px 20px
+ # join(10px, 20px, comma) => 10px, 20px
+ # join((blue, red), (#abc, #def), space) => blue red #abc #def
+ # @overload join(list1, list2, separator: auto)
+ # @param list1 [Literal] The first list to join
+ # @param list2 [Literal] The second list to join
+ # @param separator [String] How the list separator (comma or space) should be determined.
+ # If this is `comma` or `space`, that is always the separator;
+ # if this is `auto` (the default), the separator is determined as explained above.
+ def join(list1, list2, separator = Sass::Script::String.new("auto"))
+ assert_type separator, :String
+ unless %w[auto space comma].include?(separator.value)
+ throw ArgumentError.new("Separator name must be space, comma, or auto")
+ end
+ sep1 = list1.separator if list1.is_a?(Sass::Script::List)
+ sep2 = list2.separator if list2.is_a?(Sass::Script::List)
+ Sass::Script::List.new(
+ list1.to_a + list2.to_a,
+ if separator.value == 'auto'
+ sep1 || sep2 || :space
+ else
+ separator.value.to_sym
+ end)
+ end
+
private
# This method implements the pattern of transforming a numeric value into
@@ -620,6 +620,24 @@ def test_nth
assert_error_message("List index is 2 but list is only 1 item long for `nth'", "nth(foo, 2)")
end
+ def test_join
+ assert_equal("1 2 3", evaluate("join(1 2, 3)"))
+ assert_equal("1 2 3", evaluate("join(1, 2 3)"))
+ assert_equal("1 2 3 4", evaluate("join(1 2, 3 4)"))
+ assert_equal("1, 2, 3", evaluate("join((1, 2), 3)"))
+ assert_equal("1, 2, 3", evaluate("join(1, (2, 3))"))
+ assert_equal("1, 2, 3, 4", evaluate("join((1, 2), (3, 4))"))
+
+ assert_equal("1 2", evaluate("join(1, 2)"))
+ assert_equal("1 2 3 4", evaluate("join(1 2, (3, 4))"))
+ assert_equal("1, 2, 3, 4", evaluate("join((1, 2), 3 4)"))
+
+ assert_equal("1 2", evaluate("join(1, 2, auto)"))
+ assert_equal("1, 2, 3, 4", evaluate("join(1 2, 3 4, comma)"))
+ assert_equal("1 2 3 4", evaluate("join((1, 2), (3, 4), space)"))
+ assert_equal("1, 2", evaluate("join(1, 2, comma)"))
+ end
+
def test_keyword_args_rgb
assert_equal(%Q{white}, evaluate("rgb($red: 255, $green: 255, $blue: 255)"))
end

0 comments on commit 8385789

Please sign in to comment.