Skip to content

Instantly share code, notes, and snippets.

View dustinpoissant's full-sized avatar

Dustin Poissant dustinpoissant

View GitHub Profile
@dustinpoissant
dustinpoissant / formatNumber.func.js
Last active January 25, 2017 21:33
A JavaScript function to format a number into a string.
function formatNumber(v, format){
if(format.indexOf(".")>-1){
var neg = false;
if(v<0)neg = true;
v = Math.abs(v);
var bd = format.indexOf(".");
var ad = format.length-bd-1;
var b = Math.floor(v);
var a = v - b;
b+="";
@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 / 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);
};
@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 / 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 / jQuery.getFormData.func.js
Created November 20, 2016 20:20
A jQuery plugin that gets the forms data as an Object.
$.fn.hasAttr=function(a){var b=$(this).attr(a);return void 0!==typeof b&&b!==!1};
$.fn.getFormData = function(){
var data = {};
$(this).find("input, select, textarea").each(function(i, input){
var $input = $(input);
if($input.hasAttr("name")){
if($input.attr("type") && $input.hasAttr("type") && $input.attr("type").toLowerCase() == "checkbox"){
if($input.is(":checked")){
data[$input.attr("name")] = true;
} else {
@dustinpoissant
dustinpoissant / getScrollbarWidth.js
Last active November 19, 2016 21:22
Get Scrollbar Width
function getScrollbarWidth () {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
@dustinpoissant
dustinpoissant / String.replaceAll.js
Created November 11, 2016 15:36
Replace all occurrences in a string
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
@dustinpoissant
dustinpoissant / Math.randInt.js
Created November 8, 2016 19:46
Generate a Random Integer within a range.
Math.__proto__.randInt = function(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
};
@dustinpoissant
dustinpoissant / saveAs.func.js
Last active September 13, 2016 14:03
Saves text in a text file
function saveAs(text, filename){
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=urf-8,'+encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
};