UNPKG

ninjs-lodash

Version:
132 lines (111 loc) 4.29 kB
/** * String Utils / Formatting * ninjs.lodash.str */ 'use strict' const _ = require('lodash') const REGX_APLHA_NUM = /[^a-z0-9]+/g const REGX_DASHES = /^-|-$/g const REGX_LINES = /\r\n|\n\r|\n|\r/g const REGX_LINES_COMPACT = /[^\r\n]+/g const REGX_COMMAS = /(\d+)(\d{3})/ const REGX_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg // const REGX_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/mg exports = module.exports = { "isString": isString, "notString": notString, "guid": guid, "ns": ns, "prune": prune, "strip": strip, "insertString": insertString, "commas": commas, "lines": lines, "rawlines": rawlines, "bytes": bytes, "niceBytes": niceBytes, "cleanData": cleanData } // original _.isString before clobbering it with mixin let _isString = _.isString _.mixin(exports) // 16 character unique id function isString(val, retFalseIfEmpty) { return retFalseIfEmpty ? (val && _isString(val) ? true : false) : (_isString(val) ? true : false) } function notString(val) { return !isString(val, true) } // 16 character unique id function guid() { return Math.random().toString(36).substring(2, 8) + Math.random().toString(36).substring(2, 8) } // returns string of joined args with "." function ns(src) { return _.join(_.compact(_.toArray(arguments)), ".") } // trims end of string, can trimmed back to next clean word, // or add suffix to end of trimmed string function prune(str, max, nice, suf) { max = max || 140 nice = _.isBoolean(nice) ? nice : false if (!str || max <= 0 || str.length <= max) return str suf = suf || "..." str = str.substr(0, max) return nice ? str.substr(0, Math.min(str.length, str.lastIndexOf(" "))) + suf : str } // returns src string stripped of any comments function strip(src) { return src && _.isString(src) ? _.replace(src, REGX_COMMENTS, "") : src } function lines(str) { if (!str || !_.isString(str)) return [] let ret = str.match(REGX_LINES_COMPACT) || [] ret = _.compact(_.map(ret, (line) => { let trimTest = _.trim(line) return trimTest ? line : '' })) return ret } function rawlines(str) { if (!str || !_.isString(str)) return [] return _.replace(str, REGX_LINES, "\n").split("\n") } function insertString(orig, findString, str) { let findIndex = orig.indexOf(findString) if (findIndex < 0) return orig let afterIndex = findIndex + findString.length return orig.slice(0, afterIndex) + str + orig.slice(afterIndex) } function commas(nStr) { let x = (nStr || '').split('.'), x1 = x[0], x2 = x.length > 1 ? '.' + x[1] : '' while (REGX_COMMAS.test(x1)) x1 = _.replace(x1, REGX_COMMAS, '$1' + ',' + '$2') return x1 + x2 } function slug(str) { return _.replace(_.replace(_.toLower(str), REGX_APLHA_NUM, '-'), REGX_DASHES, "") } // bytes // http://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript // precise and pretty -> best function bytes(val, decimals) { if(val == 0) return '0 Byte' let k = 1000 // or 1024 for binary let dm = decimals + 1 || 3 let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] let i = Math.floor(Math.log(val) / Math.log(k)) return parseFloat((val / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i] } // Stays on floor and focuses on format (not precise) function niceBytes(val){ let units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] let n = parseInt(val, 10) || 0 let l = 0 while(n > 1024) { n = n/1024; l++; } return(n.toFixed(n >= 10 ? 0 : 1) + ' ' + units[l]) } // Results: // niceBytes(435) // 435 bytes // niceBytes(3398) // 3.3 KB // niceBytes(490398) // 479 KB // niceBytes(6544528) // 6.2 MB // niceBytes(23483023) // 22 MB // niceBytes(3984578493) // 3.7 GB // niceBytes(30498505889) // 28 GB // niceBytes(9485039485039445) // 8.4 PB function cleanData(data) { return ~['"', "'"].indexOf(data[0]) && ~['"', "'"].indexOf(data[data.length - 1]) && data[0] === data[data.length - 1] ? data.substring(1, data.length - 1) : data }