Merge pull request #101 from stcherenkov/master

Add isBigNumber method
This commit is contained in:
Michael M 2017-01-08 12:12:33 +00:00 committed by GitHub
commit 6a0c00daaf
4 changed files with 113 additions and 1 deletions

View File

@ -328,6 +328,19 @@
BigNumber.ROUND_HALF_FLOOR = 8;
BigNumber.EUCLID = 9;
/*
* Check if passed instance is BigNumber
*
* @param {*} instance - what to check
* @returns {boolean}
*/
BigNumber.isBigNumber = function (instance) {
if (!instance || !instance._isBigNumberInstance) {
return false;
}
return true;
}
/*
* Configure infrequently-changing library-wide settings.
@ -2549,6 +2562,8 @@
return n.s < 0 ? '-' + str : str;
};
P._isBigNumberInstance = true;
// Aliases for BigDecimal methods.
//P.add = P.plus; // P.add included above

View File

@ -86,6 +86,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#max" >max</a></li>
<li><a href="#min" >min</a></li>
<li><a href="#random" >random</a></li>
<li><a href="#is-big-number" >isBigNumber</a></li>
</ul>
<a href="#constructor-properties">Properties</a>
@ -744,6 +745,21 @@ BigNumber.random(20) // '0.78193327636914089009'</pre>
<h5 id="is-big-number">
isBigNumber<code class='inset'>.isBigNumber(instance) <i>&rArr; boolean</i></code>
</h5>
<p><code>instance</code>: <i>any</i></p>
<p>
Checks if passed instance is BigNumber.
</p>
<pre>BigNumber.isBigNumber(42) // false
BigNumber.isBigNumber(new BigNumber('42')) // true
var BN = BigNumber.another();
BigNumber.isBigNumber(new BN('42')) // true</pre>
<h4 id="constructor-properties">Properties</h4>
<p>
The library's enumerated rounding modes are stored as properties of the constructor.<br />

View File

@ -37,7 +37,8 @@ console.log( '\n STARTING TESTS...\n' );
'toNumber',
'toPrecision',
'toString',
'trunc'
'trunc',
'isBigNumber'
]
.forEach( function (method) {
arr = require('./' + method);

80
test/isBigNumber.js Normal file
View File

@ -0,0 +1,80 @@
var count = (function isBigNumber(BigNumber) {
var start = +new Date(),
log,
error,
u,
passed = 0,
total = 0;
if (typeof window === 'undefined') {
log = console.log;
error = console.error;
} else {
log = function (str) { document.body.innerHTML += str.replace('\n', '<br>') };
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
str.replace('\n', '<br>') + '</div>' };
}
if (!BigNumber && typeof require === 'function') BigNumber = require('../bignumber');
function assert(expected, actual) {
total++;
if (expected !== actual) {
error('\n Test number: ' + total + ' failed');
error(' Expected: ' + expected);
error(' Actual: ' + actual);
//process.exit();
} else {
passed++;
//log('\n Expected and actual: ' + actual);
}
}
function assertException(func, message) {
var actual;
total++;
try {
func();
} catch (e) {
actual = e;
}
if (actual && actual.name == 'BigNumber Error') {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
error('\n Test number: ' + total + ' failed');
error('\n Expected: ' + message + ' to raise a BigNumber Error.');
error(' Actual: ' + (actual || 'no exception'));
//process.exit();
}
}
function T(expected, value){
assert(expected, BigNumber.isBigNumber(value));
}
log('\n Testing isBigNumber...');
T(false, u);
T(false, void 0);
T(false, null);
T(false, '0');
T(false, 0);
T(false, 1);
T(false, NaN);
T(true, new BigNumber(0));
T(true, new BigNumber('0'));
T(true, new BigNumber(1));
T(true, new BigNumber('1'));
var AnotherBigNumber = BigNumber.another();
T(true, new AnotherBigNumber(0));
T(true, new AnotherBigNumber('0'));
T(true, new AnotherBigNumber(1));
T(true, new AnotherBigNumber('1'));
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];
})(this.BigNumber);
if (typeof module !== 'undefined' && module.exports) module.exports = count;