Skip to content

Instantly share code, notes, and snippets.

@mannieschumpert
Created December 6, 2013 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannieschumpert/7828253 to your computer and use it in GitHub Desktop.
Save mannieschumpert/7828253 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
@import "SassyLists";
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
// Compares two values based on alphabetical order
// -------------------------------------------------------------------------------
// @private
// -------------------------------------------------------------------------------
// @usage sort()
// -------------------------------------------------------------------------------
// @param $a [Literal] : first value
// @param $b [Literal] : second value
// @param $matrix [List] : alphabetical order
// -------------------------------------------------------------------------------
// @return [Boolean]
@function _compare($a, $b, $matrix) {
$a: to-lower-case($a + unquote(""));
$b: to-lower-case($b + unquote(""));
$min: min(str-length($a), str-length($b));
@for $i from 1 through $min {
$char-a: str-slice($a, $i, $i);
$index-a: index($matrix, $char-a);
@if not $index-a {
$index-a: 1;
@warn "#{$char-a} from #{$a} not found in matrix.";
}
$char-b: str-slice($b, $i, $i);
$index-b: index($matrix, $char-b);
@if not $index-b {
$index-b: 1;
@warn "#{$char-b} from #{$b} not found in matrix.";
}
@if $index-a != $index-b { @return $index-a > $index-b; }
}
@return str-length($a) > str-length($b);
}
// Sorts values of $list
// -------------------------------------------------------------------------------
// @documentation http://sassylists.com/documentation/#sort
// -------------------------------------------------------------------------------
// @alias `order()`
// -------------------------------------------------------------------------------
// @dependence `last()`
// @dependence `insert-nth()`
// @dependence `_compare()` (utils)
// -------------------------------------------------------------------------------
// @example sort(8 abc 10 true aab 7 bac !important 2.8 aaa 4 14px acc 590 aaa)
// !important 2.8 4 7 8 10 14px 590 aaa aaa aab abc acc bac true
// -------------------------------------------------------------------------------
// @param $list [List] : list
// @param $matrix [List] : alphabetical order to follow
// -------------------------------------------------------------------------------
// @raise [Warning] if nested list found
// -------------------------------------------------------------------------------
// @return [List]
@function sort($list, $matrix: null) {
$missing-dependencies: dependencies("_compare", "insert-nth");
@if not $missing-dependencies {
$result: nth($list, 1);
$matrix: if($matrix != null, $matrix,
" " "!" "\"" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/" ":" ";" "<" "=" ">" "?" "@" "[" "\\" "]" "^" "_"
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
);
@for $i from 1 through length($list) {
$item: nth($list, $i);
@if length($item) == 1 {
$index: 0;
@for $j from 1 through length($result) {
@if _compare($item, nth($result, $j), $matrix) {
$index: $j;
}
}
$result: insert-nth($result, $index + 1, $item);
}
@else {
@warn "List found. Omitted.";
}
}
@return $result;
}
@else {
@warn "#{$missing-dependencies} missing for `sort`. Please make sure you're including all dependencies.";
@return false;
}
}
// Dependency checker
// -------------------------------------------------------------------------------
// @param $args [List] : name of required functions
// -------------------------------------------------------------------------------
// @return [List] | false
@function dependencies($args...) {
$result: ();
@each $function in $args {
@if not function_exists($function) {
$result: append($result, $function);
}
}
@return if($result == (), false, $result);
}
// Find keys from map for a given value
// -------------------------------------------------------------------------------
// @param $map [Map] : map
// @param $value [Literal] : value to look for in map
// -------------------------------------------------------------------------------
// @return [List] | false
@function find-keys($map, $value) {
$result: ();
@each $key in map-keys($map) {
@if map-get($map, $key) == $value {
$result: append($result, $key);
}
}
@return if($result == (), false, $result);
}
// Sort map based on keys
// -------------------------------------------------------------------------------
// @param $map [Map] : map
// -------------------------------------------------------------------------------
// @dependence `sort()`
// -------------------------------------------------------------------------------
// @return [Map]
@function sort-keys($map) {
$result: ();
@each $key in sort(map-keys($map)) {
$result: map-merge($result, ($key: map-get($map, $key)));
}
@return $result;
}
// Sort map based on values
// -------------------------------------------------------------------------------
// @param $map [Map] : map
// -------------------------------------------------------------------------------
// @dependence `sort()`
// @dependence `find-keys()`
// -------------------------------------------------------------------------------
// @return [Map]
@function sort-values($map) {
$result: ();
$tmp: ();
@each $value in sort(map-values($map)) {
$keys: find-keys($map, $value);
@each $key in $keys {
@if not index($tmp, $key) {
$result: map-merge($result, ($key: $value));
$tmp: append($tmp, $key);
}
}
}
@return $result;
}
// Sample
$mq-breakpoints: (
mobile: 200,
tablet: 400,
desktop: 600,
wide: 800,
// Tweakpoints
mobileLandscape: 300,
desktopAd: 400,
// Article layout
rightCol: 250,
leftCol: 550
);
// Demo
// Sorry, the debug() function from SassyLists displays maps as lists
map {
sorted-keys: debug(sort-keys($mq-breakpoints));
sorted-vals: debug(sort-values($mq-breakpoints));
}
map {
sorted-keys: "[ [ desktop, 600 ], [ desktopAd, 400 ], [ leftCol, 550 ], [ mobile, 200 ], [ mobileLandscape, 300 ], [ rightCol, 250 ], [ tablet, 400 ], [ wide, 800 ] ]";
sorted-vals: "[ [ mobile, 200 ], [ rightCol, 250 ], [ mobileLandscape, 300 ], [ tablet, 400 ], [ desktopAd, 400 ], [ leftCol, 550 ], [ desktop, 600 ], [ wide, 800 ] ]";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment