Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created January 20, 2014 11:25
Show Gist options
  • Save lunelson/8518525 to your computer and use it in GitHub Desktop.
Save lunelson/8518525 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.2.13)
// Compass (v0.12.2)
// ----
// general list helper functions (partly 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;
}
// hash-list helper functions
@function tuple-key($tuple) { @return nth($tuple, 1); }
@function tuple-value($tuple) { @return nth($tuple, 2); }
// hash-list versions of map-keys(), -values() and -has-key() functions
@function list-keys($list) {
$output: ();
@each $tuple in $list { $output: append($output, tuple-key($tuple), 'comma'); }
@return $output;
}
@function list-values($list) {
$output: ();
@each $tuple in $list { $output: append($output, tuple-value($tuple), 'comma'); }
@return $output;
}
@function list-has-key($list, $key) {
@each $tuple in $list { @if tuple-key($tuple) == $key { @return true; } }
@return false;
}
// hash-list versions of map-get() and map-merge()
@function list-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 list-merge($list1, $list2) {
$keys1: list-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;
}
// hash-list versions of map-get-z() and map-merge-z()
@function list-get-z($list, $keys...) {
@each $key in $keys {
@if $list == null { @return null; }
@else { $list: list-get($list, $key); } }
@return $list;
}
@function list-merge-z($hash, $keys-and-value...) {
$arg-length: length($keys-and-value);
$value: nth($keys-and-value, $arg-length);
$key-length: $arg-length - 1;
$keys: slice($keys-and-value, 1, $key-length);
@if $key-length == 0 { $value: list-merge($hash, list($value ())); }
@else { $start: if(type-of($value) == 'list', 0, 1);
@for $i from $start through $key-length {
$new-hash: (); $old-hash: ();
@if $i == 0 { $new-hash: $value; } @else { $new-hash: list(nth($keys, $key-length + 1 - $i) $value); }
@if $i == $key-length { $old-hash: $hash; } @else { $old-hash: list-get-z($hash, slice($keys, 1, $key-length - $i)...) or (); }
$value: list-merge($old-hash, $new-hash); } }
@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);
}
// // list helper functions
// @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;
// }
// // nested "Z" -get() and -merge() functions
// @function map-get-z($map, $keys...) {
// @each $key in $keys { @if $map == null { @return null; } @else { $map: map-get($map, $key); } }
// @return $map;
// }
// /* new version here, handles single non-map argument */
// @function map-merge-z($map, $keys-and-value...) {
// $arg-length: length($keys-and-value);
// $value: nth($keys-and-value, $arg-length);
// $key-length: $arg-length - 1;
// $keys: slice($keys-and-value, 1, $key-length);
// @if $key-length == 0 { $value: map-merge($map, ($value: ())); }
// @else { $start: if(type-of($value) == 'map', 0, 1);
// @for $i from $start through $key-length {
// $new-map: if($i == 0, $value, (nth($keys, $key-length + 1 - $i): $value));
// $old-map: if($i == $key-length, $map, map-get-z($map, slice($keys, 1, $key-length - $i)...) or ());
// $value: map-merge($old-map, $new-map); } }
// @return $value;
// }
// // additional -get() variations
// @function map-get-any($map, $keys...) {
// @each $key in $keys { @if map-has-key($map, $key) { @return map-get($map, $key); } }
// @return null; }
// @function map-get-all($map, $keys...) {
// $output: ();
// @each $key in $keys { $output: append($output, map-get($map, $key), 'comma'); }
// @return $output; }
// $hash-map: (
// alpha: (
// one: 1,
// two: 2,
// ),
// beta: (
// three: 3,
// four: 4,
// ),
// );
$hash-list: (
alpha (
one 1,
two 2
),
beta (
three 3,
four 4
)
);
.debug {
// out: inspect($hash-map);
// out: inspect($hash-list);
// out: inspect(map-merge-z($hash-map, gamma));
// out: inspect(map-merge-z($hash-map, gamma, 5,4,3,2,1));
// out: inspect(list-merge-z($hash-list, gamma, 5,4,3,2,1));
// out: inspect(map-merge-z($hash-map, beta, five,4,3,2,1));
out: list-debug(list-merge-z($hash-list, gamma));
out: list-debug(list-merge-z($hash-list, beta, five,4,3,2,1));
$out-list: list-merge-z($hash-list, beta, five,4,3,2,1);
check: list-debug(list-get-z($out-list, beta, five, 4, 3, 2));
check: list-debug(list-get-z($out-list, beta, five, 4, 3));
check: list-debug(list-get-z($out-list, beta, five, 4));
check: list-debug(list-get-z($out-list, beta, five));
check: list-debug(list-get-z($out-list, beta));
out: list-debug(slice((alpha), 1, 0));
}
.debug {
out: "( ( alpha, ( ( one, 1 ), ( two, 2 ) ) ), ( beta, ( ( three, 3 ), ( four, 4 ) ) ), ( gamma, ( ) ) )";
out: "( ( alpha, ( ( one, 1 ), ( two, 2 ) ) ), ( beta, ( ( three, 3 ), ( four, 4 ), ( five, 4 3 2 1 ) ) ) )";
check: "1";
check: "2 1";
check: "3 2 1";
check: "4 3 2 1";
check: "( ( three, 3 ), ( four, 4 ), ( five, 4 3 2 1 ) )";
out: "( )";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment