mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-17 00:36:26 +10:00
17 lines
10 KiB
JavaScript
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]="&",u==="HTML_ENTITIES"&&(n[160]=" ",n[161]="¡",n[162]="¢",n[163]="£",n[164]="¤",n[165]="¥",n[166]="¦",n[167]="§",n[168]="¨",n[169]="©",n[170]="ª",n[171]="«",n[172]="¬",n[173]="­",n[174]="®",n[175]="¯",n[176]="°",n[177]="±",n[178]="²",n[179]="³",n[180]="´",n[181]="µ",n[182]="¶",n[183]="·",n[184]="¸",n[185]="¹",n[186]="º",n[187]="»",n[188]="¼",n[189]="½",n[190]="¾",n[191]="¿",n[192]="À",n[193]="Á",n[194]="Â",n[195]="Ã",n[196]="Ä",n[197]="Å",n[198]="Æ",n[199]="Ç",n[200]="È",n[201]="É",n[202]="Ê",n[203]="Ë",n[204]="Ì",n[205]="Í",n[206]="Î",n[207]="Ï",n[208]="Ð",n[209]="Ñ",n[210]="Ò",n[211]="Ó",n[212]="Ô",n[213]="Õ",n[214]="Ö",n[215]="×",n[216]="Ø",n[217]="Ù",n[218]="Ú",n[219]="Û",n[220]="Ü",n[221]="Ý",n[222]="Þ",n[223]="ß",n[224]="à",n[225]="á",n[226]="â",n[227]="ã",n[228]="ä",n[229]="å",n[230]="æ",n[231]="ç",n[232]="è",n[233]="é",n[234]="ê",n[235]="ë",n[236]="ì",n[237]="í",n[238]="î",n[239]="ï",n[240]="ð",n[241]="ñ",n[242]="ò",n[243]="ó",n[244]="ô",n[245]="õ",n[246]="ö",n[247]="÷",n[248]="ø",n[249]="ù",n[250]="ú",n[251]="û",n[252]="ü",n[253]="ý",n[254]="þ",n[255]="ÿ"),a!=="ENT_NOQUOTES"&&(n[34]="""),a==="ENT_QUOTES"&&(n[39]="'"),n[60]="<",n[62]=">";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["&"]="&";for(n in i)r=i[n],s=s.split(r).join(n);return s=s.split("'").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); |