Created
January 22, 2014 10:14
-
-
Save lunelson/8556373 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// libsass (v0.7.0) | |
// ---- | |
// list helper functions (some modified from SassyLists) | |
@function list($args...) { | |
$output: (); | |
@each $arg in $args { $output: append($output, $arg); } | |
@return $output; | |
} | |
@function unlist($list) { | |
@return if(length($list) == 1, nth($list, 1), $list); | |
} | |
@function slice($list, $start: 1, $end: length($list), $sep: 'comma') { | |
$output: (); | |
@for $i from $start through $end { | |
$output: append($output, nth($list, $i), $sep); } | |
@return $output; | |
} | |
@function replace-nth($list, $value, $index, $sep: 'comma') { | |
$output: (); | |
@for $i from 1 through length($list) { | |
@if $i == $index { $output: append($output, $value, $sep); } | |
@else { $output: append($output, nth($list, $i), $sep); } } | |
@return $output; | |
} | |
// list-map helper functions, purely semantic | |
@function tuple-key($tuple) { @return nth($tuple, 1); } | |
@function tuple-value($tuple) { @return nth($tuple, 2); } | |
// list-map versions of map-keys(), -values() and -has-key() functions | |
@function map-keys($list) { | |
$output: (); | |
@each $tuple in $list { $output: append($output, tuple-key($tuple), 'comma'); } | |
@return $output; | |
} | |
@function map-values($list) { | |
$output: (); | |
@each $tuple in $list { $output: append($output, tuple-value($tuple), 'comma'); } | |
@return $output; | |
} | |
@function map-has-key($list, $key) { | |
@each $tuple in $list { @if tuple-key($tuple) == $key { @return true; } } | |
@return false; | |
} | |
// list-map versions of map-get(), -merge() and -remove() | |
@function map-get($list, $key) { | |
@if length($list) == 0 { @return null; } | |
@else if length(nth($list, 1)) == 1 { @if tuple-key($list) == $key { @return tuple-value($list); } } | |
@else { @each $tuple in $list { @if tuple-key($tuple) == $key { @return tuple-value($tuple); } } } | |
@return null; | |
} | |
@function map-merge($list1, $list2) { | |
$keys1: map-keys($list1); | |
@each $tuple in $list2 { | |
$index: index($keys1, tuple-key($tuple)); | |
@if $index { $list1: replace-nth($list1, $tuple, $index); } | |
@else { $list1: append($list1, $tuple, 'comma'); } } | |
@return $list1; | |
} | |
@function map-remove($list, $key) { | |
$keys: map-keys($list); $out: (); | |
@for $n from 1 through length($list) { | |
@if nth($keys, $n) != $key { $out: append($out, nth($list, $n), 'comma'); } } | |
@return $out; | |
} | |
// advanced: map-get-z() and map-merge-z() | |
@function map-get-z($list, $keys...) { | |
@each $key in $keys { | |
@if $list == null { @return null; } | |
@else { $list: map-get($list, $key); } } | |
@return $list; | |
} | |
@function map-merge-z($list, $keys-and-value...) { | |
$arg-length: length($keys-and-value); | |
$value: nth($keys-and-value, $arg-length); | |
$key-length: $arg-length - 1; | |
@if $key-length == 0 { $value: if(type-of($value) == 'list', map-merge($list, $value), map-merge($list, list($value ()))); } | |
@else { $start: if(type-of($value) == 'list', 0, 1); | |
@for $i from $start through $key-length { | |
$new-list: (); $old-list: (); | |
@if $i == 0 { $new-list: $value; } @else { $new-list: list(nth($keys-and-value, $key-length + 1 - $i) $value); } | |
@if $i == $key-length { $old-list: $list; } @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); } | |
$value: map-merge($old-list, $new-list); } } | |
@return $value; | |
} | |
// list debug function borrowed from sassylists | |
@function list-debug($list, $pre: false, $level: 1) { | |
@if length($list) == 0 { @return "( )"; } | |
@if length($list) == 1 { @return if($pre, "(" + type-of($list) + ") ", "") + $list; } | |
$tab: " "; | |
$indent: ""; | |
$break: if($pre, "\A ", ""); | |
@for $i from 1 to $level { $indent: $indent + $tab; } | |
$output: "(" + $break; | |
@for $i from 1 through length($list) { | |
$item: nth($list, $i); | |
$output: $output + if($pre, $indent + $tab, " "); | |
@if length($item) > 1 { | |
$output: $output + if($pre, "(list: " + length($item) + ") ", "") + list-debug($item, $pre, $level + 1); | |
} @else { | |
@if $pre { $output: $output + "(" + type-of($item) + ") "; } | |
@if length($item) == 0 { $output: $output + "( )"; } | |
@else if type-of($item) == string { $output: $output + quote($item); } | |
@else if $item == null { $output: $output + "null"; } | |
@else { $output: $output + $item; } | |
} | |
@if $i != length($list) { $output: $output + "," + $break; } | |
} | |
$output: $output + $break + if($pre, if($level > 1, $indent, ""), " ") + ")"; | |
@return quote($output); | |
} | |
// +++++++++++ testing | |
$list-map: ( alpha 1, beta 2, gamma 3 ); | |
$new-map: map-merge($list-map, (gamma 4)); | |
$short-map: map-remove($list-map, alpha); | |
$list-map-z: ( | |
alpha ( | |
beta ( | |
gamma 3 | |
) | |
) | |
); | |
.debug { | |
out: map-keys($list-map); //-> alpha, beta, gamma | |
out: map-values($list-map); //-> 1, 2, 3 | |
out: map-has-key($list-map, gamma); //-> true | |
out: map-has-key($list-map, delta); //-> false | |
out: map-get($list-map, alpha); //-> 1 | |
out: map-get($list-map, beta); //-> 2 | |
out: map-get($list-map, gamma); //-> 3 | |
out: list-debug($new-map); | |
out: list-debug($short-map); | |
out: list-debug(map-get-z($list-map-z, alpha)); | |
out: list-debug(map-get-z($list-map-z, alpha, beta)); | |
out: list-debug(map-get-z($list-map-z, alpha, beta, gamma)); | |
} | |
.debug2 { | |
out: list-debug(map-merge-z($list-map-z, delta)); | |
out: list-debug(map-merge-z($list-map-z, delta, epsilon)); | |
out: list-debug(map-merge-z($list-map-z, delta, epsilon, 5)); | |
out: list-debug(map-merge-z($list-map-z, delta, list(epsilon 5))); | |
out: list-debug(map-merge-z($list-map-z, (delta 4, epsilon 6))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.debug { | |
out: alpha, beta, gamma; | |
out: 1, 2, 3; | |
out: true; | |
out: false; | |
out: 1; | |
out: 2; | |
out: 3; | |
out: "( ( alpha, 1 ), ( beta, 2 ), gamma, 4 )"; | |
out: "( ( beta, 2 ), ( gamma, 3 ) )"; | |
out: "( beta, ( gamma, 3 ) )"; | |
out: "( gamma, 3 )"; | |
out: "3"; } | |
.debug2 { | |
out: "( alpha, ( beta, ( gamma, 3 ) ), ( delta, ( ) ) )"; | |
out: "( alpha, ( beta, ( gamma, 3 ) ), ( delta, epsilon ) )"; | |
out: "( alpha, ( beta, ( gamma, 3 ) ), ( delta, epsilon 5 ) )"; | |
out: "( alpha, ( beta, ( gamma, 3 ) ), ( delta, epsilon 5 ) )"; | |
out: "( alpha, ( beta, ( gamma, 3 ) ), ( delta, 4 ), ( epsilon, 6 ) )"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment