format-string-by-pattern
Version:
A module that formats a string based on a pattern
3 lines (2 loc) • 1.2 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self)["format-string-by-pattern"]=t()}(this,(function(){"use strict";function e(e,t,n){return e.length+1===t.length&&t.endsWith(n)}function t(t,n){if(!n)return n;var r=String(n).replace(/[^\da-zA-Z\n|]/g,""),l=t.split(/[^\da-zA-Z\n|]/g).filter(Boolean).map((function(e){return e.length})),u=t.split(/[\da-zA-Z\n|]/g).filter(Boolean),i=u[0],a=u[u.length-1];if(t.startsWith(i)){var o=u.reduce((function(e,t,n){var r=l[n],u=e.value.slice(0,r),i=e.value.slice(r);return{result:u?e.result.concat(t,u):e.result,value:i}}),{result:"",value:r}).result.slice(0,t.length);return e(o,t,a)?o.concat(a):o}var c=l.reduce((function(e,t,n){var r=u[n]||"",l="$1"+r+"$2",i=t+e.prevSlice+e.prevSeparator.length,a=new RegExp("(.{"+i+"})(.)");return{prevSeparator:r,prevSlice:i,value:e.value.replace(a,l)}}),{prevSeparator:"",prevSlice:0,value:r}).value.slice(0,t.length);return e(c,t,a)?c.concat(a):c}return function(e,n){return void 0===n?function(n){return t(e,n)}:t(e,n)}}));
//# sourceMappingURL=format-string-by-pattern.umd.js.map