Skip to content

Instantly share code, notes, and snippets.

Avatar
🏡
Working from home

Benny Neugebauer bennycode

🏡
Working from home
View GitHub Profile
@bennycode
bennycode / index.html
Last active May 21, 2020
Header, Main, Footer with Flexbox
View index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css"
>
<style>
@bennycode
bennycode / show-lines-of-code.js
Created Apr 1, 2020
Traverse directories and show lines of code
View show-lines-of-code.js
const fs = require('fs');
const path = require('path');
function walkDir(dir, callback) {
fs.readdirSync(dir).forEach(file => {
const dirPath = path.join(dir, file);
const isDirectory = fs.statSync(dirPath).isDirectory();
isDirectory ?
walkDir(dirPath, callback) : callback(path.join(dir, file));
});
View simple-server.ts
import {createServer} from 'http';
createServer((request, response) => {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World!');
response.end();
}).listen(8080);
@bennycode
bennycode / bluebird-promise-cancellation.js
Last active Feb 12, 2020
Bluebird Promise Cancellation
View bluebird-promise-cancellation.js
// Compliant Promise/A+ implementation: https://www.promisejs.org/implementing/
// Alternative implementation: https://dexie.org/docs/Promise/Promise
// Checkout the Node.js Event Loop: https://www.youtube.com/watch?v=PNa9OMajw9w
// Bluebird breaking change: http://bluebirdjs.com/docs/new-in-bluebird-3.html#cancellation-overhaul
const Promise = require('bluebird');
Promise.config({
cancellation: true,
});
@bennycode
bennycode / absence-io-example.js
Created Jan 14, 2020
Hacky absence.io API example
View absence-io-example.js
const Hawk = require('@hapi/hawk');
const request = require('request');
const API_KEY = 'KEY';
const API_KEY_ID = 'ID';
const credentials = {
id: API_KEY_ID,
key: API_KEY,
algorithm: 'sha256'
View stateful-copies.js
const test = {
tree: {
branch1: 10,
branch2: 20,
}
};
const copies = {};
for (let i = 0; i < 10; i++) {
View 50-shades-types.ts
const a: object = { name: 'Benny' };
const b: Object = { name: 'Benny' };
const c: {} = { name: 'Benny' };
const d: Record<string, string> = { name: 'Benny' };
const e: any = { name: 'Benny' };
const f: {[index: string]: string} = { name: 'Benny' };
@bennycode
bennycode / nodejs-clean-exit.ts
Created Oct 8, 2019
Node.js process.exit cleanup
View nodejs-clean-exit.ts
function emitExit(signal: string) {
const exitCode = 0;
console.log(`Received "${signal}" signal. Will terminate with exit code "${exitCode}".`);
process.exit(exitCode);
}
// Catches Ctrl + C events
process.on('SIGINT', () => emitExit('SIGINT'));
// Catches "kill pid" events (for example: nodemon restarts)
@bennycode
bennycode / tsconfig.minimum.json
Created Aug 19, 2019
Minimum Viable TypeScript config
View tsconfig.minimum.json
{
"compilerOptions": {
"lib": ["dom", "es2017"],
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"target": "es6"
}
}
View momentjs-cheatsheet.js
const moment = require('moment');
const date = moment('2019-07-23', 'YYYY-MM-DD');
const oneWeekLater = date.add(1, 'week');
console.log(oneWeekLater.format());