UNPKG

turbocommons-ts

Version:

General purpose library that implements frequently used and generic software development tasks

1 lines 15.2 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.StringUtils=void 0;var NumericUtils_1=require("./NumericUtils"),ArrayUtils_1=require("./ArrayUtils"),ObjectUtils_1=require("./ObjectUtils"),StringUtils=function(){function r(){}return r.isString=function(r){return"string"==typeof r||r instanceof String},r.forceString=function(t,e,n){if(void 0===e&&(e=""),void 0===n&&(n="must be a string"),!r.isString(t))throw new Error(e+" "+n)},r.isUrl=function(t){var e=!1;if(!r.isEmpty(t)&&r.isString(t)){e=!!(t.length<2083&&new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$","i").test(t))}return e},r.isEmpty=function(t,e){if(void 0===e&&(e=[]),!r.isString(t)){if(null==t||""==t)return!0;throw new Error("value is not a string")}return""===r.replace(t,e.concat([" ","\n","\r","\t"]),"")},r.isStartingWith=function(r,t){if(void 0===t&&(t=[]),!ArrayUtils_1.ArrayUtils.isArray(t))throw new Error("startingValues is not an array");for(var e=0,n=t;e<n.length;e++){var i=n[e];if(0===r.lastIndexOf(i,0))return!0}return!1},r.isEndingWith=function(r,t){if(void 0===t&&(t=[]),!ArrayUtils_1.ArrayUtils.isArray(t))throw new Error("endingValues is not an array");for(var e=0,n=t;e<n.length;e++){var i=n[e];if(""===i||-1!==r.indexOf(i,r.length-i.length))return!0}return!1},r.forceNonEmptyString=function(t,e,n){if(void 0===e&&(e=""),void 0===n&&(n="must be a non empty string"),!r.isString(t)||r.isEmpty(t))throw new Error(e+" "+n)},r.isCamelCase=function(){},r.isSnakeCase=function(){},r.replace=function(t,e,n,i){if(void 0===i&&(i=-1),!r.isString(t))throw new Error("string is not valid");if(!r.isString(e)&&!ArrayUtils_1.ArrayUtils.isArray(e))throw new Error("search is not a string or array");if(!r.isString(n)&&!ArrayUtils_1.ArrayUtils.isArray(n))throw new Error("replacement is not a string or array");if(!NumericUtils_1.NumericUtils.isInteger(i))throw new Error("count must be a positive integer or 0");if(0===i)return t;var o=t,s=r.isString(e)?[String(e)]:e,l=r.isString(n)?[String(n)]:n;if(l.length>1&&s.length!==l.length)throw new Error("search and replacement arrays must have the same length");for(var a=0;a<s.length;a++)if(""!==s[a]){var u=1===l.length?l[0]:l[a];void 0!==u&&null!==u||(u="");for(var f=[],g=o.split(s[a]),h=0;h<g.length;h++)f.push(g[h]),h<g.length-1&&(i<0||h<i?f.push(u):f.push(s[a]));o=f.join("")}return o},r.replaceMulti=function(t,e,n,i){if(void 0===i&&(i=-1),r.isString(t))return r.replace(t,e,n,i);var o=ObjectUtils_1.ObjectUtils.clone(t);if(ArrayUtils_1.ArrayUtils.isArray(o))for(var s=0;s<o.length;s++)o[s]=r.replaceMulti(o[s],e,n,i);else if(ObjectUtils_1.ObjectUtils.isObject(o))for(var l=0,a=ObjectUtils_1.ObjectUtils.getKeys(o);l<a.length;l++){var u=a[l];o[u]=r.replaceMulti(o[u],e,n,i)}return o},r.trim=function(t,e){return void 0===e&&(e=" \n\r"),r.trimLeft(r.trimRight(t,e),e)},r.trimLeft=function(t,e){if(void 0===e&&(e=" \n\r"),!r.isString(t))throw new Error("value is not a string");return t.replace(new RegExp("^["+e+"]+"),"")},r.trimRight=function(t,e){if(void 0===e&&(e=" \n\r"),!r.isString(t))throw new Error("value is not a string");return t.replace(new RegExp("["+e+"]+$"),"")},r.pad=function(t,e,n,i){if(void 0===n&&(n="0"),void 0===i&&(i="LEFT"),!r.isString(t)||!r.isString(n)||n.length<=0)throw new Error("string and padString must be strings");if(!NumericUtils_1.NumericUtils.isInteger(e))throw new Error("padLength is not an int");if("LEFT"!==i&&"RIGHT"!==i)throw new Error("mode must be LEFT or RIGHT");var o=t;if("RIGHT"===i)for(;o.length<e;)o+=n.substr(0,e-o.length);else for(;o.length<e;)o=n.substr(-(e-o.length))+o;return o},r.countStringOccurences=function(t,e){if(!r.isString(t)||!r.isString(e))throw new Error("value is not a string");if(""===e)throw new Error("cannot count empty string occurences");return t.split(e).length-1},r.countByCase=function(t,e){if(void 0===e&&(e=r.FORMAT_ALL_UPPER_CASE),t=r.removeAccents(t),e===r.FORMAT_ALL_UPPER_CASE)return t.replace(/[^A-Z]+/g,"").length;if(e===r.FORMAT_ALL_LOWER_CASE)return t.replace(/[^a-z]+/g,"").length;throw new Error("invalid case value")},r.countWords=function(t,e){void 0===e&&(e=" ");for(var n=0,i=r.getLines(t),o=0;o<i.length;o++)for(var s=i[o].split(e),l=0;l<s.length;l++)r.isEmpty(s[l])||n++;return n},r.countPathElements=function(t){return""==(t=0===(t=r.formatPath(t,"/")).indexOf("/")?t.substr(1):t)?0:t.split("/").length},r.limitLen=function(t,e,n){if(void 0===e&&(e=100),void 0===n&&(n=" ..."),e<=0||!NumericUtils_1.NumericUtils.isNumeric(e))throw new Error("limit must be a positive numeric value");return r.isString(t)?t.length<=e?t:n.length>e?n.substring(0,e):t.substring(0,e-n.length)+n:""},r.getDomainFromUrl=function(t){var e=r.getHostNameFromUrl(t);return(e=e.split(".")).length>2&&e.shift(),e.join(".")},r.getHostNameFromUrl=function(t){if(r.isEmpty(t)||!r.isUrl(t))return"";var e=document.createElement("a");e.href=t;var n=(e.host.match(/\./g)||[]).length;return n<=0||n>2?"":e.host},r.getLines=function(t,e){void 0===e&&(e=[/\s+/g]);var n=[];if(!r.isString(t))return n;for(var i=t.split(/\r?\n|\n|\r/),o=0;o<i.length;o++)if(r.isString(i[o])){for(var s=i[o],l=0;l<e.length;l++)s=s.replace(e[l],"");""!=s&&n.push(i[o])}return n},r.getKeyWords=function(){},r.getPath=function(t,e,n){if(void 0===e&&(e=1),void 0===n&&(n="/"),r.isEmpty(t))return"";if("/"===(t=r.formatPath(t,"/")))return t;var i=(0===t.indexOf("/")?t.substring(1):t).split("/");if(e>i.length||e<-1)return"";for(var o=[],s=i.length-e;s<i.length;s++)o.push(i[s]);return o.length<=0?t:r.formatPath(t.substring(0,t.length-o.join("/").length-1),n)},r.getPathElement=function(t,e){if(void 0===e&&(e=-1),r.isEmpty(t))return"";var n=(t=0===(t=r.formatPath(t,"/")).indexOf("/")?t.substring(1):t).split("/");if(e>=n.length||e<-n.length)throw new Error("Invalid position specified");return e<0?n[n.length+e]:n[e]},r.getPathElementWithoutExt=function(t,e,n){void 0===e&&(e=-1),void 0===n&&(n=".");var i=r.getPathElement(t,e);return i.indexOf(n)>=0&&(i=i.substr(0,i.lastIndexOf(n))),i},r.getPathExtension=function(t,e,n){void 0===e&&(e=-1),void 0===n&&(n=".");var i=r.getPathElement(t,e);return i.indexOf(n)<0?"":i.substr(i.lastIndexOf(n)+1)},r.getSchemeFromUrl=function(t){if(null==t||void 0==t)return"";if(!r.isString(t))throw new Error("Specified value must be a string");if(!r.isUrl(t))return"";var e=t.split("://");return 2===e.length?e[0]:""},r.formatCase=function(t,e){if(!r.isString(t))throw new Error("value is not a string");if(r.isEmpty(t))return t;if(e===r.FORMAT_START_CASE)return t.split(" ").map(function(r){return(r.length>0?r[0].toUpperCase():"")+(r.length>1?r.substr(1).toLowerCase():"")}).join(" ");if(e===r.FORMAT_ALL_UPPER_CASE)return t.toUpperCase();if(e===r.FORMAT_ALL_LOWER_CASE)return t.toLowerCase();if(e===r.FORMAT_FIRST_UPPER_REST_LOWER)return t.charAt(0).toUpperCase()+t.slice(1).toLowerCase();if(e.indexOf("CAMEL_CASE")>0){var n=r.removeAccents(t).replace(/[^a-z0-9]+/gi," ").trim().split(" ");if(n.length>1)for(var i=1;i<n.length;i++)n[i]=n[i].charAt(0).toUpperCase()+n[i].slice(1);return t=n.join(""),e==r.FORMAT_UPPER_CAMEL_CASE?t.charAt(0).toUpperCase()+t.slice(1):e==r.FORMAT_LOWER_CAMEL_CASE?t.charAt(0).toLowerCase()+t.slice(1):t}throw new Error("Unknown format specified")},r.formatPath=function(t,e){if(void 0===e&&(e="/"),!r.isString(t))throw new Error("path must be a string");if("/"!==e&&"\\"!==e)throw new Error("separator must be a slash or backslash");for(t=(t=t.replace(/\//g,e)).replace(/\\/g,e);t.indexOf(e+e)>=0;)t=t.replace(e+e,e);return t.length>1&&t.substr(t.length-1)===e&&(t=t.substr(0,t.length-1)),t},r.formatUrl=function(t){if(""==t)return"";if(!r.isString(t))throw new Error("url must be a string");if(r.isEmpty(t))return t;for(t=(t=(t=(t=t.trim()).replace(/\//g,"/")).replace(/\\/g,"/")).replace(/\:\/\//g,"@@@");t.indexOf("//")>=0;)t=t.replace("//","/");return t=t.replace(/@@@/g,"://"),""===this.getSchemeFromUrl(t)&&r.isUrl("http://"+t)?"http://"+t:t},r.formatForFullTextSearch=function(t,e){if(void 0===e&&(e=" "),!r.isString(t))throw new Error("value is not a string");var n=r.replace(t,e,"");return(n=(n=r.removeAccents(n)).replace(/[^\p{L}\p{N}]/gu,"")).toLowerCase()},r.compareByLevenshtein=function(t,e){if(!r.isString(t)||!r.isString(e))throw new Error("string1 and string2 must be strings");var n=t.length,i=e.length;if(n<i)return r.compareByLevenshtein(e,t);if(0==n)return i;if(t===e)return 0;for(var o=[],s=[],l=0;l<=i;l++)s.push(l);for(l=0;l<n;l++){(o=[])[0]=l+1;for(var a=t.substr(l,1),u=0;u<i;u++){var f=e.substr(u,1),g=s[u+1]+1,h=o[u]+1,c=s[u]+(a!==f?1:0);o.push(Math.min(g,h,c))}s=o}return s[i]},r.compareSimilarityPercent=function(t,e){var n=r.compareByLevenshtein(t,e);return 0===n?100:100*(1-n/Math.max(t.length,e.length))},r.generatePath=function(t,e){if(void 0===e&&(e=""),!ArrayUtils_1.ArrayUtils.isArray(t))throw new Error("elements is not an array");return""===e&&(e="/"),r.formatPath(t.join(e),e)},r.generateRandom=function(t,e,n){if(void 0===n&&(n=["0-9","a-z","A-Z"]),t<0||!NumericUtils_1.NumericUtils.isInteger(t)||e<0||!NumericUtils_1.NumericUtils.isInteger(e))throw new Error("minLength and maxLength must be positive numbers");if(e<t)throw new Error("Provided maxLength must be higher or equal than minLength");if(!ArrayUtils_1.ArrayUtils.isArray(n)||n.length<=0)throw new Error("invalid charset");for(var i="",o=0,s=n;o<s.length;o++){var l=s[o];if(!r.isString(l)||r.isEmpty(l))throw new Error("invalid charset");var a=l.substr(0,1),u=l.substr(2,1);3===l.length&&1===l.indexOf("-")&&"\\"!==a?"0123456789".indexOf(a)>=0?i+="0123456789".substring("0123456789".indexOf(a),"0123456789".indexOf(u)+1):"abcdefghijkmnopqrstuvwxyz".indexOf(a)>=0?i+="abcdefghijkmnopqrstuvwxyz".substring("abcdefghijkmnopqrstuvwxyz".indexOf(a),"abcdefghijkmnopqrstuvwxyz".indexOf(u)+1):"ABCDEFGHIJKMNOPQRSTUVWXYZ".indexOf(a)>=0&&(i+="ABCDEFGHIJKMNOPQRSTUVWXYZ".substring("ABCDEFGHIJKMNOPQRSTUVWXYZ".indexOf(a),"ABCDEFGHIJKMNOPQRSTUVWXYZ".indexOf(u)+1)):i+=r.replace(l,"\\-","-")}for(var f="",g=t===e?e:NumericUtils_1.NumericUtils.generateRandomInteger(t,e),h=0;h<g;h++)f+=i.charAt(Math.floor(Math.random()*i.length));return f},r.findMostSimilarString=function(){},r.findMostSimilarStringIndex=function(){},r.removeNewLineCharacters=function(){},r.removeAccents=function(t){if(!r.isString(t))throw new Error("value is not a string");for(var e=[{b:"A",l:"AⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ"},{b:"AA",l:"Ꜳ"},{b:"AE",l:"ÆǼǢ"},{b:"AO",l:"Ꜵ"},{b:"AU",l:"Ꜷ"},{b:"AV",l:"ꜸꜺ"},{b:"AY",l:"Ꜽ"},{b:"B",l:"BⒷBḂḄḆɃƂƁ"},{b:"C",l:"CⒸCĆĈĊČÇḈƇȻꜾ"},{b:"D",l:"DⒹDḊĎḌḐḒḎĐƋƊƉꝹ"},{b:"DZ",l:"DZDŽ"},{b:"Dz",l:"DzDž"},{b:"E",l:"EⒺEÈÉÊỀẾỄỂẼĒḔḖĔĖËẺĚȄȆẸỆȨḜĘḘḚƐƎ"},{b:"F",l:"FⒻFḞƑꝻ"},{b:"G",l:"GⒼGǴĜḠĞĠǦĢǤƓꞠꝽꝾ"},{b:"H",l:"HⒽHĤḢḦȞḤḨḪĦⱧⱵꞍ"},{b:"I",l:"IⒾIÌÍÎĨĪĬİÏḮỈǏȈȊỊĮḬƗ"},{b:"J",l:"JⒿJĴɈ"},{b:"K",l:"KⓀKḰǨḲĶḴƘⱩꝀꝂꝄꞢ"},{b:"L",l:"LⓁLĿĹĽḶḸĻḼḺŁȽⱢⱠꝈꝆꞀ"},{b:"LJ",l:"LJ"},{b:"Lj",l:"Lj"},{b:"M",l:"MⓂMḾṀṂⱮƜ"},{b:"N",l:"NⓃNǸŃÑṄŇṆŅṊṈȠƝꞐꞤ"},{b:"NJ",l:"NJ"},{b:"Nj",l:"Nj"},{b:"O",l:"OⓄOÒÓÔỒỐỖỔÕṌȬṎŌṐṒŎȮȰÖȪỎŐǑȌȎƠỜỚỠỞỢỌỘǪǬØǾƆƟꝊꝌ"},{b:"OI",l:"Ƣ"},{b:"OO",l:"Ꝏ"},{b:"OU",l:"Ȣ"},{b:"OE",l:"ŒŒ"},{b:"oe",l:"œœ"},{b:"P",l:"PⓅPṔṖƤⱣꝐꝒꝔ"},{b:"Q",l:"QⓆQꝖꝘɊ"},{b:"R",l:"RⓇRŔṘŘȐȒṚṜŖṞɌⱤꝚꞦꞂ"},{b:"S",l:"SⓈSẞŚṤŜṠŠṦṢṨȘŞⱾꞨꞄ"},{b:"T",l:"TⓉTṪŤṬȚŢṰṮŦƬƮȾꞆ"},{b:"TZ",l:"Ꜩ"},{b:"U",l:"UⓊUÙÚÛŨṸŪṺŬÜǛǗǕǙỦŮŰǓȔȖƯỪỨỮỬỰỤṲŲṶṴɄ"},{b:"V",l:"VⓋVṼṾƲꝞɅ"},{b:"VY",l:"Ꝡ"},{b:"W",l:"WⓌWẀẂŴẆẄẈⱲ"},{b:"X",l:"XⓍXẊẌ"},{b:"Y",l:"YⓎYỲÝŶỸȲẎŸỶỴƳɎỾ"},{b:"Z",l:"ZⓏZŹẐŻŽẒẔƵȤⱿⱫꝢ"},{b:"a",l:"aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐ"},{b:"aa",l:"ꜳ"},{b:"ae",l:"æǽǣ"},{b:"ao",l:"ꜵ"},{b:"au",l:"ꜷ"},{b:"av",l:"ꜹꜻ"},{b:"ay",l:"ꜽ"},{b:"b",l:"bⓑbḃḅḇƀƃɓ"},{b:"c",l:"cⓒcćĉċčçḉƈȼꜿↄ"},{b:"d",l:"dⓓdḋďḍḑḓḏđƌɖɗꝺ"},{b:"dz",l:"dzdž"},{b:"e",l:"eⓔeèéêềếễểẽēḕḗĕėëẻěȅȇẹệȩḝęḙḛɇɛǝ"},{b:"f",l:"fⓕfḟƒꝼ"},{b:"g",l:"gⓖgǵĝḡğġǧģǥɠꞡᵹꝿ"},{b:"h",l:"hⓗhĥḣḧȟḥḩḫẖħⱨⱶɥ"},{b:"hv",l:"ƕ"},{b:"i",l:"iⓘiìíîĩīĭïḯỉǐȉȋịįḭɨı"},{b:"j",l:"jⓙjĵǰɉ"},{b:"k",l:"kⓚkḱǩḳķḵƙⱪꝁꝃꝅꞣ"},{b:"l",l:"lⓛlŀĺľḷḹļḽḻſłƚɫⱡꝉꞁꝇ"},{b:"lj",l:"lj"},{b:"m",l:"mⓜmḿṁṃɱɯ"},{b:"n",l:"nⓝnǹńñṅňṇņṋṉƞɲʼnꞑꞥ"},{b:"nj",l:"nj"},{b:"o",l:"oⓞoòóôồốỗổõṍȭṏōṑṓŏȯȱöȫỏőǒȍȏơờớỡởợọộǫǭøǿɔꝋꝍɵ"},{b:"oi",l:"ƣ"},{b:"ou",l:"ȣ"},{b:"oo",l:"ꝏ"},{b:"p",l:"pⓟpṕṗƥᵽꝑꝓꝕ"},{b:"q",l:"qⓠqɋꝗꝙ"},{b:"r",l:"rⓡrŕṙřȑȓṛṝŗṟɍɽꝛꞧꞃ"},{b:"s",l:"sⓢsßśṥŝṡšṧṣṩșşȿꞩꞅẛ"},{b:"t",l:"tⓣtṫẗťṭțţṱṯŧƭʈⱦꞇ"},{b:"tz",l:"ꜩ"},{b:"u",l:"uⓤuùúûũṹūṻŭüǜǘǖǚủůűǔȕȗưừứữửựụṳųṷṵʉ"},{b:"v",l:"vⓥvṽṿʋꝟʌ"},{b:"vy",l:"ꝡ"},{b:"w",l:"wⓦwẁẃŵẇẅẘẉⱳ"},{b:"x",l:"xⓧxẋẍ"},{b:"y",l:"yⓨyỳýŷỹȳẏÿỷẙỵƴɏỿ"},{b:"z",l:"zⓩzźẑżžẓẕƶȥɀⱬꝣ"}],n={},i=0;i<e.length;i++)for(var o=e[i].l,s=0;s<o.length;s++)n[o[s]]=e[i].b;return t.replace(/[^\u0000-\u007E]/g,function(r){return n[r]||r})},r.removeWordsShorterThan=function(){},r.removeWordsLongerThan=function(){},r.removeUrls=function(){},r.removeHtmlCode=function(){},r.removeSameConsecutive=function(t,e){if(void 0===e&&(e=[]),null===t)return"";if(!r.isString(t))throw new Error("string must be a string");if(!ArrayUtils_1.ArrayUtils.isArray(e))throw new Error("set must be of the type array");return 0===e.length?t.replace(/(.|\r\n|[\r\n])\1+/gu,"$1"):t.replace(new RegExp("("+e.join("|")+")\\1+","ug"),"$1")},r.FORMAT_SENTENCE_CASE="FORMAT_SENTENCE_CASE",r.FORMAT_START_CASE="FORMAT_START_CASE",r.FORMAT_ALL_UPPER_CASE="FORMAT_ALL_UPPER_CASE",r.FORMAT_ALL_LOWER_CASE="FORMAT_ALL_LOWER_CASE",r.FORMAT_FIRST_UPPER_REST_LOWER="FORMAT_FIRST_UPPER_REST_LOWER",r.FORMAT_CAMEL_CASE="FORMAT_CAMEL_CASE",r.FORMAT_UPPER_CAMEL_CASE="FORMAT_UPPER_CAMEL_CASE",r.FORMAT_LOWER_CAMEL_CASE="FORMAT_LOWER_CAMEL_CASE",r.FORMAT_SNAKE_CASE="FORMAT_SNAKE_CASE",r.FORMAT_UPPER_SNAKE_CASE="FORMAT_UPPER_SNAKE_CASE",r.FORMAT_LOWER_SNAKE_CASE="FORMAT_LOWER_SNAKE_CASE",r}();exports.StringUtils=StringUtils;