Skip to content

Instantly share code, notes, and snippets.

View ianldgs's full-sized avatar
💻
Hacking

Ian Luca ianldgs

💻
Hacking
View GitHub Profile
@ianldgs
ianldgs / trait.js
Last active August 29, 2015 14:21
Simulating traits on javascript
'use strict'
var trait = {}
trait.whatever = function (context) {
context.prototype.exec = function () {
console.log(this.i)
}
}
@ianldgs
ianldgs / requestAnimationFrame.js
Created May 27, 2015 13:02
Polyfill all browsers requestAnimationFrame
window.requestAnimationFrame = function(){
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback){
window.setTimeout(callback, (fadeInDuration + fadeOutDuration));
}
@ianldgs
ianldgs / isCyclic.js
Last active January 3, 2023 15:25
Check if JSON has circular reference
function isCyclic (obj) {
var seenObjects = [];
function detect (obj) {
if (obj && typeof obj === 'object') {
if (seenObjects.indexOf(obj) !== -1) {
return true;
}
seenObjects.push(obj);
for (var key in obj) {
@ianldgs
ianldgs / keepAlive.js
Last active August 29, 2015 14:22
Keep mysql connection alive
var con
var keepAlive = function () {
console.log('Trying MySQL connection')
con = mysql.createConnection({host: 'localhost', user: 'root', password: '', database: 'default'})
con.connect(function (err) {
if (err) return console.log('MySQL connection failed', err.stack)
console.log('Connected to MySQL')
@ianldgs
ianldgs / getLine.js
Last active August 29, 2015 14:22
Get current line
function getLine() {
var s = new Error().stack,
r = /at\s.*:([\d]*):/g
r.exec(s)
return r.exec(s)[1]
}
@ianldgs
ianldgs / cancel_delete.sql
Last active April 16, 2018 16:53
Cancel delete mysql
DELIMITER |
CREATE TRIGGER cancel_delete BEFORE DELETE ON table
FOR EACH ROW
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'DELETE canceled';
END |
DELIMITER ;
@ianldgs
ianldgs / sha1.cs
Created June 4, 2015 02:14
Encriptografa uma string em sha1
using System.Security.Cryptography;
string hash(string str)
{
try
{
SHA1 sha1 = SHA1.Create();
byte[] hashData = sha1.ComputeHash(Encoding.UTF8.GetBytes(str));
return BitConverter.ToString(hashData).Replace("-", "").ToLower();
}
@ianldgs
ianldgs / clone.js
Created June 5, 2015 12:41
Clone a JavaScript Object
function clone(obj) {
return Object.create({o: obj}).o
}
function rand(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
@ianldgs
ianldgs / cpf.js
Last active August 29, 2015 14:22
function randomiza(n) {
return Math.round(Math.random()*n);
}
function mod(dividendo,divisor) {
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}
function gerarCPF(comPontos) {
var n = 9;