Permalink
Browse files

Add a map-remove() function for removing keys from a map.

Closes #1032
  • Loading branch information...
1 parent b73c113 commit f5b54093804e3bc71d349e71c18e16e9a30dfa20 @nex3 nex3 committed Jan 21, 2014
Showing with 39 additions and 0 deletions.
  1. +3 −0 doc-src/SASS_CHANGELOG.md
  2. +21 −0 lib/sass/script/functions.rb
  3. +15 −0 test/sass/functions_test.rb
@@ -176,6 +176,9 @@ There are a variety of functions for working with maps:
second map takes precedence, making this a good way to modify values in a map
as well.
+* The {Sass::Script::Functions#map_remove `map-remove($map, $key)` function}
+ returns a new map with a key removed.
+
* The {Sass::Script::Functions#map_keys `map-keys($map)` function} returns all
the keys in a map as a comma-separated list.
@@ -190,6 +190,9 @@ module Sass::Script
# \{#map_merge map-merge($map1, $map2)}
# : Merges two maps together into a new map.
#
+ # \{#map_remove map-remove($map, $key)}
+ # : Returns a new map with a key removed.
+ #
# \{#map_keys map-keys($map)}
# : Returns a list of all keys in a map.
#
@@ -1923,6 +1926,24 @@ def map_merge(map1, map2)
end
declare :map_get, [:map1, :map2]
+ # Returns a new map with a key removed.
+ #
+ # @example
+ # map-remove(("foo": 1, "bar": 2), "bar") => ("foo": 1)
+ # map-remove(("foo": 1, "bar": 2), "baz") => ("foo": 1, "bar": 2)
+ # @overload map_remove($map, $key)
+ # @param $map [Sass::Script::Value::Map]
+ # @param $key [Sass::Script::Value::Base]
+ # @return [Sass::Script::Value::Map]
+ # @raise [ArgumentError] if `$map` is not a map
+ def map_remove(map, key)
+ assert_type map, :Map
+ hash = to_h(map).dup
+ hash.delete key
+ map(hash)
+ end
+ declare :map_remove, [:map, :key]
+
# Returns a list of all keys in a map.
#
# @example
@@ -1349,6 +1349,21 @@ def test_map_merge
end
end
+ def test_map_remove
+ assert_equal("(foo: 1, baz: 3)",
+ perform("map-remove((foo: 1, bar: 2, baz: 3), bar)").to_sass)
+ assert_equal("()", perform("map-remove((), foo)").to_sass)
+
+ assert_warning(<<WARNING) do
+DEPRECATION WARNING: Passing lists of pairs to map-remove is deprecated and will
+be removed in future versions of Sass. Use Sass maps instead. For details, see
+http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
+WARNING
+ assert_equal("(foo: 1, baz: 3)",
+ perform("map-remove((foo 1, bar 2, baz 3), bar)").to_sass)
+ end
+ end
+
def test_map_keys
assert_equal("foo, bar",
perform("map-keys((foo: 1, bar: 2))").to_sass)

0 comments on commit f5b5409

Please sign in to comment.