Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(→‎ES6: Slight simplification)
Line 1,279: Line 1,279:


<lang JavaScript>(() => {
<lang JavaScript>(() => {

// angloDuration :: Int -> String
// angloDuration :: Int -> String
function angloDuration(intSeconds) {
function angloDuration(intSeconds) {
return zipWithEnglish(weekParts(intSeconds))
return weekParts(intSeconds)
.map((v, i) => [v, ["wk", "d", "hr", "min","sec"][i]])
.reduce((a, x) =>
.reduce((a, x) =>
a.concat(x[0] ? [`${x[0]} ${x[1]}`] : []), []
a.concat(x[0] ? [`${x[0]} ${x[1]}`] : []), []
Line 1,288: Line 1,289:
.join(', ');
.join(', ');
}
}


// weekParts :: Int -> [Int]
// weekParts :: Int -> [Int]
let weekParts = intSeconds => [undefined, 7, 24, 60, 60]
let weekParts = intSeconds => [undefined, 7, 24, 60, 60]
Line 1,295: Line 1,296:
let r = a.rem,
let r = a.rem,
mod = isNaN(x) ? r : r % x;
mod = isNaN(x) ? r : r % x;

return {
return {
rem: (r - mod) / (x || 1),
rem: (r - mod) / (x || 1),
Line 1,305: Line 1,306:
})
})
.parts,
.parts,


// zipWithEnglish :: [Int] -> [(Int, String)]
// zipWithEnglish :: [Int] -> [(Int, String)]
zipWithEnglish = (lstValues) => {
zipWithEnglish = xs => xs
var keys = ["wk", "d", "hr", "min", "sec"];
.map((v, i) => [v, ["wk", "d", "hr", "min","sec"][i]]);

return lstValues.length === keys.length ? (

lstValues.map((v, i) => [v, keys[i]])

) : undefined;
}
// TEST
// TEST
return [7259, 86400, 6E6]
return [7259, 86400, 6E6]
Line 1,322: Line 1,320:
)
)
.join("\n");
.join("\n");

})();</lang>
})();</lang>