Skip to content

Instantly share code, notes, and snippets.

View dustinpoissant's full-sized avatar

Dustin Poissant dustinpoissant

View GitHub Profile
const stringToDOM = str => new DOMParser().parseFromString(str, 'text/html').body.childNodes[0];
@dustinpoissant
dustinpoissant / cookies.js
Created November 27, 2019 20:09
Get, Set and Delete Cookies
function getCookie(name) {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
}
function setCookie(name, value, days) {
if(!days) var days = 365;
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
@dustinpoissant
dustinpoissant / String.extract.func.js
Created December 1, 2017 17:38
Extract a substring from a string between two substrings.
String.prototype.extract = function(start, finish){
var s = this.indexOf(start);
if(s == -1) return "";
var e = this.indexOf(finish, s + start.length);
if(e == -1) return "";
return this.substring(s + start.length, e);
}
@dustinpoissant
dustinpoissant / animateTransform.jquery.js
Last active July 2, 2018 20:46
jQuery Plugin to Animate Transforms
$.fn.animateTransform = function(/* [start,] end [, duration] [, callback] */){
var start = null, end = null, duration = 400, callback = function(){};
for(var i=0; i<arguments.length; i++){
if(typeof(arguments[i]) == "string"){
if(!start) start = arguments[i];
else end = arguments[i];
} else if(typeof(arguments[i]) == "number"){
duration = arguments[i];
} else if(typeof(arguments[i]) == "function"){
callback = arguments[i];
@dustinpoissant
dustinpoissant / selectText.jquery.js
Created February 7, 2017 17:06
Selects the text within an element.
jQuery.fn.selectText = function(){
var doc = document;
var element = this[0];
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
@dustinpoissant
dustinpoissant / attrs.jquery.js
Created January 31, 2017 23:04
Get all attributes of an element.
$.fn.attrs = function(){
var attributes = {};
if(this.length){
$.each( this[0].attributes, function( index, attr ) {
attributes[ attr.name ] = attr.value;
});
}
return attributes;
};
@dustinpoissant
dustinpoissant / roundTo.func.js
Created January 24, 2017 16:47
Round a number to the nearest "X"
function roundTo(number, to){
return (Math.round(number * (1/to)) / (1/to));
};
@dustinpoissant
dustinpoissant / containsObject.func.min.js
Created November 22, 2016 03:49
A quick and dirty check to see of one object contains another.
function containsObject(a,b){for(var c in a)if(!b[c]||a[c]!=b[c])return!1;return!0}
@dustinpoissant
dustinpoissant / validCSS.func.js
Last active November 28, 2016 19:23
Check if a CSS property/value is valid.
function validCSS(property, value){
var $el = $("<div></div>");
$el.css(property, value);
return ($el.css(property) == value);
}
@dustinpoissant
dustinpoissant / betterParseFloat.func.js
Last active December 1, 2016 03:08
Pasrses a number better than the orginal parseFloat function
function betterParseFloat(num){
num = (num+"").replace(new RegExp(",", 'g'), ""); // Remove commas
return (num.length==0)?0:(isNaN(parseFloat(num)))?betterParseFloat(num.substr(1)):parseFloat(num);
};