Last active
October 12, 2015 13:58
-
-
Save mildfuzz/4037683 to your computer and use it in GitHub Desktop.
Mild Fuzz Plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| JS Extentions by Mild Fuzz | |
| */ | |
| (function(){ | |
| //Adds sum functionality to the Array object | |
| Array.prototype.sum = function(){ | |
| var sum = 0; | |
| for (i = 0; i < this.length; i++){ | |
| sum += (!isNaN(Number(this[i])) ? this[i] : 0); | |
| } | |
| return sum; | |
| }; | |
| String.prototype.isEmail = function(){ | |
| var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
| return reg.test(this); | |
| }; | |
| //*/ | |
| String.prototype.titleCase = function() { | |
| return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
| }; | |
| String.prototype.recursiveReplace = function(a,b){ | |
| var newString = this; | |
| while(newString.match(a) !== null){newString = newString.replace(a,b);} | |
| return newString; | |
| }; | |
| /* | |
| Incrementally replaces each of the occurences of the the pattern /!-\d+-!/ with each argument of the function | |
| Example use: | |
| "My name is !-1-!, I live in !-2-!".simpleTemplate("John","Swindon"); | |
| */ | |
| String.prototype.simpleTemplate = function(){ | |
| var newString = this; | |
| for(var i = 0; i < arguments.length; i++){ | |
| newString = newString.replace(/!-\d+-!/, arguments[i]); | |
| if(!/!-\d+-!/g.exec(newString)){ | |
| i = arguments.length; | |
| } | |
| } | |
| return newString; | |
| }; | |
| })(); | |
| /* | |
| $().childLink(); | |
| Make element follow inner link on click | |
| */ | |
| (function(){ | |
| $.fn.childLink = function(){ | |
| $(this).each(function(){ | |
| $(this).click(function(e){ | |
| e.preventDefault(); | |
| $(this).css({'cursor':'pointer'}); | |
| window.location = $(this).find('a').attr('href'); | |
| }); | |
| }); | |
| return this; | |
| }; | |
| // })(jQuery) | |
| /* | |
| Accepts a single matched element, returns true of false if the top edge of the element is showing | |
| */ | |
| // (function(){ | |
| $.fn.isShowing = function(){ | |
| var height = $(window).height(), scrollPos = $(window).scrollTop(), thisTop = $(this).offset().top; | |
| return height + scrollPos > thisTop && thisTop > scrollPos; | |
| }; | |
| })(jQuery); | |
| /** | |
| * jQuery.fn.sortElements | |
| * -------------- | |
| * @param Function comparator: | |
| * Exactly the same behaviour as [1,2,3].sort(comparator) | |
| * | |
| * @param Function getSortable | |
| * A function that should return the element that is | |
| * to be sorted. The comparator will run on the | |
| * current collection, but you may want the actual | |
| * resulting sort to occur on a parent or another | |
| * associated element. | |
| * | |
| * E.g. $('td').sortElements(comparator, function(){ | |
| * return this.parentNode; | |
| * }) | |
| * | |
| * The <td>'s parent (<tr>) will be sorted instead | |
| * of the <td> itself. | |
| */ | |
| jQuery.fn.sortElements = (function(){ | |
| var sort = [].sort; | |
| return function(comparator, getSortable) { | |
| getSortable = getSortable || function(){return this;}; | |
| var placements = this.map(function(){ | |
| var sortElement = getSortable.call(this), | |
| parentNode = sortElement.parentNode, | |
| // Since the element itself will change position, we have | |
| // to have some way of storing its original position in | |
| // the DOM. The easiest way is to have a 'flag' node: | |
| nextSibling = parentNode.insertBefore( | |
| document.createTextNode(''), | |
| sortElement.nextSibling | |
| ); | |
| return function() { | |
| if (parentNode === this) { | |
| throw new Error( | |
| "You can't sort elements if any one is a descendant of another." | |
| ); | |
| } | |
| // Insert before flag: | |
| parentNode.insertBefore(this, nextSibling); | |
| // Remove flag: | |
| parentNode.removeChild(nextSibling); | |
| }; | |
| }); | |
| return sort.call(this, comparator).each(function(i){ | |
| placements[i].call(getSortable.call(this)); | |
| }); | |
| }; | |
| })(); | |
| /* | |
| A collection of useful scripts | |
| */ | |
| //Collect all form data into a string | |
| $.fn.getFormData = function(){ | |
| var str = ""; | |
| var areas = $(this).find('input').not('input[type=submit]').not('input[type=button]').add($(this).find('textarea')).add($(this).find('select')); | |
| areas.each(function(i){ | |
| str += $(this).attr('name')+"="+($(this).val())+(i < areas.length-1 ? "&" : ""); | |
| }); | |
| return str; | |
| }; | |
| //HMTL Toggle | |
| $.fn.htmlToggle = function(a,b){ | |
| $(this).html(($(this).html() == a ? b : a)); | |
| }; | |
| // | |
| //Depends on htmlToggle and getFormData() | |
| //Extends form object to ajax submit the form, and replaces an element with it's twin from the ajax results. | |
| //accepts callbacks for start, end, success and error | |
| $.fn.submitAndFetchOnChange = function(element, start, end, success, error){ | |
| var form = $(this); | |
| $('input[type=submit]',$(this)).remove(); | |
| $('input', $(this)).add($('select',$(this))).add($('textarea',$(this))).change(function(){ | |
| if(typeof start == 'function'){start(arguments);} | |
| if(operation){ | |
| operation.abort(); | |
| } | |
| var url = form.attr("action"), method = form.attr("method"), data = form.getFormData(); | |
| var operation = $.ajax({ | |
| url: url, | |
| type: method, | |
| data: data, | |
| success: function(data){ | |
| $(element).animate({opacity: 0},200,function(){ | |
| $(element).html($(data).find(element)).animate({opacity: 1},200); | |
| }); | |
| if(typeof success == 'function'){success(arguments);} | |
| if(typeof end == 'function'){end(arguments);} | |
| }, | |
| error: function(){ | |
| if(typeof error == 'function'){error(arguments);} | |
| if(typeof end == 'function'){end(arguments);} | |
| } | |
| }); | |
| }); | |
| }; | |
| String.prototype.isCVV = function(){ | |
| return (this.length >= 3 && this.length <=4) && Number(this) ? true : false; | |
| }; | |
| String.prototype.isCreditCard = function() | |
| { | |
| //Credit - https://sites.google.com/site/abapexamples/javascript/luhn-validation | |
| var luhnArr = [[0,2,4,6,8,1,3,5,7,9],[0,1,2,3,4,5,6,7,8,9]], sum = 0; | |
| this.replace(/\D+/g,"").replace(/[\d]/g, function(c, p, o){ | |
| sum += luhnArr[ (o.length-p)&1 ][ parseInt(c,10) ]; | |
| }); | |
| return (sum%10 === 0) && (sum > 0); | |
| }; | |
| /*! | |
| * jQuery.fn.hasAttr() | |
| * | |
| * Copyright 2011, Rick Waldron | |
| * Licensed under MIT license. | |
| * | |
| */ | |
| (function( jQuery ) { | |
| jQuery.fn.hasAttr = function( name ) { | |
| for ( var i = 0, l = this.length; i < l; i++ ) { | |
| if ( !!( this.attr( name ) !== undefined ) ) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| }; | |
| })( jQuery ); | |
| (function(){ | |
| $.fn.highlight = function(){ | |
| $(this).each(function(){ | |
| $(this).css('border','3px solid red'); | |
| }); | |
| }; | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment