Skip to content

Instantly share code, notes, and snippets.

View dbalduini's full-sized avatar

Diego Balduini dbalduini

  • Brazil
View GitHub Profile
@dbalduini
dbalduini / git_sync_upstream.sh
Created February 15, 2018 17:38
Sync origin with upstream and clean everything locally.
#!/bin/bash
git fetch upstream
## Remove local branches
git remote prune upstream
## Clean all gone local branches
git fetch -p
for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`
@dbalduini
dbalduini / unicode-table.py
Created October 26, 2017 19:54
UTF-8 (Unicode 256) table
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Creates an UTF-8 table, or unicode 256.
# Tests if the table can validate each UTF-8 char.
#
# Resources:
# https://unicode-table.com/en/#latin-1-supplement
# http://www.umich.edu/~umfandsf/other/ebooks/alice30.txt
@dbalduini
dbalduini / knapsack.py
Created October 26, 2017 19:43
Knapsack algorithm
a_weights = [400, 200, 100]
xweights = [100, 200, 300, 500, 100, 200, 600, 100, 100]
W = 500
def one_zero (weights):
d = 0 # Current disk position
c = 0 # number of files in the current disk
disks = []
n = len(weights)
@dbalduini
dbalduini / brute_unicode_search.py
Last active May 19, 2017 18:49
Brute-force unicode text search in python
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Resources:
# https://unicode-table.com/en/#latin-1-supplement
# http://www.umich.edu/~umfandsf/other/ebooks/alice30.txt
control_chars = [ unichr(i) for i in xrange(0, 32) ]
basic_latin = [ unichr(i) for i in xrange(32, 128) ]
latin_1_supplement = [ unichr(i) for i in xrange(128, 256) ]
@dbalduini
dbalduini / fix_sub_delay.py
Last active October 6, 2019 18:16
Add or Remove delay to some SubRip file format (.srt).
#!/usr/bin/python
"""
fix_sub_delay.py:
Add or Remove delay to some SubRip file format (.srt).
@author [email protected]
"""
import sys
import os
import re
from datetime import datetime, timedelta
@dbalduini
dbalduini / filterIndexes.js
Created October 25, 2016 21:00
Underscore filter function which returns the indexes instead
const _ = require('underscore')
function filterIndexes(collection, selector) {
var results = [],
predicate = _.matcher(selector);
// Works just like _.filter, but returns the indexes instead
_.each(collection, function (value, index, list) {
if (predicate(value, index, list)) {
results.push(index);
@dbalduini
dbalduini / persistentTask.js
Created August 26, 2016 13:36
persistent task in node
// The task will be re-runed after delay seconds
// if retry() is called by the caller
persistentTask = function (delay, maxRetries, task) {
function retry () {
if (maxRetries > 0) {
maxRetries = maxRetries - 1;
setTimeout(function () {
task(retry);
}, delay);
} else {
@dbalduini
dbalduini / contentSniffTest.js
Last active July 25, 2016 14:52
Test web site for Content Nniffing security vulnerability
Router.route('/sniff', function() {
var response = this.response;
response.writeHead(200, {
'Content-Type' : 'text/plain',
'X-Content-Type-Options' : 'nosniff' // comment me to redirect the browser
});
response.end('<meta http-equiv="refresh" content="0; url=http://example.com/" />');
}, {
@dbalduini
dbalduini / jquery-mock.js
Last active April 26, 2021 14:05
JQuery stub and mock with Sinon for Unit Tests
(function () {
'use strict';
var sinon = require('sinon');
var keys = ['closest', 'addClass', 'children', 'parent', 'find', 'html',
'remove', 'text', 'ready', 'unveil', 'removeAttr', 'removeClass', 'scroll',
'val', 'height', 'css', 'delay', 'queue', 'dequeue', 'attr'];
function JQueryStub (args) {
var self = this;
@dbalduini
dbalduini / waitPromiseValue.js
Last active May 15, 2016 20:50
Blocking Promise
'use strict'
const Promise = require('bluebird');
const fs = require('fs');
const deasync = require('deasync');
function readFile(f) {
return new Promise(function (resolve, reject) {
let file = fs.readFileSync(f);
resolve(file);