From a2b915eb4df6d24339fecc6cc22326003eefafe5 Mon Sep 17 00:00:00 2001 From: Joshua T Kalis Date: Tue, 3 Dec 2024 09:56:04 -0500 Subject: [PATCH] refactor: clean up of some todos --- packages/predicates/src/is-latitude/index.ts | 8 +++----- packages/predicates/src/is-longitude/index.ts | 8 +++----- packages/predicates/src/is-number/index.ts | 8 ++++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/predicates/src/is-latitude/index.ts b/packages/predicates/src/is-latitude/index.ts index 8ed729c2..9af85ef6 100644 --- a/packages/predicates/src/is-latitude/index.ts +++ b/packages/predicates/src/is-latitude/index.ts @@ -12,8 +12,7 @@ import { isFiniteNumber } from '../is-number'; -const MIN_LATITUDE = -90; -const MAX_LATITUDE = 90; +const MAX_ABSOLUTE_VALUE = 90; /** * Determines if given value is a valid latitude range. @@ -32,8 +31,7 @@ export function isLatitude(val: unknown) { return false; } - // TODO: is there a better way to tell TS this is a number now based on isFiniteNumeber? - const ensuredValue = val as number; + const abs = Math.abs(val); - return ensuredValue >= MIN_LATITUDE && ensuredValue <= MAX_LATITUDE; + return abs >= 0 && abs <= MAX_ABSOLUTE_VALUE; } diff --git a/packages/predicates/src/is-longitude/index.ts b/packages/predicates/src/is-longitude/index.ts index 032e75d2..dd900756 100644 --- a/packages/predicates/src/is-longitude/index.ts +++ b/packages/predicates/src/is-longitude/index.ts @@ -12,8 +12,7 @@ import { isFiniteNumber } from '../is-number'; -const MIN_LONGITUDE = -180; -const MAX_LONGITUDE = 180; +const MAX_ABSOLUTE_VALUE = 180; /** * Determines if given value is a valid longitude range. @@ -32,8 +31,7 @@ export function isLongitude(val: unknown) { return false; } - // TODO: is there a better way to tell TS this is a number now based on isFiniteNumeber? - const ensuredValue = val as number; + const abs = Math.abs(val); - return ensuredValue >= MIN_LONGITUDE && ensuredValue <= MAX_LONGITUDE; + return abs >= 0 && abs <= MAX_ABSOLUTE_VALUE; } diff --git a/packages/predicates/src/is-number/index.ts b/packages/predicates/src/is-number/index.ts index 6e5f9f3c..32d87271 100644 --- a/packages/predicates/src/is-number/index.ts +++ b/packages/predicates/src/is-number/index.ts @@ -23,7 +23,7 @@ import { isString } from '../is-string'; * isNumber(NaN) // true * isNumber('1.23') // false */ -export function isNumber(val: unknown) { +export function isNumber(val: unknown): val is number { return typeof val === 'number' || val instanceof Number; } @@ -38,7 +38,7 @@ export function isNumber(val: unknown) { * isFiniteNumber(NaN) // false * isFiniteNumber('1.23') // false */ -export function isFiniteNumber(val: unknown) { +export function isFiniteNumber(val: unknown): val is number { return Number.isFinite(val); } @@ -54,7 +54,7 @@ export function isFiniteNumber(val: unknown) { * isFiniteNumeric('1.23') // true * isFiniteNumeric('hi') // false */ -export function isFiniteNumeric(val: unknown) { +export function isFiniteNumeric(val: unknown): val is number { const parsed = isString(val) ? Number.parseFloat(val as string) : val; return !Number.isNaN(parsed) && isFiniteNumber(parsed); @@ -72,7 +72,7 @@ export function isFiniteNumeric(val: unknown) { * isNumeric('1.23') // true * isNumeric('hi') // false */ -export function isNumeric(val: unknown) { +export function isNumeric(val: unknown): val is number { if (isString(val)) { return ( val === 'Infinity' ||