-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
4 lines (4 loc) · 4.63 KB
/
mod.js
1
2
3
4
// Copyright (c) 2025 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
/// <reference types="./mod.d.ts" />
function e(e){return"number"==typeof e}function r(e){var r,i="";for(r=0;r<e;r++)i+="0";return i}function i(e,i,a){var t=!1,n=i-e.length;return n<0||(function(e){return"-"===e[0]}(e)&&(t=!0,e=e.substr(1)),e=a?e+r(n):r(n)+e,t&&(e="-"+e)),e}var a=String.prototype.toLowerCase,t=String.prototype.toUpperCase;function n(r){var n,s,o;switch(r.specifier){case"b":n=2;break;case"o":n=8;break;case"x":case"X":n=16;break;default:n=10}if(s=r.arg,o=parseInt(s,10),!isFinite(o)){if(!e(s))throw new Error("invalid integer. Value: "+s);o=0}return o<0&&("u"===r.specifier||10!==n)&&(o=4294967295+o+1),o<0?(s=(-o).toString(n),r.precision&&(s=i(s,r.precision,r.padRight)),s="-"+s):(s=o.toString(n),o||r.precision?r.precision&&(s=i(s,r.precision,r.padRight)):s="",r.sign&&(s=r.sign+s)),16===n&&(r.alternate&&(s="0x"+s),s=r.specifier===t.call(r.specifier)?t.call(s):a.call(s)),8===n&&r.alternate&&"0"!==s.charAt(0)&&(s="0"+s),s}var s=Math.abs,o=String.prototype.toLowerCase,c=String.prototype.toUpperCase,p=String.prototype.replace,l=/e\+(\d)$/,g=/e-(\d)$/,f=/^(\d+)$/,d=/^(\d+)e/,h=/\.0$/,u=/\.0*e/,w=/(\..*[^0])0*e/;function v(r){var i,a,t=parseFloat(r.arg);if(!isFinite(t)){if(!e(r.arg))throw new Error("invalid floating-point number. Value: "+a);t=r.arg}switch(r.specifier){case"e":case"E":a=t.toExponential(r.precision);break;case"f":case"F":a=t.toFixed(r.precision);break;case"g":case"G":s(t)<1e-4?((i=r.precision)>0&&(i-=1),a=t.toExponential(i)):a=t.toPrecision(r.precision),r.alternate||(a=p.call(a,w,"$1e"),a=p.call(a,u,"e"),a=p.call(a,h,""));break;default:throw new Error("invalid double notation. Value: "+r.specifier)}return a=p.call(a,l,"e+0$1"),a=p.call(a,g,"e-0$1"),r.alternate&&(a=p.call(a,f,"$1."),a=p.call(a,d,"$1.e")),t>=0&&r.sign&&(a=r.sign+a),a=r.specifier===c.call(r.specifier)?c.call(a):o.call(a)}function b(e){var r,i="";for(r=0;r<e;r++)i+=" ";return i}var m=String.fromCharCode,y=Array.isArray;function x(e){return e!=e}function k(e){var r={};return r.specifier=e.specifier,r.precision=void 0===e.precision?1:e.precision,r.width=e.width,r.flags=e.flags||"",r.mapping=e.mapping,r}function E(e){var r,a,t,s,o,c,p,l,g,f,d,h,u;if(!y(e))throw new TypeError("invalid argument. First argument must be an array. Value: `"+e+"`.");for(c="",p=1,l=0;l<e.length;l++)if(t=e[l],"string"==typeof t)c+=t;else{if(r=void 0!==t.precision,!(t=k(t)).specifier)throw new TypeError("invalid argument. Token is missing `specifier` property. Index: `"+l+"`. Value: `"+t+"`.");for(t.mapping&&(p=t.mapping),a=t.flags,g=0;g<a.length;g++)switch(s=a.charAt(g)){case" ":t.sign=" ";break;case"+":t.sign="+";break;case"-":t.padRight=!0,t.padZeros=!1;break;case"0":t.padZeros=a.indexOf("-")<0;break;case"#":t.alternate=!0;break;default:throw new Error("invalid flag: "+s)}if("*"===t.width){if(t.width=parseInt(arguments[p],10),p+=1,x(t.width))throw new TypeError("the argument for * width at position "+p+" is not a number. Value: `"+t.width+"`.");t.width<0&&(t.padRight=!0,t.width=-t.width)}if(r&&"*"===t.precision){if(t.precision=parseInt(arguments[p],10),p+=1,x(t.precision))throw new TypeError("the argument for * precision at position "+p+" is not a number. Value: `"+t.precision+"`.");t.precision<0&&(t.precision=1,r=!1)}switch(t.arg=arguments[p],t.specifier){case"b":case"o":case"x":case"X":case"d":case"i":case"u":r&&(t.padZeros=!1),t.arg=n(t);break;case"s":t.maxWidth=r?t.precision:-1,t.arg=String(t.arg);break;case"c":if(!x(t.arg)){if((o=parseInt(t.arg,10))<0||o>127)throw new Error("invalid character code. Value: "+t.arg);t.arg=x(o)?String(t.arg):m(o)}break;case"e":case"E":case"f":case"F":case"g":case"G":r||(t.precision=6),t.arg=v(t);break;default:throw new Error("invalid specifier: "+t.specifier)}t.maxWidth>=0&&t.arg.length>t.maxWidth&&(t.arg=t.arg.substring(0,t.maxWidth)),t.padZeros?t.arg=i(t.arg,t.width||t.precision,t.padRight):t.width&&(t.arg=(f=t.arg,d=t.width,h=t.padRight,u=void 0,(u=d-f.length)<0?f:f=h?f+b(u):b(u)+f)),c+=t.arg||"",p+=1}return c}var S=/%(?:([1-9]\d*)\$)?([0 +\-#]*)(\*|\d+)?(?:(\.)(\*|\d+)?)?[hlL]?([%A-Za-z])/g;function $(e){var r={mapping:e[1]?parseInt(e[1],10):void 0,flags:e[2],width:e[3],precision:e[5],specifier:e[6]};return"."===e[4]&&void 0===e[5]&&(r.precision="1"),r}function F(e){var r,i,a,t;for(i=[],t=0,a=S.exec(e);a;)(r=e.slice(t,S.lastIndex-a[0].length)).length&&i.push(r),i.push($(a)),t=S.lastIndex,a=S.exec(e);return(r=e.slice(t)).length&&i.push(r),i}function I(e){var r,i;if("string"!=typeof e)throw new TypeError(I("1Of3F",e));for(r=[F(e)],i=1;i<arguments.length;i++)r.push(arguments[i]);return E.apply(null,r)}export{I as default};
//# sourceMappingURL=mod.js.map