-
Notifications
You must be signed in to change notification settings - Fork 347
/
unitless.dart
145 lines (114 loc) · 4.18 KB
/
unitless.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:meta/meta.dart';
import 'package:tuple/tuple.dart';
import '../../util/number.dart';
import '../../value.dart';
/// A specialized subclass of [SassNumber] for numbers that have no units.
///
/// {@category Value}
@sealed
class UnitlessSassNumber extends SassNumber {
List<String> get numeratorUnits => const [];
List<String> get denominatorUnits => const [];
bool get hasUnits => false;
UnitlessSassNumber(num value, [Tuple2<SassNumber, SassNumber>? asSlash])
: super.protected(value, asSlash);
SassNumber withValue(num value) => UnitlessSassNumber(value);
SassNumber withSlash(SassNumber numerator, SassNumber denominator) =>
UnitlessSassNumber(value, Tuple2(numerator, denominator));
bool hasUnit(String unit) => false;
bool hasCompatibleUnits(SassNumber other) => other is UnitlessSassNumber;
@internal
bool hasPossiblyCompatibleUnits(SassNumber other) =>
other is UnitlessSassNumber;
bool compatibleWithUnit(String unit) => true;
SassNumber coerceToMatch(SassNumber other,
[String? name, String? otherName]) =>
other.withValue(value);
num coerceValueToMatch(SassNumber other, [String? name, String? otherName]) =>
value;
SassNumber convertToMatch(SassNumber other,
[String? name, String? otherName]) =>
other.hasUnits
// Call this to generate a consistent error message.
? super.convertToMatch(other, name, otherName)
: this;
num convertValueToMatch(SassNumber other,
[String? name, String? otherName]) =>
other.hasUnits
// Call this to generate a consistent error message.
? super.convertValueToMatch(other, name, otherName)
: value;
SassNumber coerce(List<String> newNumerators, List<String> newDenominators,
[String? name]) =>
SassNumber.withUnits(value,
numeratorUnits: newNumerators, denominatorUnits: newDenominators);
num coerceValue(List<String> newNumerators, List<String> newDenominators,
[String? name]) =>
value;
num coerceValueToUnit(String unit, [String? name]) => value;
SassBoolean greaterThan(Value other) {
if (other is SassNumber) {
return SassBoolean(fuzzyGreaterThan(value, other.value));
}
return super.greaterThan(other);
}
SassBoolean greaterThanOrEquals(Value other) {
if (other is SassNumber) {
return SassBoolean(fuzzyGreaterThanOrEquals(value, other.value));
}
return super.greaterThanOrEquals(other);
}
SassBoolean lessThan(Value other) {
if (other is SassNumber) {
return SassBoolean(fuzzyLessThan(value, other.value));
}
return super.lessThan(other);
}
SassBoolean lessThanOrEquals(Value other) {
if (other is SassNumber) {
return SassBoolean(fuzzyLessThanOrEquals(value, other.value));
}
return super.lessThanOrEquals(other);
}
Value modulo(Value other) {
if (other is SassNumber) {
return other.withValue(moduloLikeSass(value, other.value));
}
return super.modulo(other);
}
Value plus(Value other) {
if (other is SassNumber) {
return other.withValue(value + other.value);
}
return super.plus(other);
}
Value minus(Value other) {
if (other is SassNumber) {
return other.withValue(value - other.value);
}
return super.minus(other);
}
Value times(Value other) {
if (other is SassNumber) {
return other.withValue(value * other.value);
}
return super.times(other);
}
Value dividedBy(Value other) {
if (other is SassNumber) {
return other.hasUnits
? SassNumber.withUnits(value / other.value,
numeratorUnits: other.denominatorUnits,
denominatorUnits: other.numeratorUnits)
: UnitlessSassNumber(value / other.value);
}
return super.dividedBy(other);
}
Value unaryMinus() => UnitlessSassNumber(-value);
bool operator ==(Object other) =>
other is UnitlessSassNumber && fuzzyEquals(value, other.value);
int get hashCode => hashCache ??= fuzzyHashCode(value);
}