Skip to content

Commit

Permalink
remove ifNaN
Browse files Browse the repository at this point in the history
NaN || value === value
  • Loading branch information
ShaMan123 committed Aug 24, 2022
1 parent 378ab72 commit 684e2c9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
7 changes: 3 additions & 4 deletions src/gradient/parser/parseColorStops.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Color } from "../../color";
import { parsePercent } from "../../parser/percent";
import { ifNaN } from "../../util/internals";

const RE_KEY_VALUE_PAIRS = /\s*;\s*/;
const RE_KEY_VALUE = /\s*:\s*/;
Expand Down Expand Up @@ -29,16 +28,16 @@ function parseColorStop(el: SVGStopElement, multiplier: number) {
const color = new Color(colorValue || el.getAttribute('stop-color') || 'rgb(0,0,0)');

return {
offset: parsePercent(el.getAttribute('offset'), 0),
offset: parsePercent(el.getAttribute('offset')) || 0,
color: color.toRgb(),
opacity: ifNaN(parseFloat(opacity || el.getAttribute('stop-opacity') || ''), 1) * color.getAlpha() * multiplier
opacity: (parseFloat(opacity || el.getAttribute('stop-opacity') || '') || 1) * color.getAlpha() * multiplier
};
}

export function parseColorStops(el: SVGGradientElement, opacityAttr: string | null) {
const colorStops = [],
colorStopEls = el.getElementsByTagName('stop'),
multiplier = parsePercent(opacityAttr, 1);
multiplier = parsePercent(opacityAttr) || 1;
for (let i = colorStopEls.length; i--;) {
colorStops.push(parseColorStop(colorStopEls[i], multiplier));
}
Expand Down
7 changes: 3 additions & 4 deletions src/parser/percent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { clamp } from "../util";
import { ifNaN } from "../util/internals";
import { capValue } from "../util/misc/capValue";

const RE_PERCENT = /^(\d+\.\d+)%|(\d+)%$/;

Expand All @@ -13,11 +12,11 @@ export function isPercent(value: string | null) {
* @param valueIfNaN
* @returns ∈ [0, 1]
*/
export function parsePercent(value: string | number | null | undefined, valueIfNaN?: number) {
export function parsePercent(value: string | number | null | undefined) {
const parsed = typeof value === 'number' ?
value :
typeof value === 'string' ?
parseFloat(value) / (isPercent(value) ? 100 : 1) :
NaN;
return clamp(0, ifNaN(parsed, valueIfNaN), 1)
return capValue(0, parsed, 1)
}
10 changes: 0 additions & 10 deletions src/util/internals/ifNaN.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/util/internals/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { getRandomInt } from './getRandomInt';
export { ifNaN } from './ifNaN';
export { removeFromArray } from './removeFromArray';

0 comments on commit 684e2c9

Please sign in to comment.