Skip to content

Instantly share code, notes, and snippets.

@dhruvio
Last active August 29, 2015 14:16
Show Gist options
  • Save dhruvio/ba99d76731f9ccaabaa2 to your computer and use it in GitHub Desktop.
Save dhruvio/ba99d76731f9ccaabaa2 to your computer and use it in GitHub Desktop.
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Rebuild to run it on the right
var q = require("qwery"),
b = require("bonzo");
b(q("body")).html("hello");
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({qwery:[function(require,module,exports){(function(name,context,definition){if(typeof module!="undefined"&&module.exports)module.exports=definition();else if(typeof define=="function"&&define.amd)define(definition);else context[name]=definition()})("qwery",this,function(){var classOnly=/^\.([\w\-]+)$/,doc=document,win=window,html=doc.documentElement,nodeType="nodeType";var isAncestor="compareDocumentPosition"in html?function(element,container){return(container.compareDocumentPosition(element)&16)==16}:function(element,container){container=container==doc||container==window?html:container;return container!==element&&container.contains(element)};function toArray(ar){return[].slice.call(ar,0)}function isNode(el){var t;return el&&typeof el==="object"&&(t=el.nodeType)&&(t==1||t==9)}function arrayLike(o){return typeof o==="object"&&isFinite(o.length)}function flatten(ar){for(var r=[],i=0,l=ar.length;i<l;++i)arrayLike(ar[i])?r=r.concat(ar[i]):r[r.length]=ar[i];return r}function uniq(ar){var a=[],i,j;label:for(i=0;i<ar.length;i++){for(j=0;j<a.length;j++){if(a[j]==ar[i]){continue label}}a[a.length]=ar[i]}return a}function normalizeRoot(root){if(!root)return doc;if(typeof root=="string")return qwery(root)[0];if(!root[nodeType]&&arrayLike(root))return root[0];return root}function qwery(selector,opt_root){var m,root=normalizeRoot(opt_root);if(!root||!selector)return[];if(selector===win||isNode(selector)){return!opt_root||selector!==win&&isNode(root)&&isAncestor(selector,root)?[selector]:[]}if(selector&&arrayLike(selector))return flatten(selector);if(doc.getElementsByClassName&&selector=="string"&&(m=selector.match(classOnly))){return toArray(root.getElementsByClassName(m[1]))}if(selector&&(selector.document||selector.nodeType&&selector.nodeType==9)){return!opt_root?[selector]:[]}return toArray(root.querySelectorAll(selector))}qwery.uniq=uniq;return qwery},this)},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({bonzo:[function(require,module,exports){(function(name,context,definition){if(typeof module!="undefined"&&module.exports)module.exports=definition();else if(typeof define=="function"&&define.amd)define(definition);else context[name]=definition()})("bonzo",this,function(){var win=window,doc=win.document,html=doc.documentElement,parentNode="parentNode",specialAttributes=/^(checked|value|selected|disabled)$/i,specialTags=/^(select|fieldset|table|tbody|tfoot|td|tr|colgroup)$/i,simpleScriptTagRe=/\s*<script +src=['"]([^'"]+)['"]>/,table=["<table>","</table>",1],td=["<table><tbody><tr>","</tr></tbody></table>",3],option=["<select>","</select>",1],noscope=["_","",0,1],tagMap={thead:table,tbody:table,tfoot:table,colgroup:table,caption:table,tr:["<table><tbody>","</tbody></table>",2],th:td,td:td,col:["<table><colgroup>","</colgroup></table>",2],fieldset:["<form>","</form>",1],legend:["<form><fieldset>","</fieldset></form>",2],option:option,optgroup:option,script:noscope,style:noscope,link:noscope,param:noscope,base:noscope},stateAttributes=/^(checked|selected|disabled)$/,hasClass,addClass,removeClass,uidMap={},uuids=0,digit=/^-?[\d\.]+$/,dattr=/^data-(.+)$/,px="px",setAttribute="setAttribute",getAttribute="getAttribute",features=function(){var e=doc.createElement("p");return{transform:function(){var props=["transform","webkitTransform","MozTransform","OTransform","msTransform"],i;for(i=0;i<props.length;i++){if(props[i]in e.style)return props[i]}}(),classList:"classList"in e}}(),whitespaceRegex=/\s+/,toString=String.prototype.toString,unitless={lineHeight:1,zoom:1,zIndex:1,opacity:1,boxFlex:1,WebkitBoxFlex:1,MozBoxFlex:1},query=doc.querySelectorAll&&function(selector){return doc.querySelectorAll(selector)};function getStyle(el,property){var value=null,computed=doc.defaultView.getComputedStyle(el,"");computed&&(value=computed[property]);return el.style[property]||value}function isNode(node){return node&&node.nodeName&&(node.nodeType==1||node.nodeType==11)}function normalize(node,host,clone){var i,l,ret;if(typeof node=="string")return bonzo.create(node);if(isNode(node))node=[node];if(clone){ret=[];for(i=0,l=node.length;i<l;i++)ret[i]=cloneNode(host,node[i]);return ret}return node}function classReg(c){return new RegExp("(^|\\s+)"+c+"(\\s+|$)")}function each(ar,fn,opt_scope,opt_rev){var ind,i=0,l=ar.length;for(;i<l;i++){ind=opt_rev?ar.length-i-1:i;fn.call(opt_scope||ar[ind],ar[ind],ind,ar)}return ar}function deepEach(ar,fn,opt_scope){for(var i=0,l=ar.length;i<l;i++){if(isNode(ar[i])){deepEach(ar[i].childNodes,fn,opt_scope);fn.call(opt_scope||ar[i],ar[i],i,ar)}}return ar}function camelize(s){return s.replace(/-(.)/g,function(m,m1){return m1.toUpperCase()})}function decamelize(s){return s?s.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase():s}function data(el){el[getAttribute]("data-node-uid")||el[setAttribute]("data-node-uid",++uuids);var uid=el[getAttribute]("data-node-uid");return uidMap[uid]||(uidMap[uid]={})}function clearData(el){var uid=el[getAttribute]("data-node-uid");if(uid)delete uidMap[uid]}function dataValue(d){var f;try{return d===null||d===undefined?undefined:d==="true"?true:d==="false"?false:d==="null"?null:(f=parseFloat(d))==d?f:d}catch(e){}return undefined}function some(ar,fn,opt_scope){for(var i=0,j=ar.length;i<j;++i)if(fn.call(opt_scope||null,ar[i],i,ar))return true;return false}function styleProperty(p){p=="transform"&&(p=features.transform)||/^transform-?[Oo]rigin$/.test(p)&&(p=features.transform+"Origin");return p?camelize(p):null}function insert(target,host,fn,rev){var i=0,self=host||this,r=[],nodes=query&&typeof target=="string"&&target.charAt(0)!="<"?query(target):target;each(normalize(nodes),function(t,j){each(self,function(el){fn(t,r[i++]=j>0?cloneNode(self,el):el)},null,rev)},this,rev);self.length=i;each(r,function(e){self[--i]=e},null,!rev);return self}function xy(el,x,y){var $el=bonzo(el),style=$el.css("position"),offset=$el.offset(),rel="relative",isRel=style==rel,delta=[parseInt($el.css("left"),10),parseInt($el.css("top"),10)];if(style=="static"){$el.css("position",rel);style=rel}isNaN(delta[0])&&(delta[0]=isRel?0:el.offsetLeft);isNaN(delta[1])&&(delta[1]=isRel?0:el.offsetTop);x!=null&&(el.style.left=x-offset.left+delta[0]+px);y!=null&&(el.style.top=y-offset.top+delta[1]+px)}if(features.classList){hasClass=function(el,c){return el.classList.contains(c)};addClass=function(el,c){el.classList.add(c)};removeClass=function(el,c){el.classList.remove(c)}}else{hasClass=function(el,c){return classReg(c).test(el.className)};addClass=function(el,c){el.className=(el.className+" "+c).trim()};removeClass=function(el,c){el.className=el.className.replace(classReg(c)," ").trim()}}function setter(el,v){return typeof v=="function"?v.call(el,el):v}function scroll(x,y,type){var el=this[0];if(!el)return this;if(x==null&&y==null){return(isBody(el)?getWindowScroll():{x:el.scrollLeft,y:el.scrollTop})[type]}if(isBody(el)){win.scrollTo(x,y)}else{x!=null&&(el.scrollLeft=x);y!=null&&(el.scrollTop=y)}return this}function Bonzo(elements){this.length=0;if(elements){elements=typeof elements!=="string"&&!elements.nodeType&&typeof elements.length!=="undefined"?elements:[elements];this.length=elements.length;for(var i=0;i<elements.length;i++)this[i]=elements[i]}}Bonzo.prototype={get:function(index){return this[index]||null},each:function(fn,opt_scope){return each(this,fn,opt_scope)},deepEach:function(fn,opt_scope){return deepEach(this,fn,opt_scope)},map:function(fn,opt_reject){var m=[],n,i;for(i=0;i<this.length;i++){n=fn.call(this,this[i],i);opt_reject?opt_reject(n)&&m.push(n):m.push(n)}return m},html:function(h,opt_text){var method=opt_text?"textContent":"innerHTML",that=this,append=function(el,i){each(normalize(h,that,i),function(node){el.appendChild(node)})},updateElement=function(el,i){try{if(opt_text||typeof h=="string"&&!specialTags.test(el.tagName)){return el[method]=h}}catch(e){}append(el,i)};return typeof h!="undefined"?this.empty().each(updateElement):this[0]?this[0][method]:""},text:function(opt_text){return this.html(opt_text,true)},append:function(node){var that=this;return this.each(function(el,i){each(normalize(node,that,i),function(i){el.appendChild(i)})})},prepend:function(node){var that=this;return this.each(function(el,i){var first=el.firstChild;each(normalize(node,that,i),function(i){el.insertBefore(i,first)})})},appendTo:function(target,opt_host){return insert.call(this,target,opt_host,function(t,el){t.appendChild(el)})},prependTo:function(target,opt_host){return insert.call(this,target,opt_host,function(t,el){t.insertBefore(el,t.firstChild)},1)},before:function(node){var that=this;return this.each(function(el,i){each(normalize(node,that,i),function(i){el[parentNode].insertBefore(i,el)})})},after:function(node){var that=this;return this.each(function(el,i){each(normalize(node,that,i),function(i){el[parentNode].insertBefore(i,el.nextSibling)},null,1)})},insertBefore:function(target,opt_host){return insert.call(this,target,opt_host,function(t,el){t[parentNode].insertBefore(el,t)})},insertAfter:function(target,opt_host){return insert.call(this,target,opt_host,function(t,el){var sibling=t.nextSibling;sibling?t[parentNode].insertBefore(el,sibling):t[parentNode].appendChild(el)},1)},replaceWith:function(node){var that=this;return this.each(function(el,i){each(normalize(node,that,i),function(i){el[parentNode]&&el[parentNode].replaceChild(i,el)})})},clone:function(opt_host){var ret=[],l,i;for(i=0,l=this.length;i<l;i++)ret[i]=cloneNode(opt_host||this,this[i]);return bonzo(ret)},addClass:function(c){c=toString.call(c).split(whitespaceRegex);return this.each(function(el){each(c,function(c){if(c&&!hasClass(el,setter(el,c)))addClass(el,setter(el,c))})})},removeClass:function(c){c=toString.call(c).split(whitespaceRegex);return this.each(function(el){each(c,function(c){if(c&&hasClass(el,setter(el,c)))removeClass(el,setter(el,c))})})},hasClass:function(c){c=toString.call(c).split(whitespaceRegex);return some(this,function(el){return some(c,function(c){return c&&hasClass(el,c)})})},toggleClass:function(c,opt_condition){c=toString.call(c).split(whitespaceRegex);return this.each(function(el){each(c,function(c){if(c){typeof opt_condition!=="undefined"?opt_condition?!hasClass(el,c)&&addClass(el,c):removeClass(el,c):hasClass(el,c)?removeClass(el,c):addClass(el,c)}})})},show:function(opt_type){opt_type=typeof opt_type=="string"?opt_type:"";return this.each(function(el){el.style.display=opt_type})},hide:function(){return this.each(function(el){el.style.display="none"})},toggle:function(opt_callback,opt_type){opt_type=typeof opt_type=="string"?opt_type:"";typeof opt_callback!="function"&&(opt_callback=null);return this.each(function(el){el.style.display=el.offsetWidth||el.offsetHeight?"none":opt_type;opt_callback&&opt_callback.call(el)})},first:function(){return bonzo(this.length?this[0]:[])},last:function(){return bonzo(this.length?this[this.length-1]:[])},next:function(){return this.related("nextSibling")},previous:function(){return this.related("previousSibling")},parent:function(){return this.related(parentNode)},related:function(method){return bonzo(this.map(function(el){el=el[method];while(el&&el.nodeType!==1){el=el[method]}return el||0},function(el){return el}))},focus:function(){this.length&&this[0].focus();return this},blur:function(){this.length&&this[0].blur();return this},css:function(o,opt_v){var p,iter=o;if(opt_v===undefined&&typeof o=="string"){opt_v=this[0];if(!opt_v)return null;if(opt_v===doc||opt_v===win){p=opt_v===doc?bonzo.doc():bonzo.viewport();return o=="width"?p.width:o=="height"?p.height:""}return(o=styleProperty(o))?getStyle(opt_v,o):null}if(typeof o=="string"){iter={};iter[o]=opt_v}function fn(el,p,v){for(var k in iter){if(iter.hasOwnProperty(k)){v=iter[k];(p=styleProperty(k))&&digit.test(v)&&!(p in unitless)&&(v+=px);try{el.style[p]=setter(el,v)}catch(e){}}}}return this.each(fn)},offset:function(opt_x,opt_y){if(opt_x&&typeof opt_x=="object"&&(typeof opt_x.top=="number"||typeof opt_x.left=="number")){return this.each(function(el){xy(el,opt_x.left,opt_x.top)})}else if(typeof opt_x=="number"||typeof opt_y=="number"){return this.each(function(el){xy(el,opt_x,opt_y)})}if(!this[0])return{top:0,left:0,height:0,width:0};var el=this[0],de=el.ownerDocument.documentElement,bcr=el.getBoundingClientRect(),scroll=getWindowScroll(),width=el.offsetWidth,height=el.offsetHeight,top=bcr.top+scroll.y-Math.max(0,de&&de.clientTop,doc.body.clientTop),left=bcr.left+scroll.x-Math.max(0,de&&de.clientLeft,doc.body.clientLeft);return{top:top,left:left,height:height,width:width}},dim:function(){if(!this.length)return{height:0,width:0};var el=this[0],de=el.nodeType==9&&el.documentElement,orig=!de&&!!el.style&&!el.offsetWidth&&!el.offsetHeight?function(t){var s={position:el.style.position||"",visibility:el.style.visibility||"",display:el.style.display||""};t.first().css({position:"absolute",visibility:"hidden",display:"block"});return s}(this):null,width=de?Math.max(el.body.scrollWidth,el.body.offsetWidth,de.scrollWidth,de.offsetWidth,de.clientWidth):el.offsetWidth,height=de?Math.max(el.body.scrollHeight,el.body.offsetHeight,de.scrollHeight,de.offsetHeight,de.clientHeight):el.offsetHeight;orig&&this.first().css(orig);return{height:height,width:width}},attr:function(k,opt_v){var el=this[0],n;if(typeof k!="string"&&!(k instanceof String)){for(n in k){k.hasOwnProperty(n)&&this.attr(n,k[n])}return this}return typeof opt_v=="undefined"?!el?null:specialAttributes.test(k)?stateAttributes.test(k)&&typeof el[k]=="string"?true:el[k]:el[getAttribute](k):this.each(function(el){specialAttributes.test(k)?el[k]=setter(el,opt_v):el[setAttribute](k,setter(el,opt_v))})},removeAttr:function(k){return this.each(function(el){stateAttributes.test(k)?el[k]=false:el.removeAttribute(k)})},val:function(s){return typeof s=="string"||typeof s=="number"?this.attr("value",s):this.length?this[0].value:null},data:function(opt_k,opt_v){var el=this[0],o,m;if(typeof opt_v==="undefined"){if(!el)return null;o=data(el);if(typeof opt_k==="undefined"){each(el.attributes,function(a){(m=(""+a.name).match(dattr))&&(o[camelize(m[1])]=dataValue(a.value))});return o}else{if(typeof o[opt_k]==="undefined")o[opt_k]=dataValue(this.attr("data-"+decamelize(opt_k)));return o[opt_k]}}else{return this.each(function(el){data(el)[opt_k]=opt_v})}},remove:function(){this.deepEach(clearData);return this.detach()},empty:function(){return this.each(function(el){deepEach(el.childNodes,clearData);while(el.firstChild){el.removeChild(el.firstChild)}})},detach:function(){return this.each(function(el){el[parentNode]&&el[parentNode].removeChild(el)})},scrollTop:function(y){return scroll.call(this,null,y,"y")},scrollLeft:function(x){return scroll.call(this,x,null,"x")}};function cloneNode(host,el){var c=el.cloneNode(true),cloneElems,elElems,i;if(host.$&&typeof host.cloneEvents=="function"){host.$(c).cloneEvents(el);cloneElems=host.$(c).find("*");elElems=host.$(el).find("*");for(i=0;i<elElems.length;i++)host.$(cloneElems[i]).cloneEvents(elElems[i])}return c}function isBody(element){return element===win||/^(?:body|html)$/i.test(element.tagName)}function getWindowScroll(){return{x:win.pageXOffset||html.scrollLeft,y:win.pageYOffset||html.scrollTop}}function createScriptFromHtml(html){var scriptEl=document.createElement("script"),matches=html.match(simpleScriptTagRe);scriptEl.src=matches[1];return scriptEl}function bonzo(els){return new Bonzo(els)}bonzo.setQueryEngine=function(q){query=q;delete bonzo.setQueryEngine};bonzo.aug=function(o,target){for(var k in o){o.hasOwnProperty(k)&&((target||Bonzo.prototype)[k]=o[k])}};bonzo.create=function(node){return typeof node=="string"&&node!==""?function(){if(simpleScriptTagRe.test(node))return[createScriptFromHtml(node)];var tag=node.match(/^\s*<([^\s>]+)/),el=doc.createElement("div"),els=[],p=tag?tagMap[tag[1].toLowerCase()]:null,dep=p?p[2]+1:1,ns=p&&p[3],pn=parentNode;el.innerHTML=p?p[0]+node+p[1]:node;while(dep--)el=el.firstChild;if(ns&&el&&el.nodeType!==1)el=el.nextSibling;do{if(!tag||el.nodeType==1){els.push(el)}}while(el=el.nextSibling);each(els,function(el){el[pn]&&el[pn].removeChild(el)});return els}():isNode(node)?[node.cloneNode(true)]:[]};bonzo.doc=function(){var vp=bonzo.viewport();return{width:Math.max(doc.body.scrollWidth,html.scrollWidth,vp.width),height:Math.max(doc.body.scrollHeight,html.scrollHeight,vp.height)}};bonzo.firstChild=function(el){for(var c=el.childNodes,i=0,j=c&&c.length||0,e;i<j;i++){if(c[i].nodeType===1)e=c[j=i]}return e};bonzo.viewport=function(){return{width:win.innerWidth,height:win.innerHeight}};bonzo.isAncestor="compareDocumentPosition"in html?function(container,element){return(container.compareDocumentPosition(element)&16)==16}:function(container,element){return container!==element&&container.contains(element)};return bonzo})},{}]},{},[]);var q=require("qwery"),b=require("bonzo");b(q("body")).html("hello");
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"qwery": "4.0.0",
"bonzo": "2.0.0"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment