Skip to content

Instantly share code, notes, and snippets.

Avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / better-nodejs-require-paths.md
Last active Aug 6, 2020
Better local require() paths for Node.js
View better-nodejs-require-paths.md

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@branneman
branneman / create-zip.js
Last active Jul 31, 2020
Node.js script to create a zip file from a list of files and directories
View create-zip.js
const stat = require('fs').statSync;
const AdmZip = require('adm-zip');
/**
* Example usage
*/
newArchive(`test-${+new Date}.zip`, [
'index.js',
'package.json',
'node_modules'
@branneman
branneman / radicals.json
Created Jun 9, 2020
JSON list of 214 Simplified Chinese Radicals, data contains radical number, pinyin, english translation, stroke count
View radicals.json
[
{
"id": 1,
"radical": "",
"pinyin": "",
"english": "one",
"strokeCount": 1
},
{
"id": 2,
@branneman
branneman / count-lint-errors.js
Last active Jul 28, 2020
Group-By-Count ESLint errors
View count-lint-errors.js
// :: (String, String) => String
const spawn = require('child_process').spawnSync;
// :: String => [String]
const getRules = raw => raw
.split('\n')
.map(line => line.trim())
.filter(line => !!line)
.filter(line => line[0] !== '/' && line[0] !== '✖')
.map(line => line.match(/[a-z-]+$/)[0]);
View example-mod-wsgi.py
def application(environ, start_response):
import sys
path = 'YOUR_WWW_ROOT_DIRECTORY'
if path not in sys.path:
sys.path.append(path)
from pyinfo import pyinfo
output = pyinfo()
start_response('200 OK', [('Content-type', 'text/html')])
return [output]
@branneman
branneman / salt-hash-password.js
Last active Jun 13, 2020
Node.js: Create salted hashed password
View salt-hash-password.js
const crypto = require('crypto')
const { promisify } = require('util')
const pbkdf2 = promisify(crypto.pbkdf2)
module.exports = { createHashPasswordFn, isPasswordCorrect }
/**
* @typedef {Object} HashPassword
* @property {String} hash
* @property {String} salt
@branneman
branneman / modules.js
Last active Jun 12, 2020
A history of different JavaScript module formats
View modules.js
/**
* Globals
*/
var Carrousel = function(elem) { this.init() };
Carrousel.prototype = { init: function() {} };
new Carrousel();
/**
* Namespacing
* - No globals (only the namespace variable itself is global)
@branneman
branneman / prototypal-inheritance.js
Last active Jun 2, 2020
ES3 & ES5 — Prototypal Inheritance
View prototypal-inheritance.js
/**
* Parent
*/
function Shape(x, y) {
this.x = x;
this.y = y;
}
Shape.prototype.constructor = Shape;
Shape.prototype.pos = function() {
return [this.x, this.y];
@branneman
branneman / primitive-reference-types-javascript.md
Last active May 18, 2020
Primitive Types & Reference Types in JavaScript
View primitive-reference-types-javascript.md

Primitive Types & Reference Types in JavaScript

An explanation of JavaScript's pass-by-value, which is unlike pass-by-reference from other languages.

Facts

  • JavaScript has 2 kinds of variable types: primitive and reference.
  • A fixed amount of memory is reserved after creation of every variable.
  • When a variable is copied, it's in-memory value is copied.
  • Passing a variable to a function via a call also creates a copy of that variable.

Primitive Types

You can’t perform that action at this time.