Skip to content

Instantly share code, notes, and snippets.

View jdfm's full-sized avatar

Jonatas Miguel jdfm

View GitHub Profile
@jdfm
jdfm / templates.js
Last active January 12, 2016 01:12
simple templates
/**
* Use a simple object to create your html with variables
* and whatever code you need to build it up.
*
* You can take advantage of type coercion here to
* end up with the final strings.
*/
var template = {
placeholder1: undefined,
placeholder2: undefined,
@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 / 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 / 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 / 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 / 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 / querify.js
Created November 15, 2013 16:02
Takes data in a hierarchy and returns a string suitably formatted for a POST request's postBody or a GET request's query component.
/**
* querify
*
* Takes data in a hierarchy and returns a string suitably formatted for a POST
* request's postBody or a GET request's query component.
*
* Note:
* This function will not play well with circular referenced objects, it
* will result in an infinite loop. Most likely. I haven't tested
* that case.
@jdfm
jdfm / mutableBind.js
Last active December 25, 2015 21:49
mutableBind - Keep the wrapper, redefine everything else.
/**
* Mutable Binds.
*
* Why are mutable binds needed?
* I don't know about "needed", I came up with the idea because I was
* curious about if it would work, how it would work, and if there are
* any use cases. As for the use cases, I don't really know. If you can
* find a legitimate use case, let me know, I'd be happy to hear from
* you.
*
@jdfm
jdfm / styled-input-type.html
Last active December 25, 2015 17:59
Minimal JS, crossbrowser, styled input[type="file"] with simple markup.
<!doctype html>
<!--
Minimal JS, crossbrowser, styled input[type="file"] with simple markup.
Tested working in IE[>=7], FF[24], O[16], C[30], S[6.0.5]; Should work in
all browsers that support opening a file browser via a label associated
to an input[type="file"]. Some adjustments might be necessary for IE[6]
support.
Tested using html5 doctype. Further testing is necessary to ensure proper