@thi.ng/strings
Advanced tools
Comparing version 1.8.13 to 1.9.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.9.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@1.8.13...@thi.ng/strings@1.9.0) (2020-07-08) | ||
### Features | ||
* **strings:** add computeCursorPos() ([c178d00](https://github.com/thi-ng/umbrella/commit/c178d00edcdbe12cec492a1629c80bf359116b66)) | ||
## [1.8.13](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@1.8.12...@thi.ng/strings@1.8.13) (2020-07-04) | ||
@@ -8,0 +19,0 @@ |
export * from "./api"; | ||
export * from "./case"; | ||
export * from "./center"; | ||
export * from "./cursor"; | ||
export * from "./float"; | ||
@@ -5,0 +6,0 @@ export * from "./format"; |
export * from "./api"; | ||
export * from "./case"; | ||
export * from "./center"; | ||
export * from "./cursor"; | ||
export * from "./float"; | ||
@@ -5,0 +6,0 @@ export * from "./format"; |
@@ -36,2 +36,17 @@ 'use strict'; | ||
const computeCursorPos = (str, pos, delim = "\n") => { | ||
if (!str.length) | ||
return [1, 1]; | ||
pos = Math.min(Math.max(0, pos), str.length); | ||
const lines = str.split(delim); | ||
const n = lines.length; | ||
for (let i = 0; i < n; i++) { | ||
const l = lines[i]; | ||
if (pos <= l.length) | ||
return [i + 1, pos + 1]; | ||
pos -= l.length + 1; | ||
} | ||
return [n, 1]; | ||
}; | ||
const padLeft = memoize.memoizeJ((n, ch = " ") => { | ||
@@ -341,2 +356,3 @@ const buf = repeat(String(ch), n); | ||
exports.charRange = charRange; | ||
exports.computeCursorPos = computeCursorPos; | ||
exports.defFormat = defFormat; | ||
@@ -343,0 +359,0 @@ exports.float = float; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/memoize"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/memoize","@thi.ng/errors"],t):t(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.strings={}),e.thi.ng.memoize,e.thi.ng.errors)}(this,(function(e,t,r){"use strict";const n=e=>e.toUpperCase(),s=e=>e.toLowerCase(),o=(e,t="-")=>s(e.replace(/([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g,(e,r,n)=>r+t+n)),i=t.memoizeJ((e,t)=>e.repeat(t)),a=t.memoizeJ((e,t="")=>r=>r.length>e?r.substr(0,e-t.length)+t:r),g=a,l=t.memoizeJ((e,t=" ")=>{const r=i(String(t),e);return t=>{if(null==t)return r;t=t.toString();const n=(e-t.length)/2;return t.length<e?r.substr(0,n)+t+r.substr(0,n+((1&e)==(1&t.length)?0:1)):a(e)(t)}}),c=t.memoizeJ((e,t=" ")=>{const r=i(String(t),e);return t=>null!=t?(t=t.toString()).length<e?r.substr(t.length)+t:t:r}),u=c(2,"0"),m=c(3,"0"),p=c(4,"0"),h=t.memoizeJ(e=>t=>d(t)||t.toFixed(e)),f=t.memoizeJ((e,t=3)=>{const r=e-t-1,n=Math.pow(10,r),s=-Math.pow(10,r-1),o=Math.pow(10,-(t-1)),i=c(e);return r=>{const a=Math.abs(r);return i(d(r)||(0===r?"0":a<o||a>=n?b(r,e):r.toFixed(t-(r<s?1:0))))}}),b=(e,t)=>e.toExponential(Math.max(t-4-(Math.log(Math.abs(e))/Math.LN10>=10?2:1)-(e<0?1:0),0)),d=e=>isNaN(e)?"NaN":e===1/0?"+∞":e===-1/0?"-∞":void 0,z=(e,...t)=>{const r=[];for(let n=0,s=0,o=e.length;n<o;n++){const o=e[n],i=typeof o;r.push("function"===i?o(t[s++]):"object"===i?o[t[s++]]:o)}return r.join("")};function*x(e,t){let r="string"==typeof e?e.charCodeAt(0):e;const n="string"==typeof t?t.charCodeAt(0):t;if(r<=n)for(;r<=n;r++)yield String.fromCharCode(r);else for(;r>=n;r--)yield String.fromCharCode(r)}const j=(...e)=>{const t={};for(let r of e)for(let e of r)t[e]=!0;return Object.freeze(t)},y=Object.freeze({"\t":!0,"\n":!0,"\v":!0,"\f":!0,"\r":!0," ":!0}),w=j(x("0","9")),M=j(x("0","9"),x("A","F"),x("a","f")),O=j(x("a","z")),S=j(x("A","Z")),C=Object.freeze(Object.assign(Object.assign({},S),O)),N=Object.freeze(Object.assign(Object.assign(Object.assign({},C),w),{_:!0})),A=j(x("!","/"),x(":","@"),x("[","`"),x("{","~")),J=/\{(\d+)\}/g,U=t.memoize1(e=>t=>t.join(e)),F=t.memoizeJ((e,t=" ")=>{const r=i(String(t),e);return t=>null!=t?(t=t.toString()).length<e?t+r.substr(t.length):t:r}),L=t.memoizeJ((e,t,r="")=>{const n=i("0",t);return s=>(s=(s>>>0).toString(e),r+(s.length<t?n.substr(s.length)+s:s))}),P=L(2,8),$=L(2,16),B=L(2,32),R=L(16,2),E=L(16,4),I=L(16,6),_=L(16,8),k="àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;",H="aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------",Z=new RegExp(k.split("").join("|"),"g"),G=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,"")}),T=t.memoizeJ((e,t="")=>r=>r.length>e?t+r.substr(r.length-e+t.length):r),v=t.memoizeJ((e,t,r=2)=>{const n=e.map(e=>[e[0],null!=e[2]?e[2]:r,e[1]]).sort((e,t)=>e[0]-t[0]);return e=>{if(0===e)return"0"+t;const r=Math.abs(e);for(let t=n.length;--t>=0;){const s=n[t];if(r>=s[0]||0===t)return(e/s[0]).toFixed(s[1])+s[2]}return""}}),W=1024,q=v([[1," bits",0],[W," Kb"],[W**2," Mb"],[W**3," Gb"]]," bits",2),K=v([[1," bytes",0],[W," KB"],[W**2," MB"],[W**3," GB"],[W**4," TB"],[W**5," PB"]]," bytes",2),D=v([[1e-12," ps"],[1e-9," ns"],[1e-6," µs"],[.001," ms"],[1," secs"],[60," mins"],[3600," hours"],[86400," days"]]," secs",3),X=v([[1e-12," pm"],[1e-9," nm"],[1e-6," µm"],[.001," mm"],[.01," cm"],[1," m"],[1e3," km"]]," m",2),Q=v([[1e-12," pg"],[1e-9," ng"],[1e-6," µg"],[.001," mg"],[1," g"],[1e3," kg"],[1e6," t"],[1e9," kt"],[1e12," Mt"]]," g",2),V=t.memoizeJ(e=>t=>e+t+e);e.ALPHA=C,e.ALPHA_NUM=N,e.B16=$,e.B32=B,e.B8=P,e.DIGITS=w,e.HEX=M,e.LOWER=O,e.PUNCTUATION=A,e.U16=E,e.U24=I,e.U32=_,e.U64=(e,t)=>_(e)+_(t),e.U8=R,e.UPPER=S,e.WS=y,e.Z2=u,e.Z3=m,e.Z4=p,e.bits=q,e.bytes=K,e.camel=(e,t="-")=>s(e).replace(new RegExp(`\\${t}+(\\w)`,"g"),(e,t)=>n(t)),e.capitalize=e=>e[0].toUpperCase()+e.substr(1),e.center=l,e.charRange=x,e.defFormat=e=>(...t)=>z(e,...t),e.float=h,e.floatFixedWidth=f,e.format=z,e.grams=Q,e.hstr=e=>null!=e?`${(e=e.toString()).length}H${e}`:"",e.ignore=e=>"",e.interpolate=(e,...t)=>t.length>0?e.replace(J,e=>String(t[parseInt(e[1],10)])):e,e.join=U,e.kebab=o,e.lower=s,e.maybeParseFloat=(e,t=0)=>{const r=parseFloat(e);return isNaN(r)?t:r},e.maybeParseInt=(e,t=0,r=10)=>{const n=parseInt(e,r);return isNaN(n)?t:n},e.meters=X,e.padLeft=c,e.padRight=F,e.percent=(e=0)=>t=>(100*t).toFixed(e)+"%",e.radix=L,e.repeat=i,e.seconds=D,e.slugify=e=>e.toLowerCase().replace(/\s+/g,"-").replace(Z,e=>H[k.indexOf(e)]).replace(/&+/g,"-and-").replace(/[^\w\-]+/g,"").replace(/\-{2,}/g,"-").replace(/(^-+)|(-+$)/g,""),e.slugifyGH=e=>e.toLowerCase().replace(/\s/g,"-").replace(Z,e=>H[k.indexOf(e)]).replace(/[^\w\-]+/g,"").replace(/(^-+)|(-+$)/g,""),e.snake=e=>o(e,"_"),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.str=e=>String(e),e.trim=G,e.truncate=a,e.truncateLeft=T,e.truncateRight=g,e.units=v,e.upper=n,e.uuid=(e,t=0)=>[R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++])].join(""),e.wrap=V,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/memoize"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/memoize","@thi.ng/errors"],t):t(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.strings={}),e.thi.ng.memoize,e.thi.ng.errors)}(this,(function(e,t,n){"use strict";const r=e=>e.toUpperCase(),o=e=>e.toLowerCase(),s=(e,t="-")=>o(e.replace(/([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g,(e,n,r)=>n+t+r)),i=t.memoizeJ((e,t)=>e.repeat(t)),a=t.memoizeJ((e,t="")=>n=>n.length>e?n.substr(0,e-t.length)+t:n),g=a,l=t.memoizeJ((e,t=" ")=>{const n=i(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)):a(e)(t)}}),c=t.memoizeJ((e,t=" ")=>{const n=i(String(t),e);return t=>null!=t?(t=t.toString()).length<e?n.substr(t.length)+t:t:n}),u=c(2,"0"),m=c(3,"0"),h=c(4,"0"),p=t.memoizeJ(e=>t=>d(t)||t.toFixed(e)),f=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(d(n)||(0===n?"0":a<s||a>=r?b(n,e):n.toFixed(t-(n<o?1:0))))}}),b=(e,t)=>e.toExponential(Math.max(t-4-(Math.log(Math.abs(e))/Math.LN10>=10?2:1)-(e<0?1:0),0)),d=e=>isNaN(e)?"NaN":e===1/0?"+∞":e===-1/0?"-∞":void 0,z=(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*x(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 j=(...e)=>{const t={};for(let n of e)for(let e of n)t[e]=!0;return Object.freeze(t)},y=Object.freeze({"\t":!0,"\n":!0,"\v":!0,"\f":!0,"\r":!0," ":!0}),M=j(x("0","9")),w=j(x("0","9"),x("A","F"),x("a","f")),O=j(x("a","z")),S=j(x("A","Z")),C=Object.freeze(Object.assign(Object.assign({},S),O)),N=Object.freeze(Object.assign(Object.assign(Object.assign({},C),M),{_:!0})),A=j(x("!","/"),x(":","@"),x("[","`"),x("{","~")),J=/\{(\d+)\}/g,U=t.memoize1(e=>t=>t.join(e)),P=t.memoizeJ((e,t=" ")=>{const n=i(String(t),e);return t=>null!=t?(t=t.toString()).length<e?t+n.substr(t.length):t:n}),F=t.memoizeJ((e,t,n="")=>{const r=i("0",t);return o=>(o=(o>>>0).toString(e),n+(o.length<t?r.substr(o.length)+o:o))}),L=F(2,8),$=F(2,16),B=F(2,32),R=F(16,2),E=F(16,4),I=F(16,6),_=F(16,8),k="àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;",H="aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------",Z=new RegExp(k.split("").join("|"),"g"),G=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,"")}),T=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""}}),W=1024,q=v([[1," bits",0],[W," Kb"],[W**2," Mb"],[W**3," Gb"]]," bits",2),K=v([[1," bytes",0],[W," KB"],[W**2," MB"],[W**3," GB"],[W**4," TB"],[W**5," PB"]]," bytes",2),D=v([[1e-12," ps"],[1e-9," ns"],[1e-6," µs"],[.001," ms"],[1," secs"],[60," mins"],[3600," hours"],[86400," days"]]," secs",3),X=v([[1e-12," pm"],[1e-9," nm"],[1e-6," µm"],[.001," mm"],[.01," cm"],[1," m"],[1e3," km"]]," m",2),Q=v([[1e-12," pg"],[1e-9," ng"],[1e-6," µg"],[.001," mg"],[1," g"],[1e3," kg"],[1e6," t"],[1e9," kt"],[1e12," Mt"]]," g",2),V=t.memoizeJ(e=>t=>e+t+e);e.ALPHA=C,e.ALPHA_NUM=N,e.B16=$,e.B32=B,e.B8=L,e.DIGITS=M,e.HEX=w,e.LOWER=O,e.PUNCTUATION=A,e.U16=E,e.U24=I,e.U32=_,e.U64=(e,t)=>_(e)+_(t),e.U8=R,e.UPPER=S,e.WS=y,e.Z2=u,e.Z3=m,e.Z4=h,e.bits=q,e.bytes=K,e.camel=(e,t="-")=>o(e).replace(new RegExp(`\\${t}+(\\w)`,"g"),(e,t)=>r(t)),e.capitalize=e=>e[0].toUpperCase()+e.substr(1),e.center=l,e.charRange=x,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)=>z(e,...t),e.float=p,e.floatFixedWidth=f,e.format=z,e.grams=Q,e.hstr=e=>null!=e?`${(e=e.toString()).length}H${e}`:"",e.ignore=e=>"",e.interpolate=(e,...t)=>t.length>0?e.replace(J,e=>String(t[parseInt(e[1],10)])):e,e.join=U,e.kebab=s,e.lower=o,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=X,e.padLeft=c,e.padRight=P,e.percent=(e=0)=>t=>(100*t).toFixed(e)+"%",e.radix=F,e.repeat=i,e.seconds=D,e.slugify=e=>e.toLowerCase().replace(/\s+/g,"-").replace(Z,e=>H[k.indexOf(e)]).replace(/&+/g,"-and-").replace(/[^\w\-]+/g,"").replace(/\-{2,}/g,"-").replace(/(^-+)|(-+$)/g,""),e.slugifyGH=e=>e.toLowerCase().replace(/\s/g,"-").replace(Z,e=>H[k.indexOf(e)]).replace(/[^\w\-]+/g,"").replace(/(^-+)|(-+$)/g,""),e.snake=e=>s(e,"_"),e.splice=(e,t,r,o=r)=>(r<0&&(r+=e.length),o<0&&(o+=e.length),r>o&&n.illegalArgs("'from' index must be <= 'to'"),o=Math.max(o,0),r<=0?t+e.substr(o):r>=e.length?e+t:e.substr(0,r)+t+e.substr(o)),e.str=e=>String(e),e.trim=G,e.truncate=a,e.truncateLeft=T,e.truncateRight=g,e.units=v,e.upper=r,e.uuid=(e,t=0)=>[R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),"-",R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++]),R(e[t++])].join(""),e.wrap=V,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/strings", | ||
"version": "1.8.13", | ||
"version": "1.9.0", | ||
"description": "Various string formatting & utility functions", | ||
@@ -89,3 +89,3 @@ "module": "./index.js", | ||
}, | ||
"gitHead": "415fcbd5b35c1041044d9f17d89718faa9999906" | ||
"gitHead": "9966e103d7dc816ac8e6989d250bfb1a57da51a4" | ||
} |
@@ -53,3 +53,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (gzipped, pre-treeshake): ESM: 2.34 KB / CJS: 2.57 KB / UMD: 2.45 KB | ||
Package sizes (gzipped, pre-treeshake): ESM: 2.44 KB / CJS: 2.67 KB / UMD: 2.54 KB | ||
@@ -56,0 +56,0 @@ ## Dependencies |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
105443
60
1467