Skip to content

Instantly share code, notes, and snippets.

@AWaselnuk
AWaselnuk / isInteger.js
Created December 19, 2014 14:18
Test that something is an integer.
function isInteger(num) {
num === Math.round(num);
}
@AWaselnuk
AWaselnuk / gist:1392d25b6d5cb79ae681
Last active February 16, 2021 13:34 — forked from carolineschnapp/gist:5397337
Conditionally load jQuery via loadScript
/* Sample JavaScript file added with ScriptTag resource.
This sample file is meant to teach best practices.
Your app will load jQuery if it's not defined.
Your app will load jQuery if jQuery is defined but is too old, e.g. < 1.7.
Your app does not change the definition of $ or jQuery outside the app.
*/
(function(){
/* Load Script function we may need to load jQuery from the Google's CDN */
@AWaselnuk
AWaselnuk / selective_error_catch.js
Created December 16, 2014 21:49
Selective error catching in JavaScript
// from: Eloquent JavaScript http://eloquentjavascript.net/08_error.html#p_CJKevweHV6
// Define our own error type
function InputError(message) {
this.message = message;
this.stack = (new Error()).stack;
}
InputError.prototype = Object.create(Error.prototype);
InputError.prototype.name = "InputError";
@AWaselnuk
AWaselnuk / toCssCase.js
Created December 5, 2014 20:02
Make a string safe for css (by removing all nonalphanumeric characters)
var str = 'A&P Grocers, Inc.';
str.replace(/\W/g, '').toLowerCase(); // apgrocersinc
@AWaselnuk
AWaselnuk / uuid.js
Created December 1, 2014 17:57
Generate a UUID with Javascript.
function generateUUID(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
@AWaselnuk
AWaselnuk / sentinal.js
Created November 25, 2014 16:31
Wait for dependencies to arrive before executing code
// From: http://stackoverflow.com/questions/16425520/angularjs-test-that-an-external-script-is-effectively-loaded
(function waiter(){
if(!window.jQuery){ return setTimeout(waiter, 37); }
$("#myDiv").fadeOut();
}())
@AWaselnuk
AWaselnuk / this.js
Last active August 29, 2015 14:10
Understanding 'this' in Javascript.
// Summary from http://www.sitepoint.com/what-is-this-in-javascript/
//// GLOBAL SCOPE
// If there's no current object, 'this' refers to the global object
window.WhoAmI = "I'm the window object"
console.log(window.WhoAmI) // "I'm the window object"
@AWaselnuk
AWaselnuk / load_script.js
Created November 19, 2014 16:32
Load external javascript files asynchronously
//this function will work cross-browser for loading scripts asynchronously
//from: http://stackoverflow.com/questions/7718935/load-scripts-asynchronously
function loadScript(src, callback)
{
var s,t,done;
done = false;
s = document.createElement('script');
s.type = 'text/javascript';
s.src = src;
s.onload = s.onreadystatechange = function() {
@AWaselnuk
AWaselnuk / improved_set_interval.js
Created November 11, 2014 15:35
Improved setInterval
// BAD
setInterval(function(){
doStuff();
}, 100);
// GOOD
(function loopMe(){
doStuff();
setTimeout(loopMe, 100);
})();
@AWaselnuk
AWaselnuk / rubocop.yml
Created October 31, 2014 13:55
Rubocop Rails config
AllCops:
Include:
- '**/Rakefile'
- '**/config.ru'
Exclude:
- 'db/**/*'
- 'bin/**/*'
- 'config/**/*'
- 'node_modules/**/*'
- '**/rails_helper.rb'