-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring.extensions.min.js
12 lines (12 loc) · 3.2 KB
/
string.extensions.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* StringExtensions
* https://github.com/karalamalar/StringExtensions
*
* @author: twitter.com/IzzetEmreErkan
* @author_uri: http://www.karalamalar.net
*
* Copyright (c) 2009-2013 İzzet Emre Erkan
* Licensed under Creative Commons Attribution-Share Alike 3.0 Unported License
* http://creativecommons.org/licenses/by-sa/3.0/
*/
String.prototype.trimLeft=function(a){return this.replace(RegExp("^["+(a||" ")+"]+"),"")};String.prototype.trimRight=function(a){return this.replace(RegExp("["+(a||" ")+"]+$"),"")};String.prototype.trim=function(a){return this.trimLeft(a).trimRight(a)};String.prototype.padLeft=function(a,b){var c=this.toString();for(b=b||" ";c.length<a;)c=b+c;return c};String.prototype.padRight=function(a,b){var c=this.toString();for(b=b||" ";c.length<a;)c+=b;return c};String.prototype.insert=function(a,b){var c=this.toString();return c.substring(0,a)+b+c.substring(a)};String.prototype.startsWith=function(a){return this.substring(0,a.length)===a};String.prototype.endsWith=function(a){return this.substring(this.length-a.length)===a};String.prototype.isNullOrEmpty=function(){return null===this||0===this.length};String.prototype.isNullOrWhitespace=function(){return null===this||0===this.replace(/\s/g,"").length};String.prototype.append=function(a,b){var c=this.toString();for(b=b||1;0<b;)c+=a,b--;return c};String.prototype.appendTo=function(a,b){return a.append(this.toString(),b)};String.prototype.prepend=function(a,b){var c=this.toString();for(b=b||1;0<b;)c=a+c,b--;return c};String.prototype.prependTo=function(a,b){return a.prepend(this.toString(),b)};String.prototype.capitalize=function(a,b){function c(a){var b=a.trimLeft();return(b.charAt(0).toUpperCase()+b.substring(1)).padLeft(a.length)}function d(a,b){e=0;for(h=b.length;e<h;e++){f=a.split(b.charAt(e));g=0;for(j=f.length;g<j;g++)f[g]=c(f[g]);a=f.join(b.charAt(e))}return a}var f,e,h,g,j;a=a?a.toLowerCase():"word";b=b||" ";switch(a){case"split":case"s":return d(this,b);case"paragraph":case"p":return d(this,".!?;");case"every":case"everyword":case"every-word":case"e":return d(this," ");default:return c(this)}};String.prototype.count=function(a){return a?this.split(a).length-1:this.trim().split(/\s+/g).length};String.prototype.isEmail=function(){return/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?)$/i.test(this)};String.prototype.isURL=function(){return/^(?:\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|!:,.;]*[\-A-Z0-9+&@#\/%=~_|])$/i.test(this)};Number.prototype.toTime=function(c){var h=Math.floor(this/3600),m=Math.floor((this-h*3600)/60),s=this-(h*3600+m*60),r=[h<10?0:'',h,':',m<10?0:'',m,':',s<10?0:'',s].join('');return c?r.trimLeft('0').trimLeft(':').trimLeft('0').trimLeft(':').trimLeft('0'):r};String.prototype.toTime=function(c){return parseInt(this,10).toTime(c)};String.prototype.valiDate=function(){if(/^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)[0-9]{2}$/.test(this)){var a=this.replace(/[- /]/g,".").split("."),b=parseInt(a[0],10),c=parseInt(a[1],10),a=parseInt(a[2],10),d=new Date(a,c-1,b);return d.getDate()===b&&d.getMonth()+1===c&&d.getFullYear()===a?d:!1}return!1};// More info about valiDate: https://github.com/karalamalar/valiDate