Skip to content

Instantly share code, notes, and snippets.

@Phize
Created October 6, 2011 19:59
Show Gist options
  • Save Phize/1268481 to your computer and use it in GitHub Desktop.
Save Phize/1268481 to your computer and use it in GitHub Desktop.
Sass function to join multiple lists with separator (An emulation of Compass's compact function).
// 複数のリストを区切り文字で連結
//
// http://twitter.com/#!/Phize/status/68721136401260544
// http://twitter.com/#!/Phize/status/68721331155386368
// http://twitter.com/#!/Phize/status/68723540031057922
//
// <code>
// $value-1: 1px 2px 3px #fff;
// $value-2: 4px 5px 6px #000;
// property: join-lists(($value-1) ($value-2));
// </code>
//
// @param list $lists 連結対象となるリストのリスト
// @param string $separator リストの区切り文字
// @return list 連結後のリスト
@function join-lists($lists, $separator: ' ') {
$value: '';
@if $lists and length($lists) > 0 {
@if nth($lists, 1) and length(nth($lists, 1)) > 0 {
$value: nth($lists, 1);
}
}
@for $i from 1 to length($lists) {
@if $i == 1 {
@if nth($lists, 1) and length(nth($lists, 1)) > 0
and nth($lists, $i + 1) and length(nth($lists, $i + 1)) > 0 {
$value: $value + $separator + nth($lists, $i + 1);
} @else if (not nth($lists, 1) or length(nth($lists, 1)) == 0)
and nth($lists, $i + 1) and length(nth($lists, $i + 1)) > 0 {
$value: $value + nth($lists, $i + 1);
}
} @else if nth($lists, $i + 1) and length(nth($lists, $i + 1)) > 0 {
$value: $value + $separator + nth($lists, $i + 1);
}
}
@return join(#{$value}, ());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment