Files
cdnjs/ajax/libs/string.js/1.2.0/string.min.js
T
2013-01-15 16:07:27 -06:00

17 lines
10 KiB
JavaScript

/*
string.js - Copyright (C) 2012, JP Richardson <jprichardson@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/(function(){"use strict";function t(e){e!==null&&e!==undefined?typeof e=="string"?this.s=e:this.s=e.toString():this.s=e,this.orig=e,e!==null&&e!==undefined?this.__defineGetter__?this.__defineGetter__("length",function(){return this.s.length}):this.length=e.length:this.length=-1}function s(){for(var e in r)(function(e){var t=r[e];n.hasOwnProperty(e)||(i.push(e),n[e]=function(){return String.prototype.s=this,t.apply(this,arguments)})})(e)}function o(){for(var e=0;e<i.length;++e)delete String.prototype[i[e]];i.length=0}function f(){var e=l(),t={};for(var r=0;r<e.length;++r){var i=e[r],s=n[i];try{var o=typeof s.apply("teststring",[]);t[i]=o}catch(u){}}return t}function l(){var e=[];if(Object.getOwnPropertyNames)return e=Object.getOwnPropertyNames(n),e.splice(e.indexOf("valueOf"),1),e.splice(e.indexOf("toString"),1),e;var t={},r=[];for(var i in String.prototype)t[i]=i;for(var i in Object.prototype)delete t[i];for(var i in t)e.push(i);return e}function c(e){return new t(e)}function h(e,t){var n=[],r;for(r=0;r<e.length;r++)n.push(e[r]),t&&t.call(e,e[r],r);return n}function m(e,t){var n={},r={},i,s={},o={},u={},a={};s[0]="HTML_SPECIALCHARS",s[1]="HTML_ENTITIES",o[0]="ENT_NOQUOTES",o[2]="ENT_COMPAT",o[3]="ENT_QUOTES",u=isNaN(e)?e?e.toUpperCase():"HTML_SPECIALCHARS":s[e],a=isNaN(t)?t?t.toUpperCase():"ENT_COMPAT":o[t];if(u!=="HTML_SPECIALCHARS"&&u!=="HTML_ENTITIES")throw new Error("Table: "+u+" not supported");n[38]="&amp;",u==="HTML_ENTITIES"&&(n[160]="&nbsp;",n[161]="&iexcl;",n[162]="&cent;",n[163]="&pound;",n[164]="&curren;",n[165]="&yen;",n[166]="&brvbar;",n[167]="&sect;",n[168]="&uml;",n[169]="&copy;",n[170]="&ordf;",n[171]="&laquo;",n[172]="&not;",n[173]="&shy;",n[174]="&reg;",n[175]="&macr;",n[176]="&deg;",n[177]="&plusmn;",n[178]="&sup2;",n[179]="&sup3;",n[180]="&acute;",n[181]="&micro;",n[182]="&para;",n[183]="&middot;",n[184]="&cedil;",n[185]="&sup1;",n[186]="&ordm;",n[187]="&raquo;",n[188]="&frac14;",n[189]="&frac12;",n[190]="&frac34;",n[191]="&iquest;",n[192]="&Agrave;",n[193]="&Aacute;",n[194]="&Acirc;",n[195]="&Atilde;",n[196]="&Auml;",n[197]="&Aring;",n[198]="&AElig;",n[199]="&Ccedil;",n[200]="&Egrave;",n[201]="&Eacute;",n[202]="&Ecirc;",n[203]="&Euml;",n[204]="&Igrave;",n[205]="&Iacute;",n[206]="&Icirc;",n[207]="&Iuml;",n[208]="&ETH;",n[209]="&Ntilde;",n[210]="&Ograve;",n[211]="&Oacute;",n[212]="&Ocirc;",n[213]="&Otilde;",n[214]="&Ouml;",n[215]="&times;",n[216]="&Oslash;",n[217]="&Ugrave;",n[218]="&Uacute;",n[219]="&Ucirc;",n[220]="&Uuml;",n[221]="&Yacute;",n[222]="&THORN;",n[223]="&szlig;",n[224]="&agrave;",n[225]="&aacute;",n[226]="&acirc;",n[227]="&atilde;",n[228]="&auml;",n[229]="&aring;",n[230]="&aelig;",n[231]="&ccedil;",n[232]="&egrave;",n[233]="&eacute;",n[234]="&ecirc;",n[235]="&euml;",n[236]="&igrave;",n[237]="&iacute;",n[238]="&icirc;",n[239]="&iuml;",n[240]="&eth;",n[241]="&ntilde;",n[242]="&ograve;",n[243]="&oacute;",n[244]="&ocirc;",n[245]="&otilde;",n[246]="&ouml;",n[247]="&divide;",n[248]="&oslash;",n[249]="&ugrave;",n[250]="&uacute;",n[251]="&ucirc;",n[252]="&uuml;",n[253]="&yacute;",n[254]="&thorn;",n[255]="&yuml;"),a!=="ENT_NOQUOTES"&&(n[34]="&quot;"),a==="ENT_QUOTES"&&(n[39]="&#39;"),n[60]="&lt;",n[62]="&gt;";for(i in n)n.hasOwnProperty(i)&&(r[String.fromCharCode(i)]=n[i]);return r}var e="1.2.0",n=String.prototype,r=t.prototype={camelize:function(){var e=this.trim().s.replace(/(\-|_|\s)+(.)?/g,function(e,t,n){return n?n.toUpperCase():""});return new t(e)},capitalize:function(){return new t(this.s.substr(0,1).toUpperCase()+this.s.substring(1).toLowerCase())},charAt:function(e){return this.s.charAt(e)},collapseWhitespace:function(){var e=this.s.replace(/[\s\xa0]+/g," ").replace(/^\s+|\s+$/g,"");return new t(e)},contains:function(e){return this.s.indexOf(e)>=0},dasherize:function(){var e=this.trim().s.replace(/[_\s]+/g,"-").replace(/([A-Z])/g,"-$1").replace(/-+/g,"-").toLowerCase();return new t(e)},decodeHtmlEntities:function(e){var n="",r="",i={},s=this.s;if(!1===(i=m("HTML_ENTITIES",e)))return!1;delete i["&"],i["&"]="&amp;";for(n in i)r=i[n],s=s.split(r).join(n);return s=s.split("&#039;").join("'"),new t(s)},endsWith:function(e){var t=this.s.length-e.length;return t>=0&&this.s.indexOf(e,t)===t},escapeHTML:function(){return new t(this.s.replace(/[&<>"']/g,function(e){return"&"+d[e]+";"}))},isAlpha:function(){return!/[^a-z\xC0-\xFF]/.test(this.s.toLowerCase())},isAlphaNumeric:function(){return!/[^0-9a-z\xC0-\xFF]/.test(this.s.toLowerCase())},isEmpty:function(){return this.s===null||this.s===undefined?!0:/^[\s\xa0]*$/.test(this.s)},isLower:function(){return this.isAlpha()&&this.s.toLowerCase()===this.s},isNumeric:function(){return!/[^0-9]/.test(this.s)},isUpper:function(){return this.isAlpha()&&this.s.toUpperCase()===this.s},left:function(e){if(e>=0){var n=this.s.substr(0,e);return new t(n)}return this.right(-e)},lines:function(){var e=this.s.split("\n");for(var t=0;t<e.length;++t)e[t]=e[t].replace(/(^\s*|\s*$)/g,"");return e},pad:function(e,n){n=n||" ";if(this.s.length>=e)return new t(this.s);e-=this.s.length;var r=Array(Math.ceil(e/2)+1).join(n),i=Array(Math.floor(e/2)+1).join(n);return new t(r+this.s+i)},padLeft:function(e,n){return n=n||" ",this.s.length>=e?new t(this.s):new t(Array(e-this.s.length+1).join(n)+this.s)},padRight:function(e,n){return n=n||" ",this.s.length>=e?new t(this.s):new t(this.s+Array(e-this.s.length+1).join(n))},parseCSV:function(e,t){e=e||",",escape="\\",typeof t=="undefined"&&(t='"');var n=0,r=[],i=[],s=this.s.length,o=!1,u=this,a=function(e){return u.s.charAt(e)};t||(o=!0);while(n<s){var f=a(n);switch(f){case t:o?a(n-1)===escape?r.push(f):o=!1:o=!0;break;case e:o&&t?r.push(f):(i.push(r.join("")),r.length=0);break;case escape:t&&a(n+1)!==t&&r.push(f);break;default:o&&r.push(f)}n+=1}return i.push(r.join("")),i},replaceAll:function(e,n){var r=this.s.split(e).join(n);return new t(r)},right:function(e){if(e>=0){var n=this.s.substr(this.s.length-e,e);return new t(n)}return this.left(-e)},slugify:function(){var e=(new t(this.s.replace(/[^\w\s-]/g,"").toLowerCase())).dasherize().s;return e.charAt(0)==="-"&&(e=e.substr(1)),new t(e)},startsWith:function(e){return this.s.lastIndexOf(e,0)===0},stripPunctuation:function(){return new t(this.s.replace(/[^\w\s]|_/g,"").replace(/\s+/g," "))},stripTags:function(){var e=this.s,n=arguments.length>0?arguments:[""];return h(n,function(t){e=e.replace(RegExp("</?"+t+"[^<>]*>","gi"),"")}),new t(e)},times:function(e){return new t((new Array(e+1)).join(this.s))},toBoolean:function(){if(typeof this.orig=="string"){var e=this.s.toLowerCase();return e==="true"||e==="yes"||e==="on"}return this.orig===!0||this.orig===1},toFloat:function(e){var t=parseFloat(this.s,10);return e?parseFloat(t.toFixed(e)):t},toInt:function(){return/^\s*-?0x/i.test(this.s)?parseInt(this.s,16):parseInt(this.s,10)},trim:function(){var e;return typeof String.prototype.trim=="undefined"?e=this.s.replace(/(^\s*|\s*$)/g,""):e=this.s.trim(),new t(e)},trimLeft:function(){var e;return n.trimLeft?e=this.s.trimLeft():e=this.s.replace(/(^\s*)/g,""),new t(e)},trimRight:function(){var e;return n.trimRight?e=this.s.trimRight():e=this.s.replace(/\s+$/,""),new t(e)},truncate:function(e,n){var r=this.s;e=~~e,n=n||"...";if(r.length<=e)return r;var i=function(e){return e.toUpperCase()!==e.toLowerCase()?"A":" "},s=r.slice(0,e+1).replace(/.(?=\W*\w*$)/g,i);return s.slice(s.length-2).match(/\w\w/)?s=s.replace(/\s*\S+$/,""):s=(new t(s.slice(0,s.length-1))).trimRight().s,(s+n).length>r.length?new t(r):new t(r.slice(0,s.length)+n)},toCSV:function(){function u(e){return e!==null&&e!==""}var e=",",n='"',r="\\",i=!0,s=!1,o=[];typeof arguments[0]=="object"?(e=arguments[0].delimiter||e,e=arguments[0].separator||e,n=arguments[0].qualifier||n,i=!!arguments[0].encloseNumbers,r=arguments[0].escapeChar||r,s=!!arguments[0].keys):typeof arguments[0]=="string"&&(e=arguments[0]),typeof arguments[1]=="string"&&(n=arguments[1]),arguments[1]===null&&(n=null);if(this.orig instanceof Array)o=this.orig;else for(var a in this.orig)this.orig.hasOwnProperty(a)&&(s?o.push(a):o.push(this.orig[a]));var f=r+n,l=[];for(var c=0;c<o.length;++c){var h=u(n);typeof o[c]=="number"&&(h&=i),h&&l.push(n);var p=(new t(o[c])).replaceAll(n,f).s;l.push(p),h&&l.push(n),e&&l.push(e)}return l.length=l.length-1,new t(l.join(""))},toString:function(){return this.s},underscore:function(){var e=this.trim().s.replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/[-\s]+/g,"_").toLowerCase();return(new t(this.s.charAt(0))).isUpper()&&(e="_"+e),new t(e)},unescapeHTML:function(){return new t(this.s.replace(/\&([^;]+);/g,function(e,t){var n;return t in p?p[t]:(n=t.match(/^#x([\da-fA-F]+)$/))?String.fromCharCode(parseInt(n[1],16)):(n=t.match(/^#(\d+)$/))?String.fromCharCode(~~n[1]):e}))},valueOf:function(){return this.s.valueOf()}},i=[],u=f();for(var a in u)(function(e){var i=n[e];typeof i=="function"&&(r[e]||(u[e]==="string"?r[e]=function(){return new t(i.apply(this,arguments))}:r[e]=i))})(a);r.repeat=r.times,r.include=r.contains,r.toInteger=r.toInt,r.toBool=r.toBoolean,r.decodeHTMLEntities=r.decodeHtmlEntities,typeof module!="undefined"&&typeof module.exports!="undefined"?(module.exports=c,module.exports.extendPrototype=s,module.exports.restorePrototype=o,module.exports.VERSION=e):typeof define=="function"&&define.amd?define([],function(){return c}):(window.S=c,window.S.extendPrototype=s,window.S.restorePrototype=o,window.S.VERSION=e);var p={lt:"<",gt:">",quot:'"',apos:"'",amp:"&"},d={};for(var v in p)d[p[v]]=v}).call(this);