UNPKG

dotnet-format

Version:

📝 Pretty accurate port of awesome .NET formatting operations for JS environment

2 lines 6.81 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("DotnetFormat",[],e):"object"==typeof exports?exports.DotnetFormat=e():t.DotnetFormat=e()}(this,(function(){return(()=>{"use strict";var t=[(t,e,r)=>{r.r(e),r.d(e,{formatDate:()=>h});var o,n=(o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(t,e)},function(t,e){function r(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)});const a=function(t){function e(r){return t.call(this,r||e.defaultMessage)||this}return n(e,t),e.defaultMessage="Input string was not in a correct format.",e}(Error);const i=function(){function t(){}return t.prototype.format=function(t,e,r){var o=this._cast(t);if(!e)return o.toString();if("string"!=typeof e)throw new a;return this._format(o,e,r||"default")},t}();function c(t,e){for(var r=t[e],o=e+1;o<t.length&&t[o]===r;)++o;return o-e}var u=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,a=r.call(t),i=[];try{for(;(void 0===e||e-- >0)&&!(o=a.next()).done;)i.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return i},f=function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(u(arguments[e]));return t},s=new Map([["d",{day:"2-digit",month:"2-digit",year:"numeric"}],["D",{day:"2-digit",month:"long",year:"numeric",weekday:"long"}],["f",{day:"2-digit",month:"long",year:"numeric",weekday:"long",hour:"2-digit",minute:"2-digit"}],["F",{day:"2-digit",month:"long",year:"numeric",weekday:"long",hour:"2-digit",minute:"2-digit",second:"2-digit"}],["g",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit"}],["G",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit",second:"2-digit"}],["m",{day:"numeric",month:"long"}],["M",{day:"numeric",month:"long"}],["t",{hour:"2-digit",minute:"2-digit"}],["T",{hour:"2-digit",minute:"2-digit",second:"2-digit"}],["U",{day:"2-digit",month:"long",year:"numeric",weekday:"long",hour:"2-digit",minute:"2-digit",second:"2-digit",timeZone:"UTC"}],["y",{year:"numeric",month:"long"}],["Y",{year:"numeric",month:"long"}]]),d=new Map([["o","yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz"],["O","yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz"],["r","ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"],["R","ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"],["s","yyyy'-'MM'-'dd'T'HH':'mm':'ss"],["u","yyyy'-'MM'-'dd HH':'mm':'ss'Z'"]]),y=(f(s.keys(),d.keys()).sort((function(t,e){return t.toLowerCase()===e.toLowerCase()?e.charCodeAt(0)-t.charCodeAt(0):t.toLowerCase().charCodeAt(0)-e.toLowerCase().charCodeAt(0)})),[":","/","."]),l=function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(e,r)};return function(e,r){function o(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}}(),g=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,a=r.call(t),i=[];try{for(;(void 0===e||e-- >0)&&!(o=a.next()).done;)i.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return i};var m=new(function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return l(e,t),e.prototype._cast=function(t){if(t instanceof Date||"bigint"==typeof t||"symbol"==typeof t||(t=new Date(t)),!(t instanceof Date)||isNaN(t.getTime()))throw new Error("Invalid date.");return t},e.prototype._format=function(t,e,r){return(null!=e?e:"").length<=1?this._formatStandard(t,e,r):this._formatCustom(t,e,r)},e.prototype._formatStandard=function(t,e,r){if(s.has(e))return t.toLocaleString(r,s.get(e));if(d.has(e))return this._formatCustom(t,d.get(e),r);throw new a},e.prototype._formatCustom=function(t,e,r){for(var o,n="",i=function(i,f){var s,d=e[i];switch(f=1,d){case"d":switch(f=c(e,i)){case 1:case 2:n+=t.getDate().toString().padStart(f,"0");break;case 3:n+=t.toLocaleString(r,{weekday:"short"});break;default:n+=t.toLocaleString(r,{weekday:"long"})}break;case"f":case"F":f=c(e,i);var l=Math.floor(t.getMilliseconds()*Math.pow(10,f-3));(l||"F"!==d)&&(n+=l.toString().padEnd(f,"0"));break;case"g":f=c(e,i),n+=t.toLocaleString(r,{era:"short",weekday:"short"}).split(" ").slice(0,-1).join(" ");break;case"h":f=c(e,i),n+=(t.getHours()%12||12).toString().padStart(Math.min(f,2),"0");break;case"H":f=c(e,i),n+=t.getHours().toString().padStart(Math.min(f,2),"0");break;case"K":case"z":f=c(e,i);var m=-1*t.getTimezoneOffset(),h=Math.abs(Math.floor(m/60)),p=Math.abs(m%60);n+="K"===d||f>=3?(m<0?"-":"+")+h.toString().padStart(2,"0")+":"+p.toString().padStart(2,"0"):(m<0?"-":"+")+h.toString().padStart(f,"0");break;case"m":f=c(e,i),n+=t.getMinutes().toString().padStart(Math.min(f,2),"0");break;case"M":switch(f=c(e,i)){case 1:case 2:n+=(t.getMonth()+1).toString().padStart(f,"0");break;case 3:n+=t.toLocaleString(r,{month:"short"});break;default:n+=t.toLocaleString(r,{month:"long"})}break;case"s":f=c(e,i),n+=t.getSeconds().toString().padStart(Math.min(f,2),"0");break;case"t":f=c(e,i);var b=t.getHours()<=12?"AM":"PM";n+=1===f?b[0]:b;break;case"y":f=c(e,i);var S=t.getFullYear();f<3&&(S%=100),n+=S.toString().padStart(f,"0");break;case":":f=1;var w=t.toLocaleString(r,{hour:"numeric",minute:"numeric"});n+=null!==(s=y.find((function(t){return w.includes(t)})))&&void 0!==s?s:":";break;case"/":f=1;var M=t.toLocaleString(r,{day:"numeric",month:"numeric",year:"numeric"});n+=y.find((function(t){return M.includes(t)}))||"/";break;case"%":if(i===e.length-1||"%"===e[i+1])throw new a;f=2,n+=u._formatCustom(t,e[i+1],r);break;case"\\":if(i===e.length-1)throw new a;f=2,n+=e[i+1];break;case"'":case'"':var v;f=(s=g(function(t,e){for(var r=e,o=t[r++],n="";r<t.length;){var i=t[r++];if(i===o)break;if("\\"===i){if(r===t.length-1)throw new a;n+=t[r++]}else n+=i}if(t[r-1]!==o)throw new a;return[r-e,n]}(e,i),2))[0],v=s[1],n+=v;break;default:f=1,n+=d}o=f},u=this,f=0,s=1;f<e.length;f+=s)i(f,s),s=o;return n},e}(i));function h(t,e,r){return m.format(t,e,r)}}],e={};function r(o){if(e[o])return e[o].exports;var n=e[o]={exports:{}};return t[o](n,n.exports,r),n.exports}return r.d=(t,e)=>{for(var o in e)r.o(e,o)&&!r.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r(0)})()})); //# sourceMappingURL=dotnet-format.js.map