Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / isInteger.js
Created December 19, 2014 14:18
Test that something is an integer.
function isInteger(num) {
num === Math.round(num);
}
@AWaselnuk
AWaselnuk / raml_handlebars_html.coffee
Last active August 29, 2015 14:13
Converting a RAML file to a Handlebars template and then to an HTML file.
## parse a .raml file and then compile Handlebar templates
# Import libraries
fs = require('fs')
appRoot = require('app-root-path')
RAML = require('raml-parser')
Handlebars = require('handlebars')
saveTemplate = (pathToView, renderedTemplate) ->
console.log 'Saving template'
@AWaselnuk
AWaselnuk / get_random_int.js
Created January 26, 2015 21:35
Get a random integer between min and max
// Returns a random integer between min (included) and max (excluded)
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
@AWaselnuk
AWaselnuk / regex_cheatsheet.js
Created February 5, 2015 18:15
Regex cheatsheet as JS comments
// From: http://regexone.com/
//
// abc… Letters
// 123… Digits
// \d any Digit
// . any Character
// \. Period
// [abc] Only a, b, or c
// [^abc] Not a, b, nor c
// [a-z] Characters a to z
@AWaselnuk
AWaselnuk / css_spinner.css
Created February 6, 2015 14:25
Single element CSS spinner
#spinner {
width: 150px;height: 150px;
-webkit-animation: sweep 1s infinite linear;
border-radius:75px;
border-bottom:5px solid #ccc;
}
@-webkit-keyframes sweep { to { -webkit-transform: rotate(360deg); } }
@AWaselnuk
AWaselnuk / tweet_sized_templating.js
Created June 19, 2015 12:17
Tweet Sized Templating Engine
// From: http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/
function t (s,d) {
for (var p in d)
s=s.replace(new RegExp('{'+p+'}','g'), d[p]);
return s;
}
// Call it like this:
// t("Hello {who}!", { who: "JavaScript" });