implement map-remove

This commit is contained in:
ConnorSkees 2020-04-02 13:49:39 -04:00
parent 8e3e23c6cd
commit ef1177ccca
2 changed files with 25 additions and 0 deletions

View File

@ -81,4 +81,19 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
Ok(Value::Map(map1)) Ok(Value::Map(map1))
}), }),
); );
f.insert(
"map-remove".to_owned(),
Box::new(|mut args, _| {
let mut map = match arg!(args, 0, "map") {
Value::Map(m) => m,
Value::List(v, ..) if v.is_empty() => SassMap::new(),
v => return Err(format!("$map: {} is not a map.", v).into()),
};
let keys = args.get_variadic()?;
for key in keys {
map.remove(&key);
}
Ok(Value::Map(map))
}),
);
} }

View File

@ -119,3 +119,13 @@ error!(
map_has_key_one_arg, map_has_key_one_arg,
"a {\n color: map-has-key(1);\n}\n", "Error: Missing argument $key." "a {\n color: map-has-key(1);\n}\n", "Error: Missing argument $key."
); );
test!(
map_remove_one,
"a {\n color: inspect(map-remove((\"foo\": 1, \"bar\": 2), \"bar\"));\n}\n",
"a {\n color: (\"foo\": 1);\n}\n"
);
test!(
map_remove_two,
"a {\n color: inspect(map-remove((\"foo\": 1, \"bar\": 2, \"baz\": 3), \"bar\", \"baz\"));\n}\n",
"a {\n color: (\"foo\": 1);\n}\n"
);