Skip to content

Instantly share code, notes, and snippets.

@sodatea
Last active May 12, 2016 15:12
Show Gist options
  • Save sodatea/c33b88f4f2e43feab483af565457a894 to your computer and use it in GitHub Desktop.
Save sodatea/c33b88f4f2e43feab483af565457a894 to your computer and use it in GitHub Desktop.
Extract the property value related to a certain edge of a box, from its shorthand syntax
@function string-split($string, $delimiter: " ") {
$list: ();
$sum: str-length($string);
@while str-length($string) > 0 {
$delimiter-index: str-index($string, $delimiter);
@if str-length($string) >= 1 and $delimiter-index == null {
$list: append($list, $string);
$string: "";
}
@if type-of($delimiter-index) == number {
$each: str-slice($string, 0, ($delimiter-index - 1));
$list: append($list, $each);
$string: str-slice($string, ($delimiter-index + 1), $sum);
}
}
@return $list;
}
@function extract-edge($values, $edge) {
$list: $values;
@if type-of($values) == string {
$list: string-split($values);
}
$length: length($list);
$map: (top: nth($list, 1));
@if $length == 4 {
$map: map-merge($map, (
right: nth($list, 2),
bottom: nth($list, 3),
left: nth($list, 4)
));
} @else if $length == 3 {
$map: map-merge($map, (
left: nth($list, 2),
right: nth($list, 2),
bottom: nth($list, 3)
));
} @else if $length == 2 {
$map: map-merge($map, (
bottom: nth($list, 1),
left: nth($list, 2),
right: nth($list, 2)
));
} @else if $length == 1 {
$map: map-merge($map, (
right: nth($list, 1),
bottom: nth($list, 1),
left: nth($list, 1)
));
}
@return map-get($map, $edge);
}
@debug extract-edge(1px 2px 3px 4px, "left");
@debug extract-edge(1px 2px 3px, "left");
@debug extract-edge(1px 2px, "left");
@debug extract-edge(1px, "left");
@debug extract-edge("1px 2px 3px 4px", "left");
@debug extract-edge("1px 2px 3px", "left");
@debug extract-edge("1px 2px", "left");
@debug extract-edge("1px", "left");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment