Convert seconds to compound duration: Difference between revisions

Content added Content deleted
Line 1,205: Line 1,205:
=={{header|JavaScript}}==
=={{header|JavaScript}}==


===ES5 Functional===
===Functional===


====ES5====


<lang JavaScript>(function () {
<lang JavaScript>(function () {
Line 1,273: Line 1,274:
86400 -> 1 d
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>


====ES6====

<lang JavaScript>(() => {

// angloDuration :: Int -> String
function angloDuration(intSeconds) {
return zipWithEnglish(weekParts(intSeconds))
.reduce((a, x) =>
a.concat(x[0] ? [`${x[0]} ${x[1]}`] : []), []
)
.join(', ');
}


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

return {
rem: (r - mod) / (x || 1),
parts: [mod].concat(a.parts)
};
}, {
rem: intSeconds,
parts: []
})
.parts,


// zipWithEnglish :: [Int] -> [(Int, String)]
zipWithEnglish = (lstValues) => {
var d = ["wk", "d", "hr", "min", "sec"];
return lstValues.length === d.length ? (
lstValues.map((a, c) => [a, d[c]])
) : undefined;
}


// TEST
return [7259, 86400, 6E6]
.map(intSeconds =>
`${intSeconds} -> ${angloDuration(intSeconds)}`
)
.join("\n");

})();</lang>


{{Out}}
<pre>7259 -> 2 hr, 59 sec
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>


=={{header|jq}}==
=={{header|jq}}==