Skip to content

Instantly share code, notes, and snippets.

View stringparser's full-sized avatar
🏠
Working from home

Javier Carrillo Milla stringparser

🏠
Working from home
View GitHub Profile
var util = require('util')
function hook_stdout(callback) {
var old_write = process.stdout.write
process.stdout.write = (function(write) {
return function(string, encoding, fd) {
write.apply(process.stdout, arguments)
callback(string, encoding, fd)
}

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

@stringparser
stringparser / gulpfile.js
Created September 19, 2014 15:45
dumb gulp@4.0 file
'use strict';
var gulp = require('./.');
gulp.task('js', function(done){
done();
});
gulp.task(function jsx(done){
done();
@stringparser
stringparser / bdd_minimal.js
Last active August 29, 2015 14:07
bdd minimal tests
var ansiJS = require('ansi-highlight');
var timer, exitCode;
function format(type, str, fn){
var test, badge;
try {
test = (fn || function(){})() || 'passed';
} catch(err){
test = err;
@stringparser
stringparser / bdd_minimal.sh
Last active August 29, 2015 14:07
bdd minimal tests
#!/bin/sh
# taked from TJ
# https://github.com/visionmedia/commander.js/blob/master/test/run
export NODE_ENV=test
echo
for file in $@; do
printf " \033[90m${file#test/}\033[0m\n --- \n"
@stringparser
stringparser / index.js
Last active August 29, 2015 14:07
Boom, require dir
var fs = require('fs');
var path = require('path');
var fileList = fs.readdirSync(
path.resolve(path.dirname(module.parent.filename), path.basename(__dirname))
).slice(1);
exports = module.exports = { };
fileList.forEach(function(fileName){
@stringparser
stringparser / document.addEventListener
Created October 26, 2014 00:45
document.addEventListener('keydown', function(e){ /* do stuff */ })
var util = { };
document.addEventListener('keydown', function(e){
var key = util.key[e.which];
if( key ){
e.preventDefault();
}
if( key === 'F1' ){

The browser field is provided by a module author as a hint to javascript bundlers or component tools when preparing modules for client side use.

terms

Below are common terms used in the rest of the document

server

This is a non-dom based javascript execution environment. It usually only contains the base javascript language spec libraries and objects along with modules to communicate with OS features (available through commonjs require).

client

//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
// License: MIT
//
// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)
//
// Permission is hereby granted, free of charge, to any person

Keybase proof

I hereby claim:

  • I am stringparser on github.
  • I am stringparser (https://keybase.io/stringparser) on keybase.
  • I have a public key whose fingerprint is 1801 9FA4 E47C 2CB8 A543 EBA3 7202 1D35 C4F2 9BCB

To claim this, I am signing this object: