UNPKG

@stdlib/esm

Version:

ES module distribution for Stdlib, a standard library for JavaScript and Node.js.

29 lines 7.25 kB
// Copyright 2010-2015 Mike Bostock // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * Neither the name of the author nor the names of contributors may be used to // endorse or promote products derived from this software without specific prior // written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // function formatDecimal(t){return Math.abs(t=Math.round(t))>=1e21?t.toLocaleString("en").replace(/,/g,""):t.toString(10)}function formatDecimalParts(t,r){if((e=(t=r?t.toExponential(r-1):t.toExponential()).indexOf("e"))<0)return null;var e,i=t.slice(0,e);return[i.length>1?i[0]+i.slice(2):i,+t.slice(e+1)]}function exponent(t){return(t=formatDecimalParts(Math.abs(t)))?t[1]:NaN}function formatGroup(t,r){return function(e,i){for(var n=e.length,o=[],a=0,f=t[0],c=0;n>0&&f>0&&(c+f+1>i&&(f=Math.max(1,i-c)),o.push(e.substring(n-=f,n+f)),!((c+=f+1)>i));)f=t[a=(a+1)%t.length];return o.reverse().join(r)}}function formatNumerals(t){return function(r){return r.replace(/[0-9]/g,(function(r){return t[+r]}))}}var prefixExponent,re=/^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;function formatSpecifier(t){if(!(r=re.exec(t)))throw new Error("invalid format: "+t);var r;return new FormatSpecifier({fill:r[1],align:r[2],sign:r[3],symbol:r[4],zero:r[5],width:r[6],comma:r[7],precision:r[8]&&r[8].slice(1),trim:r[9],type:r[10]})}function FormatSpecifier(t){this.fill=void 0===t.fill?" ":t.fill+"",this.align=void 0===t.align?">":t.align+"",this.sign=void 0===t.sign?"-":t.sign+"",this.symbol=void 0===t.symbol?"":t.symbol+"",this.zero=!!t.zero,this.width=void 0===t.width?void 0:+t.width,this.comma=!!t.comma,this.precision=void 0===t.precision?void 0:+t.precision,this.trim=!!t.trim,this.type=void 0===t.type?"":t.type+""}function formatTrim(t){t:for(var r,e=t.length,i=1,n=-1;i<e;++i)switch(t[i]){case".":n=r=i;break;case"0":0===n&&(n=i),r=i;break;default:if(!+t[i])break t;n>0&&(n=0)}return n>0?t.slice(0,n)+t.slice(r+1):t}function formatPrefixAuto(t,r){var e=formatDecimalParts(t,r);if(!e)return t+"";var i=e[0],n=e[1],o=n-(prefixExponent=3*Math.max(-8,Math.min(8,Math.floor(n/3))))+1,a=i.length;return o===a?i:o>a?i+new Array(o-a+1).join("0"):o>0?i.slice(0,o)+"."+i.slice(o):"0."+new Array(1-o).join("0")+formatDecimalParts(t,Math.max(0,r+o-1))[0]}function formatRounded(t,r){var e=formatDecimalParts(t,r);if(!e)return t+"";var i=e[0],n=e[1];return n<0?"0."+new Array(-n).join("0")+i:i.length>n+1?i.slice(0,n+1)+"."+i.slice(n+1):i+new Array(n-i.length+2).join("0")}formatSpecifier.prototype=FormatSpecifier.prototype,FormatSpecifier.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(void 0===this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(void 0===this.precision?"":"."+Math.max(0,0|this.precision))+(this.trim?"~":"")+this.type};var formatTypes={"%":function(t,r){return(100*t).toFixed(r)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:formatDecimal,e:function(t,r){return t.toExponential(r)},f:function(t,r){return t.toFixed(r)},g:function(t,r){return t.toPrecision(r)},o:function(t){return Math.round(t).toString(8)},p:function(t,r){return formatRounded(100*t,r)},r:formatRounded,s:formatPrefixAuto,X:function(t){return Math.round(t).toString(16).toUpperCase()},x:function(t){return Math.round(t).toString(16)}};function identity(t){return t}var locale,format,formatPrefix,map=Array.prototype.map,prefixes=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];function formatLocale(t){var r=void 0===t.grouping||void 0===t.thousands?identity:formatGroup(map.call(t.grouping,Number),t.thousands+""),e=void 0===t.currency?"":t.currency[0]+"",i=void 0===t.currency?"":t.currency[1]+"",n=void 0===t.decimal?".":t.decimal+"",o=void 0===t.numerals?identity:formatNumerals(map.call(t.numerals,String)),a=void 0===t.percent?"%":t.percent+"",f=void 0===t.minus?"-":t.minus+"",c=void 0===t.nan?"NaN":t.nan+"";function s(t){var s=(t=formatSpecifier(t)).fill,u=t.align,m=t.sign,l=t.symbol,h=t.zero,p=t.width,d=t.comma,g=t.precision,x=t.trim,v=t.type;"n"===v?(d=!0,v="g"):formatTypes[v]||(void 0===g&&(g=12),x=!0,v="g"),(h||"0"===s&&"="===u)&&(h=!0,s="0",u="=");var y="$"===l?e:"#"===l&&/[boxX]/.test(v)?"0"+v.toLowerCase():"",M="$"===l?i:/[%p]/.test(v)?a:"",b=formatTypes[v],w=/[defgprs%]/.test(v);function S(t){var e,i,a,l=y,S=M;if("c"===v)S=b(t)+S,t="";else{var P=(t=+t)<0||1/t<0;if(t=isNaN(t)?c:b(Math.abs(t),g),x&&(t=formatTrim(t)),P&&0==+t&&"+"!==m&&(P=!1),l=(P?"("===m?m:f:"-"===m||"("===m?"":m)+l,S=("s"===v?prefixes[8+prefixExponent/3]:"")+S+(P&&"("===m?")":""),w)for(e=-1,i=t.length;++e<i;)if(48>(a=t.charCodeAt(e))||a>57){S=(46===a?n+t.slice(e+1):t.slice(e))+S,t=t.slice(0,e);break}}d&&!h&&(t=r(t,1/0));var A=l.length+t.length+S.length,F=A<p?new Array(p-A+1).join(s):"";switch(d&&h&&(t=r(F+t,F.length?p-S.length:1/0),F=""),u){case"<":t=l+t+S+F;break;case"=":t=l+F+t+S;break;case"^":t=F.slice(0,A=F.length>>1)+l+t+S+F.slice(A);break;default:t=F+l+t+S}return o(t)}return g=void 0===g?6:/[gprs]/.test(v)?Math.max(1,Math.min(21,g)):Math.max(0,Math.min(20,g)),S.toString=function(){return t+""},S}return{format:s,formatPrefix:function(t,r){var e=s(((t=formatSpecifier(t)).type="f",t)),i=3*Math.max(-8,Math.min(8,Math.floor(exponent(r)/3))),n=Math.pow(10,-i),o=prefixes[8+i/3];return function(t){return e(n*t)+o}}}}function defaultLocale(t){return locale=formatLocale(t),format=locale.format,formatPrefix=locale.formatPrefix,locale}function precisionFixed(t){return Math.max(0,-exponent(Math.abs(t)))}function precisionPrefix(t,r){return Math.max(0,3*Math.max(-8,Math.min(8,Math.floor(exponent(r)/3)))-exponent(Math.abs(t)))}function precisionRound(t,r){return t=Math.abs(t),r=Math.abs(r)-t,Math.max(0,exponent(r)-exponent(t))+1}defaultLocale({decimal:".",thousands:",",grouping:[3],currency:["$",""],minus:"-"});export{FormatSpecifier,format,defaultLocale as formatDefaultLocale,formatLocale,formatPrefix,formatSpecifier,precisionFixed,precisionPrefix,precisionRound};