Last active
March 9, 2018 19:37
-
-
Save vanrez-nez/1e2f88c26acf9cd6b17c to your computer and use it in GitHub Desktop.
Common useful accessories for javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Some functions where taken from Jistin Windle's sketch.js at https://github.com/soulwire/sketch.js | |
// Modulus with support for negative integers | |
Mod = function(current, limit) { | |
return ( (current % limit) + limit ) % limit; | |
}; | |
function isArray( object ) { | |
return Object.prototype.toString.call( object ) == '[object Array]'; | |
} | |
function isFunction( object ) { | |
return typeof object == 'function'; | |
} | |
function isNumber( object ) { | |
return typeof object == 'number'; | |
} | |
function isString( object ) { | |
return typeof object == 'string'; | |
} | |
function extend( target, source, overwrite ) { | |
for ( var key in source ) | |
if ( overwrite || !( key in target ) ) | |
target[ key ] = source[ key ]; | |
return target; | |
} | |
function proxy( method, context ) { | |
return function() { | |
method.apply(context, arguments); | |
} | |
} | |
function clone( target ) { | |
var object = {}; | |
for ( var key in target ) { | |
// not sure why is not using hasOwnProperty | |
if ( isFunction( target[ key ] ) ) | |
object[ key ] = proxy( target[ key ], target); | |
else | |
object[ key ] = target[ key ]; | |
} | |
return object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment