Skip to content

Instantly share code, notes, and snippets.

View fernandosavio's full-sized avatar

Fernando Sávio fernandosavio

View GitHub Profile
function getFnName(fn) {
var f = typeof fn == 'function';
var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/));
return (!f && 'not a function') || (s && s[1] || 'anonymous');
}
console.log(getFnName(String)); // 'String'
console.log(getFnName(function test(){})); // 'test'
console.log(getFnName(function (){})); // 'anonymous'
import operator
def fact(n, total=1):
if n <= 1:
return total
return fact(n-1, n * total)
def fact(n):
@fernandosavio
fernandosavio / toBinaryString.js
Created June 20, 2014 18:14
A simple function to print a Number variable to binary strings without strip leading zeros.
Number.prototype.toBinaryString = function(){
var bit = 32, s = "";
while(bit--)
s += ((this >>> bit) & 1);
return s
}
@fernandosavio
fernandosavio / pt-br.js
Created September 1, 2014 13:09
Locale pt-br of Moment.js
// moment.js locale configuration
// locale : brazilian portuguese (pt-br)
// author : Caio Ribeiro Pereira : https://github.com/caio-ribeiro-pereira
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['moment'], factory); // AMD
} else if (typeof exports === 'object') {
module.exports = factory(require('../moment')); // Node
} else {
@fernandosavio
fernandosavio / Gabarito Prova C.md
Last active October 21, 2021 20:51
Responde e explicando a prova do site http://www.cprogressivo.net/2013/03/Questoes-com-gabarito-sobre-Ponteiros-em-C.html (O que eu souber, é claro)
@fernandosavio
fernandosavio / fibonacci_nth.js
Last active January 13, 2017 03:49
Find the N-th fibonacci number using Binet's Formula.
// http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html
function fibonacci_nth(i){
var v5 = Math.sqrt(5),
Phi = ( v5 + 1 ) / 2,
phi = Phi-1;
return Math.round( Math.pow(Phi, i) / v5 - Math.pow(-phi, i) / v5 );
}
<?php
function teste_unicode_regex($value) {
$min = (preg_match("/\\p{Ll}/u", $value)) ? "Sim" : "Não" ;
$mai = (preg_match("/\\p{Lu}/u", $value)) ? "Sim" : "Não" ;
$num = (preg_match("/\\d/", $value)) ? "Sim" : "Não" ;
echo <<<TEXT
=== $value ===
@fernandosavio
fernandosavio / unserialize.php
Last active November 26, 2015 18:19 — forked from phred/unserialize.php
Simple reliable and non-regex method to unserialize PHP session data
<?
//
// This is the result of about an hour's delving into PHP's hairy-ass serialization internals.
// PHP provides a session_decode function, however, it's only useful for setting the contents of
// $_SESSION. Say, for instance, you want to decode the session strings that PHP stores in its
// session files -- session_decode gets you nowhere.
//
// There are a bunch of nasty little solutions on the manual page[1] that use pretty hairy regular
// expressions to get the job done, but I found a simple way to use PHP's unserialize and recurse
// through the string extracting all of the serialized bits along the way.
@fernandosavio
fernandosavio / hightlight-gist-line.jquery.js
Last active January 26, 2017 13:29
Line highlighting for embedded Gists.
// jQuery version
$(document).on('click', '.js-line-number', function(event){
var elem = $(this).next('.js-file-line'),
bg_color = elem.css('backgroundColor'),
color_active = 'rgb(248, 238, 199)';
elem.css('backgroundColor', bg_color === color_active ? 'rgba(0, 0, 0, 0)' : color_active);
});
var elem = $(document.location.hash).trigger('click');
@fernandosavio
fernandosavio / date_validation.js
Created February 7, 2017 12:55
Validate a date (DD/MM/YYYY).
function valid_date(str_data) {
var regex = /^\d{2}\/\d{2}\/\d{4}$/,
d, m, y;
str_data = str_data.trim();
if (!regex.test(str_data)) {
return false
}