From 7d3fd9e56e6f728347cfaf81e59edf681695cb52 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 9 Jul 2024 12:49:51 -0300 Subject: [PATCH] Sprite.prototype.toNumber(value, true) - strict cast to number --- src/Sprite.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Sprite.ts b/src/Sprite.ts index 9ae6392..9c5c1bc 100644 --- a/src/Sprite.ts +++ b/src/Sprite.ts @@ -394,18 +394,35 @@ abstract class SpriteBase { return this._project.loudness; } - public toNumber(value: unknown): number { + // Strict mode uses essentially the same logic as compare() uses when treating + // the sides as numbers. It exists to handle NaN or NaN-casting values inside + // a normal "===" comparison, preferring "===" when possible, to writing all + // numeric comparisons with this.compare(). + public toNumber(value: unknown, strict: boolean = false): number { + if (Number.isNaN(value)) { + return strict ? NaN : 0; + } + if (typeof value === "number") { - if (isNaN(value)) { - return 0; - } return value; } const n = Number(value); + if (Number.isNaN(n)) { - return 0; + return strict ? NaN : 0; } + + if (strict && n === 0) { + if (value === null) { + return null; + } + + if (typeof value === "string" && value.trim().length === 0) { + return NaN; + } + } + return n; }