//定义first()函数,获取列表中的第一个列表项
@function list-first($list){
@return nth($list, 1 );
}
//定义last()函数,获取列表中的最后一个列表项
@function list-last($list){
@return nth($list,length($list));
}
//移除数组某个元素
@function remove($list, $value, $recursive: false) {
$result: ();
@for $i from 1 through length($list) {
@if type-of(nth($list, $i)) == list and $recursive {
$result: append($result, remove(nth($list, $i), $value, $recursive));
}
@else if nth($list, $i) != $value {
$result: append($result, nth($list, $i));
}
}
@return $result;
}
$list: a, b z, c, z, d, z, e, f;
$new-list: remove($list, z); // a, b z, c, d, e, f;
$new-list: remove($list, z, true); // a, b, c, d, e, f
//在某一位置上删除元素
@function remove-nth($list, $index) {
$result: null;
@if type-of($index) != number {
@warn "$index: #{quote($index)} is not a number for `remove-nth`." ;
}
@else if $index == 0 {
@warn "List index 0 must be a non-zero integer for `remove-nth`." ;
}
@else if abs($index) > length($list) {
@warn "List index is #{$index} but list is only #{length($list)} item long for `remove-nth`." ;
}
@else {
$result: ();
$index: if($index < 0 , length($list) + $index + 1 , $index);
@for $i from 1 through length($list) {
@if $i != $index {
$result: append($result, nth($list, $i));
}
}
}
@return $result;
}
$list: a, b, z, c, d, e, f;
$new-list: remove-nth($list, 3 ); // a, b, c, d, e, f
$new-list: remove-nth($list, 0 ); // error
$new-list: remove-nth($list, -2 ); // a, b, z, c, d, f
$new-list: remove-nth($list, -10 ); // error
$new-list: remove-nth($list, 100 ); // error
$new-list: remove-nth($list, zog); // error
//对数组进行切片操作
@function slice($list, $start: 1 , $end: length($list)) {
$result: null;
@if type-of($start) != number or type-of($end) != number {
@warn "Either $start or $end are not a number for `slice`." ;
}
@else if $start > $end {
@warn "The start index has to be lesser than or equals to the end index for `slice`." ;
}
@else if $start < 1 or $end < 1 {
@warn "List indexes must be non-zero integers for `slice`." ;
}
@else if $start > length($list) {
@warn "List index is #{$start} but list is only #{length($list)} item long for `slice`." ;
}
@else if $end > length($list) {
@warn "List index is #{$end} but list is only #{length($list)} item long for `slice`." ;
}
@else {
$result: ();
@for $i from $start through $end {
$result: append($result, nth($list, $i));
}
}
@return $result;
}
}
$list: a, b, c, d, e, f;
$new-list: slice($list, 3 , 5 ); // c, d, e
$new-list: slice($list, 4 , 4 ); // d
$new-list: slice($list, 5 , 3 ); // error
$new-list: slice($list, -1 , 10 ); // error
//反转数组
@function reverse($list, $recursive: false) {
$result: ();
@for $i from length($list)* -1 through -1 {
@if type-of(nth($list, abs($i))) == list and $recursive {
$result: append($result, reverse(nth($list, abs($i)), $recursive));
}
@else {
$result: append($result, nth($list, abs($i)));
}
}
@return $result;
}
$list: a, b, c d e, f, g, h;
$new-list: reverse($list); // h, g, f, c d e, b, a
$new-list: reverse($list, true); // h, g, f, e d c, b, a
//函数全部来自 http://hugogiraudel.com/ 2013 / 08 / 08 /advanced-sass-list-functions/
|