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 / tiny-require.js
Last active October 23, 2015 06:09
Guarantee a module has loaded before executing.
/**
* Tiny Require
*/
/**
* Before the module has finished downloading/evaluating,
* the callback provided is executed async. Once loaded, all
* callbacks will be executed immediately.
*
* A module is detected when it is available in the window object.
@carlosascari
carlosascari / baseN.js
Last active November 17, 2015 22:49
Convert numbers into any base e.g: 2, 8,16, 64,1024, 65500, etc.
/**
* Provides BaseN class
*
* Encode and decode numbers as any base
*
* @example
*
* var baseX = new BaseN() // Default base is 65,535
* var sample = 9007199254740991
* var encoded = baseX.encode(sample)
@carlosascari
carlosascari / EventEmiterBehaviour.js
Last active November 17, 2015 22:52
Abstract class to add Event Emitter functionality for decoupling objects, setting up patterns (PubSub, Mediator, Observer)
/**
* Provides Event Emitter functionality
*
* Allows decoupling existing classes/prototypes
* Events are emitted in Sync.
*
* Usage
* @example
*
* function YourClass()
@carlosascari
carlosascari / EventEmitterTrait.js
Last active January 24, 2016 23:04
Function that allows any object to function as a synchronous Event Emitter.
/**
* Provides an EventEmitter Trait
*
* USAGE @example
*
* function YourClass(){
* EventEmitterTrait(this) // or any object
* }
*
* var yourObject = new YourClass()
@carlosascari
carlosascari / crc2D.js
Created February 14, 2016 20:21
A Cyclic Redundancy Checksum designed for small 2D signed coordinates.
/**
* Provides crc2D function.
*
* A Cyclic Redundancy Checksum designed for small 2D signed coordinates.
*
* Based on: https://github.com/alexgorbatchev/node-crc/blob/master/src/crc16.js
*
* The algorithm provides up to a maximum of 65,536 Unique Values. For x and y,
* this means that up to a byte (256 values) can be guaranteed to not collide.
*
@carlosascari
carlosascari / GameLoopTrait.js
Created February 18, 2016 10:33
Function that allows any object to function as a game rendering loop.
/**
* Provides GameLoop Trait
*
* Based on: http://nokarma.org/2011/02/02/javascript-game-development-the-game-loop/
*
* @required EventEmitterTrait
* @module Traits
* @submodule GameLoopTrait
*/
var GameLoopTrait = (function(EventEmiterTrait, requestAnimationFrame) {
@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)
@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 / 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 / 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(){