/**
 * 將數字轉換成簡寫的形式，設定 dhms 的數值
 * @param {number} totalSeconds
 * @returns {string}
 */
export default function abbreviateNumber(totalSeconds) {
  let seconds = 0;
  let minutes = 0;
  let hours = 0;
  let days = 0;
  let result = '';
  let symbols = ['d', 'h', 'm', 's'];

  totalSeconds = parseInt(totalSeconds);
  if(!isNaN(totalSeconds)) {
    seconds = totalSeconds % 60;
    minutes = (Math.floor(totalSeconds - seconds) / 60) % 60;
    hours = (Math.floor(totalSeconds / 3600)) % 24;
    days = Math.floor(totalSeconds / (3600 * 24));
  };

  const units = [days, hours, minutes, seconds];
  for(let i = 0; i < units.length; i++) {
    if(units[i] > 0) result += units[i] + symbols[i] + " ";
  }
  result.trim();
  if(totalSeconds === 0) result = '0';

  return result;
};
