Skip to content

Commit

Permalink
Add polyfill for isSafeInteger
Browse files Browse the repository at this point in the history
(cherry picked from commit e0d2692)
  • Loading branch information
robwalch committed Dec 7, 2023
1 parent 62e2db2 commit f29943e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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')
) {
Expand Down
8 changes: 8 additions & 0 deletions src/polyfills/number.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,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;
Expand All @@ -396,7 +396,7 @@ export function getStartDTS(
);
if (
start !== null &&
isFinite(start) &&
Number.isFinite(start) &&
(result === null || start < result)
) {
return start;
Expand Down

0 comments on commit f29943e

Please sign in to comment.