Created
December 16, 2018 17:20
-
-
Save nordfjord/0679a3c0c493bfba768cc718a2e9fa30 to your computer and use it in GitHub Desktop.
Syntax coding kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FeedParser = require("feedparser"); | |
const request = require("request"); | |
const feedRequest = request("https://feed.syntax.fm/rss"); | |
const feedparser = new FeedParser(); | |
feedRequest.on("response", function(res) { | |
var stream = this; | |
if (res.statusCode !== 200) { | |
this.emit("error", new Error("Bad status code")); | |
} else { | |
stream.pipe(feedparser); | |
} | |
}); | |
const data = new Promise(resolve => { | |
const result = []; | |
feedparser.on("readable", function() { | |
let stream = this; | |
let item; | |
while ((item = stream.read())) { | |
result.push(item); | |
} | |
}); | |
feedparser.on("end", () => resolve(result)); | |
}); | |
data | |
.then(map(getItunesDuration)) | |
.then(map(toMinutes)) | |
.then(sum) | |
.then(Math.round) | |
.then(console.log); | |
function map(fn) { | |
return list => list.map(fn); | |
} | |
function getItunesDuration(data) { | |
if (!data["itunes:duration"]) return "0:0:0"; | |
return data["itunes:duration"]["#"]; | |
} | |
function toMinutes(duration) { | |
const parsedDuration = duration.split(":").map(n => parseInt(n, 10)); | |
while (parsedDuration.length < 3) parsedDuration.unshift(0); | |
const [hours, minutes, seconds] = parsedDuration; | |
return hours * 60 + minutes + seconds / 60; | |
} | |
function sum(list) { | |
return list.reduce((acc, v) => acc + v, 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5220 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment