Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Last active September 9, 2023 23:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pascalduez/356aa6366ef0f077359f to your computer and use it in GitHub Desktop.
Save pascalduez/356aa6366ef0f077359f to your computer and use it in GitHub Desktop.
JavaScript `toString` and `parseInt` implementation in Sass.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// ----
// JavaScript `toString` and `parseInt` implementation in Sass.
@function pow($num, $exp) {
$result: 1;
@if $exp > 0 {
@for $i from 1 through $exp {
$result: $result * $num;
}
}
@else {
@for $i from $exp to 0 {
$result: $result / $num;
}
}
@return $result;
}
@function charsFromBase($base: 10) {
$chars:(
// Binary
2: '01',
// Octal
8: '01234567',
// Decimal
10: '0123456789',
// Hexadecimal
16: '0123456789abcdef',
// Base 36
36: '0123456789abcdefghijklmnopqrstuvwxyz',
// Base 64
64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
);
@if not map-has-key($chars, $base) {
@warn "There is no base `#{$base}` available.";
}
@return map-get($chars, $base);
}
@function toString($num, $radix: 10) {
$chars: charsFromBase($radix);
$result: '';
$sign: '';
@if $num < 0 {
$sign: '-';
$num: abs($num);
}
@if $num >= 0 and $num < $radix {
@return $sign + str-slice($chars, ($num + 1), ($num + 1));
}
$q: $num;
@while $q != 0 {
$r: $q % $radix;
$q: floor($q / $radix);
$result: str-slice($chars, ($r + 1), ($r + 1)) + $result;
}
@return $sign + $result;
}
@function parseInt($str, $radix: 10) {
$chars: charsFromBase($radix);
$result: 0;
$is-negative: str-index($str, '-') == 1;
@if $is-negative {
$str: str-slice($str, 2);
}
@for $i from 1 through str-length($str) {
$char: str-slice($str, -$i, -$i);
$value: str-index($chars, $char) - 1;
$result: $result + ($value * pow($radix, ($i - 1)));
}
@return if($is-negative, -$result, $result);
}
// Tests
test__binary {
$fixture: (
'-1': -1,
'0': 0,
'1': 1,
'10': 2,
'11': 3,
'100': 4,
'101': 5,
'110': 6,
'111': 7,
'1000': 8,
'1001': 9,
'1010': 10,
'11110010101111': 15535,
'1001001000111100': 37436,
'111001101101100001100100111001': 968235321
);
$test-parseInt: '';
$test-toString: '';
@each $str, $num in $fixture {
$test-parseInt: $test-parseInt + if(parseInt($str, 2) == $num, '✔ ', 'X ');
$test-toString: $test-toString + if(toString($num, 2) == $str, '✔ ', 'X ');
}
test-parseInt: $test-parseInt;
test-toString: $test-toString;
}
test__hexadecimal {
$fixture: (
'-1': -1,
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'a': 10,
'3caf': 15535,
'923c': 37436,
'39b61939': 968235321
);
$test-parseInt: '';
$test-toString: '';
@each $str, $num in $fixture {
$test-parseInt: $test-parseInt + if(parseInt($str, 16) == $num, '✔ ', 'X ');
$test-toString: $test-toString + if(toString($num, 16) == $str, '✔ ', 'X ');
}
test-parseInt: $test-parseInt;
test-toString: $test-toString;
}
test__base36 {
$fixture: (
'-1': -1,
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'a': 10,
'bzj': 15535,
'svw': 37436,
'g0gn5l': 968235321
);
$test-parseInt: '';
$test-toString: '';
@each $str, $num in $fixture {
$test-parseInt: $test-parseInt + if(parseInt($str, 36) == $num, '✔ ', 'X ');
$test-toString: $test-toString + if(toString($num, 36) == $str, '✔ ', 'X ');
}
test-parseInt: $test-parseInt;
test-toString: $test-toString;
}
@charset "UTF-8";
test__binary {
test-parseInt: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
test-toString: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
}
test__hexadecimal {
test-parseInt: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
test-toString: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
}
test__base36 {
test-parseInt: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
test-toString: "✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment