Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / README.md
Last active August 29, 2015 14:13
A simple approach for handling the dom-ready event

A simple approach for handling the dom-ready event

This script is a simple, lazy and hacky replacement for dealing with the dom-ready event (DOMContentLoaded and friends). Basically, it relies on DOM parsing and script execution order by simply putting a script tag at the very end of the document and having it call a list of registered callbacks.

Because of the way the DOM rendering process works, we can guarantee that by the time that script gets executed the DOM is fully ready for use.

Usage

@shesek
shesek / install.sh
Created September 15, 2014 09:12
Install all nodejs packages contained in sub-directories
find . -type f -name package.json ! -path ./package.json ! -path '*node_modules*' | xargs -I '{}' sh -c 'cd $(dirname {}) && npm install'
@shesek
shesek / merge-connect.coffee
Last active December 26, 2015 10:19
Merge multiple connect middlewares into a single middleware that calls them in order
connect_middlewares = (fns...) -> (req, res, out) ->
curr = 0
do next = (err=null) ->
return out err if err? or not fn = fns[curr++]
fn req, res, next
sequence = (errcb) -> (fn, a..., cb) -> fn a..., (err, b...) -> if err? then errcb err else cb b...
compile = (files, callback) ->
seq = sequence callback
stored <- seq async.sortBy, files, @sort
mapped <- seq async.map, sorted, @map
reduced <- seq async.reduce, mapped, null, @reduce
<- seq @write, reduced
do @log
callback? @getClassName()
@shesek
shesek / bitcoinjs-lib-nodejs.sh
Last active July 2, 2017 16:53
Package bitcoinjs-lib as a nodejs module (VERY hacky, but good enough for now)
#!/bin/bash
# Package bitcoinjs-lib as a nodejs module
# Install bitcoinjs first (`npm install bitcoinjs-lib`),
# than run this in your app's directory.
TARGET=lib/bitcoinjs-lib.js
# Wrap all of bitcoinjs-lib and its dependencies in an IIFE
# and export the Bitcoin, Crypto and BigInteger objects.
@shesek
shesek / deterministic.coffee
Last active June 26, 2017 00:48
Bitcoin type-2 deterministic keys for nodejs/javascript, implemented in CoffeeScript
# Requires [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib/)
# (see https://gist.github.com/shesek/5847524 for getting it running under nodejs)
# Released under the MIT license
{ Address } = Bitcoin
{ SHA256 } = Crypto
sha256b = (bytes) -> SHA256 bytes, asBytes: true
lpad = (size, bytes) -> bytes.unshift 0x00 while bytes.length<size; bytes
@shesek
shesek / bitcoin-decoderawtransaction.coffee
Last active January 9, 2018 15:18
Decode Bitcoin raw transactions to bitcoinjs-lib's Transaction object with CoffeeScript
decode_raw_tx = do ->
{ Transaction, TransactionIn, TransactionOut } = Bitcoin
{ bytesToBase64 } = Crypto.util
# Parse an bytearray of length `size` as an integer
# Works for numbers up to 32-bit only
parse_int = (size) -> (bytes) ->
n = 0
n += (bytes.shift() & 0xff) << (8 * i) for i in [0...size]
(function(){var e;e=function(e){var t,n;t=+(((n=e.match(/^\s*(?:#\d+)?\s*\(([\d\.]+)\)/))!=null?n[1]:void 0)||0);if(t===t){return t}else{return 0}};$(".list").each(function(){var t,n,r;t=$(this);n=t.find(".list-header h2");r=t.find(".list-card-title:visible").get().reduce(function(t,n){return t+e(n.innerText)},0);return n.find(".total").remove().end().append($("<span>").addClass("total").text(r))});return function(e){return $("<style>").attr({type:"text/css"}).text(e).appendTo("head")}(".list-header .total { float: right }")})()
serialize = (fn) -> JSON.stringify body: ''+fn, context: fn._context?() or {}
unserialize = (str) ->
{ body, context } = JSON.parse str
args = (Object.keys context).join(', ')
vals = (v for k, v of context).map(JSON.stringify).join(', ')
eval "(function(#{args}) { return #{body}; })(#{vals})"
ctx = (ctx_fn, fn) -> fn._context = ctx_fn; fn
@shesek
shesek / express-subroute.coffee
Last active December 14, 2015 17:28
Express sub routes
###
## MOVED TO https://github.com/shesek/express-subroute
###
methods = require 'methods'
curryThis = (fn) -> (a...) -> fn this, a...
isString = (o) -> (Object::toString.call o) is '[object String]'
path_methods = [ methods..., 'use', 'all' ]