Zizzamia/perfume.js

View on GitHub
src/utils.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { W } from './constants';

export const roundByFour = (num: number) => {
  return parseFloat(num.toFixed(4));
}

export const convertToKB = (bytes: number): number | null => {
  if (typeof bytes !== 'number') {
    return null;
  }
  return roundByFour(bytes / Math.pow(1024, 2));
};

/**
 * PushTask to requestIdleCallback
 */
export const pushTask = (cb: any): void => {
  if ('requestIdleCallback' in W) {
    (W as any).requestIdleCallback(cb, { timeout: 3000 });
  } else {
    cb();
  }
};