forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringUtils.js
More file actions
1 lines (1 loc) · 2.89 KB
/
StringUtils.js
File metadata and controls
1 lines (1 loc) · 2.89 KB
1
define(function(require,exports,module){var _=require("thirdparty/lodash");function format(str){var args=[].slice.call(arguments,1);return str.replace(/\{\s*(\d+)\s*\}/g,function(match,num){return void 0!==args[num]?args[num]:match})}function regexEscape(str){return str.replace(/([.?*+\^$\[\]\\(){}|\-])/g,"\\$1")}function jQueryIdEscape(str){return str.replace(/\./g,"\\.")}function getLines(text){return text.split("\n")}function offsetToLineNum(textOrLines,offset){if(Array.isArray(textOrLines)){var lines=textOrLines,total=0,line;for(line=0;line<lines.length;line++){if(!(total<offset))return total===offset?line:line-1;total+=lines[line].length+1}return offset<=total?line-1:void 0}return textOrLines.substr(0,offset).split("\n").length-1}function startsWith(str,prefix){return str.slice(0,prefix.length)===prefix}function endsWith(str,suffix){return-1!==str.indexOf(suffix,str.length-suffix.length)}function urlSort(a,b){var a2,b2;function isFile(s){return s.lastIndexOf("/")+1<s.length}return"win"===brackets.platform?(a2=(isFile(a)?"1":"0")+a.toLowerCase(),b2=(isFile(b)?"1":"0")+b.toLowerCase()):(a2=a.toLowerCase(),b2=b.toLowerCase()),a2===b2?0:a2>b2?1:-1}function breakableUrl(url){var escUrl;return _.escape(url).replace(new RegExp(regexEscape("/"),"g"),"/​")}function prettyPrintBytes(bytes,precision){var kilobyte=1024,megabyte=1048576,gigabyte=1073741824,terabyte=1099511627776,returnVal=bytes;if(bytes>=0&&bytes<1024)returnVal=bytes+" B";else if(bytes<1048576)returnVal=(bytes/1024).toFixed(precision)+" KB";else if(bytes<gigabyte)returnVal=(bytes/1048576).toFixed(precision)+" MB";else if(bytes<terabyte)returnVal=(bytes/gigabyte).toFixed(precision)+" GB";else if(bytes>=terabyte)return(bytes/terabyte).toFixed(precision)+" TB";return returnVal}function truncate(str,len){if(str.length>len){var lastSpaceChar=(str=str.substr(0,len)).lastIndexOf(" ");return lastSpaceChar<len&&lastSpaceChar>-1&&(str=str.substr(0,lastSpaceChar)),str}}function hashCode(str){var hash=0,i,chr,len;if(0===str.length)return hash;for(i=0,len=str.length;i<len;i++)hash=(hash<<5)-hash+(chr=str.charCodeAt(i)),hash|=0;return hash}function randomString(stringLength=10,prefix=""){const randomBuffer=new Uint8Array(stringLength);crypto.getRandomValues(randomBuffer);const charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";let randomId=prefix||"";return Array.from(randomBuffer).forEach(byte=>{randomId+=charset[byte%charset.length]}),randomId}function isNumber(value){return parseFloat(value).toString()===value}exports.format=format,exports.regexEscape=regexEscape,exports.jQueryIdEscape=jQueryIdEscape,exports.getLines=getLines,exports.offsetToLineNum=offsetToLineNum,exports.urlSort=urlSort,exports.breakableUrl=breakableUrl,exports.startsWith=startsWith,exports.endsWith=endsWith,exports.prettyPrintBytes=prettyPrintBytes,exports.truncate=truncate,exports.hashCode=hashCode,exports.randomString=randomString,exports.isNumber=isNumber});