Skip to content

Instantly share code, notes, and snippets.

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:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@sshmyg
sshmyg / regex.js
Last active August 29, 2015 14:07 — forked from Integralist/regex.js
var str = "The quick brown fox jumped over the box like an ox with a sox in its mouth";
str.match(/\w(ox)/g); // ["fox", "box", "sox"]
// match (when used with a 'g' flag) returns an Array with all matches found
// if you don't use the 'g' flag then it acts the same as the 'exec' method.
str.match(/\w(ox)/); // ["fox", "ox"]
/\w(ox)/.exec(str); // ["fox", "ox"]
@sshmyg
sshmyg / Trigger
Created September 2, 2014 06:21
Trigger DOM event
/**
* @param target can be any DOM Element or other EventTarget
* @param type Event type (i.e. 'click')
*/
var trigger = function (target, type) {
if (document.createEvent) {
event = new Event(type);
target.dispatchEvent(event);
} else {
event = document.createEventObject();
@sshmyg
sshmyg / animation.vanila.js
Last active September 18, 2015 06:01
Animation
/**
* Анимация. Параметры:
* opts.delta(time) -- временная функция, time=0..1. По умолчанию linear.
* opts.step(progress) -- функция рисования, progress=0..1.
* opts.complete -- функция, которая выполнится по окончании анимации
* opts.dely -- задержка между кадрами, по умолчанию 13
*
* Пример:
var opts = {