Skip to content

Instantly share code, notes, and snippets.

View ronen-e's full-sized avatar

Ronen Elster ronen-e

  • Tel Aviv, Israel
View GitHub Profile
@ronen-e
ronen-e / 0_reuse_code.js
Created April 29, 2016 11:17
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@ronen-e
ronen-e / download-time.js
Last active June 10, 2016 21:42
Calculate resource download time
/**
* Returns download time from server
*
* @param {number} cwnd - Initial Congestion Window size
* @param {number} rtt - Roundtrip Time in Milliseconds
* @param {number} packetSize - Packet Size in Bytes
* @param {number} totalSize - Total Transfer Size in Bytes
* @return {number} time - Download Time in Milliseconds
*/
function downloadTime(cwnd, rtt, packetSize, totalSize) {
function delegate(element, eventName, selector, handler) {
var el = null;
element.addEventListener(eventName, function delegatedListener(e) {
el = e.target;
while (el !== element && el.parentNode) {
if (el.nodeType === 1 && selectorMatches(el, selector)) {
handler(e, el);
break;
}
const INIT = 'INIT';
function createStore(reducer, initialState) {
var state = initialState;
var subscribers = [];
function getState() {
return state;
}
function dispatch(action) {
@ronen-e
ronen-e / heapsort.js
Created June 11, 2016 11:55
heapsort
// 0 based index
var iParent = (i) => Math.floor((i-1)/2);
var iLeftChild = (i) => 2*i + 1;
var iRightChild = (i) => 2*i + 2;
function heapsort(list, count) {
// first build a heap
maxHeapify(list, count);
@ronen-e
ronen-e / tmpl.js
Created June 13, 2016 22:03
micro-templating
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
// http://ejohn.org/blog/javascript-micro-templating/
(function() {
var cache = {};
function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
@ronen-e
ronen-e / events.js
Created June 18, 2016 21:32
Basic Publisher
var Events = {
on: function(type, fn, context) {
this.events = this.events || {};
this.events[type] = this.events[type] || [];
fn = typeof fn === 'function' ? fn : context[fn];
this.events[type].push({ fn: fn, context: context || this });
return this.off.bind(this, type, fn, context);
},
@ronen-e
ronen-e / a-plus-b.js
Created July 1, 2016 19:12
A plus B CLI
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (buf) {
var tokens = formatInput(buf);
var a = parseInt(tokens[0]);
var b = parseInt(tokens[1]);
console.log(a+b);
process.exit();
});
function formatInput(input) {
@ronen-e
ronen-e / prefix-factory.js
Last active September 17, 2016 22:45
css classnames prefix factory
function prefixFactory(prefix) {
return function prefixer(string) {
return string.split(' ')
.map( s => prefix + s)
.join(' ');
};
}
export default prefixFactory;
@ronen-e
ronen-e / post-css-prefix-webpack-config.js
Last active December 29, 2016 10:48
class prefixer using postcss-loader for webpack
import classPrfx from 'postcss-class-prefix';
import precss from 'precss'; // for scss support
module: {
loaders: [
{
test: /\.scss$/,
loaders: [
'style',
'css',