Convert seconds to compound duration: Difference between revisions
Content deleted Content added
m →JS :: ES6: (tidying) |
|||
Line 1,948: | Line 1,948: | ||
====ES6==== |
====ES6==== |
||
<lang JavaScript>(() => { |
|||
const main = () => { |
|||
const localNames = ['wk', 'd', 'hr', 'min', 'sec']; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// compoundDuration :: [String] -> Int -> String |
// compoundDuration :: [String] -> Int -> String |
||
const compoundDuration = |
const compoundDuration = labels => |
||
weekParts(intSeconds) |
intSeconds => weekParts(intSeconds) |
||
.map((v, i) => [v, labels[i]]) |
.map((v, i) => [v, labels[i]]) |
||
.reduce((a, x) => |
.reduce((a, x) => |
||
a.concat(x[0] ? [ |
a.concat(x[0] ? ([ |
||
⚫ | |||
⚫ | |||
) |
) |
||
.join(', '), |
.join(', '), |
||
Line 1,963: | Line 1,973: | ||
weekParts = intSeconds => [0, 7, 24, 60, 60] |
weekParts = intSeconds => [0, 7, 24, 60, 60] |
||
.reduceRight((a, x) => { |
.reduceRight((a, x) => { |
||
const |
|||
⚫ | |||
mod = x !== 0 ? r % x : r; |
mod = x !== 0 ? r % x : r; |
||
return Tuple((r - mod) / (x || 1))( |
|||
⚫ | |||
⚫ | |||
}, Tuple(intSeconds)([]))[1] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
.parts; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// ---------------------- GENERIC ---------------------- |
|||
})(["wk", "d", "hr", "min", "sec"]);</lang> |
|||
// Tuple (,) :: a -> b -> (a, b) |
|||
const Tuple = a => |
|||
b => ({ |
|||
type: 'Tuple', |
|||
'0': a, |
|||
'1': b, |
|||
length: 2 |
|||
⚫ | |||
⚫ | |||
return main(); |
|||
})();</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>7259 -> 2 hr, 59 sec |
<pre>7259 -> 2 hr, 59 sec |