fix: respect duration options in formatting (#32053)
This commit is contained in:
parent
936b66e7c9
commit
08de97b1ce
2 changed files with 114 additions and 8 deletions
99
cypress/integration/utils.js
Normal file
99
cypress/integration/utils.js
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
context("Utils", () => {
|
||||
before(() => {
|
||||
cy.login();
|
||||
cy.visit("/app");
|
||||
});
|
||||
|
||||
function run_util(name, ...args) {
|
||||
return cy
|
||||
.window()
|
||||
.its("frappe")
|
||||
.then((frappe) => {
|
||||
return frappe.utils[name](...args);
|
||||
});
|
||||
}
|
||||
|
||||
it("should round hidden seconds to minutes", () => {
|
||||
run_util("seconds_to_duration", 89, { hide_seconds: 1 }).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: 0,
|
||||
hours: 0,
|
||||
minutes: 1,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
|
||||
run_util("seconds_to_duration", -89, { hide_seconds: 1 }).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: -0,
|
||||
hours: -0,
|
||||
minutes: -1,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
|
||||
run_util("seconds_to_duration", 91, { hide_seconds: 1 }).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: 0,
|
||||
hours: 0,
|
||||
minutes: 2,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
|
||||
run_util("seconds_to_duration", -91, { hide_seconds: 1 }).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: -0,
|
||||
hours: -0,
|
||||
minutes: -2,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse days, hours, minutes and seconds", () => {
|
||||
run_util("seconds_to_duration", 60 * 60 * 24 + 60 * 60 + 60 + 1).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: 1,
|
||||
hours: 1,
|
||||
minutes: 1,
|
||||
seconds: 1,
|
||||
});
|
||||
});
|
||||
|
||||
run_util("seconds_to_duration", (60 * 60 * 24 + 60 * 60 + 60 + 1) * -1).then(
|
||||
(duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: -1,
|
||||
hours: -1,
|
||||
minutes: -1,
|
||||
seconds: -1,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
run_util("seconds_to_duration", 60 * 60 * 24 + 60 * 60 + 60 + 1, {
|
||||
hide_days: 1,
|
||||
hide_seconds: 1,
|
||||
}).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: 0,
|
||||
hours: 25,
|
||||
minutes: 1,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
|
||||
run_util("seconds_to_duration", (60 * 60 * 24 + 60 * 60 + 60 + 1) * -1, {
|
||||
hide_days: 1,
|
||||
hide_seconds: 1,
|
||||
}).then((duration) => {
|
||||
expect(duration).to.deep.equal({
|
||||
days: 0,
|
||||
hours: -25,
|
||||
minutes: -1,
|
||||
seconds: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1115,7 +1115,7 @@ Object.assign(frappe.utils, {
|
|||
if (value) {
|
||||
let total_duration = frappe.utils.seconds_to_duration(value, duration_options);
|
||||
|
||||
if (total_duration.days) {
|
||||
if (total_duration.days && duration_options.hide_days !== 1) {
|
||||
duration += total_duration.days + __("d", null, "Days (Field: Duration)");
|
||||
}
|
||||
if (total_duration.hours) {
|
||||
|
|
@ -1126,7 +1126,7 @@ Object.assign(frappe.utils, {
|
|||
duration += duration.length ? " " : "";
|
||||
duration += total_duration.minutes + __("m", null, "Minutes (Field: Duration)");
|
||||
}
|
||||
if (total_duration.seconds) {
|
||||
if (total_duration.seconds && duration_options.hide_seconds !== 1) {
|
||||
duration += duration.length ? " " : "";
|
||||
duration += total_duration.seconds + __("s", null, "Seconds (Field: Duration)");
|
||||
}
|
||||
|
|
@ -1143,19 +1143,26 @@ Object.assign(frappe.utils, {
|
|||
},
|
||||
|
||||
seconds_to_duration(seconds, duration_options) {
|
||||
const round = seconds > 0 ? Math.floor : Math.ceil;
|
||||
const floor = seconds > 0 ? Math.floor : Math.ceil;
|
||||
const round_base_60 = (seconds) => floor(seconds / 60 + (seconds > 0 ? 0.5 : -0.5));
|
||||
|
||||
const total_duration = {
|
||||
days: round(seconds / 86400), // 60 * 60 * 24
|
||||
hours: round((seconds % 86400) / 3600),
|
||||
minutes: round((seconds % 3600) / 60),
|
||||
seconds: round(seconds % 60),
|
||||
days: floor(seconds / 86400), // 60 * 60 * 24
|
||||
hours: floor((seconds % 86400) / 3600),
|
||||
minutes: floor((seconds % 3600) / 60),
|
||||
seconds: floor(seconds % 60),
|
||||
};
|
||||
|
||||
if (duration_options && duration_options.hide_days) {
|
||||
total_duration.hours = round(seconds / 3600);
|
||||
total_duration.hours = floor(seconds / 3600);
|
||||
total_duration.days = 0;
|
||||
}
|
||||
|
||||
if (duration_options && duration_options.hide_seconds) {
|
||||
total_duration.minutes += round_base_60(total_duration.seconds);
|
||||
total_duration.seconds = 0;
|
||||
}
|
||||
|
||||
return total_duration;
|
||||
},
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue