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 / 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 / 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 / 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 / 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 / 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 / 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 / svg2png.js
Created September 4, 2017 20:31
Converts a svg element into a png image instance
/**
* @param {SVGSVGElement} svg
* @param {Number} [w] Width of png image. Default: 128
* @param {Number} [h] Height of png image. Default: 128
* @return {Image} png image
*/
const svg2png = (svg, w=128, h=128) => {
svg.setAttribute('width', `${w}px`);
svg.setAttribute('height', `${h}px`);
const xml = new XMLSerializer().serializeToString(svg);
@carlosascari
carlosascari / normie.less
Last active November 22, 2017 06:54
Custom css reset (in less) based on normalize.css
/**
* Copyright(c) 2017 Carlos Ascari Gutierrez Hermosillo.
* MIT License.
*/
// Based on normalize.css 7.0.0
// https://github.com/necolas/normalize.css/tree/7.0.0
// + This version allows removing IE support by version as well as
// as hiding rarely used tags.
@carlosascari
carlosascari / osm06.sqlite
Created February 14, 2018 18:59
OpenStreetMap database schema for api 0.6 for Sqlite
--
-- OpenStreetMap database schema for api 0.6
--
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
BEGIN TRANSACTION;
CREATE TABLE acls(
@carlosascari
carlosascari / $.js
Last active June 24, 2018 05:34
jQuery-like selector. One-liner
/**
* jQuery selector substitute
* A quick one-liner for a jQuery-like selector, when a project
* does not need the extra weight early on, but may need jQuery later on.
* @param {String} query - CSS selector
* @param {HTMLElement} context - Element to query, defaults to `document`
* @return {Array<Element>}
*/
$ = (query, context=document) => Array.prototype.slice.call(context.querySelectorAll(query));