|
| 1 | +@function pfe-strip-unit($num) { |
| 2 | + @return $num / ($num * 0 + 1); |
| 3 | +} |
| 4 | + |
| 5 | +@function pfe-size-pem($pxval, $base: $pfe-global--font-size-root) { |
| 6 | + @return pfe-strip-unit($pxval) / $base * 1em; |
| 7 | +} |
| 8 | + |
| 9 | +@function pfe-size-prem($pxval, $base: $pfe-global--font-size-root) { |
| 10 | + @return pfe-strip-unit($pxval) / $base * 1rem; |
| 11 | +} |
| 12 | + |
| 13 | +/// Map deep get |
| 14 | +/// @author Hugo Giraudel |
| 15 | +/// @access public |
| 16 | +/// @param {Map} $map - Map |
| 17 | +/// @param {Arglist} $keys - Key chain |
| 18 | +/// @return {*} - Desired value |
| 19 | +@function map-deep-get($map, $keys...) { |
| 20 | + @each $key in $keys { |
| 21 | + @if type-of($map) == "map" { |
| 22 | + $map: map-get($map, $key); |
| 23 | + } |
| 24 | + @else { |
| 25 | + @warn "Map provided is not a map." |
| 26 | + } |
| 27 | + } |
| 28 | + @return $map; |
| 29 | +} |
| 30 | + |
| 31 | +// Collect all maps and merge them together |
| 32 | +@function map-collect($maps...) { |
| 33 | + $collection: (); |
| 34 | + @each $map in $maps { |
| 35 | + $collection: map-merge($collection, $map); |
| 36 | + } |
| 37 | + @return $collection; |
| 38 | +} |
| 39 | + |
| 40 | +// New color function to only return theme colors |
| 41 | +@function color( $name, $theme: "light", $opacity: 1 ) { |
| 42 | + $map: map-get($color-map, $name); |
| 43 | + $error: false; |
| 44 | + $color: null; |
| 45 | + @if $map != null { |
| 46 | + $color: map-get($map, $theme); |
| 47 | + } |
| 48 | + @else { |
| 49 | + $error: true; |
| 50 | + } |
| 51 | + @if type-of($color) == color { |
| 52 | + @if $opacity == 1 { |
| 53 | + @return $color; |
| 54 | + } |
| 55 | + @if $opacity < 1 { |
| 56 | + @return rgba( $color, $opacity ); |
| 57 | + } |
| 58 | + } |
| 59 | + @else { |
| 60 | + $error: true; |
| 61 | + } |
| 62 | + @if $error { |
| 63 | + @warn "#{$name} is not a valid color"; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// https://hugogiraudel.com/2013/08/08/advanced-sass-list-functions/ |
| 68 | +// Get the first item in a list |
| 69 | +@function first($list) { |
| 70 | + @return nth($list, 1); |
| 71 | +} |
| 72 | + |
| 73 | +// Get the last item in a list |
| 74 | +@function last($list) { |
| 75 | + @return nth($list, length($list)); |
| 76 | +} |
| 77 | + |
| 78 | +@function str-replace($string, $search, $replace: "") { |
| 79 | + @if type-of($string) == "string" and type-of($search) == "string" { |
| 80 | + $index: str-index($string, $search); |
| 81 | + @if $index { |
| 82 | + @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); |
| 83 | + } |
| 84 | + } |
| 85 | + @return $string; |
| 86 | +} |
| 87 | + |
| 88 | +@function str-split($string, $separator) { |
| 89 | + // empty array/list |
| 90 | + $split-arr: (); |
| 91 | + // first index of separator in string |
| 92 | + $index: str-index("#{$string}", $separator); |
| 93 | + // loop through string |
| 94 | + @while $index != null { |
| 95 | + // get the substring from the first character to the separator |
| 96 | + $item: str-slice($string, 1, $index - 1); |
| 97 | + // push item to array |
| 98 | + $split-arr: append($split-arr, $item); |
| 99 | + // remove item and separator from string |
| 100 | + $string: str-slice($string, $index + 1); |
| 101 | + // find new index of separator |
| 102 | + $index: str-index($string, $separator); |
| 103 | + } |
| 104 | + // add the remaining string to list (the last item) and return |
| 105 | + @return append($split-arr, $string); |
| 106 | +} |
| 107 | + |
| 108 | +// https://github.com/HugoGiraudel/SassyStrings/blob/master/dist/_SassyStrings.scss |
| 109 | +/// Check whether `$string` stars with `$needle`. |
| 110 | +/// @param {String} $string - string to check |
| 111 | +/// @param {String} $needle - substring to check |
| 112 | +/// @return {Bool} |
| 113 | +@function str-starts-with($string, $needle) { |
| 114 | + @return str-slice($string, 1, str-length($needle)) == $needle; |
| 115 | +} |
| 116 | + |
| 117 | +// https://hugogiraudel.com/2013/08/08/advanced-sass-list-functions/#removing-values-from-list |
| 118 | +@function remove($list, $value, $recursive: false) { |
| 119 | + $result: (); |
| 120 | + @for $i from 1 through length($list) { |
| 121 | + @if type-of(nth($list, $i)) == list and $recursive { |
| 122 | + $result: append($result, remove(nth($list, $i), $value, $recursive)); |
| 123 | + } |
| 124 | + @else if nth($list, $i) != $value { |
| 125 | + $result: append($result, nth($list, $i)); |
| 126 | + } |
| 127 | + } |
| 128 | + @return $result; |
| 129 | +} |
0 commit comments