Skip to content

Instantly share code, notes, and snippets.

View jdfm's full-sized avatar

Jonatas Miguel jdfm

View GitHub Profile
@jdfm
jdfm / hasNativeCode.js
Last active August 29, 2015 14:03
Method to detect if some function is a native function or method.
var hasNativeCode = (function(hasNativeCodeRegexp){ // will this need changes because of ES6?
return function(f){
return f && ( typeof f === 'function' && ( // This case checks functions/methods that are reported as functions
!f.hasOwnProperty('prototype') || // native methods don't have prototypes
hasNativeCodeRegexp.test(String(f)) // we don't care about the function name or arguments, just check if it's body is { [native code] }
)) || ( typeof f === 'object' && ( // certain browsers (that I know of, IE7 and IE8) report some native methods as object
!('constructor' in f) && // these methods don't have a constructor property and it can't be set
hasNativeCodeRegexp.test(String(f)) // it's string value will still be the native code string, so check for it
));
};
@jdfm
jdfm / isInteger.js
Created July 7, 2014 00:53
Determine if a number is an integer or not.
var isInteger = (function(NEGATIVE_INFINITY, POSITIVE_INFINITY){
return function(n){
return typeof n === 'number' && // Are you even an number? Rules out non numeric types.
n > NEGATIVE_INFINITY && // n larger than negative infinity? Rules out NaN and negative infinity.
n < POSITIVE_INFINITY && // n smaller than positive infinity? Rules out NaN and positive infinity.
!(n % 1); // n has a remainder of 0 when calculating modulo 1? Rules out floating points.
};
}(-1/0, 1/0)); // just making sure we're getting negative and positive infinity
console.assert(isInteger(false) === false, 'booleans are not integers')
@jdfm
jdfm / callfn.js
Last active August 29, 2015 14:10
Extract bind, call and apply to use as standalone functions, instead of as methods of other functions.
var bind = Function.prototype.bind ? Function.prototype.bind.bind(Function.prototype.call) : (function(){
/**
* Some browsers don't have the bind function, so you can use the code
* below to achieve more or less the same results... A more complete
* implementation of binder can be found on mdn, this was just a quick hack.
*/
function binder(fn, that){
for(var i = 0, iLen = arguments.length, arr = new Array(iLen); i < iLen; i++){
arr[i] = arguments[i];
}
@jdfm
jdfm / bundle.js
Created May 20, 2015 20:53
bind an array of items to a function at once
/**
* I feel like javascript functions are missing a method. Here's why:
* call is to bind as apply is to ?
*
* Javascript lacks a native method of binding an array as a list of parameters
* to a function. Here's what I came up with:
*/
if(Function.prototype.bind && !Function.prototype.bundle){
Function.prototype.bundle = function(thisArg, argsArray){
return this.bind.apply(this, [thisArg].concat(Object.prototype.toString.call(argsArray) === '[object Array]'? argsArray: []));
@jdfm
jdfm / dragDrop.js
Created May 20, 2015 21:02
drag and drop code that uses a state machine to tame the madness
/**
* I stumbled upon this solution while trying to help out a colleague.
* I was studying a book on the theory of computation at the time
* which, coincidentally, talked about state machines, it then ocurred
* to me that a state machine could help solve the problems we were
* having at the time.
*/
(function(){
// 0 = not doing anything
// 1 = is dragging
@jdfm
jdfm / small-nest.js
Last active August 29, 2015 14:21
Reducing "if" nesting - via http://qr.ae/f3BuB
// Original nested version
if ( a > 0 ) {
if ( b > 10 ) {
if ( c > 0 ) {
//complex code
}
}
}
return;
@jdfm
jdfm / switch fizzbuzz.js
Created June 23, 2015 23:54
use a for switch block to implement a fizz buzz
for ( var i = 1; i <= 100; i++ ) switch ( true ) {
case !( i % 3 ) && !( i % 5 ): console.log('fizzbuzz', i); break;
case !( i % 3 ): console.log('fizz', i); break;
case !( i % 5 ): console.log('buzz', i); break;
default:
}
@jdfm
jdfm / switch(value) case expression.js
Last active August 29, 2015 14:23
It's possible to use a switch block as an alternative to if else if else, but with a few caveats...
// classic example
if ( somevalue === 1 ) {
// do the thing
} else if ( somevalue === 2 ) {
// do some other thing
}
switch ( somevalue ) {
case 1: /* do some thing */ break;
case 2: /* do some other thing */ break;
@jdfm
jdfm / if else switch.js
Last active August 29, 2015 14:23
it's possible to place a switch block right after an else (or an if(condition)) statement, making it look like an alternate conditional block
if ( condition ) {
// do the thing
} else switch ( state ) {
case 1: /* do something */ break;
default: case 2: /* do something else */
}
@jdfm
jdfm / unrenderable-comment.md
Last active December 1, 2015 15:43
markdown specific comments that won't be in the rendered document - via http://stackoverflow.com/a/20885980/4644997

//: # If you can see this, this variation doesn't work

Above are a few examples of comments in markdown, the purpose of these comments is to not render an empty line in it's place. If anything is visible above this paragraph, it's because one of the variants being tested does not work with the rendering engine you are using.