Skip to content

Instantly share code, notes, and snippets.

@minnacaptain
minnacaptain / parseStuff.ts
Created July 23, 2021 13:22
Get the calculateAverageScore function from the internet
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 July 23, 2021 13:11
Testing the calculate function
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"
)
@minnacaptain
minnacaptain / calculate.ts
Created July 23, 2021 12:53
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 July 23, 2021 12:41
Get content from internet
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 July 23, 2021 12:17
Calculate the average score
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 July 26, 2021 06:09
Parse the example data
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)