UNPKG

typed-string-interpolation

Version:

String interpolation with correct return type based on passed variable substitutions

2 lines (1 loc) 1.05 kB
"use strict";var d=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var x=(e,n)=>{for(var r in n)d(e,r,{get:n[r],enumerable:!0})},y=(e,n,r,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of R(n))!f.call(e,a)&&a!==r&&d(e,a,{get:()=>n[a],enumerable:!(t=g(n,a))||t.enumerable});return e};var h=e=>y(d({},"__esModule",{value:!0}),e);var E={};x(E,{stringInterpolation:()=>O});module.exports=h(E);function O(e,n,{pattern:r=/\{{([^{]+)}}/g,sanity:t=!0,raw:a=!1}={}){if(!e&&t)throw new Error("Empty string");let w=t?Object.keys(n):void 0,o=[],i=0,s=0,p=!0,l;for(;l=r.exec(e);){let V=l.index,c=l[0],u=l[1];if(V>i&&o.push(e.slice(i,V)),t&&u&&!w.includes(u))throw new Error(`Variable '${u}' not found`);let b=n[u];p=p&&(typeof b=="string"||typeof b=="number"),o.push(b),i=V+c.length,s++}if(!s)return e;if(i<e.length&&o.push(e.slice(i)),t&&s!==w.length)throw new Error("Variable count mismatch");return p&&!a?o.join(""):o}0&&(module.exports={stringInterpolation});