Skip to content

Commit b14a15f

Browse files
authored
Merge pull request #144 from nutboltu/feat/rm
feat: add rm support to strigify and uglify-js as package dependencies
2 parents b4f32ae + 0614409 commit b14a15f

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

big.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@
251251
* 4 valueOf
252252
* n? {number|undefined} Caller's argument.
253253
* k? {number|undefined}
254+
* rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
254255
*/
255-
function stringify(x, id, n, k) {
256+
function stringify(x, id, n, k, rm) {
256257
var e, s,
257258
Big = x.constructor,
258259
z = !x.c[0];
@@ -268,7 +269,7 @@
268269
n = k - x.e;
269270

270271
// Round?
271-
if (x.c.length > ++k) round(x, n, Big.RM);
272+
if (x.c.length > ++k) round(x, n, rm === undefined ? Big.RM : rm);
272273

273274
// toFixed: recalculate k as x.e may have changed if value rounded up.
274275
if (id == 2) k = x.e + n + 1;
@@ -865,9 +866,10 @@
865866
* places and rounded using Big.RM.
866867
*
867868
* dp? {number} Integer, 0 to MAX_DP inclusive.
869+
* rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
868870
*/
869-
P.toExponential = function (dp) {
870-
return stringify(this, 1, dp, dp);
871+
P.toExponential = function (dp, rm) {
872+
return stringify(this, 1, dp, dp, rm);
871873
};
872874

873875

@@ -876,12 +878,13 @@
876878
* places and rounded using Big.RM.
877879
*
878880
* dp? {number} Integer, 0 to MAX_DP inclusive.
879-
*
881+
* rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
882+
*
880883
* (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
881884
* (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
882885
*/
883-
P.toFixed = function (dp) {
884-
return stringify(this, 2, dp, this.e + dp);
886+
P.toFixed = function (dp, rm) {
887+
return stringify(this, 2, dp, this.e + dp, rm);
885888
};
886889

887890

@@ -891,9 +894,10 @@
891894
* the integer part of the value in normal notation.
892895
*
893896
* sd {number} Integer, 1 to MAX_DP inclusive.
897+
* rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
894898
*/
895-
P.toPrecision = function (sd) {
896-
return stringify(this, 3, sd, sd - 1);
899+
P.toPrecision = function (sd, rm) {
900+
return stringify(this, 3, sd, sd - 1, rm);
897901
};
898902

899903

doc/bigAPI.html

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,14 @@ <h5 id="times">
656656

657657

658658
<h5 id="toE">
659-
toExponential<code class='inset'>.toExponential(dp) &rArr;
659+
toExponential<code class='inset'>.toExponential(dp, rm) &rArr;
660660
<i>string</i></code>
661661
</h5>
662-
<p><code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive</p>
662+
<p>
663+
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
664+
<br />
665+
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
666+
</p>
663667
<p>
664668
Returns a string representing the value of this Big number in exponential
665669
notation to a fixed number of <code>dp</code> decimal places.
@@ -668,7 +672,7 @@ <h5 id="toE">
668672
If the value of this Big number in exponential notation has more digits to
669673
the right of the decimal point than is specified by <code>dp</code>, the
670674
return value will be rounded to <code>dp</code> decimal places using
671-
rounding mode <a href='#rm'><code>Big.RM</code></a>.
675+
rounding mode <a href='#rm'><code>rm</code></a>.
672676
</p>
673677
<p>
674678
If the value of this Big number in exponential notation has fewer digits
@@ -678,10 +682,12 @@ <h5 id="toE">
678682
<p>
679683
If <code>dp</code> is omitted or is undefined, the number of digits
680684
after the decimal point defaults to the minimum number of digits
681-
necessary to represent the value exactly.
685+
necessary to represent the value exactly.<br />
686+
if <code>rm</code> is omitted or is undefined, the current
687+
<a href='#rm'><code>Big.RM</code></a> setting is used.
682688
</p>
683689
<p>
684-
Throws if <code>dp</code> is invalid.
690+
Throws if <code>dp</code> or <code>rm</code> is invalid.
685691
</p>
686692
<pre>
687693
x = 45.6
@@ -698,11 +704,13 @@ <h5 id="toE">
698704

699705

700706
<h5 id="toF">
701-
toFixed<code class='inset'>.toFixed(dp) &rArr;
707+
toFixed<code class='inset'>.toFixed(dp, rm) &rArr;
702708
<i>string</i></code>
703709
</h5>
704710
<p>
705711
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
712+
<br />
713+
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
706714
</p>
707715
<p>
708716
Returns a string representing the value of this Big number in normal
@@ -712,7 +720,7 @@ <h5 id="toF">
712720
If the value of this Big number in normal notation has more digits to the
713721
right of the decimal point than is specified by <code>dp</code>, the
714722
return value will be rounded to <code>dp</code> decimal places using
715-
rounding mode <a href='#rm'><code>Big.RM</code></a>.
723+
rounding mode <a href='#rm'><code>rm</code></a>.
716724
</p>
717725
<p>
718726
If the value of this Big number in normal notation has fewer fraction
@@ -728,10 +736,12 @@ <h5 id="toF">
728736
If <code>dp</code> is omitted or is undefined, the return value is
729737
simply the value in normal notation. This is also unlike
730738
<code>Number.prototype.toFixed</code>, which returns the value to zero
731-
decimal places.
739+
decimal places.<br />
740+
if <code>rm</code> is omitted or is undefined, the current
741+
<a href='#rm'><code>Big.RM</code></a> setting is used.
732742
</p>
733743
<p>
734-
Throws if <code>dp</code> is invalid.
744+
Throws if <code>dp</code> or <code>rm</code> is invalid.
735745
</p>
736746
<pre>
737747
x = 45.6
@@ -745,10 +755,14 @@ <h5 id="toF">
745755

746756

747757
<h5 id="toP">
748-
toPrecision<code class='inset'>.toPrecision(sd) &rArr;
758+
toPrecision<code class='inset'>.toPrecision(sd, rm) &rArr;
749759
<i>string</i></code>
750760
</h5>
751-
<p><code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive</p>
761+
<p>
762+
<code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive
763+
<br />
764+
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
765+
</p>
752766
<p>
753767
Returns a string representing the value of this Big number to the
754768
specified number of <code>sd</code> significant digits.
@@ -757,7 +771,7 @@ <h5 id="toP">
757771
If the value of this Big number has more digits than is specified by
758772
<code>sd</code>, the return value will be rounded to <code>sd</code>
759773
significant digits using rounding mode
760-
<a href='#rm'><code>Big.RM</code></a>.
774+
<a href='#rm'><code>rm</code></a>.
761775
</p>
762776
<p>
763777
If the value of this Big number has fewer digits than is specified by
@@ -770,10 +784,12 @@ <h5 id="toP">
770784
</p>
771785
<p>
772786
If <code>sd</code> is omitted or is undefined, the return value is
773-
the same as <code>.toString()</code>.
787+
the same as <code>.toString()</code>.<br />
788+
if <code>rm</code> is omitted or is undefined, the current
789+
<a href='#rm'><code>Big.RM</code></a> setting is used.
774790
</p>
775791
<p>
776-
Throws if <code>sd</code> is invalid.
792+
Throws if <code>sd</code> or <code>rm</code> is invalid.
777793
</p>
778794
<pre>
779795
x = 45.6

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
"collective": {
4747
"type": "opencollective",
4848
"url": "https://opencollective.com/bigjs"
49+
},
50+
"devDependencies": {
51+
"uglify-js": "^3.10.2"
4952
}
50-
}
53+
}

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
uglify-js@^3.10.2:
6+
version "3.10.2"
7+
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.10.2.tgz#8cfa1209fd04199cc8a7f9930ddedb30b0f1912d"
8+
integrity sha512-GXCYNwqoo0MbLARghYjxVBxDCnU0tLqN7IPLdHHbibCb1NI5zBkU2EPcy/GaVxc0BtTjqyGXJCINe6JMR2Dpow==

0 commit comments

Comments
 (0)