Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing remainingTime module #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 21 additions & 51 deletions src/modules/remainingTime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,59 +85,29 @@ function remainingTime(date: string | number | Date): RemainingTime & ToString &
}

/**
*
* @param remainingTime contains years, months, days, hours, minutes and seconds remianed to a specific date
* @returns string that shows remained time in persian.
* when the year is zero we are not going to show the user `سال`
* when the year is zero and the month is also zero we are not going to show both `سال` and `ماه`
* when the year, month and hour are zero we are going to omit related phrases from the output.
* when the year, month, hour and minute are zero we only show the seconds remianed in output.
*/
* @returns Converts the remaining time to a Persian string representation.
* Persian string representation of the remaining time, showing non-zero fields with their respective units.
*/
const toString = (remainingTime: RemainingTime): string => {
const { faYears, faMonths, faDays, faHours, faMinutes, faSeconds } = convertToFaDigit(remainingTime);
const { years, months, days, hours, minutes } = remainingTime;

const remainingTimeInSeconds =
years * secondsInYear +
months * secondsInMonth +
days * secondsInDay +
hours * secondsInHour +
minutes * secondsInMinute;

const year = remainingTimeInSeconds < secondsInYear ? `` : `${faYears} سال و `;
const mongth = remainingTimeInSeconds < secondsInMonth ? `` : `${faMonths} ماه و `;
const day = remainingTimeInSeconds < secondsInDay ? `` : `${faDays} روز و `;
const hour = remainingTimeInSeconds < secondsInHour ? `` : `${faHours} ساعت و `;
const minute = remainingTimeInSeconds < secondsInMinute ? `` : `${faMinutes} دقیقه و `;
const second = `${faSeconds} ثانیه`;

return year + mongth + day + hour + minute + second;
const { years, months, days, hours, minutes , seconds } = remainingTime;
const result :string[] = [];

if (years > 0)
result.push(`${years} سال`);
if (months > 0)
result.push(`${months} ماه`);
if (days > 0)
result.push(`${days} روز`);
if (hours > 0)
result.push(`${hours} ساعت`);
if (minutes > 0)
result.push(`${minutes} دقیقه`);
if (seconds > 0)
result.push(`${seconds} ثانیه`);

return digitsEnToFa(result.join(" و "));
};

/**
*
* @param remainingTime
* @returns convert years, months, days, hours, minutes and seconds to farsi digits and return them in an object with keys:
* `faYears`, `faMonths`, `faHours`, `faMinutes`, `faSeconds`
*/
const convertToFaDigit = (
remainingTime: RemainingTime,
): {
faYears: string;
faMonths: string;
faDays: string;
faHours: string;
faMinutes: string;
faSeconds: string;
} => {
return {
faYears: digitsEnToFa(remainingTime.years),
faMonths: digitsEnToFa(remainingTime.months),
faDays: digitsEnToFa(remainingTime.days),
faHours: digitsEnToFa(remainingTime.hours),
faMinutes: digitsEnToFa(remainingTime.minutes),
faSeconds: digitsEnToFa(remainingTime.seconds),
};
};

export default remainingTime;
export default remainingTime;