Skip to content

Instantly share code, notes, and snippets.

View fernandosavio's full-sized avatar

Fernando Sávio fernandosavio

View GitHub Profile
@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 / 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
}
import operator
def fact(n, total=1):
if n <= 1:
return total
return fact(n-1, n * total)
def fact(n):
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'
@fernandosavio
fernandosavio / bubble_sort.php
Last active August 29, 2015 13:56
Bubble Sort implementado em PHP com flag se o array já estiver ordenado
<?php
/**
* Bubble Sort implementado em PHP com flag se o array já estiver ordenado
*/
function bubble_sort($arr) {
$flag = false;
while(!$flag) {
$flag = true;
for ($i=1; $i < count($arr); $i++) {
@fernandosavio
fernandosavio / converter.py
Created January 28, 2014 19:37
Script de conversão de vídeos usando `python`, `ffmpeg` e o módulo `argparse` para tratar dos argumentos recebidos por terminal.
#!/usr/bin/env python
# -*- coding: utf8 -*-
import argparse, re
from os import path, getcwd, mkdir, listdir, devnull
from subprocess import Popen
# valida se 'string' segue o padrão de resolução de tela. 9999x9999 ou 9999X9999
def isScreenSize(string):
regex = re.compile(r"^\d+x\d+$", re.IGNORECASE)
@fernandosavio
fernandosavio / validate-date.js
Last active December 22, 2015 18:49
Validação de data
function validateDate(data){
var p = data.split("/"),
isNumber = /^\d{1,2}\/\d{1,2}\/\d{4}$/,
date = new Date(+p[2], +p[1] -1 , +p[0]); // dd/mm/yyy
return isNumber.test(data) && !(date.getDate() !== +p[0] || date.getMonth() !== (+p[1] - 1) || date.getFullYear() !== +p[2]);
};
@fernandosavio
fernandosavio / delayedForEach.js
Last active May 30, 2022 06:44
An Array forEach with a delay between steps.
/**
* An array forEach with a delay between steps.
*
* @param {Function} callback Function to execute for each element. It receives three arguments, the element value, the element index and the array being traversed, respectivily.
* @param {Number} timeout Number of milliseconds that the function call should be delayed by.
* @param {Object} thisArg Object to use as this when executing callback.
* @this {Array}
* @return {undefined}
*/
Array.prototype.delayedForEach = function(callback, timeout, thisArg){
@fernandosavio
fernandosavio / cidades-brasil.json
Last active April 8, 2018 06:04
JSON com as cidades brasileira segundo o IBGE.
{
"Acre": ["Acrel\u00e2ndia","Assis Brasil","Brasil\u00e9ia","Bujari","Capixaba","Cruzeiro do Sul","Epitaciol\u00e2ndia","Feij\u00f3","Jord\u00e3o","M\u00e2ncio Lima","Manoel Urbano","Marechal Thaumaturgo","Pl\u00e1cido de Castro","Porto Acre","Porto Walter","Rio Branco","Rodrigues Alves","Santa Rosa do Purus","Sena Madureira","Senador Guiomard","Tarauac\u00e1","Xapuri"],
"Alagoas": ["\u00c1gua Branca","Anadia","Arapiraca","Atalaia","Barra de Santo Ant\u00f4nio","Barra de S\u00e3o Miguel","Batalha","Bel\u00e9m","Belo Monte","Boca da Mata","Branquinha","Cacimbinhas","Cajueiro","Campestre","Campo Alegre","Campo Grande","Canapi","Capela","Carneiros","Ch\u00e3 Preta","Coit\u00e9 do N\u00f3ia","Col\u00f4nia Leopoldina","Coqueiro Seco","Coruripe","Cra\u00edbas","Delmiro Gouveia","Dois Riachos","Estrela de Alagoas","Feira Grande","Feliz Deserto","Flexeiras","Girau do Ponciano","Ibateguara","Igaci","Igreja Nova","Inhapi","Jacar\u00e9 dos Homens","Jacu\u00edpe","Japaratinga","Jaramataia","Jequi\u00e1 da Praia","Jo
@fernandosavio
fernandosavio / pagination.php
Created May 25, 2013 00:43
Script que retorna um range do números das páginas baseado nos parâmetros recebidos.
<?php
function pagination ($index, $teto, $amplitude = 7, $piso = 1){
$padding = intval($amplitude/2);
$amplitude = min(array($amplitude,$teto));
$left = $padding;
$right = $amplitude - $left - 1;
if($index - $padding < $piso){
$left = $index - $piso;
$right = $amplitude - ($left + 1);