From e0d2692f392e970330b0181e0c80ff0556d58ea9 Mon Sep 17 00:00:00 2001 From: Rob Walch Date: Wed, 6 Dec 2023 15:01:04 -0800 Subject: [PATCH] Add polyfill for isSafeInteger --- build-config.js | 8 ++++++++ src/polyfills/number.ts | 8 ++++++++ src/utils/mp4-tools.ts | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/build-config.js b/build-config.js index 33c969e0b67..d0a3a233c2c 100644 --- a/build-config.js +++ b/build-config.js @@ -147,6 +147,14 @@ const babelTsWithPresetEnvTargets = ({ targets, stripConsole }) => 'isFiniteNumber', path.resolve('src/polyfills/number'), ); + } else if ( + espath.get('callee').matchesPattern('Number.isSafeInteger') + ) { + espath.node.callee = importHelper.addNamed( + espath, + 'isSafeInteger', + path.resolve('src/polyfills/number'), + ); } else if ( espath.get('callee').matchesPattern('Number.MAX_SAFE_INTEGER') ) { diff --git a/src/polyfills/number.ts b/src/polyfills/number.ts index 61799aab851..de2854d8e12 100644 --- a/src/polyfills/number.ts +++ b/src/polyfills/number.ts @@ -1,7 +1,15 @@ +// https://caniuse.com/mdn-javascript_builtins_number_isfinite export const isFiniteNumber = Number.isFinite || function (value) { return typeof value === 'number' && isFinite(value); }; +// https://caniuse.com/mdn-javascript_builtins_number_issafeinteger +export const isSafeInteger = + Number.isSafeInteger || + function (value) { + return typeof value === 'number' && Math.abs(value) <= MAX_SAFE_INTEGER; + }; + export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; diff --git a/src/utils/mp4-tools.ts b/src/utils/mp4-tools.ts index 57a8a89433b..0216d1b180b 100644 --- a/src/utils/mp4-tools.ts +++ b/src/utils/mp4-tools.ts @@ -548,7 +548,7 @@ export function getStartDTS( // convert base time to seconds const startTime = baseTime / scale; if ( - isFinite(startTime) && + Number.isFinite(startTime) && (result === null || startTime < result) ) { return startTime; @@ -560,7 +560,7 @@ export function getStartDTS( ); if ( start !== null && - isFinite(start) && + Number.isFinite(start) && (result === null || start < result) ) { return start;