Skip to content

Instantly share code, notes, and snippets.

View NeoTech's full-sized avatar

Andreas Pettersson NeoTech

  • Roughedge AB
  • Sweden
  • 06:45 (UTC +02:00)
View GitHub Profile
@NeoTech
NeoTech / csv2xlsx.pl
Created November 30, 2015 10:32
csv2xlsx
#!/usr/bin/perl
# http://cpansearch.perl.org/src/HMBRAND/Text-CSV_XS-1.01/examples/csv2xls
# csv2xls: Convert csv to xls
# (m)'11 [06 Oct 2011] Copyright H.M.Brand 2007-2013
# modified 09/24/2013-11/05/2013 David Rotman, Cedarville University
# Many of these changes are stylistic changes based on personal preferences.
# The original script by H.M.Brand can be used without concern for accurateness
# and functionality.
@NeoTech
NeoTech / currying.js
Created September 22, 2015 14:06
Short explanation of currying and its uses. And maybe pitfalls.
/**
This creates a factory with the objective to make less code for repetitive tasks.
Normal way would have been to code each of these functions individually.
Instead we createa currying function that acts as a factory.
**/
function UnitConversion() {
// Takes single object.
var args = Array.prototype.slice.call(arguments);
if(args[0].length > 0) {
@NeoTech
NeoTech / deepextend.js
Created September 22, 2015 14:04
How to properly clone an object/constructor in Javascript without loosing data.
Object.deepExtend = function(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
@NeoTech
NeoTech / eventproto.js
Created September 22, 2015 14:00
How to handle event emitters in Node.JS and a common prototype pattern for it.
var EventEmitter = require('events').EventEmitter;
function EventProto() {
if (!(this instanceof EventProto)) {
console.log('Not an instance; returning new.');
return new EventProto();
}
EventEmitter.call(this);
var _self = this;
}
@NeoTech
NeoTech / async.js
Last active September 22, 2015 13:56
Asynchron IO and how it executes.
/**
* Created by WARMACHINE on 2015-09-22.
*/
// Example of code execution in a asynchron enviroment
// For simulate lag from db, timeouts will be used.
console.log('I will execute first');
var delay1 = 0;
setTimeout((function() {
@NeoTech
NeoTech / Middleware.js
Last active September 22, 2015 13:58
A simplified example of Middleware execution.
/** This is a common question im running into, what is the difference between a promise design pattern and a middleware.. **/
/* This pattern is commonly found in connect, koa, express and other middleware based frameworks. */
/* This is not a hook pattern with pre/post */
function Middleware() {
if (!(this instanceof Middleware)) {
if (global.MODE_LVL < 5) {
console.log('Not an instance; returning new callbackStack Client');
}
return new Middleware();
#-- Loop through subfolders shenaningans and unrar every bloody archive you find.
for /r "./" %i in (`dir *.rar /s/b`) do 7z x %i
@NeoTech
NeoTech / js-web-debug.js
Last active August 29, 2015 13:57
JS Website debugger
(function() {
var scriptURI = prompt('Enter your script','https://gist.githubusercontent.com/NeoTech/6778e791e297151dd4a5/raw/b4604119b690d797ae350e0f6ef6eaa5775b07f1/screenshot.js');
var callback = prompt('Enter a Callback', "(function() { console.log('Script loaded'); })");
var getScript = (function getScript(url,success) {
var head=document.getElementsByTagName('body')[0],done=false;
var script=document.createElement('script');
script.src=url;
script.onload=script.onreadystatechange=function(){
if(!done && (!this.readyState || this.readyState=='loaded' || this.readyState=='complete')) {
done=true;

Managing dependencies

Ok, so you've built your first module, but now you want to make it use one of the many libraries available via the npm registry.

finding modules

There are a few ways to find a module. You can use http://npmjs.org or http://npmsearch.com to find modules that may fit what you need.

some advice for choosing modules

building your first node module

This is pretty simple, lets dive in!

choose a name

Find a name that isn't taken and clearly describes what your module is doing

$ npm view your-first-node-module