Skip to content

Instantly share code, notes, and snippets.

Avatar
🕉️
all good

José Pedro Dias JosePedroDias

🕉️
all good
View GitHub Profile
@JosePedroDias
JosePedroDias / tpl.js
Last active Aug 29, 2015
dumb tpl expansions
View tpl.js
var fetchFnComment = function(f) {
return f.toString()
.replace(/^[^\/]+\/\*!?/, '')
.replace(/\*\/[^\/]+$/, '');
};
var applyTemplate = function(targetEl, tpl, model) {
tpl = fetchFnComment(tpl);
for (var k in model) {
tpl = tpl.replace( new RegExp('({{'+k+'}})', 'g'), model[k] );
View expandTemplate.js
var expandTemplate = function(tplS, modelO) {
for (var k in modelO) {
tplS = tplS.replace( new RegExp('({{'+k+'}})', 'g'), modelO[k] );
}
return tplS;
};
@JosePedroDias
JosePedroDias / listToArr.js
Last active Aug 29, 2015
use this for nodeList to array conversion
View listToArr.js
var listToArr = function(lst) {
var l = lst.length;
var arr = new Array(l); // optimizable *list-to-array
for (var i = 0; i < l; ++i) {
arr[i] = lst[i];
}
return arr;
};
@JosePedroDias
JosePedroDias / ajaxSync.js
Created Jul 11, 2014
synchronous AJAX. don't do it :P
View ajaxSync.js
var ajax = function(uri) {
var xhr = new XMLHttpRequest();
xhr.open('GET', uri, false);
xhr.send(null);
return xhr.responseText;
};
@JosePedroDias
JosePedroDias / README.md
Last active Aug 29, 2015
HTTPS server for tests
View README.md

prepare structure

mkdir files certs
npm install express

create credentials

fire the console:

@JosePedroDias
JosePedroDias / bufferAsHex.js
Created Jul 30, 2014
print buffer as hex string
View bufferAsHex.js
var i, w, res = [];
for (i = 0; i < b.length; ++i ) {
w = b[i];
res.push( w.toString(16) );
if (i % 2 !== 0) {
res.push(' ');
}
}
console.log( res.join('') );
@JosePedroDias
JosePedroDias / uriChanged.js
Created Aug 1, 2014
node uriChanged.js <URI_GOES_HERE>
View uriChanged.js
var request = require('request');
var timer, firstBody, deltaT, t = 0;
var fetchURI = function(uri, cb) {
request({uri:uri}, function (error, response, body) {
if (error) {
cb(error);
}
@JosePedroDias
JosePedroDias / urlChanged.go
Created Sep 26, 2014
keeps pinging endpoint until response changes
View urlChanged.go
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
@JosePedroDias
JosePedroDias / handleState.js
Last active Aug 29, 2015
state w/ location.search and optional history API
View handleState.js
(function() {
var clone = function(o) {
return JSON.parse( JSON.stringify(o) );
};
var parseQueryString = function(url) {
var aParams = {};
if (url.match(/\?(.+)/i)) {
var queryStr = url.replace(/^(.*)\?([^\#]+)(\#(.*))?/g, "$2");
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>commonmark usage demo</title>
<script src="http://jgm.github.io/stmd/js/stmd.js"></script>
<style id="jsbin-css">
* { box-sizing: border-box }