From b5ee2eceb0ebb24b9b382f3bc9c2f5cebb63c5f5 Mon Sep 17 00:00:00 2001 From: Stanislav Cherenkov Date: Fri, 16 Sep 2016 08:49:53 +0300 Subject: [PATCH] Add isBigNumber method add tests add doc --- bignumber.js | 15 +++++++++ doc/API.html | 16 +++++++++ test/every-test.js | 3 +- test/isBigNumber.js | 80 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 test/isBigNumber.js diff --git a/bignumber.js b/bignumber.js index 610aa75..ce722a5 100644 --- a/bignumber.js +++ b/bignumber.js @@ -330,6 +330,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. @@ -2540,6 +2553,8 @@ return n.s < 0 ? '-' + str : str; }; + P._isBigNumberInstance = true; + // Aliases for BigDecimal methods. //P.add = P.plus; // P.add included above diff --git a/doc/API.html b/doc/API.html index 7dc947f..db0c101 100644 --- a/doc/API.html +++ b/doc/API.html @@ -86,6 +86,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
  • max
  • min
  • random
  • +
  • isBigNumber
  • Properties @@ -743,6 +744,21 @@ BigNumber.random(20) // '0.78193327636914089009' +
    + isBigNumber.isBigNumber(instance) ⇒ boolean +
    +

    instance: any

    +

    + Checks if passed instance is BigNumber. +

    +
    BigNumber.isBigNumber(42)                  // false
    +BigNumber.isBigNumber(new BigNumber('42')) // true
    +
    +var BN = BigNumber.another();
    +BigNumber.isBigNumber(new BN('42'))        // true
    + + +

    Properties

    The library's enumerated rounding modes are stored as properties of the constructor.
    diff --git a/test/every-test.js b/test/every-test.js index 9d6465e..a174e30 100644 --- a/test/every-test.js +++ b/test/every-test.js @@ -37,7 +37,8 @@ console.log( '\n STARTING TESTS...\n' ); 'toNumber', 'toPrecision', 'toString', - 'trunc' + 'trunc', + 'isBigNumber' ] .forEach( function (method) { arr = require('./' + method); diff --git a/test/isBigNumber.js b/test/isBigNumber.js new file mode 100644 index 0000000..97f9189 --- /dev/null +++ b/test/isBigNumber.js @@ -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', '
    ') }; + error = function (str) { document.body.innerHTML += '

    ' + + str.replace('\n', '
    ') + '
    ' }; + } + + 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;