Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / expandTemplate.js
Last active August 29, 2015 14:03
expand template
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 August 29, 2015 14:03
use this for nodeList to array conversion
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 July 11, 2014 16:37
synchronous AJAX. don't do it :P
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 August 29, 2015 14:03
HTTPS server for tests

prepare structure

mkdir files certs
npm install express

create credentials

fire the console:

@JosePedroDias
JosePedroDias / bufferAsHex.js
Created July 30, 2014 18:11
print buffer as hex string
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 August 1, 2014 15:51
node uriChanged.js <URI_GOES_HERE>
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 / ajax.js
Last active February 27, 2016 03:12
AJAX advanced
function ajax(o) {
'use strict';
var xhr = new XMLHttpRequest();
if (o.withCredentials) { xhr.withCredentials = true; }
xhr.open(o.method || 'GET', o.url, true);
var cbInner = function() {
if (xhr.readyState === 4 && xhr.status > 199 && xhr.status < 300) {
return o.cb(null, JSON.parse(xhr.response));
}
@JosePedroDias
JosePedroDias / urlChanged.go
Created September 26, 2014 04:25
keeps pinging endpoint until response changes
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
@JosePedroDias
JosePedroDias / handleState.js
Last active August 29, 2015 14:07
state w/ location.search and optional history API
(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");
@JosePedroDias
JosePedroDias / index.html
Created October 6, 2014 19:53
commonmark usage demo // source http://jsbin.com/fegihu/5
<!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 }