Skip to content

Instantly share code, notes, and snippets.

View carlosascari's full-sized avatar
:octocat:
Mediatek Hacker

Ascari carlosascari

:octocat:
Mediatek Hacker
  • Home
View GitHub Profile
@carlosascari
carlosascari / EventEmitter.js
Created November 17, 2016 20:58
EventEmiter es6 class intented to extend existing classes
class EventEmitter {
constructor() {
this.cbs = {};
}
on(evt, cb) {
if (evt && typeof cb === 'function') {
if (!this.cbs[evt]) {
this.cbs[evt] = [];
}
this.cbs[evt].push(cb);
@carlosascari
carlosascari / bijection.js
Last active November 13, 2016 21:16
A bijective function implemenation for shortening urls based on their id
// Based on: http://www.geeksforgeeks.org/how-to-design-a-tiny-url-or-url-shortener/
const bijection = module.exports = {};
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');
const base = charset.length;
const lookup = (function(obj){
for (let i = 0, l = charset.length; i < l; i++) {
const char = charset[i];
obj[char] = char.charCodeAt(0);
}
return obj;
@carlosascari
carlosascari / unlinkRecursive.js
Created October 17, 2016 18:37
Unlink/Delete files and folders recursively
const fs = require('fs');
const unlinkRecursive = path => {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file, index) => {
fs.lstatSync(`${path}/${file}`).isDirectory()
? unlinkRecursive(`${path}/${file}`)
: fs.unlinkSync(`${path}/${file}`);
});
fs.rmdirSync(path);
}
@carlosascari
carlosascari / AcrosticRegistration.md
Last active September 12, 2016 17:04
Acrostic Registration - Registering new users with poetry

Acrostic Registration

Registering new users with poetry

An acrostic is a poem (or other form of writing) in which the first letter (or syllable, or word) of each line (or paragraph, or other recurring feature in the text) spells out a word, message or the alphabet.... As a form of constrained writing, an acrostic can be used as a mnemonic device to aid memory retrieval.

A Person chooses a username; ascari for example.

The number of letters will be the number of lines in the poem, beginning each with a letter from the username chosen.

and behold! the

@carlosascari
carlosascari / ezScroller.js
Created May 24, 2016 22:44
Basic automatic scroller with requestAnimationFrame
// @todo https://miketaylr.com/posts/2014/11/document-body-scrolltop.html
let animReference = null;
function _start(loop)
{
animReference = requestAnimationFrame(loop);
}
function _stop()
{
@carlosascari
carlosascari / parseHTML.js
Created March 14, 2016 22:17
A cross browser way of parsing an html string.
/**
* A cross browser way of parsing a html string similar to jQuery's `$(<HTML String>)`
*
* @method parseHTML
* @param htmlString {String}
* @return {HTMLElement}
*/
function parseHTML(htmlString)
{
var html = document.createDocumentFragment()
@carlosascari
carlosascari / multiline.js
Last active March 14, 2016 21:56
Write strings with line breaks in javascript.
/**
* Provides multiline function
*
* Allows you to write strings with line breaks without backtick support.
*
* Simply provide a function with your multi lines script inside a
* multi line comment.
*
* Usage @example
* var result = multiline(function(){
@carlosascari
carlosascari / occurrences.js
Created February 29, 2016 21:43
Count the occurrences of substring in a string.
/**
* Count the occurrences of substring in a string;
*
* @method occurrences
* @param haystack {String}
* @param needle {String}
* @param [allowOverlapping=false] {Boolean}
* @author Vitim.us http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
*/
function occurrences(haystack, needle, allowOverlapping) {
@carlosascari
carlosascari / BresenhamLineAlgorithm.js
Created February 21, 2016 05:57
Bresenham Line Algorithm function returns a Array of points that make up a line.
/**
* Provides Bresenham Line Algorithm in a function.
*
* @reference http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm#JavaScript
*/
/**
* @method bresenham_line
* @param x1 {Number}
* @param y1 {Number}
@carlosascari
carlosascari / rename_function.js
Last active February 19, 2016 06:45
A Hacky way of renaming an existing function
/**
* Renames and existing function.
*
* @private
* @method rename_function
* @param name {String}
* @param fn {Function}
* @return Function
*/
function rename_function(name, fn)