Skip to content

Instantly share code, notes, and snippets.

View MiguelCastillo's full-sized avatar

Miguel Castillo MiguelCastillo

View GitHub Profile
@MiguelCastillo
MiguelCastillo / main.js
Last active August 29, 2015 13:59
Sample require 2
define( [
"app", "common", "utils", "contacts", "hiring"
], function(app, common, utils, contacts, hiring) {
});
@MiguelCastillo
MiguelCastillo / index.html
Last active August 29, 2015 13:59
requirejs index.html
<html>
<head>
<script src="require.js" data-main="main"></script>
</head>
<body>
Welcome to the modern era!
</body>
</html>
@MiguelCastillo
MiguelCastillo / gist:b945ceb0ca99c23b71ec
Last active August 29, 2015 14:02
Regex to test if function is minified and if its big enough that it's hard enough to read that you can just skip...
// Heuristic to guess if code is minified.
// http://regex101.com/r/oE3nP0
if ( /function[ ]?\w*\([\w,]*\)\{(?:\S[ ]?){1000,}\}/g.test(text) ) {
return;
}
for (var i in el[0]) {
if (i[0] === 'o' && i[1] === 'n') {
defineEvent(el, i.substr(2));
}
}
function defineEvent(el, evtType) {
var evtDef = {
name: evtType,
@MiguelCastillo
MiguelCastillo / gist:1a9dd73c7d7f74b0e35e
Last active August 29, 2015 14:03
Strip Minified code
function stripMinified(text) {
// var regex = /function[ ]?\w*\([\w,]*\)\{(?:\S[\s]?){150,}\}/gm;
// var regex = /(?:\S[\s]?){150,}[\n]$/gm;
var regex = /(?:.){250,}/gm;
return text.replace(regex, function() {return "";});
}
@MiguelCastillo
MiguelCastillo / gist:d9ddf33fe7f1e715e981
Last active August 29, 2015 14:03
Windows drive letters to lower case
function fixPath(path) {
return path.replace(/^([A-Za-z]+:)?\//, function (match) {
return match.toLocaleLowerCase();
});
}
@MiguelCastillo
MiguelCastillo / httpurl.js
Last active August 29, 2015 14:03
Regex to parse HTTP urls
// https://regex101.com/r/aH9kH3/21
function parseUrl (urlString) {
return /^((https?:)(\/\/\/?)(?:([\w]+)(?::([\w]*))?@)?([\d\w\.-]+)(?::(\d+))?)?([\/\\\w\.()-]*)?(?:([?][^#]*)?(#.*)?)*/gmi.exec(urlString);
}
@MiguelCastillo
MiguelCastillo / fileurl.js
Last active August 29, 2015 14:03
Regex to parse FILE urls
// http://regex101.com/r/zT4pG0/6
function parseUrl (urlString) {
return /(?:^(file:)(\/\/\/?))?(([A-Za-z-]+:)?[/\\d\w\.\s-]+)/gmi.exec(urlString);
}
@MiguelCastillo
MiguelCastillo / printStack.js
Created December 11, 2014 17:31
printStack
function printStack() {
try {
throw new TypeError("");
}
catch(ex) {
console.log(ex.stack);
}
}
@MiguelCastillo
MiguelCastillo / taskManager.js
Last active August 29, 2015 14:11
Sample task manager to handle recursion with trampoline
/**
* Task manager to handle queuing up async tasks in an optimal manner
*/
var TaskManager = {
_asyncQueue: [],
asyncTask: function(task) {
if (TaskManager._asyncQueue.push(task) === 1) {
async(TaskManager.taskRunner(TaskManager._asyncQueue));
}
},