Skip to content

Instantly share code, notes, and snippets.

@loilo
loilo / bubble.md
Last active April 27, 2023 00:21
Make Vue events bubble

Make Vue events bubble

Vue events don't bubble the component tree on their own. However when writing wrapper components this can be the desired behaviour.

This code registers a global bubble directive which allows to re-emit all given events:

Let's say we want to bubble events start, accelerate and brake of our component Car.

Without any help, we'd roughly have to do this:

@antishok
antishok / NODE_ENV production.md
Last active September 25, 2020 07:09
What does `NODE_ENV=production` do?
@dImrich
dImrich / curved3Plane.js
Created January 6, 2017 18:00
Bend Three JS Plane Geometry With Bezier Curve
//Bend Three JS plane geometry with bezier curve
//Curved plane generation, bezier curve plane,
function bendPlaneGeometry(planeGeometry, centerBendZ)
{
var curve = new THREE.CubicBezierCurve3(
planeGeometry.vertices[0],
new THREE.Vector3(planeGeometry.parameters.width/2, 0, centerBendZ ),
new THREE.Vector3(planeGeometry.parameters.width/2, 0, centerBendZ ),
planeGeometry.vertices[(planeGeometry.vertices.length/2) - 1]
);
@theatlasroom
theatlasroom / basic-post-receive-hook-with-pm2
Last active August 16, 2022 23:00
Basic post-receive hook - node + pm2
#!/bin/sh
# This is for an expressjs node app, it uses npm + bower packages and pm2 to start the app
# [pm2](https://github.com/Unitech/pm2)
# Assumes you've created a [bare git repo](https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server) with on your server with git init --bare
# Adapted from http://javascript.tutorialhorizon.com/2014/08/17/push-to-deploy-a-nodejs-application-using-git-hooks/
PORT=1337
APP_NAME="app-name"
APP_ROOT="/var/www/app"
@mrded
mrded / server.js
Created December 4, 2015 16:17
CORS Proxy on node.js + express
var express = require('express'),
request = require('request');
var app = express();
// Forward all requests from /api to http://foo.com/api
app.use('/api', function(req, res) {
req.pipe(request("http://foo.com/api" + req.url)).pipe(res);
});
@CMCDragonkai
CMCDragonkai / ternary_comparison.hs
Last active October 31, 2015 00:33
Haskell: Ternary Comparison Operators (inspired by ternary choice operator http://zenzike.com/posts/2011-08-01-the-conditional-choice-operator)
{-
The comparison operators are non-associative and not composable.
Thus it is illegal to write: 1 < 2 < 3
The input types of < (Int) does not match the output type (Bool).
1 < 2 < 3 can however be written in math notation, because it denotes:
(1 < 2) && (2 < 3) or (1 < 2) ^ (2 < 3)
It is however possible to create a new ternary operator that supports such a
notation!
-}
@branneman
branneman / better-nodejs-require-paths.md
Last active April 27, 2024 04:16
Better local require() paths for Node.js

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

var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
@ricardobeat
ricardobeat / node-0.8.20-hangup.js
Created February 21, 2013 19:27
Silence socket hangup errors.
function hangups (req, res, next){
var reqd = domain.create()
reqd.add(req)
reqd.add(res)
reqd.on('error', function (error) {
if (error.code !== 'ECONNRESET') console.error(error, req.url)
reqd.dispose()
})
next()
}
@punund
punund / getImageSize.coffee
Created November 11, 2012 19:57
Retrieves first chunk of data of remote image via HTTP GET to find out its size in pixels. PNG, JPEG
getImageSize = (image, cb) ->
fromHex = (hex) -> parseInt "0x#{hex}", 16
req = http.request image, (res) ->
res.setEncoding 'hex'
res.on 'error', (e) -> cb "getImageSize error (#{image}): " + e, null, image
res.on 'data', (chunk) ->
m = switch res.headers['content-type']
when 'image/jpeg'
chunk.match 'ffc0001108(....)(....)'
when 'image/png'