Skip to content

Instantly share code, notes, and snippets.

@kasnder
Last active January 21, 2020 12:16
Show Gist options
  • Save kasnder/2b0e03e8e5ca848220ad9b366e402308 to your computer and use it in GitHub Desktop.
Save kasnder/2b0e03e8e5ca848220ad9b366e402308 to your computer and use it in GitHub Desktop.
Some TypeScript code that I've use repeatedly
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