Fix a couple number hashing bugs

This commit is contained in:
Natalie Weizenbaum 2018-01-12 16:07:01 -08:00
parent b5a838c9c5
commit e5af175b55
3 changed files with 15 additions and 3 deletions

View File

@ -8,6 +8,9 @@
* `var()` may now be passed in place of multiple arguments to `rgb()`, `rgba()`,
`hsl()` and `hsla()`.
* Fix some cases where equivalent numbers wouldn't count as the same keys in
maps.
### Dart API
* Add a `functions` parameter to `compile()`, `compleString()`,

View File

@ -8,14 +8,23 @@ import '../value.dart';
/// The maximum distance two Sass numbers are allowed to be from one another
/// before they're considered different.
final epsilon = 1 / (math.pow(10, SassNumber.precision));
final epsilon = 1 / math.pow(10, SassNumber.precision);
/// `epsilon / 2`, cached since [math.pow] may not be computed at compile-time
/// and thus this probably won't be constant-folded.
final _epsilonOver2 = epsilon / 2;
/// Returns whether [number1] and [number2] are equal within [epsilon].
bool fuzzyEquals(num number1, num number2) =>
(number1 - number2).abs() < epsilon;
/// Returns a hash code for [number] that matches [fuzzyEquals].
int fuzzyHashCode(num number) => (number % epsilon).hashCode;
int fuzzyHashCode(num number) {
var remainder = number % epsilon;
var truncated = number - remainder;
if (remainder >= _epsilonOver2) truncated += epsilon;
return truncated.hashCode;
}
/// Returns whether [number1] is less than [number2], and not [fuzzyEquals].
bool fuzzyLessThan(num number1, num number2) =>

View File

@ -612,7 +612,7 @@ class SassNumber extends Value {
var innerMap = _conversions[unit];
return innerMap == null
? multiplier
: multiplier * innerMap.values.first;
: multiplier / innerMap.values.first;
});
/// Throws a [SassScriptException] with the given [message].