Skip to content

Instantly share code, notes, and snippets.

@minnacaptain
minnacaptain / parseStuff.ts
Created Jul 23, 2021
Get the calculateAverageScore function from the internet
View parseStuff.ts
import { calculateAverageScore } from 'https://raw.githubusercontent.com/minnacaptain/have-you-tried-deno-yet/master/calculate.ts'
const getContentFromInternet = () =>
fetch(
"https://raw.githubusercontent.com/minnacaptain/have-you-tried-deno-yet/master/example_data.txt",
).then((t) => t.text());
console.log(
"Average score: " + await calculateAverageScore(getContentFromInternet),
);
@minnacaptain
minnacaptain / calculate.test.ts
Created Jul 23, 2021
Testing the calculate function
View calculate.test.ts
import { assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import { calculateAverageScore } from './calculate.ts'
const mockGetContent = () => Promise.resolve(
"student_name,horoscope,exam_name,exam_score,feedback_score,would_recommend\n" +
"Braylen Natalia,Gemini,AWS Certified Cloud Practitioner,1,4.26,Yes\n" +
"Kash Macey,Leo,AWS Certified SysOps Administrator,2,3.04,Yes\n" +
"Erica Daniela,Sagittarius,AWS Certified Cloud Practitioner,3,7.61,Yes"
)
View calculate.ts
export const calculateAverageScore = (getContent: () => Promise<string>) =>
getContent().then((result) => {
const lines = result.split("\n")
.map((l) => l.split(","))
.filter((_, i) => i !== 0);
const scores = lines.map((l) => Number(l[3]));
return scores.reduce((a, c) => a + c) / scores.length;
});
@minnacaptain
minnacaptain / parseStuff.ts
Created Jul 23, 2021
Get content from internet
View parseStuff.ts
const getContentFromInternet = () =>
fetch(
"https://raw.githubusercontent.com/minnacaptain/have-you-tried-deno-yet/master/example_data.txt",
).then((t) => t.text());
// calculateAverageScore is unchanged
console.log("Average score: " + await calculateAverageScore(getContentFromInternet));
@minnacaptain
minnacaptain / parseStuff.ts
Created Jul 23, 2021
Calculate the average score
View parseStuff.ts
const getContent = () => Deno.readTextFile('./example_data.txt')
const calculateAverageScore = (getContent: () => Promise<string>) => getContent().then(result => {
const lines = result.split("\n")
.map(l => l.split(","))
.filter((_, i) => i !== 0)
const scores = lines.map(l => Number(l[3]))
return scores.reduce((a, c) => a + c) / scores.length
})
@minnacaptain
minnacaptain / parseStuff.ts
Last active Jul 26, 2021
Parse the example data
View parseStuff.ts
const getContent = () => Deno.readTextFile('./example_data.txt')
const calculateAverageScore = (getContent: () => Promise<string>) => getContent().then(result => {
const lines = result.split("\n").map(l => l.split(","))
console.log(lines)
})
calculateAverageScore(getContent)