Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / ugly.js
Created August 8, 2013 12:21
JS: EventEmitter2 "event passthrough" ugliness.
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var a = new EventEmitter2({wildcard: true});
a.onAny(function(foo, bar) {
console.log(this.event, foo, bar);
});
a.emit('a', 1, 2);
// logs a 1 2
// Is there a more elegant way of forwarding all events from one
@cowboy
cowboy / stream.js
Last active December 20, 2015 10:31
JS streams: can I prevent the "myThing" stream from being killed when the "noise" stream being piped into it ends?
var es = require('event-stream');
function makeThing() {
var out = es.through();
var substream = es.through();
substream.pipe(out);
setInterval(function() {
substream.write('internal data\n');
}, 200);
@cowboy
cowboy / stream-pipe.js
Created July 22, 2013 19:15
Node.js stream pipe question (v0.8.16)
var Stream = require('stream');
var a = new Stream;
a.pipe(process.stdout);
a.emit('data', 'omg test 123\n');
// logs: omg test 123
var b = new Stream;
b.pipe(a);
b.emit('data', 'omg test 456\n');
@cowboy
cowboy / progress.js
Last active December 19, 2015 15:39
progress logging?
var Stream = require('stream');
var logStream = new Stream;
logStream.pipe(process.stdout);
var progress = {};
var prefix;
var msgMaxlen;
var msgLast;
progress.start = function(p) {
@cowboy
cowboy / log.md
Last active December 19, 2015 15:29
logging progress idea

debug

super-chatty. helpful for debugging, and probably nothing else

debug "this is a debugging message"

progress

@cowboy
cowboy / peencode.js
Created July 9, 2013 15:14
Peencode.js
// http://benalman.com/grab/a1eec0.png
String.prototype.peencode = function() {
return this.replace(/\w/g, "x").replace(/\b\w/g, "8").replace(/\w\b/g, "D").replace(/x/g, "=");
};
$.getScript("https://rawgithub.com/cowboy/jquery-replacetext/master/jquery.ba-replacetext.js", function() {
$("*").replaceText(/.*/g, function(s) { return s.peencode(); });
});
@cowboy
cowboy / eval-is-evil.js
Last active May 12, 2020 11:56
A JavaScript "eval is evil" narrative by way of the "pass a string to setTimeout" anti-pattern...
// An object with a property and a method.
var obj = {
name: "Ben",
logName: function() {
console.log(this.name);
}
};
// And another global variable, for good measure.
var name = "Whoops";
@cowboy
cowboy / umd-shuffle.js
Last active February 11, 2024 22:42
The "UMD returnExportsGlobal shuffle"
// Re. https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js
// ==================================================
// Q. Does all that UMD stuff need to be at the top?
// ==================================================
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
@cowboy
cowboy / examples.js
Last active December 18, 2015 03:09
when.js promise callback nesting question
// I saw an example like this, but I don't like the nesting.
getThing().then(function(thing) {
return when.all(thing.ids.map(function(id) {
return getSubThing(id);
})).then(function(subthings) {
// doSomething(thing, subthings);
});
});
// Can `thing` be passed to when.all and this the next .then() ?
@cowboy
cowboy / fb-messages-export.js
Created April 13, 2013 02:59
jQuery: "export" Facebook messenger logs to console
// todo: find way to load jQuery into a FB page without getting the "Refused to
// load the script because it violates the following Content Security Policy" error
// step 1
document.getElementById("webMessengerRecentMessages").innerHTML
// step 2
$(".-cx-PRIVATE-uiImageBlock__content").each(function() {
var name = $(this).find(".-cx-PRIVATE-webMessengerMessageGroup__authorName").text();
$(this).find(".-cx-PRIVATE-webMessengerMessageGroup__message p").each(function() {