@thi.ng/strings
Advanced tools
Comparing version 1.12.0 to 1.13.0
@@ -6,2 +6,21 @@ # Change Log | ||
# [1.13.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@1.12.0...@thi.ng/strings@1.13.0) (2021-01-10) | ||
### Features | ||
* **strings:** add stripAnsi(), lengthAnsi() fns ([86fa81a](https://github.com/thi-ng/umbrella/commit/86fa81acb7dfcf1dc3d6f5600cbf427ee44cf722)) | ||
* **strings:** add tab conversion fns ([aefdd97](https://github.com/thi-ng/umbrella/commit/aefdd97e27fce2405860e817b9c5b4aedb6e59e4)) | ||
* **strings:** add wordWrap*() fns ([2a283c0](https://github.com/thi-ng/umbrella/commit/2a283c018592d8cc76f4ef83b69c6ce3c378aca6)) | ||
* **strings:** update padLeft/Right() args ([118f97f](https://github.com/thi-ng/umbrella/commit/118f97f1fca27671c53d184484a7b435e6eedf88)) | ||
### Performance Improvements | ||
* **strings:** simplify string default delim regexp ([bb62760](https://github.com/thi-ng/umbrella/commit/bb62760f2069a1f7edeaa09ce0e0639047789af3)) | ||
# [1.12.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@1.11.4...@thi.ng/strings@1.12.0) (2021-01-05) | ||
@@ -8,0 +27,0 @@ |
export * from "./api"; | ||
export * from "./ansi"; | ||
export * from "./case"; | ||
@@ -22,2 +23,3 @@ export * from "./center"; | ||
export * from "./split"; | ||
export * from "./tabs"; | ||
export * from "./trim"; | ||
@@ -29,2 +31,3 @@ export * from "./truncate"; | ||
export * from "./wrap"; | ||
export * from "./word-wrap"; | ||
//# sourceMappingURL=index.d.ts.map |
export * from "./api"; | ||
export * from "./ansi"; | ||
export * from "./case"; | ||
@@ -22,2 +23,3 @@ export * from "./center"; | ||
export * from "./split"; | ||
export * from "./tabs"; | ||
export * from "./trim"; | ||
@@ -29,1 +31,2 @@ export * from "./truncate"; | ||
export * from "./wrap"; | ||
export * from "./word-wrap"; |
117
lib/index.js
@@ -11,2 +11,6 @@ 'use strict'; | ||
const RE = /\x1b\[[0-9;]+m/g; | ||
const stripAnsi = (x) => x.replace(RE, ""); | ||
const lengthAnsi = (x) => stripAnsi(x).length; | ||
const upper = (x) => x.toUpperCase(); | ||
@@ -107,5 +111,9 @@ const lower = (x) => x.toLowerCase(); | ||
const buf = repeat(String(ch), n); | ||
return (x) => x != null | ||
? ((x = x.toString()), x.length < n ? buf.substr(x.length) + x : x) | ||
: buf; | ||
return (x, len) => { | ||
if (x == null) | ||
return buf; | ||
x = x.toString(); | ||
len = len !== undefined ? len : x.length; | ||
return len < n ? buf.substr(len) + x : x; | ||
}; | ||
}); | ||
@@ -213,5 +221,9 @@ const Z2 = padLeft(2, "0"); | ||
const buf = repeat(String(ch), n); | ||
return (x) => x != null | ||
? ((x = x.toString()), x.length < n ? x + buf.substr(x.length) : x) | ||
: buf; | ||
return (x, len) => { | ||
if (x == null) | ||
return buf; | ||
x = x.toString(); | ||
len = len !== undefined ? len : x.length; | ||
return len < n ? x + buf.substr(len) : x; | ||
}; | ||
}); | ||
@@ -268,3 +280,3 @@ | ||
function* split(src, delim = /(\r\n)|\n|\r/g, includeDelim = false) { | ||
function* split(src, delim = /\r?\n/g, includeDelim = false) { | ||
let i = 0; | ||
@@ -285,2 +297,49 @@ const n = src.length; | ||
const nextTab = (x, tabSize) => Math.floor((x + tabSize) / tabSize) * tabSize; | ||
const tabsToSpaces = (src, tabSize = 4) => src | ||
.split(/\r?\n/g) | ||
.map((line) => tabsToSpacesLine(line, tabSize)) | ||
.join("\n"); | ||
const tabsToSpacesLine = (line, tabSize = 4) => { | ||
let res = ""; | ||
let words = line.split(/\t/g); | ||
let n = words.length - 1; | ||
for (let i = 0; i < n; i++) { | ||
const w = words[i]; | ||
res += w; | ||
res += repeat(" ", nextTab(res.length, tabSize) - res.length); | ||
} | ||
res += words[n]; | ||
return res; | ||
}; | ||
const spacesToTabs = (src, tabSize = 4) => src | ||
.split(/\r?\n/g) | ||
.map((line) => spacesToTabsLine(line, tabSize)) | ||
.join("\n"); | ||
const spacesToTabsLine = (line, tabSize = 4) => { | ||
const re = /\s{2,}/g; | ||
let i = 0; | ||
let res = ""; | ||
let m; | ||
while ((m = re.exec(line))) { | ||
const numSpaces = m[0].length; | ||
res += line.substring(i, m.index); | ||
i = m.index; | ||
const end = m.index + numSpaces; | ||
while (i < end) { | ||
const j = nextTab(i, tabSize); | ||
if (j <= end) { | ||
res += "\t"; | ||
i = j; | ||
} | ||
else { | ||
res += repeat(" ", end - i); | ||
i = end; | ||
} | ||
} | ||
i = end; | ||
} | ||
return res + line.substr(i); | ||
}; | ||
const trim = memoize.memoize1((chars = " \t\n\r") => { | ||
@@ -377,2 +436,37 @@ chars = `(${chars | ||
const wordWrap = (str, lineWidth) => wordWrapLines(str, lineWidth).join("\n"); | ||
const wordWrapLines = (str, lineWidth = 80) => { | ||
const res = []; | ||
for (let line of str.split("\n")) { | ||
if (!line.length) { | ||
res.push(""); | ||
continue; | ||
} | ||
wordWrapLine(line, lineWidth, res); | ||
} | ||
return res; | ||
}; | ||
const wordWrapLine = (line, lineWidth = 80, acc = []) => { | ||
let ln = 0; | ||
let curr = []; | ||
for (let w of line.split(" ")) { | ||
const l = lengthAnsi(w) + (ln > 0 ? 1 : 0); | ||
if (ln + l <= lineWidth) { | ||
curr.push(w, " "); | ||
ln += l; | ||
} | ||
else { | ||
acc.push(trimLine(curr)); | ||
curr = [w, " "]; | ||
ln = l; | ||
} | ||
} | ||
ln && acc.push(trimLine(curr)); | ||
return acc; | ||
}; | ||
const trimLine = (x) => { | ||
/^\s+$/.test(x[x.length - 1]) && x.pop(); | ||
return x.join(""); | ||
}; | ||
exports.ALPHA = ALPHA; | ||
@@ -419,2 +513,3 @@ exports.ALPHA_NUM = ALPHA_NUM; | ||
exports.kebab = kebab; | ||
exports.lengthAnsi = lengthAnsi; | ||
exports.lower = lower; | ||
@@ -433,5 +528,10 @@ exports.maybeParseFloat = maybeParseFloat; | ||
exports.snake = snake; | ||
exports.spacesToTabs = spacesToTabs; | ||
exports.spacesToTabsLine = spacesToTabsLine; | ||
exports.splice = splice; | ||
exports.split = split; | ||
exports.str = str; | ||
exports.stripAnsi = stripAnsi; | ||
exports.tabsToSpaces = tabsToSpaces; | ||
exports.tabsToSpacesLine = tabsToSpacesLine; | ||
exports.trim = trim; | ||
@@ -446,2 +546,5 @@ exports.truncate = truncate; | ||
exports.uuid = uuid; | ||
exports.wordWrap = wordWrap; | ||
exports.wordWrapLine = wordWrapLine; | ||
exports.wordWrapLines = wordWrapLines; | ||
exports.wrap = wrap; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/memoize"),require("@thi.ng/hex"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/memoize","@thi.ng/hex","@thi.ng/errors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.strings={}),e.thi.ng.memoize,e.thi.ng.hex,e.thi.ng.errors)}(this,(function(e,t,n,r){"use strict";const o=e=>e.toUpperCase(),s=e=>e.toLowerCase(),i=(e,t="-")=>s(e.replace(/([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g,((e,n,r)=>n+t+r))),a=e=>i(e,"_"),g=t.memoizeJ(((e,t)=>e.repeat(t))),l=t.memoizeJ(((e,t="")=>n=>n.length>e?n.substr(0,e-t.length)+t:n)),u=l,c=t.memoizeJ(((e,t=" ")=>{const n=g(String(t),e);return t=>{if(null==t)return n;t=t.toString();const r=(e-t.length)/2;return t.length<e?n.substr(0,r)+t+n.substr(0,r+((1&e)==(1&t.length)?0:1)):l(e)(t)}})),p=t.memoizeJ(((e,t,n="")=>{const r=g("0",t);return o=>(o=(o>>>0).toString(e),n+(o.length<t?r.substr(o.length)+o:o))})),f=p(2,8),h=p(2,16),m=p(2,32),b=n.U8,d=n.U16,x=n.U24,z=n.U32,U=n.U64HL,y={0:"\0",b:"\b",t:"\t",r:"\r",v:"\v",f:"\f",n:"\n","'":"'",'"':'"',"\\":"\\"},S={0:"0",8:"b",9:"t",10:"n",11:"v",12:"f",13:"r",33:"'",34:'"',92:"\\"},C=t.memoizeJ(((e,t=" ")=>{const n=g(String(t),e);return t=>null!=t?(t=t.toString()).length<e?n.substr(t.length)+t:t:n})),A=C(2,"0"),M=C(3,"0"),j=C(4,"0"),E=t.memoizeJ((e=>t=>B(t)||t.toFixed(e))),$=t.memoizeJ(((e,t=3)=>{const n=e-t-1,r=Math.pow(10,n),o=-Math.pow(10,n-1),s=Math.pow(10,-(t-1)),i=C(e);return n=>{const a=Math.abs(n);return i(B(n)||(0===n?"0":a<s||a>=r?w(n,e):n.toFixed(t-(n<o?1:0))))}})),w=(e,t)=>e.toExponential(Math.max(t-4-(Math.log(Math.abs(e))/Math.LN10>=10?2:1)-(e<0?1:0),0)),B=e=>isNaN(e)?"NaN":e===1/0?"+∞":e===-1/0?"-∞":void 0,O=(e,...t)=>{const n=[];for(let r=0,o=0,s=e.length;r<s;r++){const s=e[r],i=typeof s;n.push("function"===i?s(t[o++]):"object"===i?s[t[o++]]:s)}return n.join("")};function*P(e,t){let n="string"==typeof e?e.charCodeAt(0):e;const r="string"==typeof t?t.charCodeAt(0):t;if(n<=r)for(;n<=r;n++)yield String.fromCharCode(n);else for(;n>=r;n--)yield String.fromCharCode(n)}const N=(...e)=>{const t={};for(let n of e)for(let e of n)t[e]=!0;return Object.freeze(t)},F=Object.freeze({"\t":!0,"\n":!0,"\v":!0,"\f":!0,"\r":!0," ":!0}),J=N(P("0","9")),L=N(P("0","9"),P("A","F"),P("a","f")),v=N(P("a","z")),R=N(P("A","Z")),I=Object.freeze(Object.assign(Object.assign({},R),v)),_=Object.freeze(Object.assign(Object.assign(Object.assign({},I),J),{_:!0})),k=N(P("!","/"),P(":","@"),P("[","`"),P("{","~")),H=/\{(\d+)\}/g,T=/\{([a-z0-9_.-]+)\}/gi,Z=t.memoize1((e=>t=>t.join(e))),G=t.memoizeJ(((e,t=" ")=>{const n=g(String(t),e);return t=>null!=t?(t=t.toString()).length<e?t+n.substr(t.length):t:n})),q="àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;",K=new RegExp(q.split("").join("|"),"g");const W=t.memoize1(((e=" \t\n\r")=>{e=`(${e.split("").map((e=>`\\${e}`)).join("|")})`;const t=new RegExp(`(^${e}+)|(${e}+$)`,"g");return e=>e.replace(t,"")})),D=t.memoizeJ(((e,t="")=>n=>n.length>e?t+n.substr(n.length-e+t.length):n)),V=t.memoizeJ(((e,t,n=2)=>{const r=e.map((e=>[e[0],null!=e[2]?e[2]:n,e[1]])).sort(((e,t)=>e[0]-t[0]));return e=>{if(0===e)return`0${t}`;const n=Math.abs(e);for(let t=r.length;--t>=0;){const o=r[t];if(n>=o[0]||0===t)return(e/o[0]).toFixed(o[1])+o[2]}return""}})),X=1024,Q=V([[1," bits",0],[X," Kb"],[X**2," Mb"],[X**3," Gb"]]," bits",2),Y=V([[1," bytes",0],[X," KB"],[X**2," MB"],[X**3," GB"],[X**4," TB"],[X**5," PB"]]," bytes",2),ee=V([[1e-12," ps"],[1e-9," ns"],[1e-6," µs"],[.001," ms"],[1," secs"],[60," mins"],[3600," hours"],[86400," days"]]," secs",3),te=V([[1e-12," pm"],[1e-9," nm"],[1e-6," µm"],[.001," mm"],[.01," cm"],[1," m"],[1e3," km"]]," m",2),ne=V([[1e-12," pg"],[1e-9," ng"],[1e-6," µg"],[.001," mg"],[1," g"],[1e3," kg"],[1e6," t"],[1e9," kt"],[1e12," Mt"]]," g",2),re=t.memoizeJ((e=>t=>e+t+e));e.ALPHA=I,e.ALPHA_NUM=_,e.B16=h,e.B32=m,e.B8=f,e.BOM="\ufeff",e.DIGITS=J,e.ESCAPES=y,e.ESCAPES_REV=S,e.HEX=L,e.LOWER=v,e.PUNCTUATION=k,e.U16=d,e.U24=x,e.U32=z,e.U64=U,e.U8=b,e.UPPER=R,e.WS=F,e.Z2=A,e.Z3=M,e.Z4=j,e.bits=Q,e.bytes=Y,e.camel=(e,t="-")=>s(e).replace(new RegExp(`\\${t}+(\\w)`,"g"),((e,t)=>o(t))),e.capitalize=e=>e[0].toUpperCase()+e.substr(1),e.center=c,e.charRange=P,e.computeCursorPos=(e,t,n="\n")=>{if(!e.length)return[1,1];t=Math.min(Math.max(0,t),e.length);const r=e.split(n),o=r.length;for(let e=0;e<o;e++){const n=r[e];if(t<=n.length)return[e+1,t+1];t-=n.length+1}return[o,1]},e.defFormat=e=>(...t)=>O(e,...t),e.escape=e=>e.replace(/[\0\b\t\n\v\f\r'"\\]/g,(e=>`\\${S[e.charCodeAt(0)]}`)).replace(/[\ud800-\udfff]{2}/g,(e=>`\\U${z(e.codePointAt(0))}`)).replace(/[^\u0020-\u007e]/g,(e=>`\\u${d(e.charCodeAt(0))}`)),e.float=E,e.floatFixedWidth=$,e.format=O,e.grams=ne,e.hstr=e=>null!=e?`${(e=e.toString()).length}H${e}`:"",e.ignore=e=>"",e.interpolate=(e,...t)=>t.length>0?e.replace(H,((e,n)=>String(t[parseInt(n,10)]))):e,e.interpolateKeys=(e,t)=>e.replace(T,((e,n)=>null!=t[n]?String(t[n]):r.illegalArgs(`missing key: ${n}`))),e.join=Z,e.kebab=i,e.lower=s,e.maybeParseFloat=(e,t=0)=>{const n=parseFloat(e);return isNaN(n)?t:n},e.maybeParseInt=(e,t=0,n=10)=>{const r=parseInt(e,n);return isNaN(r)?t:r},e.meters=te,e.padLeft=C,e.padRight=G,e.percent=(e=0)=>t=>(100*t).toFixed(e)+"%",e.radix=p,e.repeat=g,e.seconds=ee,e.slugify=e=>e.toLowerCase().replace(/\s+/g,"-").replace(K,(e=>"aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------"[q.indexOf(e)])).replace(/&+/g,"-and-").replace(/[^\w\-]+/g,"").replace(/\-{2,}/g,"-").replace(/(^-+)|(-+$)/g,""),e.slugifyGH=e=>e.toLowerCase().replace(/[!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~\u0000-\u001f\u2000-\u206f\u2700-\u27bf\u2e00-\u2e7f]/g,"").replace(/\s/g,"-"),e.snake=a,e.splice=(e,t,n,o=n)=>(n<0&&(n+=e.length),o<0&&(o+=e.length),n>o&&r.illegalArgs("'from' index must be <= 'to'"),o=Math.max(o,0),n<=0?t+e.substr(o):n>=e.length?e+t:e.substr(0,n)+t+e.substr(o)),e.split=function*(e,t=/(\r\n)|\n|\r/g,n=!1){let r=0;const o=e.length,s=~~n;for(;r<o;){const n=t.exec(e);if(!n)return void(yield e.substring(r));const o=n[0].length;yield e.substring(r,n.index+s*o),r=n.index+o}},e.str=e=>String(e),e.trim=W,e.truncate=l,e.truncateLeft=D,e.truncateRight=u,e.unescape=e=>e.replace(/\\u([0-9a-fA-F]{4})/g,((e,t)=>String.fromCharCode(parseInt(t,16)))).replace(/\\U([0-9a-fA-F]{8})/g,((e,t)=>String.fromCodePoint(parseInt(t,16)))).replace(/\\([0btnvfr'"\\])/g,((e,t)=>y[t])),e.units=V,e.upper=o,e.upperSnake=e=>a(e).toUpperCase(),e.uuid=(e,t=0)=>n.U32BE(e,t)+"-"+n.U16BE(e,t+4)+"-"+n.U16BE(e,t+6)+"-"+n.U16BE(e,t+8)+"-"+n.U48BE(e,t+10),e.wrap=re,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/memoize"),require("@thi.ng/hex"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/memoize","@thi.ng/hex","@thi.ng/errors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.strings={}),e.thi.ng.memoize,e.thi.ng.hex,e.thi.ng.errors)}(this,(function(e,t,n,r){"use strict";const s=/\x1b\[[0-9;]+m/g,o=e=>e.replace(s,""),i=e=>o(e).length,a=e=>e.toUpperCase(),g=e=>e.toLowerCase(),l=(e,t="-")=>g(e.replace(/([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g,((e,n,r)=>n+t+r))),u=e=>l(e,"_"),c=t.memoizeJ(((e,t)=>e.repeat(t))),p=t.memoizeJ(((e,t="")=>n=>n.length>e?n.substr(0,e-t.length)+t:n)),h=p,f=t.memoizeJ(((e,t=" ")=>{const n=c(String(t),e);return t=>{if(null==t)return n;t=t.toString();const r=(e-t.length)/2;return t.length<e?n.substr(0,r)+t+n.substr(0,r+((1&e)==(1&t.length)?0:1)):p(e)(t)}})),m=t.memoizeJ(((e,t,n="")=>{const r=c("0",t);return s=>(s=(s>>>0).toString(e),n+(s.length<t?r.substr(s.length)+s:s))})),d=m(2,8),b=m(2,16),x=m(2,32),z=n.U8,S=n.U16,U=n.U24,y=n.U32,j=n.U64HL,A={0:"\0",b:"\b",t:"\t",r:"\r",v:"\v",f:"\f",n:"\n","'":"'",'"':'"',"\\":"\\"},C={0:"0",8:"b",9:"t",10:"n",11:"v",12:"f",13:"r",33:"'",34:'"',92:"\\"},M=t.memoizeJ(((e,t=" ")=>{const n=c(String(t),e);return(t,r)=>null==t?n:(t=t.toString(),(r=void 0!==r?r:t.length)<e?n.substr(r)+t:t)})),w=M(2,"0"),E=M(3,"0"),$=M(4,"0"),B=t.memoizeJ((e=>t=>P(t)||t.toFixed(e))),L=t.memoizeJ(((e,t=3)=>{const n=e-t-1,r=Math.pow(10,n),s=-Math.pow(10,n-1),o=Math.pow(10,-(t-1)),i=M(e);return n=>{const a=Math.abs(n);return i(P(n)||(0===n?"0":a<o||a>=r?O(n,e):n.toFixed(t-(n<s?1:0))))}})),O=(e,t)=>e.toExponential(Math.max(t-4-(Math.log(Math.abs(e))/Math.LN10>=10?2:1)-(e<0?1:0),0)),P=e=>isNaN(e)?"NaN":e===1/0?"+∞":e===-1/0?"-∞":void 0,N=(e,...t)=>{const n=[];for(let r=0,s=0,o=e.length;r<o;r++){const o=e[r],i=typeof o;n.push("function"===i?o(t[s++]):"object"===i?o[t[s++]]:o)}return n.join("")};function*T(e,t){let n="string"==typeof e?e.charCodeAt(0):e;const r="string"==typeof t?t.charCodeAt(0):t;if(n<=r)for(;n<=r;n++)yield String.fromCharCode(n);else for(;n>=r;n--)yield String.fromCharCode(n)}const v=(...e)=>{const t={};for(let n of e)for(let e of n)t[e]=!0;return Object.freeze(t)},F=Object.freeze({"\t":!0,"\n":!0,"\v":!0,"\f":!0,"\r":!0," ":!0}),J=v(T("0","9")),R=v(T("0","9"),T("A","F"),T("a","f")),I=v(T("a","z")),_=v(T("A","Z")),k=Object.freeze(Object.assign(Object.assign({},_),I)),H=Object.freeze(Object.assign(Object.assign(Object.assign({},k),J),{_:!0})),W=v(T("!","/"),T(":","@"),T("[","`"),T("{","~")),Z=/\{(\d+)\}/g,G=/\{([a-z0-9_.-]+)\}/gi,q=t.memoize1((e=>t=>t.join(e))),K=t.memoizeJ(((e,t=" ")=>{const n=c(String(t),e);return(t,r)=>null==t?n:(t=t.toString(),(r=void 0!==r?r:t.length)<e?t+n.substr(r):t)})),D="àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;",V=new RegExp(D.split("").join("|"),"g");const X=(e,t)=>Math.floor((e+t)/t)*t,Q=(e,t=4)=>{let n="",r=e.split(/\t/g),s=r.length-1;for(let e=0;e<s;e++){n+=r[e],n+=c(" ",X(n.length,t)-n.length)}return n+=r[s],n},Y=(e,t=4)=>{const n=/\s{2,}/g;let r,s=0,o="";for(;r=n.exec(e);){const n=r[0].length;o+=e.substring(s,r.index),s=r.index;const i=r.index+n;for(;s<i;){const e=X(s,t);e<=i?(o+="\t",s=e):(o+=c(" ",i-s),s=i)}s=i}return o+e.substr(s)},ee=t.memoize1(((e=" \t\n\r")=>{e=`(${e.split("").map((e=>`\\${e}`)).join("|")})`;const t=new RegExp(`(^${e}+)|(${e}+$)`,"g");return e=>e.replace(t,"")})),te=t.memoizeJ(((e,t="")=>n=>n.length>e?t+n.substr(n.length-e+t.length):n)),ne=t.memoizeJ(((e,t,n=2)=>{const r=e.map((e=>[e[0],null!=e[2]?e[2]:n,e[1]])).sort(((e,t)=>e[0]-t[0]));return e=>{if(0===e)return`0${t}`;const n=Math.abs(e);for(let t=r.length;--t>=0;){const s=r[t];if(n>=s[0]||0===t)return(e/s[0]).toFixed(s[1])+s[2]}return""}})),re=1024,se=ne([[1," bits",0],[re," Kb"],[re**2," Mb"],[re**3," Gb"]]," bits",2),oe=ne([[1," bytes",0],[re," KB"],[re**2," MB"],[re**3," GB"],[re**4," TB"],[re**5," PB"]]," bytes",2),ie=ne([[1e-12," ps"],[1e-9," ns"],[1e-6," µs"],[.001," ms"],[1," secs"],[60," mins"],[3600," hours"],[86400," days"]]," secs",3),ae=ne([[1e-12," pm"],[1e-9," nm"],[1e-6," µm"],[.001," mm"],[.01," cm"],[1," m"],[1e3," km"]]," m",2),ge=ne([[1e-12," pg"],[1e-9," ng"],[1e-6," µg"],[.001," mg"],[1," g"],[1e3," kg"],[1e6," t"],[1e9," kt"],[1e12," Mt"]]," g",2),le=t.memoizeJ((e=>t=>e+t+e)),ue=(e,t=80)=>{const n=[];for(let r of e.split("\n"))r.length?ce(r,t,n):n.push("");return n},ce=(e,t=80,n=[])=>{let r=0,s=[];for(let o of e.split(" ")){const e=i(o)+(r>0?1:0);r+e<=t?(s.push(o," "),r+=e):(n.push(pe(s)),s=[o," "],r=e)}return r&&n.push(pe(s)),n},pe=e=>(/^\s+$/.test(e[e.length-1])&&e.pop(),e.join(""));e.ALPHA=k,e.ALPHA_NUM=H,e.B16=b,e.B32=x,e.B8=d,e.BOM="\ufeff",e.DIGITS=J,e.ESCAPES=A,e.ESCAPES_REV=C,e.HEX=R,e.LOWER=I,e.PUNCTUATION=W,e.U16=S,e.U24=U,e.U32=y,e.U64=j,e.U8=z,e.UPPER=_,e.WS=F,e.Z2=w,e.Z3=E,e.Z4=$,e.bits=se,e.bytes=oe,e.camel=(e,t="-")=>g(e).replace(new RegExp(`\\${t}+(\\w)`,"g"),((e,t)=>a(t))),e.capitalize=e=>e[0].toUpperCase()+e.substr(1),e.center=f,e.charRange=T,e.computeCursorPos=(e,t,n="\n")=>{if(!e.length)return[1,1];t=Math.min(Math.max(0,t),e.length);const r=e.split(n),s=r.length;for(let e=0;e<s;e++){const n=r[e];if(t<=n.length)return[e+1,t+1];t-=n.length+1}return[s,1]},e.defFormat=e=>(...t)=>N(e,...t),e.escape=e=>e.replace(/[\0\b\t\n\v\f\r'"\\]/g,(e=>`\\${C[e.charCodeAt(0)]}`)).replace(/[\ud800-\udfff]{2}/g,(e=>`\\U${y(e.codePointAt(0))}`)).replace(/[^\u0020-\u007e]/g,(e=>`\\u${S(e.charCodeAt(0))}`)),e.float=B,e.floatFixedWidth=L,e.format=N,e.grams=ge,e.hstr=e=>null!=e?`${(e=e.toString()).length}H${e}`:"",e.ignore=e=>"",e.interpolate=(e,...t)=>t.length>0?e.replace(Z,((e,n)=>String(t[parseInt(n,10)]))):e,e.interpolateKeys=(e,t)=>e.replace(G,((e,n)=>null!=t[n]?String(t[n]):r.illegalArgs(`missing key: ${n}`))),e.join=q,e.kebab=l,e.lengthAnsi=i,e.lower=g,e.maybeParseFloat=(e,t=0)=>{const n=parseFloat(e);return isNaN(n)?t:n},e.maybeParseInt=(e,t=0,n=10)=>{const r=parseInt(e,n);return isNaN(r)?t:r},e.meters=ae,e.padLeft=M,e.padRight=K,e.percent=(e=0)=>t=>(100*t).toFixed(e)+"%",e.radix=m,e.repeat=c,e.seconds=ie,e.slugify=e=>e.toLowerCase().replace(/\s+/g,"-").replace(V,(e=>"aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------"[D.indexOf(e)])).replace(/&+/g,"-and-").replace(/[^\w\-]+/g,"").replace(/\-{2,}/g,"-").replace(/(^-+)|(-+$)/g,""),e.slugifyGH=e=>e.toLowerCase().replace(/[!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~\u0000-\u001f\u2000-\u206f\u2700-\u27bf\u2e00-\u2e7f]/g,"").replace(/\s/g,"-"),e.snake=u,e.spacesToTabs=(e,t=4)=>e.split(/\r?\n/g).map((e=>Y(e,t))).join("\n"),e.spacesToTabsLine=Y,e.splice=(e,t,n,s=n)=>(n<0&&(n+=e.length),s<0&&(s+=e.length),n>s&&r.illegalArgs("'from' index must be <= 'to'"),s=Math.max(s,0),n<=0?t+e.substr(s):n>=e.length?e+t:e.substr(0,n)+t+e.substr(s)),e.split=function*(e,t=/\r?\n/g,n=!1){let r=0;const s=e.length,o=~~n;for(;r<s;){const n=t.exec(e);if(!n)return void(yield e.substring(r));const s=n[0].length;yield e.substring(r,n.index+o*s),r=n.index+s}},e.str=e=>String(e),e.stripAnsi=o,e.tabsToSpaces=(e,t=4)=>e.split(/\r?\n/g).map((e=>Q(e,t))).join("\n"),e.tabsToSpacesLine=Q,e.trim=ee,e.truncate=p,e.truncateLeft=te,e.truncateRight=h,e.unescape=e=>e.replace(/\\u([0-9a-fA-F]{4})/g,((e,t)=>String.fromCharCode(parseInt(t,16)))).replace(/\\U([0-9a-fA-F]{8})/g,((e,t)=>String.fromCodePoint(parseInt(t,16)))).replace(/\\([0btnvfr'"\\])/g,((e,t)=>A[t])),e.units=ne,e.upper=a,e.upperSnake=e=>u(e).toUpperCase(),e.uuid=(e,t=0)=>n.U32BE(e,t)+"-"+n.U16BE(e,t+4)+"-"+n.U16BE(e,t+6)+"-"+n.U16BE(e,t+8)+"-"+n.U48BE(e,t+10),e.wordWrap=(e,t)=>ue(e,t).join("\n"),e.wordWrapLine=ce,e.wordWrapLines=ue,e.wrap=le,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/strings", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"description": "Various string formatting & utility functions", | ||
@@ -89,3 +89,3 @@ "module": "./index.js", | ||
}, | ||
"gitHead": "f651fb74189143238678d3fd41fc556d1a3c874b" | ||
"gitHead": "ec0b1d686c9d5f8f73e2c170b9915c2dd875903f" | ||
} |
@@ -1,2 +0,1 @@ | ||
import type { Stringer } from "./api"; | ||
/** | ||
@@ -6,15 +5,15 @@ * @param n - target length | ||
*/ | ||
export declare const padLeft: (n: number, ch?: string | number) => Stringer<any>; | ||
export declare const padLeft: (n: number, ch?: string | number) => (x: any, length?: number) => string; | ||
/** | ||
* Zero-padded 2 digit formatter. | ||
*/ | ||
export declare const Z2: Stringer<any>; | ||
export declare const Z2: (x: any, length?: number | undefined) => string; | ||
/** | ||
* Zero-padded 3 digit formatter. | ||
*/ | ||
export declare const Z3: Stringer<any>; | ||
export declare const Z3: (x: any, length?: number | undefined) => string; | ||
/** | ||
* Zero-padded 4 digit formatter. | ||
*/ | ||
export declare const Z4: Stringer<any>; | ||
export declare const Z4: (x: any, length?: number | undefined) => string; | ||
//# sourceMappingURL=pad-left.d.ts.map |
@@ -9,5 +9,9 @@ import { memoizeJ } from "@thi.ng/memoize"; | ||
const buf = repeat(String(ch), n); | ||
return (x) => x != null | ||
? ((x = x.toString()), x.length < n ? buf.substr(x.length) + x : x) | ||
: buf; | ||
return (x, len) => { | ||
if (x == null) | ||
return buf; | ||
x = x.toString(); | ||
len = len !== undefined ? len : x.length; | ||
return len < n ? buf.substr(len) + x : x; | ||
}; | ||
}); | ||
@@ -14,0 +18,0 @@ /** |
@@ -1,2 +0,1 @@ | ||
import type { Stringer } from "./api"; | ||
/** | ||
@@ -6,3 +5,3 @@ * @param n - target length | ||
*/ | ||
export declare const padRight: (n: number, ch?: string | number) => Stringer<any>; | ||
export declare const padRight: (n: number, ch?: string | number) => (x: any, length?: number) => string; | ||
//# sourceMappingURL=pad-right.d.ts.map |
@@ -9,5 +9,9 @@ import { memoizeJ } from "@thi.ng/memoize"; | ||
const buf = repeat(String(ch), n); | ||
return (x) => x != null | ||
? ((x = x.toString()), x.length < n ? x + buf.substr(x.length) : x) | ||
: buf; | ||
return (x, len) => { | ||
if (x == null) | ||
return buf; | ||
x = x.toString(); | ||
len = len !== undefined ? len : x.length; | ||
return len < n ? x + buf.substr(len) : x; | ||
}; | ||
}); |
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
export function* split(src, delim = /(\r\n)|\n|\r/g, includeDelim = false) { | ||
export function* split(src, delim = /\r?\n/g, includeDelim = false) { | ||
let i = 0; | ||
@@ -18,0 +18,0 @@ const n = src.length; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
137176
70
2041