Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generated by SassMeister.com.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// ----
@function char-at(
$str,
$index: 1
) {
@return str-slice($str, $index, $index);
}
// String split
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
// Splits a String into a list of substrings using `$separator`.
// If separator is omitted, the list returned contains one element consisting of the entire string.
// If separator is an empty string, str is converted to a list of all characters.
// Integer specifying a limit on the number of splits to be found.
// The split method still splits on every match of separator, but it truncates the returned list to at most limit elements.
// --------
// @param [string] $str
// @param [string] $separator
// @param [$number] $limit
// --------
// @return [list]
@function str-split(
$str,
$separator: null,
$limit: null
) {
$result: zip(());
@if not $separator {
@return ($str,);
}
@if $separator == '' {
@for $i from 1 through str-length($str) {
$result: append($result, str-slice($str, $i, $i));
}
@return $result;
}
$running: true;
$progress: $str;
$length: str-length($separator);
@while $running {
$index: str-index($progress, $separator);
@if $index {
$result: append($result, str-slice($progress, 1, ($index - 1)));
$progress: str-slice($progress, ($index + $length));
}
@else {
$running: false;
}
}
$result: append($result, $progress);
@if $limit and $limit > 0 {
$limit: if($limit > length($result), length($result), $limit);
$return: ();
@for $i from 1 through $limit {
$return: append($return, nth($result, $i));
}
@return $return;
}
@return $result;
}
// Searches the map for a given value
// and returns the corresponding key if successful.
// --------
// @param [map] $map
// @param [literal] $value
// --------
// @return [literal]
@function map-search($map, $value) {
$keys: map-keys($map);
$values: map-values($map);
$index: index($values, $value);
@if not $index {
@warn "No value found for '#{$value}' in map.";
@return false;
}
@return nth($keys, $index);
}
$morse: (
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: '--..',
0: '-----',
1: '.----',
2: '..---',
3: '...--',
4: '....-',
5: '.....',
6: '-....',
7: '--...',
8: '---..',
9: '----.',
'.': '.-.-.-',
',': '--..--',
'?': '..--..',
'-': '-....-',
'=': '-...-',
':': '---...',
';': '-.-.-.',
'(': '-.--.',
')': '-.--.-',
'/': '-..-.',
'"': '.-..-.',
'$': '...-..-',
"'": '.----.',
'': '.-.-..',
'_': '..--.-',
'@': '.--.-.',
'!': '-.-.--',
'+': '.-.-.',
'~': '.-...',
'#': '...-.-',
'&': '. ...',
'': '-..-.',
' ': '/'
);
// Morse encoder.
// --------
// @param [string] $message
// --------
// @return [string]
@function morse-encode(
$message
) {
@if type-of($message) != string {
@warn '`$message` must be a string, #{type-of($message)} given.';
}
$length: str-length($message);
$result: '';
@for $i from 1 through $length {
$char: to-upper-case(char-at($message, $i));
$encoded: if(map-has-key($morse, $char), map-get($morse, $char), '?');
$result: $result + $encoded + if($i < $length, ' ', '')
}
@return $result;
}
// Morse decoder.
// --------
// @param [string] $message
// --------
// @return [string]
@function morse-decode(
$message
) {
@if type-of($message) != string {
@warn '`$message` must be a string, #{type-of($message)} given.';
}
$message: str-split($message, ' ');
$result: '';
@each $part in $message {
$char: map-search($morse, $part);
@if $char {
$result: $result + $char;
}
}
@return to-lower-case($result);
}
morse {
$fixture: (
'berlin': '-... . .-. .-.. .. -.',
'amiens': '.- -- .. . -. ...',
'sass all the things !': '... .- ... ... / .- .-.. .-.. / - .... . / - .... .. -. --. ... / -.-.--'
);
$tests: '';
@each $plain, $encoded in $fixture {
$pass: morse-encode($plain) == $encoded;
$tests: $tests + if($pass, '', 'X ');
}
tests: $tests;
$tests: '';
@each $plain, $encoded in $fixture {
$pass: morse-decode($encoded) == $plain;
$tests: $tests + if($pass, '', 'X ');
}
tests: $tests;
}
@charset "UTF-8";
morse {
tests: "✔ ✔ ✔ ";
tests: "✔ ✔ ✔ ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.