Skip to content

Instantly share code, notes, and snippets.

Avatar

Moritz Kneilmann MoritzKn

View GitHub Profile
@MoritzKn
MoritzKn / fetchJson.js
Last active Dec 18, 2020
Fetch and validate JSON (the fetch wrapper that I write over and over again)
View fetchJson.js
async function fetchJson(url, options, validate) {
const response = await fetch(url, options);
let json;
let jsonError;
try {
json = await response.json();
} catch (error) {
jsonError = error;
}
@MoritzKn
MoritzKn / aligned-table.js
Last active Dec 18, 2020
Print aligned tables
View aligned-table.js
function alignedTable({
alignments = [],
separator = ' ',
prefix = '',
suffix = '',
newLine = '\n',
rows = [],
} = {}) {
function toString() {
const lengths = {};
@MoritzKn
MoritzKn / smooth-expand.js
Last active Feb 21, 2020
POC Smooth Element Expand Animation
View smooth-expand.js
function getElementsAfter(el, {stopAt, skip, only} = {}) {
let elements = [];
while (el) {
while (el.nextElementSibling) {
el = el.nextElementSibling
elements.push(el)
}
while (el && !el.nextElementSibling) {
el = el.parentElement
View rubberBandEffect.ts
function rubberBandEffect(
val: number,
lowerBounds: number,
upperBounds: number,
padding: number = 25,
): number {
const lowerLimit = lowerBounds - padding;
const upperLimit = upperBounds + padding;
if (val < lowerLimit) {
@MoritzKn
MoritzKn / nodeColors.js
Last active Oct 12, 2016
Terminal colors in Nodejs without extra dependencies
View nodeColors.js
const util = require('util');
const colorize = (str, colorCodes) => `\x1b[${colorCodes[0]}m${str}\x1b[${colorCodes[1]}m`;
const bold = str => colorize(str, util.inspect.colors.bold);
const italic = str => colorize(str, util.inspect.colors.italic);
const underline = str => colorize(str, util.inspect.colors.underline);
const inverse = str => colorize(str, util.inspect.colors.inverse);
const white = str => colorize(str, util.inspect.colors.white);
const grey = str => colorize(str, util.inspect.colors.grey);
@MoritzKn
MoritzKn / reverseArrayWithIterator.js
Last active Apr 27, 2021
Demo of generators and iterator
View reverseArrayWithIterator.js
let a = [1, 2, 3, 4];
for (let item of a) {
console.log(item);
}
function* reverse () {
for (let i = a.length - 1; i >= 0; i--) {
yield a[i];
}