Skip to content

Instantly share code, notes, and snippets.

View victornpb's full-sized avatar
⌨️

Victor victornpb

⌨️
View GitHub Profile
@victornpb
victornpb / MyConstructor.js
Created April 23, 2014 04:27
Javascript Constructor template
var MyConstructor = (function(){
function Constructor(options){
this.name = "foo";
}
//methods
Constructor.prototype = {
getName: function(){
@victornpb
victornpb / ajaxLoad.js
Created April 23, 2014 04:45
ajaxLoad(element, url, parameters)
function ajaxLoad(element, url, parameters){
if(typeof element=="string") element=document.querySelector(element);
var Ajax=(function(){
try{return new XMLHttpRequest()}
catch(e){try{return new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){try {return new ActiveXObject("Microsoft.XMLHTTP")}
catch(e){ return null; }}}
})();
if(Ajax){
@victornpb
victornpb / DomReady.js
Created April 23, 2014 04:55
DomReady
var DomReady = new function(){
var listeners = [];
var done = 0;
function onDomReady(e){
if(e.type=='readystatechange' && (document.readyState!='interactive' && document.readyState!='complete')) return;
if(!done++)
for(var i=0;i<listeners.length;i++){
@victornpb
victornpb / insaneLoop.js
Created June 27, 2014 05:26
insaneLoop
/**
* An alternative to setInterval and requestAnimationFrame that will call insanely fast for testing purposes.
* This will alternate between syncronous/blocking and assyncrounous non-blocking execution.
* Be very careful using this, this can freeze your browser.
*
* Your callback should return true in order to keep the loop running, return false to stop.
*/
function insaneLoop(callback, maxBlockingExecutionTime, assyncInterval){
maxBlockingExecutionTime = maxBlockingExecutionTime | 50;
@victornpb
victornpb / pedrapapeltesoura.c
Last active April 29, 2024 05:10
Joguinho do Pedra-papel-tesoura criado por Victor B. em 1-dec-2014 para ministrar linguagem C de programação aos alunos do curso de robotica e automacao 2014 no UNICERP.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define PEDRA 1
#define PAPEL 2
#define TESOURA 3
int pontosJogador;
int pontosComputador;
@victornpb
victornpb / caracoroa.c
Created December 4, 2014 21:57
Joguinho do cara-coroa desenvolvido durante a aula 4-dec-2014 no curso de linguagem C do curso de robotica e automacao 2014 no UNICERP. por Victor B.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define CARA 1
#define COROA 2
int pontosJogador;
int pontosComputador;
var compileTemplate = function(html) {
var re = /{{([^}]*(?:}[^}]+)*}*)}}/g,
reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g,
code = 'var r=[];\n', cursor = 0, match;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* AES implementation in JavaScript (c) Chris Veness 2005-2014 / MIT Licence */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* jshint node:true *//* global define */
'use strict';
/**
* AES (Rijndael cipher) encryption routines,
@victornpb
victornpb / getDeepVal.js
Last active February 22, 2019 14:36
access a object value using a path like getDeepVal(obj, "foo.bar.baz")
/**
* Access a deep value inside an object
* Works by passing a path like "foo.bar", also works with nested arrays like "foo[0][1].baz"
* @author Victor B. https://gist.github.com/victornpb/4c7882c1b9d36292308e
* Unit tests: http://jsfiddle.net/Victornpb/0u1qygrh/
* @param {any} object Any object
* @param {string} path Property path to access e.g.: "foo.bar[0].baz.1.a.b"
* @param {any} [defaultValue=undefined] Optional value to return when property doesn't exist or is undefined
* @return {any}
*/
@victornpb
victornpb / String.format().js
Last active January 10, 2023 17:17
String.format() that supports argument list, array, objects, and nested objects.
if (!String.prototype.format) {
/**
* formats a string replacing tokens with an argument list, array, objects, and nested objects.
* @args Can be a list of arguments, array or object
*
* Usage:
* "hello {0} world {0}!".format("foo", "bar"); //"hello foo world bar"
* "hello {0} world {0}!".format(["foo", "bar"]); //"hello foo world bar"
* "hello {name} world {test}!".format({name: "foo", test: "bar"}); //"hello foo world bar"
* "hello {obj.name} world {obj.test[0]}!".format({obj:{name: "foo", test: ["bar"]}}); //"hello foo world bar"