Convert seconds to compound duration: Difference between revisions
Convert seconds to compound duration (view source)
Revision as of 22:19, 13 April 2022
, 2 years ago→JS ES6: Tidied.
Drkameleon (talk | contribs) |
(→JS ES6: Tidied.) |
||
Line 2,217:
====ES6====
<lang JavaScript>(() => {
const localNames = ['wk', 'd', 'hr', 'min', 'sec'];▼
return [7259, 86400, 6E6]▼
.map(intSeconds =>▼
`${intSeconds} -> ${▼
compoundDuration(localNames)(intSeconds)▼
}`).join('\n');▼
};▼
// compoundDuration :: [String] -> Int -> String
const compoundDuration = labels =>
.map((v, i) => [v, labels[i]])
.reduce((a, x) =>
a.concat(
)
.join(
const weekParts = nSeconds => [0, 7, 24, 60, 60]
.reduceRight((a, x) => {
const
r = a[0],
mod = x !== 0 ? r % x : r;
return Tuple((r - mod) / (x || 1))(▼
[mod].concat(a[1])▼
▲ );
}, Tuple(intSeconds)([]))[1]▼
▲ // ---------------------- GENERIC ----------------------
const Tuple = a =>▼
▲ '0': a,
▲ '1': b,
▲ });
// ---------------------- TEST -----------------------
// ---▼
// main :: IO ()
▲ return [7259, 86400, 6E6]
▲ };
▲ // MAIN ---
return main();
})();</lang>
|