jk-gan / redux-flipper

Redux middleware for React Native for Flipper
MIT License
160 stars 21 forks source link

Feature request: Remove dependency to dayjs #69

Open lundn opened 1 year ago

lundn commented 1 year ago

Request feature This module uses dayjs to output two timestamps in the HH:mm:ss.SSS format. It seems unnecessary to include a dependency on dayjs for this simple formatting.

Possible solution Writing a custom function to format a timestamp based on milliseconds, in the HH:mm:ss.SSS format, you can remove one dependency in the module.

const padToGivenDigits = (number: number, pad: number) => {
  return String(number).padStart(pad, '0');
};

const formatTimestampByMilliseconds = (milliseconds: number) => {
  try {
    const date = new Date(milliseconds);

    return `${[
      padToGivenDigits(date.getHours(), 2),
      padToGivenDigits(date.getMinutes(), 2),
      padToGivenDigits(date.getSeconds(), 2),
    ].join(':')}.${padToGivenDigits(date.getMilliseconds(), 3)}`;
  } catch (_) {
    //Swallow error
    return '';
  }
};

const startTime = 1667375896785 //Date.now();
const timestamp = formatTimestampByMilliseconds(startTime)
// timestamp -> 07:58:16.785