Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created October 24, 2013 20:24
Show Gist options
  • Save lunelson/7144368 to your computer and use it in GitHub Desktop.
Save lunelson/7144368 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.7)
// ----
@function has-any($map, $keys...) {
@each $key in $keys {
@if map-has-key($map, $key) { @return $key; }
}
@return false;
}
// @function has($map, $key) {} // return key if found
// @function has-any($map, $keys...) {} // use has()
// @function get($map, $key) {} // alias
// @function get-any($map, $keys...) {} // get($map,has-any($map,$keys...))
// @function nhas($map, $key) {} // look through each level and return key list
// @function nhas-any($map, $keys...) {} // use nhas()
// @function nget($map, $keys...) {} // nested get
// @function nget-any($map, $keys...) {} // nget($map,nhas-any($map,$keys...))
.maptest {
test: has-any((above: 5, below: 3), yes, no, below, good)
}
@function quotient($a, $b) {
@return $a * 1 / $b * 1;
}
@function q($a, $b) { @return quotient($a, $b); }
@mixin testmix($map: (bar: 8)) {
out: q(map-get($map, foo) or 4, map-get($map,bar));
}
@mixin testmix2($map) {
@each $key, $value in $map {
#{$key}: $value;
}
}
@mixin bp($map) {
$above: map-get($map, above);
$below: map-get($map, below);
$adj: map-get($map, adj);
$scale: map-get($map, scale);
}
$testmap: (foo: 5, bar: 8);
.test-out {
fail: map-get($testmap, foo) / map-get($testmap,bar);
pass: q(map-get($testmap, foo), map-get($testmap,bar));
@include testmix();
@include testmix2((foo: 5, bar: 8));
@media (min-width: 40em) {
@at-root html {
font-size: 125%;
}
width: 40em;
}
}
@function list-has-key($list, $key) {
$out: true;
@each $item in $list {
//$out: if(type_of($item) == 'list', list-has-key($item, $key), $item == $key);
$out: if($item == $key, true, false);
}
@return $out;
}
@function contains($c, $item) {
@return if(type_of($c) == 'map', map-has-key($c, $item), index($c, $item) != false);
}
// pre 3.3 nested list
$testlist: (a (d 4), b 5, c (d 2));
$testlist2: (a (d 4), b (c 5));
// 3.3+ nested map
// helper function for pre 3.3 lists
@function list-get($list, $key) { // simple pair retrieval
@if length($list) == 1 { $list: nth($list, 1); }
@each $pair in $list {
//@if length($pair) == 1 { $pair: nth($pair, 1); }
$index: index($pair, $key);
@if $index { @return nth($pair, 3 - $index); }
}
@return false;
}
// master "get" function
@function get($map, $keys...) {
$func: type_of($map);
@each $key in $keys {
@if $map == null { @return null; } @else { $map: call(#{$func}-get, $map, $key); }
} @return $map;
}
$list3: (a b,);
$list3b: nth($list3,1);
.test {
out: contains((a: b, c: d), a);
out: list-has-key('a' b c, 'a');
//out: map-has-key((a b c), a);
out: length($list3);
out: length($list3b);
out: map-get($testlist2, a);
// @each $pair in $list3 {
// index: index($pair, a);
// out: nth($pair,1) nth($pair,2);
// }
// index: index($list3b, a);
// out: nth($list3b,1) nth($list3b,2);
//out: get($testlist2,a,d);
// out: nth(a 2, 1);
// out: index((a 2,), a);
// out: get($testlist2, a, d);
// out: length($testlist2);
// out: nth($testlist2,1);
// out: nth(nth($testlist2,1),2);
// out: list-get($testlist2, a);
// out: index($testlist2, a);
}
@function str-remove($str, $sub) {
$i: str-index($str, $sub);
@if $i == 0 { @return $str; }
@else { @return str-remove(str-slice($str, 1, $i - 1) + str-slice($str, $i + 1, str-length($str)), $sub); }
}
@function map-debug($map) {
$result: unquote("( ");
@each $key, $value in $map {
@if type_of($value) == 'map' {
$result: unquote("#{$result}#{$key}: #{map-debug($value)}");
} @else { $result: unquote("#{$result}#{$key}: #{$value}, "); }
}
$result: str-remove($result,", )");
$result: unquote("#{$result} )");
@return quote($result);
}
$map:(
small: 30em,
medium: 45em,
large: (
one: 60em
)
);
.review {
out: map-debug($map);
$str: 'lunelsen';
str: str-slice($str,1,3) + str-slice($str,5,8);
str: str-index($str,'u');
str: str-remove($str, 'e');
}
// breakpoint setup routine; abstract to a mixin
$bp-specs: (
small: (
width: 30em,
margin: 1em,
scale: 100%
),
medium: (
width: 45em,
margin: 2em,
scale: 125%
)
);
$bp-alt: (
1: (
width: 30em,
margin: 1em,
scale: 100%
),
2: (
width: 45em,
margin: 2em,
scale: 125%
)
);
$bp-calcs: ();
@each $bp-alias, $bp-spec in $bp-specs {
$width: map-get($bp-spec, width);
$margin: map-get($bp-spec, margin);
$scale: map-get($bp-spec, scale);
$bp-value: $width + $margin;
$bp-calcs: map-merge($bp-calcs, (#{$bp-alias}: $bp-value));
@if $scale != 100% { @media (min-width: #{$bp-value}) { html { font-size: $scale; } } }
}
.review {
out: map-debug($bp-calcs);
out: map-get($bp-calcs, small);
// out: map-get($bp-alt, 1);
$str: 'lunelsen';
str: str-slice($str,1,3) + str-slice($str,5,8);
str: str-index($str,'u');
str: str-remove($str, 'e');
@media (min-width: 40em) {
parent: "the value is #{&}";
}
parent: "#{&}";
}
@function unlist($list) { @return nth($list, 1); }
@function list($item) { @return ($item,); }
.test {
.nested, .again {
.item, .another {
@each $parent-selector in & {
parent-selector: "#{nth($parent-selector,length($parent-selector))}";
}
}
}
}
.maptest {
test: below;
}
.test-out {
fail: 5/8;
pass: 0.625;
out: 0.5;
foo: 5;
bar: 8;
}
@media (min-width: 40em) {
.test-out {
width: 40em;
}
html {
font-size: 125%;
}
}
.test {
out: true;
out: false;
out: 1;
out: 2;
out: d 4;
}
.review {
out: "( small: 30em, medium: 45em, large: ( one: 60em ) )";
str: "lunlsen";
str: 2;
str: "lunlsn";
}
@media (min-width: 47em) {
html {
font-size: 125%;
}
}
.review {
out: "( small: 31em, medium: 47em, )";
out: 31em;
str: "lunlsen";
str: 2;
str: "lunlsn";
parent: ".review";
}
@media (min-width: 40em) {
.review {
parent: "the value is .review";
}
}
.test .nested .item, .test .nested .another, .test .again .item, .test .again .another {
parent-selector: ".item";
parent-selector: ".another";
parent-selector: ".item";
parent-selector: ".another";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment