Convert seconds to compound duration: Difference between revisions
Content added Content deleted
m (Changed header from 'freebasic' to 'FreeBASIC' as previously this task was listed as 'not implemented' in this language) |
(→JS ES6: Slightly less parochial assumptions about local labels) |
||
Line 1,372: | Line 1,372: | ||
====ES6==== |
====ES6==== |
||
<lang JavaScript>(() => { |
<lang JavaScript>((localNames) => { |
||
// |
// compoundDuration :: [String] -> Int -> String |
||
const compoundDuration = (labels, intSeconds) => |
|||
weekParts(intSeconds) |
|||
.map((v, i) => [v, labels[i]]) |
|||
.reduce((a, x) => |
|||
a.concat(x[0] ? [`${x[0]} ${x[1] || '?'}`] : []), [] |
|||
) |
|||
.join(', '), |
|||
// weekParts :: Int -> [Int] |
// weekParts :: Int -> [Int] |
||
weekParts = intSeconds => [ |
weekParts = intSeconds => [0, 7, 24, 60, 60] |
||
.reduceRight((a, x) => { |
|||
let r = a.rem, |
|||
mod = x !== 0 ? r % x : r; |
|||
return { |
|||
rem: (r - mod) / (x || 1), |
|||
parts: [mod].concat(a.parts) |
|||
}; |
|||
}, { |
|||
rem: intSeconds, |
|||
parts: [] |
|||
}) |
|||
.parts; |
|||
Line 1,403: | Line 1,403: | ||
return [7259, 86400, 6E6] |
return [7259, 86400, 6E6] |
||
.map(intSeconds => |
.map(intSeconds => |
||
`${intSeconds} -> ${ |
`${intSeconds} -> ${compoundDuration(localNames, intSeconds)}` |
||
) |
) |
||
.join("\n"); |
.join("\n"); |
||
})();</lang> |
})(["wk", "d", "hr", "min", "sec"]);</lang> |
||
{{Out}} |
{{Out}} |