Last active
January 21, 2020 12:16
-
-
Save kasnder/2b0e03e8e5ca848220ad9b366e402308 to your computer and use it in GitHub Desktop.
Some TypeScript code that I've use repeatedly
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 fs = require('fs'); | |
const parseDomain = require("parse-domain"); | |
function readJson(filename) { | |
return JSON.parse( | |
fs.readFileSync(filename) | |
); | |
} | |
function getDomain(url) { | |
let parsed = parseDomain(url); | |
let host; | |
try { | |
return parsed.domain + "." + parsed.tld; | |
} catch (e) { | |
return; | |
} | |
} | |
function head(obj, num = 5) { | |
let keys = Object.keys(obj) | |
let out = {} | |
for (let i in keys) { | |
if (i >= num) | |
break; | |
let key = keys[i] | |
out[key] = obj[key] | |
} | |
return out | |
} | |
function uniq(arr) { | |
return Array.from(new Set(arr)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment