Skip to content

Instantly share code, notes, and snippets.

View constantology's full-sized avatar

christos constandinou constantology

  • muigui.com
  • london
View GitHub Profile
@constantology
constantology / iterate-non-standard-stream.js
Last active October 7, 2020 09:59
Easy-peasy-lemon-squeezy iterating non-standard stream implementations...
// For non-standard streams — APIs that stream but the library exposes something else and not the underlying Node.js stream, e.g `xml-stream` and `xml-stream-saxjs`, — you can use the following to perform standard `for await of...` iteration over each item emitted in events.
const sleep = require('./sleep');
module.exports = async function* iterateNonStandardStream({
endEvent = 'end',
errorEvent = 'error',
itemEvent,
stream,
timeout = 900000
@constantology
constantology / process.logger.js
Created July 24, 2014 09:39
using node's process to emit events to log stuff from anywhere
// use like this:
// process.emit( 'app:log', module, arg1, arg2, ..., argN );
var Module = require('module');
function logConsole(method, module) {
var args = [(new Date()).toJSON(), method];
var index = 1;
if (module instanceof Module) {
function Person(/* whatever args your gonna have*/) {
this.onSave_ = this.onSave.bind( this );
this.onSetAge_ = this.onSetAge.bind( this );
}
Person.prototype.save = function() {
this.database.insert( this.properties, this.onSave_ );
};
Person.prototype.setAge = function( newAge ) {
this.constructor.validate( "age", this, newAge, this.onSetAge_ );
@constantology
constantology / __proto__.js
Last active October 14, 2015 01:28
An explanation of `__proto__` using an example and a `__proto__` "polyfill" for MSIE 9 & 10.
function Collection() {
this.push.apply( this, arguments );
}
Collection.prototype = [];
Collection.prototype.push = function( item ) { // we need to wrap this.push in a function or we'll get a recursion error from
if ( arguments.length > 1 ) { // the arguments.length always being > 1 since the Array index is passed to the iterator
this.slice.call( arguments ).map( function( val ) { this.push( val ); }, this );
return this.length;
@constantology
constantology / element_expandos.js
Created December 9, 2012 22:14
easy way to get a list of every expando property on all the supplied element in a list
var val = [document, new Date(), true, 1, 'foo'];
'a abbr acronym address area article aside audio b bdi bdo big blockquote body br button canvas caption '
+ 'cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption '
+ 'figure footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins '
+ 'kbd keygen label legend li link map mark meta meter nav noscript object ol optgroup option output p '
+ 'param pre progress q rp rt ruby samp script section select small source span split strong style sub '
+ 'summary sup table tbody td textarea tfoot th thead time title tr track tt ul var video wbr'.split( ' ' ).map( function( tag ) {
var el = document.createElement( tag );
return Object.getOwnPropertyNames( el ).filter( function( attr ) {
var value = val.concat( [attr] ), i = value.length;
@constantology
constantology / more_goodera_example.js
Last active October 13, 2015 15:28
using functions to `case` match in `switch` statements

the main caveat is that each case function will be executed until a match is found, however, this would not be any different from doing the same thing using if/else.

note: i am not advocating this method, it's just something i've never seen done before and thought it was interesting enough to share.

@constantology
constantology / event_keycode.js
Created November 14, 2012 19:18
1 character string range
var Events = Array.apply( [], Array( 90 ).reduce( function( e, v, i ) { // String.fromCharCode( 122 ) === 'z'
v = 33 + i; // String.fromCharCode( 33 ) === '!'
// so 123 - 33 === 90
e.KEY_CODE[v] = String.fromCharCode( v );
e.KEY_MAP[e.KEY_CODE[v]] = v;
return e;
}, { KEY_CODE : Object.create( null ), KEY_MAP : Object.create( null ) } );
/**
@constantology
constantology / SuperView.js
Created November 14, 2012 15:24
Backbone.View.$uper
;!function() {
"use strict";
// fn is the super class' callSuper that has being curried with its __super__ prototype
// proto was curried in by $uper's custom extend method
function callNestedSuper( fn, proto/*, ...args*/ ) {
var args = Super._.toArray( arguments ).slice( 1 ), val1, val2;
if ( args[1] === this.__lastMethod__ ) // avoid possible recursion errors
return this;
@constantology
constantology / node.update.sh
Created October 14, 2012 10:49
shell script to update node to a specific version
#!/bin/sh
# add these two variables to your: ~/.bash_profile or ~/.bashrc or just hardcode the directories in place of the variables below
# export NODE_REPO=/usr/local/Library/node/repo
# export NODE_JS=/usr/local/Library/node/js
# example of upgrading to version 0.8.12
# $ sh node.update.sh v0.8.12
cd $NODE_REPO;
@constantology
constantology / npm.dedupe.sh
Created October 14, 2012 10:41
shell script to update and deduplicate npm modules
#!/bin/sh
# change the below line to point to your: PATH/TO/INSTALLED/NODEJS/lib/node_modules or just cd into the directory before running it
# cd $NODE_PATH;
npm update;
modules=`ls`;
for dir in $modules