Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
joshuabaker / gist:1217961
Created September 14, 2011 22:15
hereDoc hack for javascript
function hereDoc(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
// usage
var tennysonQuote = hereDoc(function() {/*!
Theirs not to make reply,
@neilk
neilk / nonBlockingForEach.js
Last active November 25, 2021 11:06
Works a little bit like `Array.prototype.forEach()`, but uses `process.nextTick` to allow other processes to execute. NOTE. This isn't meant to be practical; if you use this in production code you're probably doing it wrong.
/**
* Allow other processes to execute while iterating over
* an array. Useful for large arrays, or long-running processing
*
* @param {Function} fn iterator fed each element of the array.
* @param {Function} next executed when done
*/
Array.prototype.nonBlockingForEach = function(fn, next) {
var arr = this;
var i = 0;
@padolsey
padolsey / makeInterpolator.js
Last active February 21, 2024 11:50
Dead simple straight-up performant interpolation
/**
* Outputs a new function with interpolated object property values.
* Use like so:
* var fn = makeInterpolator('some/url/{param1}/{param2}');
* fn({ param1: 123, param2: 456 }); // => 'some/url/123/456'
*/
var makeInterpolator = (function() {
var rc = {
'\n': '\\n', '\"': '\\\"',
'\u2028': '\\u2028', '\u2029': '\\u2029'
@dexygen
dexygen / meek.js
Last active December 20, 2015 03:09
Genuinely Unobtrusive Javascript
(function(scope) {
function destroyAutoGlobals(options) {
var allElements = document.getElementsByTagName("*"), elementId;
for (var i=allElements.length; i--; ) {
elementId = allElements[i].id;
if (elementId && window[elementId] instanceof HTMLElement) {
options && options.verbose && console.log('Destroying window["' + elementId + '"]');
window[elementId] = null;
@jamonholmgren
jamonholmgren / user_controller.rb
Created October 23, 2013 19:06
Cleaner params
class UserController < ApplicationController
def create
@user = User.create(UserInput.new(params).create)
end
def update
@user = User.find(params[:id].to_i)
@user.update_attributes(UserInput.new(params).update)
end
@sstur
sstur / dom-to-json.js
Last active October 8, 2023 04:17
Stringify DOM nodes using JSON (and revive again)
function toJSON(node) {
let propFix = { for: 'htmlFor', class: 'className' };
let specialGetters = {
style: (node) => node.style.cssText,
};
let attrDefaultValues = { style: '' };
let obj = {
nodeType: node.nodeType,
};
if (node.tagName) {
@calvinmetcalf
calvinmetcalf / es5-readable.js
Created January 13, 2015 15:35
whatwg readable streams translated to es5
var Promise = global.Promise || require('lie');
function noop(){}
function typeIsObject(x) {
return (typeof x === 'object' && x !== null) || typeof x === 'function';
}
function ExclusiveStreamReader () {
throw new Error('not implimented');
}
function ReadableStream(opts) {
var start = opts.start || noop;
@WebReflection
WebReflection / html-escape.md
Last active August 21, 2022 16:27
How to escape and unescape from a language to another

update

I've created a little repository that simply exposes the final utility as npm module.

It's called html-escaper


there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.

var path = require('path');
var fs = require('fs');
var cheerio = require('cheerio');
var createTemplate = function(id, markup) {
var $ = cheerio.load('<script type="text/ng-template"></script>');
$('script').attr('id', id).html(markup).html();
return $.html();
};
@cjohansen
cjohansen / reactorama.js
Created May 27, 2015 10:07
createComponent is 18 lines of JavaScript that puts a lean and enjoyable interface on top of React's somewhat clunky and non-JSX-hostile API.
// Most components are defined fully by their render function,
// and all they need to access is the props
var myComponent = createComponent(function (props) {
return React.DOM.h1({}, "Hello " + props.name);
});
// ...which can be done very succinctly with ES6:
const {h1, div} = React.DOM;
const myComponent = createComponent(({name}) => h1({}, `Hello ${name}`));