Skip to content

Instantly share code, notes, and snippets.

View kfiil's full-sized avatar

Kenneth Fiil kfiil

View GitHub Profile
@kfiil
kfiil / guid.js
Created October 1, 2015 06:20
Create GUID / UUID in JavaScript
//For an rfc4122 version 4 compliant solution, this one-liner(ish) solution is the most compact I could come up with
//http://stackoverflow.com/a/2117523
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
//http://guid.us/GUID/JavaScript
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
@kfiil
kfiil / value-is-null-or-undefined.js
Created December 18, 2018 11:14
Check for null, undefined, or blank variables in JavaScript
/**
Test for "value is null or undefined" is
**/
if ( some_variable == null ){
// some_variable is either null or undefined
}
// So these two lines are equivalent:
if ( typeof(some_variable) !== "undefined" && some_variable !== null ) {}
if ( some_variable != null ) {}
@kfiil
kfiil / js-pad-date-zero.js
Created March 17, 2019 12:36
Javascript add leading zeroes to date
// Source: https://stackoverflow.com/a/12550320
function pad(n){return n<10 ? '0'+n : n}
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'