Skip to content

Commit 744cd7f

Browse files
committed
Remove nullToNaN
1 parent e2d640c commit 744cd7f

2 files changed

Lines changed: 32 additions & 41 deletions

File tree

lib/src/value/color.ts

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ function getColorSpace(options: ChannelOptions): KnownColorSpace {
117117
throw valueError('No color space found');
118118
}
119119

120-
/**
121-
* Convert from the ColorJS representation of a missing component (`null`) to
122-
* `NaN`.
123-
*/
124-
function nullToNaN(val: number | null): number {
125-
return val ?? NaN;
126-
}
127-
128120
/**
129121
* Convert from the ColorJS representation of a missing component (`null`) to
130122
* `0`.
@@ -650,8 +642,7 @@ export class SassColor extends Value {
650642
*/
651643
get red(): number {
652644
emitColor4ApiGetterDeprecation('red');
653-
const val = nullToZero(coordToRgb(this.color.srgb.red));
654-
return fuzzyRound(val);
645+
return fuzzyRound(coordToRgb(this.color.srgb.red)) ?? 0;
655646
}
656647

657648
/**
@@ -661,8 +652,7 @@ export class SassColor extends Value {
661652
*/
662653
get green(): number {
663654
emitColor4ApiGetterDeprecation('green');
664-
const val = nullToZero(coordToRgb(this.color.srgb.green));
665-
return fuzzyRound(val);
655+
return fuzzyRound(coordToRgb(this.color.srgb.green)) ?? 0;
666656
}
667657

668658
/**
@@ -672,8 +662,7 @@ export class SassColor extends Value {
672662
*/
673663
get blue(): number {
674664
emitColor4ApiGetterDeprecation('blue');
675-
const val = nullToZero(coordToRgb(this.color.srgb.blue));
676-
return fuzzyRound(val);
665+
return fuzzyRound(coordToRgb(this.color.srgb.blue)) ?? 0;
677666
}
678667

679668
/**
@@ -1157,49 +1146,47 @@ export class SassColor extends Value {
11571146
coords = this.color
11581147
.to('srgb')
11591148
.coords.map(coordToRgb)
1160-
.map(nullToNaN)
1161-
.map(fuzzyRound) as [number, number, number];
1149+
.map(fuzzyRound) as [number | null, number | null, number | null];
11621150
otherCoords = other.color
11631151
.to('srgb')
11641152
.coords.map(coordToRgb)
1165-
.map(nullToNaN)
1166-
.map(fuzzyRound) as [number, number, number];
1153+
.map(fuzzyRound) as [number | null, number | null, number | null];
11671154
}
11681155
return (
1169-
fuzzyEquals(nullToNaN(coords[0]), nullToNaN(otherCoords[0])) &&
1170-
fuzzyEquals(nullToNaN(coords[1]), nullToNaN(otherCoords[1])) &&
1171-
fuzzyEquals(nullToNaN(coords[2]), nullToNaN(otherCoords[2]))
1156+
fuzzyEquals(coords[0], otherCoords[0]) &&
1157+
fuzzyEquals(coords[1], otherCoords[1]) &&
1158+
fuzzyEquals(coords[2], otherCoords[2])
11721159
);
11731160
}
11741161
return (
11751162
this.space === other.space &&
1176-
fuzzyEquals(nullToNaN(coords[0]), nullToNaN(otherCoords[0])) &&
1177-
fuzzyEquals(nullToNaN(coords[1]), nullToNaN(otherCoords[1])) &&
1178-
fuzzyEquals(nullToNaN(coords[2]), nullToNaN(otherCoords[2])) &&
1163+
fuzzyEquals(coords[0], otherCoords[0]) &&
1164+
fuzzyEquals(coords[1], otherCoords[1]) &&
1165+
fuzzyEquals(coords[2], otherCoords[2]) &&
11791166
fuzzyEquals(this.alpha, other.alpha)
11801167
);
11811168
}
11821169

11831170
hashCode(): number {
11841171
let coords = this.color.coords;
11851172
if (this.isLegacy) {
1186-
coords = this.color
1187-
.to('srgb')
1188-
.coords.map(coordToRgb)
1189-
.map(nullToNaN)
1190-
.map(fuzzyRound) as [number, number, number];
1173+
coords = this.color.to('srgb').coords.map(coordToRgb).map(fuzzyRound) as [
1174+
number,
1175+
number,
1176+
number,
1177+
];
11911178
return (
1192-
fuzzyHashCode(nullToNaN(coords[0])) ^
1193-
fuzzyHashCode(nullToNaN(coords[1])) ^
1194-
fuzzyHashCode(nullToNaN(coords[2])) ^
1179+
fuzzyHashCode(coords[0]) ^
1180+
fuzzyHashCode(coords[1]) ^
1181+
fuzzyHashCode(coords[2]) ^
11951182
fuzzyHashCode(this.alpha)
11961183
);
11971184
}
11981185
return (
11991186
hash(this.space) ^
1200-
fuzzyHashCode(nullToNaN(coords[0])) ^
1201-
fuzzyHashCode(nullToNaN(coords[1])) ^
1202-
fuzzyHashCode(nullToNaN(coords[2])) ^
1187+
fuzzyHashCode(coords[0]) ^
1188+
fuzzyHashCode(coords[1]) ^
1189+
fuzzyHashCode(coords[2]) ^
12031190
fuzzyHashCode(this.alpha)
12041191
);
12051192
}

lib/src/value/utils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export const precision = 10;
1616
const epsilon = 10 ** (-precision - 1);
1717

1818
/** Whether `num1` and `num2` are equal within `epsilon`. */
19-
export function fuzzyEquals(num1: number, num2: number): boolean {
19+
export function fuzzyEquals(num1: number | null, num2: number | null): boolean {
20+
if (num1 === null || num2 === null) {
21+
return false;
22+
}
2023
return Math.abs(num1 - num2) < epsilon;
2124
}
2225

@@ -25,10 +28,8 @@ export function fuzzyEquals(num1: number, num2: number): boolean {
2528
*
2629
* Two numbers that `fuzzyEquals` each other must have the same hash code.
2730
*/
28-
export function fuzzyHashCode(num: number): number {
29-
return !isFinite(num) || isNaN(num)
30-
? hash(num)
31-
: hash(Math.round(num / epsilon));
31+
export function fuzzyHashCode(num: number | null): number {
32+
return num === null ? hash(num) : hash(Math.round(num / epsilon));
3233
}
3334

3435
/** Whether `num1` < `num2`, within `epsilon`. */
@@ -72,7 +73,10 @@ export function fuzzyAsInt(num: number): number | null {
7273
*
7374
* If `num` `fuzzyEquals` `x.5`, rounds away from zero.
7475
*/
75-
export function fuzzyRound(num: number): number {
76+
export function fuzzyRound(num: number | null): number | null {
77+
if (num === null) {
78+
return num;
79+
}
7680
if (num > 0) {
7781
return fuzzyLessThan(num % 1, 0.5) ? Math.floor(num) : Math.ceil(num);
7882
} else {

0 commit comments

Comments
 (0)