d3-format
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -1,7 +0,7 @@ | ||
// https://d3js.org/d3-format/ v1.3.2 Copyright 2018 Mike Bostock | ||
// https://d3js.org/d3-format/ v1.4.0 Copyright 2019 Mike Bostock | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.d3 = global.d3 || {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
(global = global || self, factory(global.d3 = global.d3 || {})); | ||
}(this, function (exports) { 'use strict'; | ||
@@ -58,3 +58,16 @@ // Computes the decimal coefficient and exponent of the specified number x with | ||
function formatSpecifier(specifier) { | ||
return new FormatSpecifier(specifier); | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
var match; | ||
return new FormatSpecifier({ | ||
fill: match[1], | ||
align: match[2], | ||
sign: match[3], | ||
symbol: match[4], | ||
zero: match[5], | ||
width: match[6], | ||
comma: match[7], | ||
precision: match[8] && match[8].slice(1), | ||
trim: match[9], | ||
type: match[10] | ||
}); | ||
} | ||
@@ -65,14 +78,12 @@ | ||
function FormatSpecifier(specifier) { | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
var match; | ||
this.fill = match[1] || " "; | ||
this.align = match[2] || ">"; | ||
this.sign = match[3] || "-"; | ||
this.symbol = match[4] || ""; | ||
this.zero = !!match[5]; | ||
this.width = match[6] && +match[6]; | ||
this.comma = !!match[7]; | ||
this.precision = match[8] && +match[8].slice(1); | ||
this.trim = !!match[9]; | ||
this.type = match[10] || ""; | ||
this.fill = specifier.fill === undefined ? " " : specifier.fill + ""; | ||
this.align = specifier.align === undefined ? ">" : specifier.align + ""; | ||
this.sign = specifier.sign === undefined ? "-" : specifier.sign + ""; | ||
this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + ""; | ||
this.zero = !!specifier.zero; | ||
this.width = specifier.width === undefined ? undefined : +specifier.width; | ||
this.comma = !!specifier.comma; | ||
this.precision = specifier.precision === undefined ? undefined : +specifier.precision; | ||
this.trim = !!specifier.trim; | ||
this.type = specifier.type === undefined ? "" : specifier.type + ""; | ||
} | ||
@@ -86,5 +97,5 @@ | ||
+ (this.zero ? "0" : "") | ||
+ (this.width == null ? "" : Math.max(1, this.width | 0)) | ||
+ (this.width === undefined ? "" : Math.max(1, this.width | 0)) | ||
+ (this.comma ? "," : "") | ||
+ (this.precision == null ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ (this.trim ? "~" : "") | ||
@@ -151,10 +162,14 @@ + this.type; | ||
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
var map = Array.prototype.map, | ||
prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
function formatLocale(locale) { | ||
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity, | ||
currency = locale.currency, | ||
decimal = locale.decimal, | ||
numerals = locale.numerals ? formatNumerals(locale.numerals) : identity, | ||
percent = locale.percent || "%"; | ||
var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""), | ||
currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "", | ||
currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "", | ||
decimal = locale.decimal === undefined ? "." : locale.decimal + "", | ||
numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)), | ||
percent = locale.percent === undefined ? "%" : locale.percent + "", | ||
minus = locale.minus === undefined ? "-" : locale.minus + "", | ||
nan = locale.nan === undefined ? "NaN" : locale.nan + ""; | ||
@@ -179,3 +194,3 @@ function newFormat(specifier) { | ||
// The "" type, and any invalid type, is an alias for ".12~g". | ||
else if (!formatTypes[type]) precision == null && (precision = 12), trim = true, type = "g"; | ||
else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g"; | ||
@@ -187,4 +202,4 @@ // If zero fill is specified, padding goes after sign and before digits. | ||
// For SI-prefix, the suffix is lazily computed. | ||
var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? percent : ""; | ||
var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : ""; | ||
@@ -201,3 +216,3 @@ // What format function should we use? | ||
// For fixed precision, it must be in [0, 20]. | ||
precision = precision == null ? 6 | ||
precision = precision === undefined ? 6 | ||
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision)) | ||
@@ -219,3 +234,3 @@ : Math.max(0, Math.min(20, precision)); | ||
var valueNegative = value < 0; | ||
value = formatType(Math.abs(value), precision); | ||
value = isNaN(value) ? nan : formatType(Math.abs(value), precision); | ||
@@ -229,3 +244,4 @@ // Trim insignificant zeros. | ||
// Compute the prefix and suffix. | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : ""); | ||
@@ -297,3 +313,4 @@ | ||
grouping: [3], | ||
currency: ["$", ""] | ||
currency: ["$", ""], | ||
minus: "-" | ||
}); | ||
@@ -321,2 +338,3 @@ | ||
exports.FormatSpecifier = FormatSpecifier; | ||
exports.formatDefaultLocale = defaultLocale; | ||
@@ -331,2 +349,2 @@ exports.formatLocale = formatLocale; | ||
}))); | ||
})); |
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-format/ v1.3.2 Copyright 2018 Mike Bostock | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.d3=t.d3||{})}(this,function(t){"use strict";function n(t,n){if((r=(t=n?t.toExponential(n-1):t.toExponential()).indexOf("e"))<0)return null;var r,e=t.slice(0,r);return[e.length>1?e[0]+e.slice(2):e,+t.slice(r+1)]}function r(t){return(t=n(Math.abs(t)))?t[1]:NaN}var e,i=/^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;function o(t){return new a(t)}function a(t){if(!(n=i.exec(t)))throw new Error("invalid format: "+t);var n;this.fill=n[1]||" ",this.align=n[2]||">",this.sign=n[3]||"-",this.symbol=n[4]||"",this.zero=!!n[5],this.width=n[6]&&+n[6],this.comma=!!n[7],this.precision=n[8]&&+n[8].slice(1),this.trim=!!n[9],this.type=n[10]||""}function u(t,r){var e=n(t,r);if(!e)return t+"";var i=e[0],o=e[1];return o<0?"0."+new Array(-o).join("0")+i:i.length>o+1?i.slice(0,o+1)+"."+i.slice(o+1):i+new Array(o-i.length+2).join("0")}o.prototype=a.prototype,a.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(null==this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(null==this.precision?"":"."+Math.max(0,0|this.precision))+(this.trim?"~":"")+this.type};var s={"%":function(t,n){return(100*t).toFixed(n)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,n){return t.toExponential(n)},f:function(t,n){return t.toFixed(n)},g:function(t,n){return t.toPrecision(n)},o:function(t){return Math.round(t).toString(8)},p:function(t,n){return u(100*t,n)},r:u,s:function(t,r){var i=n(t,r);if(!i)return t+"";var o=i[0],a=i[1],u=a-(e=3*Math.max(-8,Math.min(8,Math.floor(a/3))))+1,s=o.length;return u===s?o:u>s?o+new Array(u-s+1).join("0"):u>0?o.slice(0,u)+"."+o.slice(u):"0."+new Array(1-u).join("0")+n(t,Math.max(0,r+u-1))[0]},X:function(t){return Math.round(t).toString(16).toUpperCase()},x:function(t){return Math.round(t).toString(16)}};function c(t){return t}var f,h=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];function l(t){var n,i,a=t.grouping&&t.thousands?(n=t.grouping,i=t.thousands,function(t,r){for(var e=t.length,o=[],a=0,u=n[0],s=0;e>0&&u>0&&(s+u+1>r&&(u=Math.max(1,r-s)),o.push(t.substring(e-=u,e+u)),!((s+=u+1)>r));)u=n[a=(a+1)%n.length];return o.reverse().join(i)}):c,u=t.currency,f=t.decimal,l=t.numerals?function(t){return function(n){return n.replace(/[0-9]/g,function(n){return t[+n]})}}(t.numerals):c,m=t.percent||"%";function p(t){var n=(t=o(t)).fill,r=t.align,i=t.sign,c=t.symbol,p=t.zero,g=t.width,d=t.comma,M=t.precision,x=t.trim,y=t.type;"n"===y?(d=!0,y="g"):s[y]||(null==M&&(M=12),x=!0,y="g"),(p||"0"===n&&"="===r)&&(p=!0,n="0",r="=");var b="$"===c?u[0]:"#"===c&&/[boxX]/.test(y)?"0"+y.toLowerCase():"",v="$"===c?u[1]:/[%p]/.test(y)?m:"",w=s[y],j=/[defgprs%]/.test(y);function k(t){var o,u,s,c=b,m=v;if("c"===y)m=w(t)+m,t="";else{var k=(t=+t)<0;if(t=w(Math.abs(t),M),x&&(t=function(t){t:for(var n,r=t.length,e=1,i=-1;e<r;++e)switch(t[e]){case".":i=n=e;break;case"0":0===i&&(i=e),n=e;break;default:if(i>0){if(!+t[e])break t;i=0}}return i>0?t.slice(0,i)+t.slice(n+1):t}(t)),k&&0==+t&&(k=!1),c=(k?"("===i?i:"-":"-"===i||"("===i?"":i)+c,m=("s"===y?h[8+e/3]:"")+m+(k&&"("===i?")":""),j)for(o=-1,u=t.length;++o<u;)if(48>(s=t.charCodeAt(o))||s>57){m=(46===s?f+t.slice(o+1):t.slice(o))+m,t=t.slice(0,o);break}}d&&!p&&(t=a(t,1/0));var S=c.length+t.length+m.length,P=S<g?new Array(g-S+1).join(n):"";switch(d&&p&&(t=a(P+t,P.length?g-m.length:1/0),P=""),r){case"<":t=c+t+m+P;break;case"=":t=c+P+t+m;break;case"^":t=P.slice(0,S=P.length>>1)+c+t+m+P.slice(S);break;default:t=P+c+t+m}return l(t)}return M=null==M?6:/[gprs]/.test(y)?Math.max(1,Math.min(21,M)):Math.max(0,Math.min(20,M)),k.toString=function(){return t+""},k}return{format:p,formatPrefix:function(t,n){var e=p(((t=o(t)).type="f",t)),i=3*Math.max(-8,Math.min(8,Math.floor(r(n)/3))),a=Math.pow(10,-i),u=h[8+i/3];return function(t){return e(a*t)+u}}}}function m(n){return f=l(n),t.format=f.format,t.formatPrefix=f.formatPrefix,f}m({decimal:".",thousands:",",grouping:[3],currency:["$",""]}),t.formatDefaultLocale=m,t.formatLocale=l,t.formatSpecifier=o,t.precisionFixed=function(t){return Math.max(0,-r(Math.abs(t)))},t.precisionPrefix=function(t,n){return Math.max(0,3*Math.max(-8,Math.min(8,Math.floor(r(n)/3)))-r(Math.abs(t)))},t.precisionRound=function(t,n){return t=Math.abs(t),n=Math.abs(n)-t,Math.max(0,r(n)-r(t))+1},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
// https://d3js.org/d3-format/ v1.4.0 Copyright 2019 Mike Bostock | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t=t||self).d3=t.d3||{})}(this,function(t){"use strict";function n(t,n){if((i=(t=n?t.toExponential(n-1):t.toExponential()).indexOf("e"))<0)return null;var i,r=t.slice(0,i);return[r.length>1?r[0]+r.slice(2):r,+t.slice(i+1)]}function i(t){return(t=n(Math.abs(t)))?t[1]:NaN}var r,e=/^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;function o(t){if(!(n=e.exec(t)))throw new Error("invalid format: "+t);var n;return new a({fill:n[1],align:n[2],sign:n[3],symbol:n[4],zero:n[5],width:n[6],comma:n[7],precision:n[8]&&n[8].slice(1),trim:n[9],type:n[10]})}function a(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 s(t,i){var r=n(t,i);if(!r)return t+"";var e=r[0],o=r[1];return o<0?"0."+new Array(-o).join("0")+e:e.length>o+1?e.slice(0,o+1)+"."+e.slice(o+1):e+new Array(o-e.length+2).join("0")}o.prototype=a.prototype,a.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 u={"%":function(t,n){return(100*t).toFixed(n)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,n){return t.toExponential(n)},f:function(t,n){return t.toFixed(n)},g:function(t,n){return t.toPrecision(n)},o:function(t){return Math.round(t).toString(8)},p:function(t,n){return s(100*t,n)},r:s,s:function(t,i){var e=n(t,i);if(!e)return t+"";var o=e[0],a=e[1],s=a-(r=3*Math.max(-8,Math.min(8,Math.floor(a/3))))+1,u=o.length;return s===u?o:s>u?o+new Array(s-u+1).join("0"):s>0?o.slice(0,s)+"."+o.slice(s):"0."+new Array(1-s).join("0")+n(t,Math.max(0,i+s-1))[0]},X:function(t){return Math.round(t).toString(16).toUpperCase()},x:function(t){return Math.round(t).toString(16)}};function c(t){return t}var f,h=Array.prototype.map,l=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];function d(t){var n,e,a=void 0===t.grouping||void 0===t.thousands?c:(n=h.call(t.grouping,Number),e=t.thousands+"",function(t,i){for(var r=t.length,o=[],a=0,s=n[0],u=0;r>0&&s>0&&(u+s+1>i&&(s=Math.max(1,i-u)),o.push(t.substring(r-=s,r+s)),!((u+=s+1)>i));)s=n[a=(a+1)%n.length];return o.reverse().join(e)}),s=void 0===t.currency?"":t.currency[0]+"",f=void 0===t.currency?"":t.currency[1]+"",d=void 0===t.decimal?".":t.decimal+"",m=void 0===t.numerals?c:function(t){return function(n){return n.replace(/[0-9]/g,function(n){return t[+n]})}}(h.call(t.numerals,String)),p=void 0===t.percent?"%":t.percent+"",g=void 0===t.minus?"-":t.minus+"",v=void 0===t.nan?"NaN":t.nan+"";function M(t){var n=(t=o(t)).fill,i=t.align,e=t.sign,c=t.symbol,h=t.zero,M=t.width,y=t.comma,x=t.precision,b=t.trim,w=t.type;"n"===w?(y=!0,w="g"):u[w]||(void 0===x&&(x=12),b=!0,w="g"),(h||"0"===n&&"="===i)&&(h=!0,n="0",i="=");var S="$"===c?s:"#"===c&&/[boxX]/.test(w)?"0"+w.toLowerCase():"",j="$"===c?f:/[%p]/.test(w)?p:"",k=u[w],z=/[defgprs%]/.test(w);function A(t){var o,s,u,c=S,f=j;if("c"===w)f=k(t)+f,t="";else{var p=(t=+t)<0;if(t=isNaN(t)?v:k(Math.abs(t),x),b&&(t=function(t){t:for(var n,i=t.length,r=1,e=-1;r<i;++r)switch(t[r]){case".":e=n=r;break;case"0":0===e&&(e=r),n=r;break;default:if(e>0){if(!+t[r])break t;e=0}}return e>0?t.slice(0,e)+t.slice(n+1):t}(t)),p&&0==+t&&(p=!1),c=(p?"("===e?e:g:"-"===e||"("===e?"":e)+c,f=("s"===w?l[8+r/3]:"")+f+(p&&"("===e?")":""),z)for(o=-1,s=t.length;++o<s;)if(48>(u=t.charCodeAt(o))||u>57){f=(46===u?d+t.slice(o+1):t.slice(o))+f,t=t.slice(0,o);break}}y&&!h&&(t=a(t,1/0));var A=c.length+t.length+f.length,N=A<M?new Array(M-A+1).join(n):"";switch(y&&h&&(t=a(N+t,N.length?M-f.length:1/0),N=""),i){case"<":t=c+t+f+N;break;case"=":t=c+N+t+f;break;case"^":t=N.slice(0,A=N.length>>1)+c+t+f+N.slice(A);break;default:t=N+c+t+f}return m(t)}return x=void 0===x?6:/[gprs]/.test(w)?Math.max(1,Math.min(21,x)):Math.max(0,Math.min(20,x)),A.toString=function(){return t+""},A}return{format:M,formatPrefix:function(t,n){var r=M(((t=o(t)).type="f",t)),e=3*Math.max(-8,Math.min(8,Math.floor(i(n)/3))),a=Math.pow(10,-e),s=l[8+e/3];return function(t){return r(a*t)+s}}}}function m(n){return f=d(n),t.format=f.format,t.formatPrefix=f.formatPrefix,f}m({decimal:".",thousands:",",grouping:[3],currency:["$",""],minus:"-"}),t.FormatSpecifier=a,t.formatDefaultLocale=m,t.formatLocale=d,t.formatSpecifier=o,t.precisionFixed=function(t){return Math.max(0,-i(Math.abs(t)))},t.precisionPrefix=function(t,n){return Math.max(0,3*Math.max(-8,Math.min(8,Math.floor(i(n)/3)))-i(Math.abs(t)))},t.precisionRound=function(t,n){return t=Math.abs(t),n=Math.abs(n)-t,Math.max(0,i(n)-i(t))+1},Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-format", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Format numbers for human consumption.", | ||
@@ -25,2 +25,6 @@ "keywords": [ | ||
}, | ||
"files": [ | ||
"dist/**/*.js", | ||
"src/**/*.js" | ||
], | ||
"scripts": { | ||
@@ -34,7 +38,7 @@ "pretest": "rollup -c", | ||
"d3-queue": "3", | ||
"eslint": "5", | ||
"rollup": "0.64", | ||
"rollup-plugin-terser": "1", | ||
"eslint": "6", | ||
"rollup": "1", | ||
"rollup-plugin-terser": "5", | ||
"tape": "4" | ||
} | ||
} |
@@ -207,3 +207,3 @@ # d3-format | ||
```js | ||
{ | ||
FormatSpecifier { | ||
"fill": " ", | ||
@@ -231,2 +231,21 @@ "align": ">", | ||
<a name="FormatSpecifier" href="#FormatSpecifier">#</a> new d3.<b>FormatSpecifier</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/formatSpecifier.js "Source") | ||
Given the specified *specifier* object, returning an object with exposed fields that correspond to the [format specification mini-language](#locale_format) and a toString method that reconstructs the specifier. For example, `new FormatSpecifier({type: "s"})` returns: | ||
```js | ||
FormatSpecifier { | ||
"fill": " ", | ||
"align": ">", | ||
"sign": "-", | ||
"symbol": "", | ||
"zero": false, | ||
"width": undefined, | ||
"comma": false, | ||
"precision": undefined, | ||
"trim": false, | ||
"type": "s" | ||
} | ||
``` | ||
<a name="precisionFixed" href="#precisionFixed">#</a> d3.<b>precisionFixed</b>(<i>step</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionFixed.js "Source") | ||
@@ -318,3 +337,5 @@ | ||
* `numerals` - optional; an array of ten strings to replace the numerals 0-9. | ||
* `percent` - optional; the percent suffix (defaults to `"%"`). | ||
* `percent` - optional; the percent sign (defaults to `"%"`). | ||
* `minus` - optional; the minus sign (defaults to hyphen-minus, `"-"`). | ||
* `nan` - optional; the not-a-number value (defaults `"NaN"`). | ||
@@ -321,0 +342,0 @@ Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands. |
@@ -1,2 +0,2 @@ | ||
import formatLocale from "./locale"; | ||
import formatLocale from "./locale.js"; | ||
@@ -11,3 +11,4 @@ var locale; | ||
grouping: [3], | ||
currency: ["$", ""] | ||
currency: ["$", ""], | ||
minus: "-" | ||
}); | ||
@@ -14,0 +15,0 @@ |
@@ -1,2 +0,2 @@ | ||
import formatDecimal from "./formatDecimal"; | ||
import formatDecimal from "./formatDecimal.js"; | ||
@@ -3,0 +3,0 @@ export default function(x) { |
@@ -1,2 +0,2 @@ | ||
import formatDecimal from "./formatDecimal"; | ||
import formatDecimal from "./formatDecimal.js"; | ||
@@ -3,0 +3,0 @@ export var prefixExponent; |
@@ -1,2 +0,2 @@ | ||
import formatDecimal from "./formatDecimal"; | ||
import formatDecimal from "./formatDecimal.js"; | ||
@@ -3,0 +3,0 @@ export default function(x, p) { |
@@ -5,3 +5,16 @@ // [[fill]align][sign][symbol][0][width][,][.precision][~][type] | ||
export default function formatSpecifier(specifier) { | ||
return new FormatSpecifier(specifier); | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
var match; | ||
return new FormatSpecifier({ | ||
fill: match[1], | ||
align: match[2], | ||
sign: match[3], | ||
symbol: match[4], | ||
zero: match[5], | ||
width: match[6], | ||
comma: match[7], | ||
precision: match[8] && match[8].slice(1), | ||
trim: match[9], | ||
type: match[10] | ||
}); | ||
} | ||
@@ -11,15 +24,13 @@ | ||
function FormatSpecifier(specifier) { | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
var match; | ||
this.fill = match[1] || " "; | ||
this.align = match[2] || ">"; | ||
this.sign = match[3] || "-"; | ||
this.symbol = match[4] || ""; | ||
this.zero = !!match[5]; | ||
this.width = match[6] && +match[6]; | ||
this.comma = !!match[7]; | ||
this.precision = match[8] && +match[8].slice(1); | ||
this.trim = !!match[9]; | ||
this.type = match[10] || ""; | ||
export function FormatSpecifier(specifier) { | ||
this.fill = specifier.fill === undefined ? " " : specifier.fill + ""; | ||
this.align = specifier.align === undefined ? ">" : specifier.align + ""; | ||
this.sign = specifier.sign === undefined ? "-" : specifier.sign + ""; | ||
this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + ""; | ||
this.zero = !!specifier.zero; | ||
this.width = specifier.width === undefined ? undefined : +specifier.width; | ||
this.comma = !!specifier.comma; | ||
this.precision = specifier.precision === undefined ? undefined : +specifier.precision; | ||
this.trim = !!specifier.trim; | ||
this.type = specifier.type === undefined ? "" : specifier.type + ""; | ||
} | ||
@@ -33,7 +44,7 @@ | ||
+ (this.zero ? "0" : "") | ||
+ (this.width == null ? "" : Math.max(1, this.width | 0)) | ||
+ (this.width === undefined ? "" : Math.max(1, this.width | 0)) | ||
+ (this.comma ? "," : "") | ||
+ (this.precision == null ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ (this.trim ? "~" : "") | ||
+ this.type; | ||
}; |
@@ -1,3 +0,3 @@ | ||
import formatPrefixAuto from "./formatPrefixAuto"; | ||
import formatRounded from "./formatRounded"; | ||
import formatPrefixAuto from "./formatPrefixAuto.js"; | ||
import formatRounded from "./formatRounded.js"; | ||
@@ -4,0 +4,0 @@ export default { |
@@ -1,6 +0,6 @@ | ||
export {default as formatDefaultLocale, format, formatPrefix} from "./defaultLocale"; | ||
export {default as formatLocale} from "./locale"; | ||
export {default as formatSpecifier} from "./formatSpecifier"; | ||
export {default as precisionFixed} from "./precisionFixed"; | ||
export {default as precisionPrefix} from "./precisionPrefix"; | ||
export {default as precisionRound} from "./precisionRound"; | ||
export {default as formatDefaultLocale, format, formatPrefix} from "./defaultLocale.js"; | ||
export {default as formatLocale} from "./locale.js"; | ||
export {default as formatSpecifier, FormatSpecifier} from "./formatSpecifier.js"; | ||
export {default as precisionFixed} from "./precisionFixed.js"; | ||
export {default as precisionPrefix} from "./precisionPrefix.js"; | ||
export {default as precisionRound} from "./precisionRound.js"; |
@@ -1,18 +0,22 @@ | ||
import exponent from "./exponent"; | ||
import formatGroup from "./formatGroup"; | ||
import formatNumerals from "./formatNumerals"; | ||
import formatSpecifier from "./formatSpecifier"; | ||
import formatTrim from "./formatTrim"; | ||
import formatTypes from "./formatTypes"; | ||
import {prefixExponent} from "./formatPrefixAuto"; | ||
import identity from "./identity"; | ||
import exponent from "./exponent.js"; | ||
import formatGroup from "./formatGroup.js"; | ||
import formatNumerals from "./formatNumerals.js"; | ||
import formatSpecifier from "./formatSpecifier.js"; | ||
import formatTrim from "./formatTrim.js"; | ||
import formatTypes from "./formatTypes.js"; | ||
import {prefixExponent} from "./formatPrefixAuto.js"; | ||
import identity from "./identity.js"; | ||
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
var map = Array.prototype.map, | ||
prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
export default function(locale) { | ||
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity, | ||
currency = locale.currency, | ||
decimal = locale.decimal, | ||
numerals = locale.numerals ? formatNumerals(locale.numerals) : identity, | ||
percent = locale.percent || "%"; | ||
var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""), | ||
currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "", | ||
currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "", | ||
decimal = locale.decimal === undefined ? "." : locale.decimal + "", | ||
numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)), | ||
percent = locale.percent === undefined ? "%" : locale.percent + "", | ||
minus = locale.minus === undefined ? "-" : locale.minus + "", | ||
nan = locale.nan === undefined ? "NaN" : locale.nan + ""; | ||
@@ -37,3 +41,3 @@ function newFormat(specifier) { | ||
// The "" type, and any invalid type, is an alias for ".12~g". | ||
else if (!formatTypes[type]) precision == null && (precision = 12), trim = true, type = "g"; | ||
else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g"; | ||
@@ -45,4 +49,4 @@ // If zero fill is specified, padding goes after sign and before digits. | ||
// For SI-prefix, the suffix is lazily computed. | ||
var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? percent : ""; | ||
var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : ""; | ||
@@ -59,3 +63,3 @@ // What format function should we use? | ||
// For fixed precision, it must be in [0, 20]. | ||
precision = precision == null ? 6 | ||
precision = precision === undefined ? 6 | ||
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision)) | ||
@@ -77,3 +81,3 @@ : Math.max(0, Math.min(20, precision)); | ||
var valueNegative = value < 0; | ||
value = formatType(Math.abs(value), precision); | ||
value = isNaN(value) ? nan : formatType(Math.abs(value), precision); | ||
@@ -87,3 +91,4 @@ // Trim insignificant zeros. | ||
// Compute the prefix and suffix. | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : ""); | ||
@@ -90,0 +95,0 @@ |
@@ -1,2 +0,2 @@ | ||
import exponent from "./exponent"; | ||
import exponent from "./exponent.js"; | ||
@@ -3,0 +3,0 @@ export default function(step) { |
@@ -1,2 +0,2 @@ | ||
import exponent from "./exponent"; | ||
import exponent from "./exponent.js"; | ||
@@ -3,0 +3,0 @@ export default function(step, value) { |
@@ -1,2 +0,2 @@ | ||
import exponent from "./exponent"; | ||
import exponent from "./exponent.js"; | ||
@@ -3,0 +3,0 @@ export default function(step, max) { |
344
49298
21
599