Sass Functions
Functions
- Dart Sass
- ✓
- LibSass
- since 3.5.0
- Ruby Sass
- since 3.5.0
In older versions of LibSass and Ruby Sass, the call()
function took a string representing a function’s name. This was changed to take a function value instead in preparation for a new module system where functions are no longer global and so a given name may not always refer to the same function.
Passing a string to call()
still works in all implementations, but it’s deprecated and will be disallowed in future versions.
Functions can be values too! You can’t directly write a function as a value, but you can pass a function’s name to the [meta.get-function()
function][] to get it as a value. Once you have a function value, you can pass it to the meta.call()
function to call it. This is useful for writing higher-order functions that call other functions.
@use "sass:list";
@use "sass:meta";
@use "sass:string";
/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition) {
$new-list: ();
$separator: list.separator($list);
@each $element in $list {
@if not meta.call($condition, $element) {
$new-list: list.append($new-list, $element, $separator: $separator);
}
}
@return $new-list;
}
$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
content {
@function contains-helvetica($string) {
@return string.index($string, "Helvetica");
}
font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
}
@use "sass:list"
@use "sass:meta"
@use "sass:string"
/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition)
$new-list: ()
$separator: list.separator($list)
@each $element in $list
@if not meta.call($condition, $element)
$new-list: list.append($new-list, $element, $separator: $separator)
@return $new-list
$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif
content
@function contains-helvetica($string)
@return string.index($string, "Helvetica")
font-family: remove-where($fonts, meta.get-function("contains-helvetica"))
content {
font-family: Tahoma, Geneva, Arial, sans-serif;
}
© 2006–2020 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
Licensed under the MIT License.
https://sass-lang.com/documentation/values/functions