From c32a414ef79040cd66e8d1fca6debfd4a7f08fee Mon Sep 17 00:00:00 2001 From: Mihai Cimpoesu Date: Fri, 14 Jul 2017 16:55:04 +0100 Subject: [PATCH 1/2] Update to latest version of tweetnacl lib. Also include tweetnacl-util because the functionality was extracted as a new module. --- lib/encryption.js | 2 +- lib/keystore.js | 6 ++++++ package.json | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/encryption.js b/lib/encryption.js index e931ced9..7c9b38b9 100644 --- a/lib/encryption.js +++ b/lib/encryption.js @@ -149,7 +149,7 @@ var multiDecryptString = function (keystore, pwDerivedKey, encMsg, theirPubKey, var symKey = false; for (var i=0; i < encMsg.encryptedSymKey.length; i++) { var result = _asymDecryptRaw(keystore, pwDerivedKey, encMsg.encryptedSymKey[i], theirPubKey, myPubKey, hdPathString) - if (result !== false) { + if (result !== false && result !== null) { symKey = result; break; } diff --git a/lib/keystore.js b/lib/keystore.js index a8a813f6..ad220662 100644 --- a/lib/keystore.js +++ b/lib/keystore.js @@ -7,6 +7,8 @@ var Random = bitcore.crypto.Random; var Hash = bitcore.crypto.Hash; var Mnemonic = require('bitcore-mnemonic'); var nacl = require('tweetnacl'); +nacl.util = require('tweetnacl-util'); + var scrypt = require('scrypt-async'); var encryption = require('./encryption'); @@ -183,6 +185,10 @@ KeyStore._decryptString = function (encryptedStr, pwDerivedKey) { throw new Error("Decryption failed!"); } + if (decryptedStr === null) { + decryptedStr = []; + } + return nacl.util.encodeUTF8(decryptedStr); }; diff --git a/package.json b/package.json index 02f10ef8..fd3773c7 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ "ethereumjs-util": "^5.1.1", "rlp": "^2.0.0", "scrypt-async": "^1.2.0", - "tweetnacl": "0.13.2", + "tweetnacl": "^1.0.0", + "tweetnacl-util": "^0.15.0", "web3": "^0.19.1" }, "devDependencies": { From fd2804dc92cb4766db407720cbd9961bd56df7f6 Mon Sep 17 00:00:00 2001 From: Mihai Cimpoesu Date: Fri, 14 Jul 2017 17:00:27 +0100 Subject: [PATCH 2/2] Adding dist files for the latest change --- dist/lightwallet.js | 52748 ++++++++++++++++++++------------------ dist/lightwallet.min.js | 83 +- 2 files changed, 27363 insertions(+), 25468 deletions(-) diff --git a/dist/lightwallet.js b/dist/lightwallet.js index 1f98e151..e7e9fb09 100644 --- a/dist/lightwallet.js +++ b/dist/lightwallet.js @@ -160,7 +160,7 @@ var multiDecryptString = function multiDecryptString(keystore, pwDerivedKey, enc var symKey = false; for (var i = 0; i < encMsg.encryptedSymKey.length; i++) { var result = _asymDecryptRaw(keystore, pwDerivedKey, encMsg.encryptedSymKey[i], theirPubKey, myPubKey, hdPathString); - if (result !== false) { + if (result !== false && result !== null) { symKey = result; break; } @@ -189,7 +189,7 @@ module.exports = { }; }).call(this,require("buffer").Buffer) -},{"buffer":155,"ethereumjs-util":244,"tweetnacl":341}],3:[function(require,module,exports){ +},{"buffer":152,"ethereumjs-util":240,"tweetnacl":348}],3:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -202,6 +202,8 @@ var Random = bitcore.crypto.Random; var Hash = bitcore.crypto.Hash; var Mnemonic = require('bitcore-mnemonic'); var nacl = require('tweetnacl'); +nacl.util = require('tweetnacl-util'); + var scrypt = require('scrypt-async'); var encryption = require('./encryption'); @@ -373,6 +375,10 @@ KeyStore._decryptString = function (encryptedStr, pwDerivedKey) { throw new Error("Decryption failed!"); } + if (decryptedStr === null) { + decryptedStr = []; + } + return nacl.util.encodeUTF8(decryptedStr); }; @@ -842,7 +848,7 @@ KeyStore.prototype.signTransaction = function (txParams, callback) { module.exports = KeyStore; }).call(this,require("buffer").Buffer) -},{"./encryption":2,"./signing":4,"bitcore-lib":25,"bitcore-mnemonic":97,"buffer":155,"crypto-js":189,"elliptic":226,"ethereumjs-tx":241,"scrypt-async":306,"tweetnacl":341}],4:[function(require,module,exports){ +},{"./encryption":2,"./signing":4,"bitcore-lib":25,"bitcore-mnemonic":97,"buffer":152,"crypto-js":187,"elliptic":223,"ethereumjs-tx":239,"scrypt-async":313,"tweetnacl":348,"tweetnacl-util":347}],4:[function(require,module,exports){ (function (Buffer){ "use strict"; @@ -930,7 +936,7 @@ var concatSig = function concatSig(signature) { module.exports.concatSig = concatSig; }).call(this,require("buffer").Buffer) -},{"buffer":155,"ethereumjs-tx":241,"ethereumjs-util":244}],5:[function(require,module,exports){ +},{"buffer":152,"ethereumjs-tx":239,"ethereumjs-util":240}],5:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -1040,7 +1046,7 @@ module.exports = { }; }).call(this,require("buffer").Buffer) -},{"buffer":155,"crypto-js":189,"ethereumjs-tx":241,"rlp":305,"web3/lib/solidity/coder":354}],6:[function(require,module,exports){ +},{"buffer":152,"crypto-js":187,"ethereumjs-tx":239,"rlp":311,"web3/lib/solidity/coder":361}],6:[function(require,module,exports){ 'use strict'; var CryptoJS = require('crypto-js'); @@ -1094,7 +1100,7 @@ var upgradeOldSerialized = function upgradeOldSerialized(oldSerialized, password module.exports.upgradeOldSerialized = upgradeOldSerialized; -},{"./keystore":3,"bitcore-lib":25,"bitcore-mnemonic":97,"crypto-js":189,"elliptic":226,"ethereumjs-tx":241,"scrypt-async":306,"tweetnacl":341}],7:[function(require,module,exports){ +},{"./keystore":3,"bitcore-lib":25,"bitcore-mnemonic":97,"crypto-js":187,"elliptic":223,"ethereumjs-tx":239,"scrypt-async":313,"tweetnacl":348}],7:[function(require,module,exports){ 'use strict'; var asn1 = exports; @@ -1107,7 +1113,7 @@ asn1.constants = require('./asn1/constants'); asn1.decoders = require('./asn1/decoders'); asn1.encoders = require('./asn1/encoders'); -},{"./asn1/api":8,"./asn1/base":10,"./asn1/constants":14,"./asn1/decoders":16,"./asn1/encoders":19,"bn.js":21}],8:[function(require,module,exports){ +},{"./asn1/api":8,"./asn1/base":10,"./asn1/constants":14,"./asn1/decoders":16,"./asn1/encoders":19,"bn.js":108}],8:[function(require,module,exports){ 'use strict'; var asn1 = require('../asn1'); @@ -1166,7 +1172,7 @@ Entity.prototype.encode = function encode(data, enc, /* internal */reporter) { return this._getEncoder(enc).encode(data, reporter); }; -},{"../asn1":7,"inherits":258,"vm":350}],9:[function(require,module,exports){ +},{"../asn1":7,"inherits":260,"vm":357}],9:[function(require,module,exports){ 'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; @@ -1274,7 +1280,7 @@ EncoderBuffer.prototype.join = function join(out, offset) { return out; }; -},{"../base":10,"buffer":155,"inherits":258}],10:[function(require,module,exports){ +},{"../base":10,"buffer":152,"inherits":260}],10:[function(require,module,exports){ 'use strict'; var base = exports; @@ -1813,7 +1819,7 @@ Node.prototype._isPrintstr = function isPrintstr(str) { ); }; -},{"../base":10,"minimalistic-assert":270}],12:[function(require,module,exports){ +},{"../base":10,"minimalistic-assert":271}],12:[function(require,module,exports){ 'use strict'; var inherits = require('inherits'); @@ -1933,7 +1939,7 @@ ReporterError.prototype.rethrow = function rethrow(msg) { return this; }; -},{"inherits":258}],13:[function(require,module,exports){ +},{"inherits":260}],13:[function(require,module,exports){ 'use strict'; var constants = require('../constants'); @@ -2279,7 +2285,7 @@ function derDecodeLen(buf, primitive, fail) { return len; } -},{"../../asn1":7,"inherits":258}],16:[function(require,module,exports){ +},{"../../asn1":7,"inherits":260}],16:[function(require,module,exports){ 'use strict'; var decoders = exports; @@ -2335,7 +2341,7 @@ PEMDecoder.prototype.decode = function decode(data, options) { return DERDecoder.prototype.decode.call(this, input, options); }; -},{"./der":15,"buffer":155,"inherits":258}],18:[function(require,module,exports){ +},{"./der":15,"buffer":152,"inherits":260}],18:[function(require,module,exports){ 'use strict'; var inherits = require('inherits'); @@ -2580,7 +2586,7 @@ function encodeTag(tag, primitive, cls, reporter) { return res; } -},{"../../asn1":7,"buffer":155,"inherits":258}],19:[function(require,module,exports){ +},{"../../asn1":7,"buffer":152,"inherits":260}],19:[function(require,module,exports){ 'use strict'; var encoders = exports; @@ -2613,3970 +2619,3337 @@ PEMEncoder.prototype.encode = function encode(data, options) { return out.join('\n'); }; -},{"./der":18,"inherits":258}],21:[function(require,module,exports){ +},{"./der":18,"inherits":260}],21:[function(require,module,exports){ +(function (global){ 'use strict'; +// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js +// original notice: + +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; -(function (module, exports) { - 'use strict'; +function compare(a, b) { + if (a === b) { + return 0; + } - // Utils + var x = a.length; + var y = b.length; - function assert(val, msg) { - if (!val) throw new Error(msg || 'Assertion failed'); + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i]; + y = b[i]; + break; + } } - // Could use `inherits` module, but don't want to move from single file - // architecture yet. - function inherits(ctor, superCtor) { - ctor.super_ = superCtor; - var TempCtor = function TempCtor() {}; - TempCtor.prototype = superCtor.prototype; - ctor.prototype = new TempCtor(); - ctor.prototype.constructor = ctor; + if (x < y) { + return -1; + } + if (y < x) { + return 1; + } + return 0; +} +function isBuffer(b) { + if (global.Buffer && typeof global.Buffer.isBuffer === 'function') { + return global.Buffer.isBuffer(b); } + return !!(b != null && b._isBuffer); +} - // BN +// based on node assert, original notice: - function BN(number, base, endian) { - if (BN.isBN(number)) { - return number; - } +// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 +// +// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! +// +// Originally from narwhal.js (http://narwhaljs.org) +// Copyright (c) 2009 Thomas Robinson <280north.com> +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the 'Software'), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - this.negative = 0; - this.words = null; - this.length = 0; +var util = require('util/'); +var hasOwn = Object.prototype.hasOwnProperty; +var pSlice = Array.prototype.slice; +var functionsHaveNames = function () { + return function foo() {}.name === 'foo'; +}(); +function pToString(obj) { + return Object.prototype.toString.call(obj); +} +function isView(arrbuf) { + if (isBuffer(arrbuf)) { + return false; + } + if (typeof global.ArrayBuffer !== 'function') { + return false; + } + if (typeof ArrayBuffer.isView === 'function') { + return ArrayBuffer.isView(arrbuf); + } + if (!arrbuf) { + return false; + } + if (arrbuf instanceof DataView) { + return true; + } + if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { + return true; + } + return false; +} +// 1. The assert module provides functions that throw +// AssertionError's when particular conditions are not met. The +// assert module must conform to the following interface. - // Reduction context - this.red = null; +var assert = module.exports = ok; - if (number !== null) { - if (base === 'le' || base === 'be') { - endian = base; - base = 10; - } +// 2. The AssertionError is defined in assert. +// new assert.AssertionError({ message: message, +// actual: actual, +// expected: expected }) - this._init(number || 0, base || 10, endian || 'be'); - } +var regex = /\s*function\s+([^\(\s]*)\s*/; +// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js +function getName(func) { + if (!util.isFunction(func)) { + return; } - if ((typeof module === 'undefined' ? 'undefined' : _typeof(module)) === 'object') { - module.exports = BN; + if (functionsHaveNames) { + return func.name; + } + var str = func.toString(); + var match = str.match(regex); + return match && match[1]; +} +assert.AssertionError = function AssertionError(options) { + this.name = 'AssertionError'; + this.actual = options.actual; + this.expected = options.expected; + this.operator = options.operator; + if (options.message) { + this.message = options.message; + this.generatedMessage = false; } else { - exports.BN = BN; + this.message = getMessage(this); + this.generatedMessage = true; } + var stackStartFunction = options.stackStartFunction || fail; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, stackStartFunction); + } else { + // non v8 browsers so we can have a stacktrace + var err = new Error(); + if (err.stack) { + var out = err.stack; - BN.BN = BN; - BN.wordSize = 26; - - var Buffer; - try { - Buffer = require('buf' + 'fer').Buffer; - } catch (e) {} + // try to strip useless frames + var fn_name = getName(stackStartFunction); + var idx = out.indexOf('\n' + fn_name); + if (idx >= 0) { + // once we have located the function frame + // we need to strip out everything before it (and its line) + var next_line = out.indexOf('\n', idx + 1); + out = out.substring(next_line + 1); + } - BN.isBN = function isBN(num) { - if (num instanceof BN) { - return true; + this.stack = out; } + } +}; - return num !== null && (typeof num === 'undefined' ? 'undefined' : _typeof(num)) === 'object' && num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); - }; - - BN.max = function max(left, right) { - if (left.cmp(right) > 0) return left; - return right; - }; +// assert.AssertionError instanceof Error +util.inherits(assert.AssertionError, Error); - BN.min = function min(left, right) { - if (left.cmp(right) < 0) return left; - return right; - }; +function truncate(s, n) { + if (typeof s === 'string') { + return s.length < n ? s : s.slice(0, n); + } else { + return s; + } +} +function inspect(something) { + if (functionsHaveNames || !util.isFunction(something)) { + return util.inspect(something); + } + var rawname = getName(something); + var name = rawname ? ': ' + rawname : ''; + return '[Function' + name + ']'; +} +function getMessage(self) { + return truncate(inspect(self.actual), 128) + ' ' + self.operator + ' ' + truncate(inspect(self.expected), 128); +} - BN.prototype._init = function init(number, base, endian) { - if (typeof number === 'number') { - return this._initNumber(number, base, endian); - } +// At present only the three keys mentioned above are used and +// understood by the spec. Implementations or sub modules can pass +// other keys to the AssertionError's constructor - they will be +// ignored. - if ((typeof number === 'undefined' ? 'undefined' : _typeof(number)) === 'object') { - return this._initArray(number, base, endian); - } +// 3. All of the following functions must throw an AssertionError +// when a corresponding condition is not met, with a message that +// may be undefined if not provided. All assertion methods provide +// both the actual and expected values to the assertion error for +// display purposes. - if (base === 'hex') { - base = 16; - } - assert(base === (base | 0) && base >= 2 && base <= 36); +function fail(actual, expected, message, operator, stackStartFunction) { + throw new assert.AssertionError({ + message: message, + actual: actual, + expected: expected, + operator: operator, + stackStartFunction: stackStartFunction + }); +} - number = number.toString().replace(/\s+/g, ''); - var start = 0; - if (number[0] === '-') { - start++; - } +// EXTENSION! allows for well behaved errors defined elsewhere. +assert.fail = fail; - if (base === 16) { - this._parseHex(number, start); - } else { - this._parseBase(number, base, start); - } +// 4. Pure assertion tests whether a value is truthy, as determined +// by !!guard. +// assert.ok(guard, message_opt); +// This statement is equivalent to assert.equal(true, !!guard, +// message_opt);. To test strictly for the value true, use +// assert.strictEqual(true, guard, message_opt);. - if (number[0] === '-') { - this.negative = 1; - } +function ok(value, message) { + if (!value) fail(value, true, message, '==', assert.ok); +} +assert.ok = ok; - this.strip(); +// 5. The equality assertion tests shallow, coercive equality with +// ==. +// assert.equal(actual, expected, message_opt); - if (endian !== 'le') return; +assert.equal = function equal(actual, expected, message) { + if (actual != expected) fail(actual, expected, message, '==', assert.equal); +}; - this._initArray(this.toArray(), base, endian); - }; +// 6. The non-equality assertion tests for whether two objects are not equal +// with != assert.notEqual(actual, expected, message_opt); - BN.prototype._initNumber = function _initNumber(number, base, endian) { - if (number < 0) { - this.negative = 1; - number = -number; - } - if (number < 0x4000000) { - this.words = [number & 0x3ffffff]; - this.length = 1; - } else if (number < 0x10000000000000) { - this.words = [number & 0x3ffffff, number / 0x4000000 & 0x3ffffff]; - this.length = 2; - } else { - assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) - this.words = [number & 0x3ffffff, number / 0x4000000 & 0x3ffffff, 1]; - this.length = 3; - } +assert.notEqual = function notEqual(actual, expected, message) { + if (actual == expected) { + fail(actual, expected, message, '!=', assert.notEqual); + } +}; - if (endian !== 'le') return; +// 7. The equivalence assertion tests a deep equality relation. +// assert.deepEqual(actual, expected, message_opt); - // Reverse the bytes - this._initArray(this.toArray(), base, endian); - }; +assert.deepEqual = function deepEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'deepEqual', assert.deepEqual); + } +}; - BN.prototype._initArray = function _initArray(number, base, endian) { - // Perhaps a Uint8Array - assert(typeof number.length === 'number'); - if (number.length <= 0) { - this.words = [0]; - this.length = 1; - return this; - } +assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); + } +}; - this.length = Math.ceil(number.length / 3); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - this.words[i] = 0; - } +function _deepEqual(actual, expected, strict, memos) { + // 7.1. All identical values are equivalent, as determined by ===. + if (actual === expected) { + return true; + } else if (isBuffer(actual) && isBuffer(expected)) { + return compare(actual, expected) === 0; - var j, w; - var off = 0; - if (endian === 'be') { - for (i = number.length - 1, j = 0; i >= 0; i -= 3) { - w = number[i] | number[i - 1] << 8 | number[i - 2] << 16; - this.words[j] |= w << off & 0x3ffffff; - this.words[j + 1] = w >>> 26 - off & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } else if (endian === 'le') { - for (i = 0, j = 0; i < number.length; i += 3) { - w = number[i] | number[i + 1] << 8 | number[i + 2] << 16; - this.words[j] |= w << off & 0x3ffffff; - this.words[j + 1] = w >>> 26 - off & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } - return this.strip(); - }; + // 7.2. If the expected value is a Date object, the actual value is + // equivalent if it is also a Date object that refers to the same time. + } else if (util.isDate(actual) && util.isDate(expected)) { + return actual.getTime() === expected.getTime(); - function parseHex(str, start, end) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; + // 7.3 If the expected value is a RegExp object, the actual value is + // equivalent if it is also a RegExp object with the same source and + // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). + } else if (util.isRegExp(actual) && util.isRegExp(expected)) { + return actual.source === expected.source && actual.global === expected.global && actual.multiline === expected.multiline && actual.lastIndex === expected.lastIndex && actual.ignoreCase === expected.ignoreCase; - r <<= 4; + // 7.4. Other pairs that do not both pass typeof value == 'object', + // equivalence is determined by ==. + } else if ((actual === null || (typeof actual === 'undefined' ? 'undefined' : _typeof(actual)) !== 'object') && (expected === null || (typeof expected === 'undefined' ? 'undefined' : _typeof(expected)) !== 'object')) { + return strict ? actual === expected : actual == expected; - // 'a' - 'f' - if (c >= 49 && c <= 54) { - r |= c - 49 + 0xa; + // If both values are instances of typed arrays, wrap their underlying + // ArrayBuffers in a Buffer each to increase performance + // This optimization requires the arrays to have the same type as checked by + // Object.prototype.toString (aka pToString). Never perform binary + // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their + // bit patterns are not identical. + } else if (isView(actual) && isView(expected) && pToString(actual) === pToString(expected) && !(actual instanceof Float32Array || actual instanceof Float64Array)) { + return compare(new Uint8Array(actual.buffer), new Uint8Array(expected.buffer)) === 0; - // 'A' - 'F' - } else if (c >= 17 && c <= 22) { - r |= c - 17 + 0xa; + // 7.5 For all other Object pairs, including Array objects, equivalence is + // determined by having the same number of owned properties (as verified + // with Object.prototype.hasOwnProperty.call), the same set of keys + // (although not necessarily the same order), equivalent values for every + // corresponding key, and an identical 'prototype' property. Note: this + // accounts for both named and indexed properties on Arrays. + } else if (isBuffer(actual) !== isBuffer(expected)) { + return false; + } else { + memos = memos || { actual: [], expected: [] }; - // '0' - '9' - } else { - r |= c & 0xf; + var actualIndex = memos.actual.indexOf(actual); + if (actualIndex !== -1) { + if (actualIndex === memos.expected.indexOf(expected)) { + return true; } } - return r; - } - - BN.prototype._parseHex = function _parseHex(number, start) { - // Create possibly bigger array to ensure that it fits the number - this.length = Math.ceil((number.length - start) / 6); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - this.words[i] = 0; - } - var j, w; - // Scan 24-bit chunks and add them to the number - var off = 0; - for (i = number.length - 6, j = 0; i >= start; i -= 6) { - w = parseHex(number, i, i + 6); - this.words[j] |= w << off & 0x3ffffff; - // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb - this.words[j + 1] |= w >>> 26 - off & 0x3fffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - if (i + 6 !== start) { - w = parseHex(number, start, i + 6); - this.words[j] |= w << off & 0x3ffffff; - this.words[j + 1] |= w >>> 26 - off & 0x3fffff; - } - this.strip(); - }; + memos.actual.push(actual); + memos.expected.push(expected); - function parseBase(str, start, end, mul) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; + return objEquiv(actual, expected, strict, memos); + } +} - r *= mul; +function isArguments(object) { + return Object.prototype.toString.call(object) == '[object Arguments]'; +} - // 'a' - if (c >= 49) { - r += c - 49 + 0xa; +function objEquiv(a, b, strict, actualVisitedObjects) { + if (a === null || a === undefined || b === null || b === undefined) return false; + // if one is a primitive, the other must be same + if (util.isPrimitive(a) || util.isPrimitive(b)) return a === b; + if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; + var aIsArgs = isArguments(a); + var bIsArgs = isArguments(b); + if (aIsArgs && !bIsArgs || !aIsArgs && bIsArgs) return false; + if (aIsArgs) { + a = pSlice.call(a); + b = pSlice.call(b); + return _deepEqual(a, b, strict); + } + var ka = objectKeys(a); + var kb = objectKeys(b); + var key, i; + // having the same number of owned properties (keys incorporates + // hasOwnProperty) + if (ka.length !== kb.length) return false; + //the same set of keys (although not necessarily the same order), + ka.sort(); + kb.sort(); + //~~~cheap key test + for (i = ka.length - 1; i >= 0; i--) { + if (ka[i] !== kb[i]) return false; + } + //equivalent values for every corresponding key, and + //~~~possibly expensive deep test + for (i = ka.length - 1; i >= 0; i--) { + key = ka[i]; + if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) return false; + } + return true; +} - // 'A' - } else if (c >= 17) { - r += c - 17 + 0xa; +// 8. The non-equivalence assertion tests for any deep inequality. +// assert.notDeepEqual(actual, expected, message_opt); - // '0' - '9' - } else { - r += c; - } - } - return r; +assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); } +}; - BN.prototype._parseBase = function _parseBase(number, base, start) { - // Initialize as zero - this.words = [0]; - this.length = 1; - - // Find length of limb in base - for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { - limbLen++; - } - limbLen--; - limbPow = limbPow / base | 0; +assert.notDeepStrictEqual = notDeepStrictEqual; +function notDeepStrictEqual(actual, expected, message) { + if (_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); + } +} - var total = number.length - start; - var mod = total % limbLen; - var end = Math.min(total, total - mod) + start; +// 9. The strict equality assertion tests strict equality, as determined by ===. +// assert.strictEqual(actual, expected, message_opt); - var word = 0; - for (var i = start; i < end; i += limbLen) { - word = parseBase(number, i, i + limbLen, base); +assert.strictEqual = function strictEqual(actual, expected, message) { + if (actual !== expected) { + fail(actual, expected, message, '===', assert.strictEqual); + } +}; - this.imuln(limbPow); - if (this.words[0] + word < 0x4000000) { - this.words[0] += word; - } else { - this._iaddn(word); - } - } +// 10. The strict non-equality assertion tests for strict inequality, as +// determined by !==. assert.notStrictEqual(actual, expected, message_opt); - if (mod !== 0) { - var pow = 1; - word = parseBase(number, i, number.length, base); +assert.notStrictEqual = function notStrictEqual(actual, expected, message) { + if (actual === expected) { + fail(actual, expected, message, '!==', assert.notStrictEqual); + } +}; - for (i = 0; i < mod; i++) { - pow *= base; - } +function expectedException(actual, expected) { + if (!actual || !expected) { + return false; + } - this.imuln(pow); - if (this.words[0] + word < 0x4000000) { - this.words[0] += word; - } else { - this._iaddn(word); - } - } - }; + if (Object.prototype.toString.call(expected) == '[object RegExp]') { + return expected.test(actual); + } - BN.prototype.copy = function copy(dest) { - dest.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - dest.words[i] = this.words[i]; + try { + if (actual instanceof expected) { + return true; } - dest.length = this.length; - dest.negative = this.negative; - dest.red = this.red; - }; - - BN.prototype.clone = function clone() { - var r = new BN(null); - this.copy(r); - return r; - }; + } catch (e) { + // Ignore. The instanceof check doesn't work for arrow functions. + } - BN.prototype._expand = function _expand(size) { - while (this.length < size) { - this.words[this.length++] = 0; - } - return this; - }; + if (Error.isPrototypeOf(expected)) { + return false; + } - // Remove leading `0` from `this` - BN.prototype.strip = function strip() { - while (this.length > 1 && this.words[this.length - 1] === 0) { - this.length--; - } - return this._normSign(); - }; + return expected.call({}, actual) === true; +} - BN.prototype._normSign = function _normSign() { - // -0 = 0 - if (this.length === 1 && this.words[0] === 0) { - this.negative = 0; - } - return this; - }; +function _tryBlock(block) { + var error; + try { + block(); + } catch (e) { + error = e; + } + return error; +} - BN.prototype.inspect = function inspect() { - return (this.red ? ''; - }; +function _throws(shouldThrow, block, expected, message) { + var actual; - /* - var zeros = []; - var groupSizes = []; - var groupBases = []; - var s = ''; - var i = -1; - while (++i < BN.wordSize) { - zeros[i] = s; - s += '0'; + if (typeof block !== 'function') { + throw new TypeError('"block" argument must be a function'); } - groupSizes[0] = 0; - groupSizes[1] = 0; - groupBases[0] = 0; - groupBases[1] = 0; - var base = 2 - 1; - while (++base < 36 + 1) { - var groupSize = 0; - var groupBase = 1; - while (groupBase < (1 << BN.wordSize) / base) { - groupBase *= base; - groupSize += 1; - } - groupSizes[base] = groupSize; - groupBases[base] = groupBase; + + if (typeof expected === 'string') { + message = expected; + expected = null; } - */ - var zeros = ['', '0', '00', '000', '0000', '00000', '000000', '0000000', '00000000', '000000000', '0000000000', '00000000000', '000000000000', '0000000000000', '00000000000000', '000000000000000', '0000000000000000', '00000000000000000', '000000000000000000', '0000000000000000000', '00000000000000000000', '000000000000000000000', '0000000000000000000000', '00000000000000000000000', '000000000000000000000000', '0000000000000000000000000']; + actual = _tryBlock(block); - var groupSizes = [0, 0, 25, 16, 12, 11, 10, 9, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]; + message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + (message ? ' ' + message : '.'); - var groupBases = [0, 0, 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176]; + if (shouldThrow && !actual) { + fail(actual, expected, 'Missing expected exception' + message); + } - BN.prototype.toString = function toString(base, padding) { - base = base || 10; - padding = padding | 0 || 1; + var userProvidedMessage = typeof message === 'string'; + var isUnwantedException = !shouldThrow && util.isError(actual); + var isUnexpectedException = !shouldThrow && actual && !expected; - var out; - if (base === 16 || base === 'hex') { - out = ''; - var off = 0; - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = this.words[i]; - var word = ((w << off | carry) & 0xffffff).toString(16); - carry = w >>> 24 - off & 0xffffff; - if (carry !== 0 || i !== this.length - 1) { - out = zeros[6 - word.length] + word + out; - } else { - out = word + out; - } - off += 2; - if (off >= 26) { - off -= 26; - i--; - } - } - if (carry !== 0) { - out = carry.toString(16) + out; - } - while (out.length % padding !== 0) { - out = '0' + out; - } - if (this.negative !== 0) { - out = '-' + out; - } - return out; - } + if (isUnwantedException && userProvidedMessage && expectedException(actual, expected) || isUnexpectedException) { + fail(actual, expected, 'Got unwanted exception' + message); + } - if (base === (base | 0) && base >= 2 && base <= 36) { - // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); - var groupSize = groupSizes[base]; - // var groupBase = Math.pow(base, groupSize); - var groupBase = groupBases[base]; - out = ''; - var c = this.clone(); - c.negative = 0; - while (!c.isZero()) { - var r = c.modn(groupBase).toString(base); - c = c.idivn(groupBase); + if (shouldThrow && actual && expected && !expectedException(actual, expected) || !shouldThrow && actual) { + throw actual; + } +} - if (!c.isZero()) { - out = zeros[groupSize - r.length] + r + out; - } else { - out = r + out; - } - } - if (this.isZero()) { - out = '0' + out; - } - while (out.length % padding !== 0) { - out = '0' + out; - } - if (this.negative !== 0) { - out = '-' + out; - } - return out; - } +// 11. Expected to throw an error: +// assert.throws(block, Error_opt, message_opt); - assert(false, 'Base should be between 2 and 36'); - }; +assert.throws = function (block, /*optional*/error, /*optional*/message) { + _throws(true, block, error, message); +}; - BN.prototype.toNumber = function toNumber() { - var ret = this.words[0]; - if (this.length === 2) { - ret += this.words[1] * 0x4000000; - } else if (this.length === 3 && this.words[2] === 0x01) { - // NOTE: at this stage it is known that the top bit is set - ret += 0x10000000000000 + this.words[1] * 0x4000000; - } else if (this.length > 2) { - assert(false, 'Number can only safely store up to 53 bits'); - } - return this.negative !== 0 ? -ret : ret; - }; +// EXTENSION! This is annoying to write outside this module. +assert.doesNotThrow = function (block, /*optional*/error, /*optional*/message) { + _throws(false, block, error, message); +}; - BN.prototype.toJSON = function toJSON() { - return this.toString(16); - }; +assert.ifError = function (err) { + if (err) throw err; +}; - BN.prototype.toBuffer = function toBuffer(endian, length) { - assert(typeof Buffer !== 'undefined'); - return this.toArrayLike(Buffer, endian, length); - }; +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + if (hasOwn.call(obj, key)) keys.push(key); + } + return keys; +}; - BN.prototype.toArray = function toArray(endian, length) { - return this.toArrayLike(Array, endian, length); - }; +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"util/":356}],22:[function(require,module,exports){ +'use strict'; - BN.prototype.toArrayLike = function toArrayLike(ArrayType, endian, length) { - var byteLength = this.byteLength(); - var reqLength = length || Math.max(1, byteLength); - assert(byteLength <= reqLength, 'byte array longer than desired length'); - assert(reqLength > 0, 'Requested array length <= 0'); +exports.byteLength = byteLength; +exports.toByteArray = toByteArray; +exports.fromByteArray = fromByteArray; - this.strip(); - var littleEndian = endian === 'le'; - var res = new ArrayType(reqLength); +var lookup = []; +var revLookup = []; +var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array; - var b, i; - var q = this.clone(); - if (!littleEndian) { - // Assume big-endian - for (i = 0; i < reqLength - byteLength; i++) { - res[i] = 0; - } +var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; +for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i]; + revLookup[code.charCodeAt(i)] = i; +} - for (i = 0; !q.isZero(); i++) { - b = q.andln(0xff); - q.iushrn(8); +revLookup['-'.charCodeAt(0)] = 62; +revLookup['_'.charCodeAt(0)] = 63; - res[reqLength - i - 1] = b; - } - } else { - for (i = 0; !q.isZero(); i++) { - b = q.andln(0xff); - q.iushrn(8); +function placeHoldersCount(b64) { + var len = b64.length; + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4'); + } - res[i] = b; - } + // the number of equal signs (place holders) + // if there are two placeholders, than the two characters before it + // represent one byte + // if there is only one, then the three characters before it represent 2 bytes + // this is just a cheap hack to not do indexOf twice + return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0; +} - for (; i < reqLength; i++) { - res[i] = 0; - } - } +function byteLength(b64) { + // base64 is 4/3 + up to two characters of the original data + return b64.length * 3 / 4 - placeHoldersCount(b64); +} - return res; - }; +function toByteArray(b64) { + var i, l, tmp, placeHolders, arr; + var len = b64.length; + placeHolders = placeHoldersCount(b64); - if (Math.clz32) { - BN.prototype._countBits = function _countBits(w) { - return 32 - Math.clz32(w); - }; - } else { - BN.prototype._countBits = function _countBits(w) { - var t = w; - var r = 0; - if (t >= 0x1000) { - r += 13; - t >>>= 13; - } - if (t >= 0x40) { - r += 7; - t >>>= 7; - } - if (t >= 0x8) { - r += 4; - t >>>= 4; - } - if (t >= 0x02) { - r += 2; - t >>>= 2; - } - return r + t; - }; - } + arr = new Arr(len * 3 / 4 - placeHolders); - BN.prototype._zeroBits = function _zeroBits(w) { - // Short-cut - if (w === 0) return 26; + // if there are placeholders, only get up to the last complete 4 chars + l = placeHolders > 0 ? len - 4 : len; - var t = w; - var r = 0; - if ((t & 0x1fff) === 0) { - r += 13; - t >>>= 13; - } - if ((t & 0x7f) === 0) { - r += 7; - t >>>= 7; - } - if ((t & 0xf) === 0) { - r += 4; - t >>>= 4; - } - if ((t & 0x3) === 0) { - r += 2; - t >>>= 2; - } - if ((t & 0x1) === 0) { - r++; - } - return r; - }; + var L = 0; - // Return number of used bits in a BN - BN.prototype.bitLength = function bitLength() { - var w = this.words[this.length - 1]; - var hi = this._countBits(w); - return (this.length - 1) * 26 + hi; - }; + for (i = 0; i < l; i += 4) { + tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)]; + arr[L++] = tmp >> 16 & 0xFF; + arr[L++] = tmp >> 8 & 0xFF; + arr[L++] = tmp & 0xFF; + } - function toBitArray(num) { - var w = new Array(num.bitLength()); + if (placeHolders === 2) { + tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4; + arr[L++] = tmp & 0xFF; + } else if (placeHolders === 1) { + tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2; + arr[L++] = tmp >> 8 & 0xFF; + arr[L++] = tmp & 0xFF; + } - for (var bit = 0; bit < w.length; bit++) { - var off = bit / 26 | 0; - var wbit = bit % 26; + return arr; +} - w[bit] = (num.words[off] & 1 << wbit) >>> wbit; - } +function tripletToBase64(num) { + return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; +} - return w; +function encodeChunk(uint8, start, end) { + var tmp; + var output = []; + for (var i = start; i < end; i += 3) { + tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2]; + output.push(tripletToBase64(tmp)); } + return output.join(''); +} - // Number of trailing zero bits - BN.prototype.zeroBits = function zeroBits() { - if (this.isZero()) return 0; +function fromByteArray(uint8) { + var tmp; + var len = uint8.length; + var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes + var output = ''; + var parts = []; + var maxChunkLength = 16383; // must be multiple of 3 - var r = 0; - for (var i = 0; i < this.length; i++) { - var b = this._zeroBits(this.words[i]); - r += b; - if (b !== 26) break; - } - return r; - }; + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength)); + } - BN.prototype.byteLength = function byteLength() { - return Math.ceil(this.bitLength() / 8); - }; + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1]; + output += lookup[tmp >> 2]; + output += lookup[tmp << 4 & 0x3F]; + output += '=='; + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + uint8[len - 1]; + output += lookup[tmp >> 10]; + output += lookup[tmp >> 4 & 0x3F]; + output += lookup[tmp << 2 & 0x3F]; + output += '='; + } - BN.prototype.toTwos = function toTwos(width) { - if (this.negative !== 0) { - return this.abs().inotn(width).iaddn(1); - } - return this.clone(); - }; + parts.push(output); - BN.prototype.fromTwos = function fromTwos(width) { - if (this.testn(width - 1)) { - return this.notn(width).iaddn(1).ineg(); - } - return this.clone(); - }; + return parts.join(''); +} - BN.prototype.isNeg = function isNeg() { - return this.negative !== 0; - }; +},{}],23:[function(require,module,exports){ +'use strict'; - // Return negative clone of `this` - BN.prototype.neg = function neg() { - return this.clone().ineg(); - }; +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; - BN.prototype.ineg = function ineg() { - if (!this.isZero()) { - this.negative ^= 1; - } +/*! bignumber.js v4.0.2 https://github.com/MikeMcl/bignumber.js/LICENCE */ - return this; - }; +;(function (globalObj) { + 'use strict'; - // Or `num` with `this` in-place - BN.prototype.iuor = function iuor(num) { - while (this.length < num.length) { - this.words[this.length++] = 0; - } + /* + bignumber.js v4.0.2 + A JavaScript library for arbitrary-precision arithmetic. + https://github.com/MikeMcl/bignumber.js + Copyright (c) 2017 Michael Mclaughlin + MIT Expat Licence + */ - for (var i = 0; i < num.length; i++) { - this.words[i] = this.words[i] | num.words[i]; - } + var BigNumber, + isNumeric = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, + mathceil = Math.ceil, + mathfloor = Math.floor, + notBool = ' not a boolean or binary digit', + roundingMode = 'rounding mode', + tooManyDigits = 'number type has more than 15 significant digits', + ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_', + BASE = 1e14, + LOG_BASE = 14, + MAX_SAFE_INTEGER = 0x1fffffffffffff, + // 2^53 - 1 + // MAX_INT32 = 0x7fffffff, // 2^31 - 1 + POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], + SQRT_BASE = 1e7, - return this.strip(); - }; - BN.prototype.ior = function ior(num) { - assert((this.negative | num.negative) === 0); - return this.iuor(num); - }; + /* + * The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and + * the arguments to toExponential, toFixed, toFormat, and toPrecision, beyond which an + * exception is thrown (if ERRORS is true). + */ + MAX = 1E9; // 0 to MAX_INT32 - // Or `num` with `this` - BN.prototype.or = function or(num) { - if (this.length > num.length) return this.clone().ior(num); - return num.clone().ior(this); - }; - BN.prototype.uor = function uor(num) { - if (this.length > num.length) return this.clone().iuor(num); - return num.clone().iuor(this); - }; + /* + * Create and return a BigNumber constructor. + */ + function constructorFactory(config) { + var div, + parseNumeric, - // And `num` with `this` in-place - BN.prototype.iuand = function iuand(num) { - // b = min-length(num, this) - var b; - if (this.length > num.length) { - b = num; - } else { - b = this; - } - for (var i = 0; i < b.length; i++) { - this.words[i] = this.words[i] & num.words[i]; - } + // id tracks the caller function, so its name can be included in error messages. + id = 0, + P = BigNumber.prototype, + ONE = new BigNumber(1), - this.length = b.length; - return this.strip(); - }; + /********************************* EDITABLE DEFAULTS **********************************/ - BN.prototype.iand = function iand(num) { - assert((this.negative | num.negative) === 0); - return this.iuand(num); - }; + /* + * The default values below must be integers within the inclusive ranges stated. + * The values can also be changed at run-time using BigNumber.config. + */ - // And `num` with `this` - BN.prototype.and = function and(num) { - if (this.length > num.length) return this.clone().iand(num); - return num.clone().iand(this); - }; + // The maximum number of decimal places for operations involving division. + DECIMAL_PLACES = 20, + // 0 to MAX - BN.prototype.uand = function uand(num) { - if (this.length > num.length) return this.clone().iuand(num); - return num.clone().iuand(this); - }; + /* + * The rounding mode used when rounding to the above decimal places, and when using + * toExponential, toFixed, toFormat and toPrecision, and round (default value). + * UP 0 Away from zero. + * DOWN 1 Towards zero. + * CEIL 2 Towards +Infinity. + * FLOOR 3 Towards -Infinity. + * HALF_UP 4 Towards nearest neighbour. If equidistant, up. + * HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. + * HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. + * HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. + * HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. + */ + ROUNDING_MODE = 4, + // 0 to 8 - // Xor `num` with `this` in-place - BN.prototype.iuxor = function iuxor(num) { - // a.length > b.length - var a; - var b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } + // EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS] - for (var i = 0; i < b.length; i++) { - this.words[i] = a.words[i] ^ b.words[i]; - } + // The exponent value at and beneath which toString returns exponential notation. + // Number type: -7 + TO_EXP_NEG = -7, + // 0 to -MAX - if (this !== a) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } + // The exponent value at and above which toString returns exponential notation. + // Number type: 21 + TO_EXP_POS = 21, + // 0 to MAX - this.length = a.length; + // RANGE : [MIN_EXP, MAX_EXP] - return this.strip(); - }; + // The minimum exponent value, beneath which underflow to zero occurs. + // Number type: -324 (5e-324) + MIN_EXP = -1e7, + // -1 to -MAX - BN.prototype.ixor = function ixor(num) { - assert((this.negative | num.negative) === 0); - return this.iuxor(num); - }; + // The maximum exponent value, above which overflow to Infinity occurs. + // Number type: 308 (1.7976931348623157e+308) + // For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow. + MAX_EXP = 1e7, + // 1 to MAX - // Xor `num` with `this` - BN.prototype.xor = function xor(num) { - if (this.length > num.length) return this.clone().ixor(num); - return num.clone().ixor(this); - }; + // Whether BigNumber Errors are ever thrown. + ERRORS = true, + // true or false - BN.prototype.uxor = function uxor(num) { - if (this.length > num.length) return this.clone().iuxor(num); - return num.clone().iuxor(this); - }; + // Change to intValidatorNoErrors if ERRORS is false. + isValidInt = intValidatorWithErrors, + // intValidatorWithErrors/intValidatorNoErrors - // Not ``this`` with ``width`` bitwidth - BN.prototype.inotn = function inotn(width) { - assert(typeof width === 'number' && width >= 0); + // Whether to use cryptographically-secure random number generation, if available. + CRYPTO = false, + // true or false - var bytesNeeded = Math.ceil(width / 26) | 0; - var bitsLeft = width % 26; + /* + * The modulo mode used when calculating the modulus: a mod n. + * The quotient (q = a / n) is calculated according to the corresponding rounding mode. + * The remainder (r) is calculated as: r = a - n * q. + * + * UP 0 The remainder is positive if the dividend is negative, else is negative. + * DOWN 1 The remainder has the same sign as the dividend. + * This modulo mode is commonly known as 'truncated division' and is + * equivalent to (a % n) in JavaScript. + * FLOOR 3 The remainder has the same sign as the divisor (Python %). + * HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function. + * EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). + * The remainder is always positive. + * + * The truncated division, floored division, Euclidian division and IEEE 754 remainder + * modes are commonly used for the modulus operation. + * Although the other rounding modes can also be used, they may not give useful results. + */ + MODULO_MODE = 1, + // 0 to 9 - // Extend the buffer with leading zeroes - this._expand(bytesNeeded); + // The maximum number of significant digits of the result of the toPower operation. + // If POW_PRECISION is 0, there will be unlimited significant digits. + POW_PRECISION = 0, + // 0 to MAX - if (bitsLeft > 0) { - bytesNeeded--; - } + // The format specification used by the BigNumber.prototype.toFormat method. + FORMAT = { + decimalSeparator: '.', + groupSeparator: ',', + groupSize: 3, + secondaryGroupSize: 0, + fractionGroupSeparator: '\xA0', // non-breaking space + fractionGroupSize: 0 + }; - // Handle complete words - for (var i = 0; i < bytesNeeded; i++) { - this.words[i] = ~this.words[i] & 0x3ffffff; - } + /******************************************************************************************/ - // Handle the residue - if (bitsLeft > 0) { - this.words[i] = ~this.words[i] & 0x3ffffff >> 26 - bitsLeft; - } + // CONSTRUCTOR - // And remove leading zeroes - return this.strip(); - }; - BN.prototype.notn = function notn(width) { - return this.clone().inotn(width); - }; + /* + * The BigNumber constructor and exported function. + * Create and return a new instance of a BigNumber object. + * + * n {number|string|BigNumber} A numeric value. + * [b] {number} The base of n. Integer, 2 to 64 inclusive. + */ + function BigNumber(n, b) { + var c, + e, + i, + num, + len, + str, + x = this; - // Set `bit` of `this` - BN.prototype.setn = function setn(bit, val) { - assert(typeof bit === 'number' && bit >= 0); + // Enable constructor usage without new. + if (!(x instanceof BigNumber)) { - var off = bit / 26 | 0; - var wbit = bit % 26; + // 'BigNumber() constructor call without new: {n}' + if (ERRORS) raise(26, 'constructor call without new', n); + return new BigNumber(n, b); + } - this._expand(off + 1); + // 'new BigNumber() base not an integer: {b}' + // 'new BigNumber() base out of range: {b}' + if (b == null || !isValidInt(b, 2, 64, id, 'base')) { - if (val) { - this.words[off] = this.words[off] | 1 << wbit; - } else { - this.words[off] = this.words[off] & ~(1 << wbit); - } + // Duplicate. + if (n instanceof BigNumber) { + x.s = n.s; + x.e = n.e; + x.c = (n = n.c) ? n.slice() : n; + id = 0; + return; + } - return this.strip(); - }; + if ((num = typeof n == 'number') && n * 0 == 0) { + x.s = 1 / n < 0 ? (n = -n, -1) : 1; - // Add `num` to `this` in-place - BN.prototype.iadd = function iadd(num) { - var r; + // Fast path for integers. + if (n === ~~n) { + for (e = 0, i = n; i >= 10; i /= 10, e++) {} + x.e = e; + x.c = [n]; + id = 0; + return; + } - // negative + positive - if (this.negative !== 0 && num.negative === 0) { - this.negative = 0; - r = this.isub(num); - this.negative ^= 1; - return this._normSign(); + str = n + ''; + } else { + if (!isNumeric.test(str = n + '')) return parseNumeric(x, str, num); + x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1; + } + } else { + b = b | 0; + str = n + ''; - // positive + negative - } else if (this.negative === 0 && num.negative !== 0) { - num.negative = 0; - r = this.isub(num); - num.negative = 1; - return r._normSign(); - } + // Ensure return value is rounded to DECIMAL_PLACES as with other bases. + // Allow exponential notation to be used with base 10 argument. + if (b == 10) { + x = new BigNumber(n instanceof BigNumber ? n : str); + return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE); + } - // a.length > b.length - var a, b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } + // Avoid potential interpretation of Infinity and NaN as base 44+ values. + // Any number in exponential form will fail due to the [Ee][+-]. + if ((num = typeof n == 'number') && n * 0 != 0 || !new RegExp('^-?' + (c = '[' + ALPHABET.slice(0, b) + ']+') + '(?:\\.' + c + ')?$', b < 37 ? 'i' : '').test(str)) { + return parseNumeric(x, str, num, b); + } - var carry = 0; - for (var i = 0; i < b.length; i++) { - r = (a.words[i] | 0) + (b.words[i] | 0) + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } - for (; carry !== 0 && i < a.length; i++) { - r = (a.words[i] | 0) + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } + if (num) { + x.s = 1 / n < 0 ? (str = str.slice(1), -1) : 1; - this.length = a.length; - if (carry !== 0) { - this.words[this.length] = carry; - this.length++; - // Copy the rest of the words - } else if (a !== this) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } + if (ERRORS && str.replace(/^0\.0*|\./, '').length > 15) { - return this; - }; + // 'new BigNumber() number type has more than 15 significant digits: {n}' + raise(id, tooManyDigits, n); + } - // Add `num` to `this` - BN.prototype.add = function add(num) { - var res; - if (num.negative !== 0 && this.negative === 0) { - num.negative = 0; - res = this.sub(num); - num.negative ^= 1; - return res; - } else if (num.negative === 0 && this.negative !== 0) { - this.negative = 0; - res = num.sub(this); - this.negative = 1; - return res; - } + // Prevent later check for length on converted number. + num = false; + } else { + x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1; + } - if (this.length > num.length) return this.clone().iadd(num); + str = convertBase(str, 10, b, x.s); + } - return num.clone().iadd(this); - }; + // Decimal point? + if ((e = str.indexOf('.')) > -1) str = str.replace('.', ''); - // Subtract `num` from `this` in-place - BN.prototype.isub = function isub(num) { - // this - (-num) = this + num - if (num.negative !== 0) { - num.negative = 0; - var r = this.iadd(num); - num.negative = 1; - return r._normSign(); + // Exponential form? + if ((i = str.search(/e/i)) > 0) { - // -this - num = -(this + num) - } else if (this.negative !== 0) { - this.negative = 0; - this.iadd(num); - this.negative = 1; - return this._normSign(); - } + // Determine exponent. + if (e < 0) e = i; + e += +str.slice(i + 1); + str = str.substring(0, i); + } else if (e < 0) { - // At this point both numbers are positive - var cmp = this.cmp(num); + // Integer. + e = str.length; + } - // Optimization - zeroify - if (cmp === 0) { - this.negative = 0; - this.length = 1; - this.words[0] = 0; - return this; - } + // Determine leading zeros. + for (i = 0; str.charCodeAt(i) === 48; i++) {} - // a > b - var a, b; - if (cmp > 0) { - a = this; - b = num; - } else { - a = num; - b = this; - } + // Determine trailing zeros. + for (len = str.length; str.charCodeAt(--len) === 48;) {} + str = str.slice(i, len + 1); - var carry = 0; - for (var i = 0; i < b.length; i++) { - r = (a.words[i] | 0) - (b.words[i] | 0) + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } - for (; carry !== 0 && i < a.length; i++) { - r = (a.words[i] | 0) + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } + if (str) { + len = str.length; - // Copy rest of the words - if (carry === 0 && i < a.length && a !== this) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } + // Disallow numbers with over 15 significant digits if number type. + // 'new BigNumber() number type has more than 15 significant digits: {n}' + if (num && ERRORS && len > 15 && (n > MAX_SAFE_INTEGER || n !== mathfloor(n))) { + raise(id, tooManyDigits, x.s * n); + } - this.length = Math.max(this.length, i); + e = e - i - 1; - if (a !== this) { - this.negative = 1; - } + // Overflow? + if (e > MAX_EXP) { - return this.strip(); - }; + // Infinity. + x.c = x.e = null; - // Subtract `num` from `this` - BN.prototype.sub = function sub(num) { - return this.clone().isub(num); - }; + // Underflow? + } else if (e < MIN_EXP) { - function smallMulTo(self, num, out) { - out.negative = num.negative ^ self.negative; - var len = self.length + num.length | 0; - out.length = len; - len = len - 1 | 0; + // Zero. + x.c = [x.e = 0]; + } else { + x.e = e; + x.c = []; - // Peel one iteration (compiler can't do it, because of code complexity) - var a = self.words[0] | 0; - var b = num.words[0] | 0; - var r = a * b; + // Transform base - var lo = r & 0x3ffffff; - var carry = r / 0x4000000 | 0; - out.words[0] = lo; + // e is the base 10 exponent. + // i is where to slice str to get the first element of the coefficient array. + i = (e + 1) % LOG_BASE; + if (e < 0) i += LOG_BASE; - for (var k = 1; k < len; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = carry >>> 26; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { - var i = k - j | 0; - a = self.words[i] | 0; - b = num.words[j] | 0; - r = a * b + rword; - ncarry += r / 0x4000000 | 0; - rword = r & 0x3ffffff; - } - out.words[k] = rword | 0; - carry = ncarry | 0; - } - if (carry !== 0) { - out.words[k] = carry | 0; - } else { - out.length--; - } - - return out.strip(); - } - - // TODO(indutny): it may be reasonable to omit it for users who don't need - // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit - // multiplication (like elliptic secp256k1). - var comb10MulTo = function comb10MulTo(self, num, out) { - var a = self.words; - var b = num.words; - var o = out.words; - var c = 0; - var lo; - var mid; - var hi; - var a0 = a[0] | 0; - var al0 = a0 & 0x1fff; - var ah0 = a0 >>> 13; - var a1 = a[1] | 0; - var al1 = a1 & 0x1fff; - var ah1 = a1 >>> 13; - var a2 = a[2] | 0; - var al2 = a2 & 0x1fff; - var ah2 = a2 >>> 13; - var a3 = a[3] | 0; - var al3 = a3 & 0x1fff; - var ah3 = a3 >>> 13; - var a4 = a[4] | 0; - var al4 = a4 & 0x1fff; - var ah4 = a4 >>> 13; - var a5 = a[5] | 0; - var al5 = a5 & 0x1fff; - var ah5 = a5 >>> 13; - var a6 = a[6] | 0; - var al6 = a6 & 0x1fff; - var ah6 = a6 >>> 13; - var a7 = a[7] | 0; - var al7 = a7 & 0x1fff; - var ah7 = a7 >>> 13; - var a8 = a[8] | 0; - var al8 = a8 & 0x1fff; - var ah8 = a8 >>> 13; - var a9 = a[9] | 0; - var al9 = a9 & 0x1fff; - var ah9 = a9 >>> 13; - var b0 = b[0] | 0; - var bl0 = b0 & 0x1fff; - var bh0 = b0 >>> 13; - var b1 = b[1] | 0; - var bl1 = b1 & 0x1fff; - var bh1 = b1 >>> 13; - var b2 = b[2] | 0; - var bl2 = b2 & 0x1fff; - var bh2 = b2 >>> 13; - var b3 = b[3] | 0; - var bl3 = b3 & 0x1fff; - var bh3 = b3 >>> 13; - var b4 = b[4] | 0; - var bl4 = b4 & 0x1fff; - var bh4 = b4 >>> 13; - var b5 = b[5] | 0; - var bl5 = b5 & 0x1fff; - var bh5 = b5 >>> 13; - var b6 = b[6] | 0; - var bl6 = b6 & 0x1fff; - var bh6 = b6 >>> 13; - var b7 = b[7] | 0; - var bl7 = b7 & 0x1fff; - var bh7 = b7 >>> 13; - var b8 = b[8] | 0; - var bl8 = b8 & 0x1fff; - var bh8 = b8 >>> 13; - var b9 = b[9] | 0; - var bl9 = b9 & 0x1fff; - var bh9 = b9 >>> 13; - - out.negative = self.negative ^ num.negative; - out.length = 19; - /* k = 0 */ - lo = Math.imul(al0, bl0); - mid = Math.imul(al0, bh0); - mid = mid + Math.imul(ah0, bl0) | 0; - hi = Math.imul(ah0, bh0); - var w0 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w0 >>> 26) | 0; - w0 &= 0x3ffffff; - /* k = 1 */ - lo = Math.imul(al1, bl0); - mid = Math.imul(al1, bh0); - mid = mid + Math.imul(ah1, bl0) | 0; - hi = Math.imul(ah1, bh0); - lo = lo + Math.imul(al0, bl1) | 0; - mid = mid + Math.imul(al0, bh1) | 0; - mid = mid + Math.imul(ah0, bl1) | 0; - hi = hi + Math.imul(ah0, bh1) | 0; - var w1 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w1 >>> 26) | 0; - w1 &= 0x3ffffff; - /* k = 2 */ - lo = Math.imul(al2, bl0); - mid = Math.imul(al2, bh0); - mid = mid + Math.imul(ah2, bl0) | 0; - hi = Math.imul(ah2, bh0); - lo = lo + Math.imul(al1, bl1) | 0; - mid = mid + Math.imul(al1, bh1) | 0; - mid = mid + Math.imul(ah1, bl1) | 0; - hi = hi + Math.imul(ah1, bh1) | 0; - lo = lo + Math.imul(al0, bl2) | 0; - mid = mid + Math.imul(al0, bh2) | 0; - mid = mid + Math.imul(ah0, bl2) | 0; - hi = hi + Math.imul(ah0, bh2) | 0; - var w2 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w2 >>> 26) | 0; - w2 &= 0x3ffffff; - /* k = 3 */ - lo = Math.imul(al3, bl0); - mid = Math.imul(al3, bh0); - mid = mid + Math.imul(ah3, bl0) | 0; - hi = Math.imul(ah3, bh0); - lo = lo + Math.imul(al2, bl1) | 0; - mid = mid + Math.imul(al2, bh1) | 0; - mid = mid + Math.imul(ah2, bl1) | 0; - hi = hi + Math.imul(ah2, bh1) | 0; - lo = lo + Math.imul(al1, bl2) | 0; - mid = mid + Math.imul(al1, bh2) | 0; - mid = mid + Math.imul(ah1, bl2) | 0; - hi = hi + Math.imul(ah1, bh2) | 0; - lo = lo + Math.imul(al0, bl3) | 0; - mid = mid + Math.imul(al0, bh3) | 0; - mid = mid + Math.imul(ah0, bl3) | 0; - hi = hi + Math.imul(ah0, bh3) | 0; - var w3 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w3 >>> 26) | 0; - w3 &= 0x3ffffff; - /* k = 4 */ - lo = Math.imul(al4, bl0); - mid = Math.imul(al4, bh0); - mid = mid + Math.imul(ah4, bl0) | 0; - hi = Math.imul(ah4, bh0); - lo = lo + Math.imul(al3, bl1) | 0; - mid = mid + Math.imul(al3, bh1) | 0; - mid = mid + Math.imul(ah3, bl1) | 0; - hi = hi + Math.imul(ah3, bh1) | 0; - lo = lo + Math.imul(al2, bl2) | 0; - mid = mid + Math.imul(al2, bh2) | 0; - mid = mid + Math.imul(ah2, bl2) | 0; - hi = hi + Math.imul(ah2, bh2) | 0; - lo = lo + Math.imul(al1, bl3) | 0; - mid = mid + Math.imul(al1, bh3) | 0; - mid = mid + Math.imul(ah1, bl3) | 0; - hi = hi + Math.imul(ah1, bh3) | 0; - lo = lo + Math.imul(al0, bl4) | 0; - mid = mid + Math.imul(al0, bh4) | 0; - mid = mid + Math.imul(ah0, bl4) | 0; - hi = hi + Math.imul(ah0, bh4) | 0; - var w4 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w4 >>> 26) | 0; - w4 &= 0x3ffffff; - /* k = 5 */ - lo = Math.imul(al5, bl0); - mid = Math.imul(al5, bh0); - mid = mid + Math.imul(ah5, bl0) | 0; - hi = Math.imul(ah5, bh0); - lo = lo + Math.imul(al4, bl1) | 0; - mid = mid + Math.imul(al4, bh1) | 0; - mid = mid + Math.imul(ah4, bl1) | 0; - hi = hi + Math.imul(ah4, bh1) | 0; - lo = lo + Math.imul(al3, bl2) | 0; - mid = mid + Math.imul(al3, bh2) | 0; - mid = mid + Math.imul(ah3, bl2) | 0; - hi = hi + Math.imul(ah3, bh2) | 0; - lo = lo + Math.imul(al2, bl3) | 0; - mid = mid + Math.imul(al2, bh3) | 0; - mid = mid + Math.imul(ah2, bl3) | 0; - hi = hi + Math.imul(ah2, bh3) | 0; - lo = lo + Math.imul(al1, bl4) | 0; - mid = mid + Math.imul(al1, bh4) | 0; - mid = mid + Math.imul(ah1, bl4) | 0; - hi = hi + Math.imul(ah1, bh4) | 0; - lo = lo + Math.imul(al0, bl5) | 0; - mid = mid + Math.imul(al0, bh5) | 0; - mid = mid + Math.imul(ah0, bl5) | 0; - hi = hi + Math.imul(ah0, bh5) | 0; - var w5 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w5 >>> 26) | 0; - w5 &= 0x3ffffff; - /* k = 6 */ - lo = Math.imul(al6, bl0); - mid = Math.imul(al6, bh0); - mid = mid + Math.imul(ah6, bl0) | 0; - hi = Math.imul(ah6, bh0); - lo = lo + Math.imul(al5, bl1) | 0; - mid = mid + Math.imul(al5, bh1) | 0; - mid = mid + Math.imul(ah5, bl1) | 0; - hi = hi + Math.imul(ah5, bh1) | 0; - lo = lo + Math.imul(al4, bl2) | 0; - mid = mid + Math.imul(al4, bh2) | 0; - mid = mid + Math.imul(ah4, bl2) | 0; - hi = hi + Math.imul(ah4, bh2) | 0; - lo = lo + Math.imul(al3, bl3) | 0; - mid = mid + Math.imul(al3, bh3) | 0; - mid = mid + Math.imul(ah3, bl3) | 0; - hi = hi + Math.imul(ah3, bh3) | 0; - lo = lo + Math.imul(al2, bl4) | 0; - mid = mid + Math.imul(al2, bh4) | 0; - mid = mid + Math.imul(ah2, bl4) | 0; - hi = hi + Math.imul(ah2, bh4) | 0; - lo = lo + Math.imul(al1, bl5) | 0; - mid = mid + Math.imul(al1, bh5) | 0; - mid = mid + Math.imul(ah1, bl5) | 0; - hi = hi + Math.imul(ah1, bh5) | 0; - lo = lo + Math.imul(al0, bl6) | 0; - mid = mid + Math.imul(al0, bh6) | 0; - mid = mid + Math.imul(ah0, bl6) | 0; - hi = hi + Math.imul(ah0, bh6) | 0; - var w6 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w6 >>> 26) | 0; - w6 &= 0x3ffffff; - /* k = 7 */ - lo = Math.imul(al7, bl0); - mid = Math.imul(al7, bh0); - mid = mid + Math.imul(ah7, bl0) | 0; - hi = Math.imul(ah7, bh0); - lo = lo + Math.imul(al6, bl1) | 0; - mid = mid + Math.imul(al6, bh1) | 0; - mid = mid + Math.imul(ah6, bl1) | 0; - hi = hi + Math.imul(ah6, bh1) | 0; - lo = lo + Math.imul(al5, bl2) | 0; - mid = mid + Math.imul(al5, bh2) | 0; - mid = mid + Math.imul(ah5, bl2) | 0; - hi = hi + Math.imul(ah5, bh2) | 0; - lo = lo + Math.imul(al4, bl3) | 0; - mid = mid + Math.imul(al4, bh3) | 0; - mid = mid + Math.imul(ah4, bl3) | 0; - hi = hi + Math.imul(ah4, bh3) | 0; - lo = lo + Math.imul(al3, bl4) | 0; - mid = mid + Math.imul(al3, bh4) | 0; - mid = mid + Math.imul(ah3, bl4) | 0; - hi = hi + Math.imul(ah3, bh4) | 0; - lo = lo + Math.imul(al2, bl5) | 0; - mid = mid + Math.imul(al2, bh5) | 0; - mid = mid + Math.imul(ah2, bl5) | 0; - hi = hi + Math.imul(ah2, bh5) | 0; - lo = lo + Math.imul(al1, bl6) | 0; - mid = mid + Math.imul(al1, bh6) | 0; - mid = mid + Math.imul(ah1, bl6) | 0; - hi = hi + Math.imul(ah1, bh6) | 0; - lo = lo + Math.imul(al0, bl7) | 0; - mid = mid + Math.imul(al0, bh7) | 0; - mid = mid + Math.imul(ah0, bl7) | 0; - hi = hi + Math.imul(ah0, bh7) | 0; - var w7 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w7 >>> 26) | 0; - w7 &= 0x3ffffff; - /* k = 8 */ - lo = Math.imul(al8, bl0); - mid = Math.imul(al8, bh0); - mid = mid + Math.imul(ah8, bl0) | 0; - hi = Math.imul(ah8, bh0); - lo = lo + Math.imul(al7, bl1) | 0; - mid = mid + Math.imul(al7, bh1) | 0; - mid = mid + Math.imul(ah7, bl1) | 0; - hi = hi + Math.imul(ah7, bh1) | 0; - lo = lo + Math.imul(al6, bl2) | 0; - mid = mid + Math.imul(al6, bh2) | 0; - mid = mid + Math.imul(ah6, bl2) | 0; - hi = hi + Math.imul(ah6, bh2) | 0; - lo = lo + Math.imul(al5, bl3) | 0; - mid = mid + Math.imul(al5, bh3) | 0; - mid = mid + Math.imul(ah5, bl3) | 0; - hi = hi + Math.imul(ah5, bh3) | 0; - lo = lo + Math.imul(al4, bl4) | 0; - mid = mid + Math.imul(al4, bh4) | 0; - mid = mid + Math.imul(ah4, bl4) | 0; - hi = hi + Math.imul(ah4, bh4) | 0; - lo = lo + Math.imul(al3, bl5) | 0; - mid = mid + Math.imul(al3, bh5) | 0; - mid = mid + Math.imul(ah3, bl5) | 0; - hi = hi + Math.imul(ah3, bh5) | 0; - lo = lo + Math.imul(al2, bl6) | 0; - mid = mid + Math.imul(al2, bh6) | 0; - mid = mid + Math.imul(ah2, bl6) | 0; - hi = hi + Math.imul(ah2, bh6) | 0; - lo = lo + Math.imul(al1, bl7) | 0; - mid = mid + Math.imul(al1, bh7) | 0; - mid = mid + Math.imul(ah1, bl7) | 0; - hi = hi + Math.imul(ah1, bh7) | 0; - lo = lo + Math.imul(al0, bl8) | 0; - mid = mid + Math.imul(al0, bh8) | 0; - mid = mid + Math.imul(ah0, bl8) | 0; - hi = hi + Math.imul(ah0, bh8) | 0; - var w8 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w8 >>> 26) | 0; - w8 &= 0x3ffffff; - /* k = 9 */ - lo = Math.imul(al9, bl0); - mid = Math.imul(al9, bh0); - mid = mid + Math.imul(ah9, bl0) | 0; - hi = Math.imul(ah9, bh0); - lo = lo + Math.imul(al8, bl1) | 0; - mid = mid + Math.imul(al8, bh1) | 0; - mid = mid + Math.imul(ah8, bl1) | 0; - hi = hi + Math.imul(ah8, bh1) | 0; - lo = lo + Math.imul(al7, bl2) | 0; - mid = mid + Math.imul(al7, bh2) | 0; - mid = mid + Math.imul(ah7, bl2) | 0; - hi = hi + Math.imul(ah7, bh2) | 0; - lo = lo + Math.imul(al6, bl3) | 0; - mid = mid + Math.imul(al6, bh3) | 0; - mid = mid + Math.imul(ah6, bl3) | 0; - hi = hi + Math.imul(ah6, bh3) | 0; - lo = lo + Math.imul(al5, bl4) | 0; - mid = mid + Math.imul(al5, bh4) | 0; - mid = mid + Math.imul(ah5, bl4) | 0; - hi = hi + Math.imul(ah5, bh4) | 0; - lo = lo + Math.imul(al4, bl5) | 0; - mid = mid + Math.imul(al4, bh5) | 0; - mid = mid + Math.imul(ah4, bl5) | 0; - hi = hi + Math.imul(ah4, bh5) | 0; - lo = lo + Math.imul(al3, bl6) | 0; - mid = mid + Math.imul(al3, bh6) | 0; - mid = mid + Math.imul(ah3, bl6) | 0; - hi = hi + Math.imul(ah3, bh6) | 0; - lo = lo + Math.imul(al2, bl7) | 0; - mid = mid + Math.imul(al2, bh7) | 0; - mid = mid + Math.imul(ah2, bl7) | 0; - hi = hi + Math.imul(ah2, bh7) | 0; - lo = lo + Math.imul(al1, bl8) | 0; - mid = mid + Math.imul(al1, bh8) | 0; - mid = mid + Math.imul(ah1, bl8) | 0; - hi = hi + Math.imul(ah1, bh8) | 0; - lo = lo + Math.imul(al0, bl9) | 0; - mid = mid + Math.imul(al0, bh9) | 0; - mid = mid + Math.imul(ah0, bl9) | 0; - hi = hi + Math.imul(ah0, bh9) | 0; - var w9 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w9 >>> 26) | 0; - w9 &= 0x3ffffff; - /* k = 10 */ - lo = Math.imul(al9, bl1); - mid = Math.imul(al9, bh1); - mid = mid + Math.imul(ah9, bl1) | 0; - hi = Math.imul(ah9, bh1); - lo = lo + Math.imul(al8, bl2) | 0; - mid = mid + Math.imul(al8, bh2) | 0; - mid = mid + Math.imul(ah8, bl2) | 0; - hi = hi + Math.imul(ah8, bh2) | 0; - lo = lo + Math.imul(al7, bl3) | 0; - mid = mid + Math.imul(al7, bh3) | 0; - mid = mid + Math.imul(ah7, bl3) | 0; - hi = hi + Math.imul(ah7, bh3) | 0; - lo = lo + Math.imul(al6, bl4) | 0; - mid = mid + Math.imul(al6, bh4) | 0; - mid = mid + Math.imul(ah6, bl4) | 0; - hi = hi + Math.imul(ah6, bh4) | 0; - lo = lo + Math.imul(al5, bl5) | 0; - mid = mid + Math.imul(al5, bh5) | 0; - mid = mid + Math.imul(ah5, bl5) | 0; - hi = hi + Math.imul(ah5, bh5) | 0; - lo = lo + Math.imul(al4, bl6) | 0; - mid = mid + Math.imul(al4, bh6) | 0; - mid = mid + Math.imul(ah4, bl6) | 0; - hi = hi + Math.imul(ah4, bh6) | 0; - lo = lo + Math.imul(al3, bl7) | 0; - mid = mid + Math.imul(al3, bh7) | 0; - mid = mid + Math.imul(ah3, bl7) | 0; - hi = hi + Math.imul(ah3, bh7) | 0; - lo = lo + Math.imul(al2, bl8) | 0; - mid = mid + Math.imul(al2, bh8) | 0; - mid = mid + Math.imul(ah2, bl8) | 0; - hi = hi + Math.imul(ah2, bh8) | 0; - lo = lo + Math.imul(al1, bl9) | 0; - mid = mid + Math.imul(al1, bh9) | 0; - mid = mid + Math.imul(ah1, bl9) | 0; - hi = hi + Math.imul(ah1, bh9) | 0; - var w10 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w10 >>> 26) | 0; - w10 &= 0x3ffffff; - /* k = 11 */ - lo = Math.imul(al9, bl2); - mid = Math.imul(al9, bh2); - mid = mid + Math.imul(ah9, bl2) | 0; - hi = Math.imul(ah9, bh2); - lo = lo + Math.imul(al8, bl3) | 0; - mid = mid + Math.imul(al8, bh3) | 0; - mid = mid + Math.imul(ah8, bl3) | 0; - hi = hi + Math.imul(ah8, bh3) | 0; - lo = lo + Math.imul(al7, bl4) | 0; - mid = mid + Math.imul(al7, bh4) | 0; - mid = mid + Math.imul(ah7, bl4) | 0; - hi = hi + Math.imul(ah7, bh4) | 0; - lo = lo + Math.imul(al6, bl5) | 0; - mid = mid + Math.imul(al6, bh5) | 0; - mid = mid + Math.imul(ah6, bl5) | 0; - hi = hi + Math.imul(ah6, bh5) | 0; - lo = lo + Math.imul(al5, bl6) | 0; - mid = mid + Math.imul(al5, bh6) | 0; - mid = mid + Math.imul(ah5, bl6) | 0; - hi = hi + Math.imul(ah5, bh6) | 0; - lo = lo + Math.imul(al4, bl7) | 0; - mid = mid + Math.imul(al4, bh7) | 0; - mid = mid + Math.imul(ah4, bl7) | 0; - hi = hi + Math.imul(ah4, bh7) | 0; - lo = lo + Math.imul(al3, bl8) | 0; - mid = mid + Math.imul(al3, bh8) | 0; - mid = mid + Math.imul(ah3, bl8) | 0; - hi = hi + Math.imul(ah3, bh8) | 0; - lo = lo + Math.imul(al2, bl9) | 0; - mid = mid + Math.imul(al2, bh9) | 0; - mid = mid + Math.imul(ah2, bl9) | 0; - hi = hi + Math.imul(ah2, bh9) | 0; - var w11 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w11 >>> 26) | 0; - w11 &= 0x3ffffff; - /* k = 12 */ - lo = Math.imul(al9, bl3); - mid = Math.imul(al9, bh3); - mid = mid + Math.imul(ah9, bl3) | 0; - hi = Math.imul(ah9, bh3); - lo = lo + Math.imul(al8, bl4) | 0; - mid = mid + Math.imul(al8, bh4) | 0; - mid = mid + Math.imul(ah8, bl4) | 0; - hi = hi + Math.imul(ah8, bh4) | 0; - lo = lo + Math.imul(al7, bl5) | 0; - mid = mid + Math.imul(al7, bh5) | 0; - mid = mid + Math.imul(ah7, bl5) | 0; - hi = hi + Math.imul(ah7, bh5) | 0; - lo = lo + Math.imul(al6, bl6) | 0; - mid = mid + Math.imul(al6, bh6) | 0; - mid = mid + Math.imul(ah6, bl6) | 0; - hi = hi + Math.imul(ah6, bh6) | 0; - lo = lo + Math.imul(al5, bl7) | 0; - mid = mid + Math.imul(al5, bh7) | 0; - mid = mid + Math.imul(ah5, bl7) | 0; - hi = hi + Math.imul(ah5, bh7) | 0; - lo = lo + Math.imul(al4, bl8) | 0; - mid = mid + Math.imul(al4, bh8) | 0; - mid = mid + Math.imul(ah4, bl8) | 0; - hi = hi + Math.imul(ah4, bh8) | 0; - lo = lo + Math.imul(al3, bl9) | 0; - mid = mid + Math.imul(al3, bh9) | 0; - mid = mid + Math.imul(ah3, bl9) | 0; - hi = hi + Math.imul(ah3, bh9) | 0; - var w12 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w12 >>> 26) | 0; - w12 &= 0x3ffffff; - /* k = 13 */ - lo = Math.imul(al9, bl4); - mid = Math.imul(al9, bh4); - mid = mid + Math.imul(ah9, bl4) | 0; - hi = Math.imul(ah9, bh4); - lo = lo + Math.imul(al8, bl5) | 0; - mid = mid + Math.imul(al8, bh5) | 0; - mid = mid + Math.imul(ah8, bl5) | 0; - hi = hi + Math.imul(ah8, bh5) | 0; - lo = lo + Math.imul(al7, bl6) | 0; - mid = mid + Math.imul(al7, bh6) | 0; - mid = mid + Math.imul(ah7, bl6) | 0; - hi = hi + Math.imul(ah7, bh6) | 0; - lo = lo + Math.imul(al6, bl7) | 0; - mid = mid + Math.imul(al6, bh7) | 0; - mid = mid + Math.imul(ah6, bl7) | 0; - hi = hi + Math.imul(ah6, bh7) | 0; - lo = lo + Math.imul(al5, bl8) | 0; - mid = mid + Math.imul(al5, bh8) | 0; - mid = mid + Math.imul(ah5, bl8) | 0; - hi = hi + Math.imul(ah5, bh8) | 0; - lo = lo + Math.imul(al4, bl9) | 0; - mid = mid + Math.imul(al4, bh9) | 0; - mid = mid + Math.imul(ah4, bl9) | 0; - hi = hi + Math.imul(ah4, bh9) | 0; - var w13 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w13 >>> 26) | 0; - w13 &= 0x3ffffff; - /* k = 14 */ - lo = Math.imul(al9, bl5); - mid = Math.imul(al9, bh5); - mid = mid + Math.imul(ah9, bl5) | 0; - hi = Math.imul(ah9, bh5); - lo = lo + Math.imul(al8, bl6) | 0; - mid = mid + Math.imul(al8, bh6) | 0; - mid = mid + Math.imul(ah8, bl6) | 0; - hi = hi + Math.imul(ah8, bh6) | 0; - lo = lo + Math.imul(al7, bl7) | 0; - mid = mid + Math.imul(al7, bh7) | 0; - mid = mid + Math.imul(ah7, bl7) | 0; - hi = hi + Math.imul(ah7, bh7) | 0; - lo = lo + Math.imul(al6, bl8) | 0; - mid = mid + Math.imul(al6, bh8) | 0; - mid = mid + Math.imul(ah6, bl8) | 0; - hi = hi + Math.imul(ah6, bh8) | 0; - lo = lo + Math.imul(al5, bl9) | 0; - mid = mid + Math.imul(al5, bh9) | 0; - mid = mid + Math.imul(ah5, bl9) | 0; - hi = hi + Math.imul(ah5, bh9) | 0; - var w14 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w14 >>> 26) | 0; - w14 &= 0x3ffffff; - /* k = 15 */ - lo = Math.imul(al9, bl6); - mid = Math.imul(al9, bh6); - mid = mid + Math.imul(ah9, bl6) | 0; - hi = Math.imul(ah9, bh6); - lo = lo + Math.imul(al8, bl7) | 0; - mid = mid + Math.imul(al8, bh7) | 0; - mid = mid + Math.imul(ah8, bl7) | 0; - hi = hi + Math.imul(ah8, bh7) | 0; - lo = lo + Math.imul(al7, bl8) | 0; - mid = mid + Math.imul(al7, bh8) | 0; - mid = mid + Math.imul(ah7, bl8) | 0; - hi = hi + Math.imul(ah7, bh8) | 0; - lo = lo + Math.imul(al6, bl9) | 0; - mid = mid + Math.imul(al6, bh9) | 0; - mid = mid + Math.imul(ah6, bl9) | 0; - hi = hi + Math.imul(ah6, bh9) | 0; - var w15 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w15 >>> 26) | 0; - w15 &= 0x3ffffff; - /* k = 16 */ - lo = Math.imul(al9, bl7); - mid = Math.imul(al9, bh7); - mid = mid + Math.imul(ah9, bl7) | 0; - hi = Math.imul(ah9, bh7); - lo = lo + Math.imul(al8, bl8) | 0; - mid = mid + Math.imul(al8, bh8) | 0; - mid = mid + Math.imul(ah8, bl8) | 0; - hi = hi + Math.imul(ah8, bh8) | 0; - lo = lo + Math.imul(al7, bl9) | 0; - mid = mid + Math.imul(al7, bh9) | 0; - mid = mid + Math.imul(ah7, bl9) | 0; - hi = hi + Math.imul(ah7, bh9) | 0; - var w16 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w16 >>> 26) | 0; - w16 &= 0x3ffffff; - /* k = 17 */ - lo = Math.imul(al9, bl8); - mid = Math.imul(al9, bh8); - mid = mid + Math.imul(ah9, bl8) | 0; - hi = Math.imul(ah9, bh8); - lo = lo + Math.imul(al8, bl9) | 0; - mid = mid + Math.imul(al8, bh9) | 0; - mid = mid + Math.imul(ah8, bl9) | 0; - hi = hi + Math.imul(ah8, bh9) | 0; - var w17 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w17 >>> 26) | 0; - w17 &= 0x3ffffff; - /* k = 18 */ - lo = Math.imul(al9, bl9); - mid = Math.imul(al9, bh9); - mid = mid + Math.imul(ah9, bl9) | 0; - hi = Math.imul(ah9, bh9); - var w18 = (c + lo | 0) + ((mid & 0x1fff) << 13) | 0; - c = (hi + (mid >>> 13) | 0) + (w18 >>> 26) | 0; - w18 &= 0x3ffffff; - o[0] = w0; - o[1] = w1; - o[2] = w2; - o[3] = w3; - o[4] = w4; - o[5] = w5; - o[6] = w6; - o[7] = w7; - o[8] = w8; - o[9] = w9; - o[10] = w10; - o[11] = w11; - o[12] = w12; - o[13] = w13; - o[14] = w14; - o[15] = w15; - o[16] = w16; - o[17] = w17; - o[18] = w18; - if (c !== 0) { - o[19] = c; - out.length++; - } - return out; - }; - - // Polyfill comb - if (!Math.imul) { - comb10MulTo = smallMulTo; - } - - function bigMulTo(self, num, out) { - out.negative = num.negative ^ self.negative; - out.length = self.length + num.length; - - var carry = 0; - var hncarry = 0; - for (var k = 0; k < out.length - 1; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = hncarry; - hncarry = 0; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { - var i = k - j; - var a = self.words[i] | 0; - var b = num.words[j] | 0; - var r = a * b; - - var lo = r & 0x3ffffff; - ncarry = ncarry + (r / 0x4000000 | 0) | 0; - lo = lo + rword | 0; - rword = lo & 0x3ffffff; - ncarry = ncarry + (lo >>> 26) | 0; - - hncarry += ncarry >>> 26; - ncarry &= 0x3ffffff; - } - out.words[k] = rword; - carry = ncarry; - ncarry = hncarry; - } - if (carry !== 0) { - out.words[k] = carry; - } else { - out.length--; - } - - return out.strip(); - } - - function jumboMulTo(self, num, out) { - var fftm = new FFTM(); - return fftm.mulp(self, num, out); - } - - BN.prototype.mulTo = function mulTo(num, out) { - var res; - var len = this.length + num.length; - if (this.length === 10 && num.length === 10) { - res = comb10MulTo(this, num, out); - } else if (len < 63) { - res = smallMulTo(this, num, out); - } else if (len < 1024) { - res = bigMulTo(this, num, out); - } else { - res = jumboMulTo(this, num, out); - } - - return res; - }; - - // Cooley-Tukey algorithm for FFT - // slightly revisited to rely on looping instead of recursion - - function FFTM(x, y) { - this.x = x; - this.y = y; - } - - FFTM.prototype.makeRBT = function makeRBT(N) { - var t = new Array(N); - var l = BN.prototype._countBits(N) - 1; - for (var i = 0; i < N; i++) { - t[i] = this.revBin(i, l, N); - } - - return t; - }; - - // Returns binary-reversed representation of `x` - FFTM.prototype.revBin = function revBin(x, l, N) { - if (x === 0 || x === N - 1) return x; - - var rb = 0; - for (var i = 0; i < l; i++) { - rb |= (x & 1) << l - i - 1; - x >>= 1; - } - - return rb; - }; - - // Performs "tweedling" phase, therefore 'emulating' - // behaviour of the recursive algorithm - FFTM.prototype.permute = function permute(rbt, rws, iws, rtws, itws, N) { - for (var i = 0; i < N; i++) { - rtws[i] = rws[rbt[i]]; - itws[i] = iws[rbt[i]]; - } - }; - - FFTM.prototype.transform = function transform(rws, iws, rtws, itws, N, rbt) { - this.permute(rbt, rws, iws, rtws, itws, N); - - for (var s = 1; s < N; s <<= 1) { - var l = s << 1; - - var rtwdf = Math.cos(2 * Math.PI / l); - var itwdf = Math.sin(2 * Math.PI / l); + if (i < len) { + if (i) x.c.push(+str.slice(0, i)); - for (var p = 0; p < N; p += l) { - var rtwdf_ = rtwdf; - var itwdf_ = itwdf; + for (len -= LOG_BASE; i < len;) { + x.c.push(+str.slice(i, i += LOG_BASE)); + } - for (var j = 0; j < s; j++) { - var re = rtws[p + j]; - var ie = itws[p + j]; + str = str.slice(i); + i = LOG_BASE - str.length; + } else { + i -= len; + } - var ro = rtws[p + j + s]; - var io = itws[p + j + s]; + for (; i--; str += '0') {} + x.c.push(+str); + } + } else { - var rx = rtwdf_ * ro - itwdf_ * io; + // Zero. + x.c = [x.e = 0]; + } - io = rtwdf_ * io + itwdf_ * ro; - ro = rx; + id = 0; + } - rtws[p + j] = re + ro; - itws[p + j] = ie + io; + // CONSTRUCTOR PROPERTIES - rtws[p + j + s] = re - ro; - itws[p + j + s] = ie - io; - /* jshint maxdepth : false */ - if (j !== l) { - rx = rtwdf * rtwdf_ - itwdf * itwdf_; + BigNumber.another = constructorFactory; - itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; - rtwdf_ = rx; - } - } - } - } - }; + BigNumber.ROUND_UP = 0; + BigNumber.ROUND_DOWN = 1; + BigNumber.ROUND_CEIL = 2; + BigNumber.ROUND_FLOOR = 3; + BigNumber.ROUND_HALF_UP = 4; + BigNumber.ROUND_HALF_DOWN = 5; + BigNumber.ROUND_HALF_EVEN = 6; + BigNumber.ROUND_HALF_CEIL = 7; + BigNumber.ROUND_HALF_FLOOR = 8; + BigNumber.EUCLID = 9; - FFTM.prototype.guessLen13b = function guessLen13b(n, m) { - var N = Math.max(m, n) | 1; - var odd = N & 1; - var i = 0; - for (N = N / 2 | 0; N; N = N >>> 1) { - i++; - } + /* + * Configure infrequently-changing library-wide settings. + * + * Accept an object or an argument list, with one or many of the following properties or + * parameters respectively: + * + * DECIMAL_PLACES {number} Integer, 0 to MAX inclusive + * ROUNDING_MODE {number} Integer, 0 to 8 inclusive + * EXPONENTIAL_AT {number|number[]} Integer, -MAX to MAX inclusive or + * [integer -MAX to 0 incl., 0 to MAX incl.] + * RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or + * [integer -MAX to -1 incl., integer 1 to MAX incl.] + * ERRORS {boolean|number} true, false, 1 or 0 + * CRYPTO {boolean|number} true, false, 1 or 0 + * MODULO_MODE {number} 0 to 9 inclusive + * POW_PRECISION {number} 0 to MAX inclusive + * FORMAT {object} See BigNumber.prototype.toFormat + * decimalSeparator {string} + * groupSeparator {string} + * groupSize {number} + * secondaryGroupSize {number} + * fractionGroupSeparator {string} + * fractionGroupSize {number} + * + * (The values assigned to the above FORMAT object properties are not checked for validity.) + * + * E.g. + * BigNumber.config(20, 4) is equivalent to + * BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 }) + * + * Ignore properties/parameters set to null or undefined. + * Return an object with the properties current values. + */ + BigNumber.config = BigNumber.set = function () { + var v, + p, + i = 0, + r = {}, + a = arguments, + o = a[0], + has = o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) == 'object' ? function () { + if (o.hasOwnProperty(p)) return (v = o[p]) != null; + } : function () { + if (a.length > i) return (v = a[i++]) != null; + }; - return 1 << i + 1 + odd; - }; + // DECIMAL_PLACES {number} Integer, 0 to MAX inclusive. + // 'config() DECIMAL_PLACES not an integer: {v}' + // 'config() DECIMAL_PLACES out of range: {v}' + if (has(p = 'DECIMAL_PLACES') && isValidInt(v, 0, MAX, 2, p)) { + DECIMAL_PLACES = v | 0; + } + r[p] = DECIMAL_PLACES; - FFTM.prototype.conjugate = function conjugate(rws, iws, N) { - if (N <= 1) return; + // ROUNDING_MODE {number} Integer, 0 to 8 inclusive. + // 'config() ROUNDING_MODE not an integer: {v}' + // 'config() ROUNDING_MODE out of range: {v}' + if (has(p = 'ROUNDING_MODE') && isValidInt(v, 0, 8, 2, p)) { + ROUNDING_MODE = v | 0; + } + r[p] = ROUNDING_MODE; - for (var i = 0; i < N / 2; i++) { - var t = rws[i]; + // EXPONENTIAL_AT {number|number[]} + // Integer, -MAX to MAX inclusive or [integer -MAX to 0 inclusive, 0 to MAX inclusive]. + // 'config() EXPONENTIAL_AT not an integer: {v}' + // 'config() EXPONENTIAL_AT out of range: {v}' + if (has(p = 'EXPONENTIAL_AT')) { - rws[i] = rws[N - i - 1]; - rws[N - i - 1] = t; + if (isArray(v)) { + if (isValidInt(v[0], -MAX, 0, 2, p) && isValidInt(v[1], 0, MAX, 2, p)) { + TO_EXP_NEG = v[0] | 0; + TO_EXP_POS = v[1] | 0; + } + } else if (isValidInt(v, -MAX, MAX, 2, p)) { + TO_EXP_NEG = -(TO_EXP_POS = (v < 0 ? -v : v) | 0); + } + } + r[p] = [TO_EXP_NEG, TO_EXP_POS]; - t = iws[i]; + // RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or + // [integer -MAX to -1 inclusive, integer 1 to MAX inclusive]. + // 'config() RANGE not an integer: {v}' + // 'config() RANGE cannot be zero: {v}' + // 'config() RANGE out of range: {v}' + if (has(p = 'RANGE')) { - iws[i] = -iws[N - i - 1]; - iws[N - i - 1] = -t; - } - }; + if (isArray(v)) { + if (isValidInt(v[0], -MAX, -1, 2, p) && isValidInt(v[1], 1, MAX, 2, p)) { + MIN_EXP = v[0] | 0; + MAX_EXP = v[1] | 0; + } + } else if (isValidInt(v, -MAX, MAX, 2, p)) { + if (v | 0) MIN_EXP = -(MAX_EXP = (v < 0 ? -v : v) | 0);else if (ERRORS) raise(2, p + ' cannot be zero', v); + } + } + r[p] = [MIN_EXP, MAX_EXP]; - FFTM.prototype.normalize13b = function normalize13b(ws, N) { - var carry = 0; - for (var i = 0; i < N / 2; i++) { - var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + Math.round(ws[2 * i] / N) + carry; + // ERRORS {boolean|number} true, false, 1 or 0. + // 'config() ERRORS not a boolean or binary digit: {v}' + if (has(p = 'ERRORS')) { - ws[i] = w & 0x3ffffff; + if (v === !!v || v === 1 || v === 0) { + id = 0; + isValidInt = (ERRORS = !!v) ? intValidatorWithErrors : intValidatorNoErrors; + } else if (ERRORS) { + raise(2, p + notBool, v); + } + } + r[p] = ERRORS; - if (w < 0x4000000) { - carry = 0; - } else { - carry = w / 0x4000000 | 0; - } - } + // CRYPTO {boolean|number} true, false, 1 or 0. + // 'config() CRYPTO not a boolean or binary digit: {v}' + // 'config() crypto unavailable: {crypto}' + if (has(p = 'CRYPTO')) { - return ws; - }; + if (v === true || v === false || v === 1 || v === 0) { + if (v) { + v = typeof crypto == 'undefined'; + if (!v && crypto && (crypto.getRandomValues || crypto.randomBytes)) { + CRYPTO = true; + } else if (ERRORS) { + raise(2, 'crypto unavailable', v ? void 0 : crypto); + } else { + CRYPTO = false; + } + } else { + CRYPTO = false; + } + } else if (ERRORS) { + raise(2, p + notBool, v); + } + } + r[p] = CRYPTO; - FFTM.prototype.convert13b = function convert13b(ws, len, rws, N) { - var carry = 0; - for (var i = 0; i < len; i++) { - carry = carry + (ws[i] | 0); + // MODULO_MODE {number} Integer, 0 to 9 inclusive. + // 'config() MODULO_MODE not an integer: {v}' + // 'config() MODULO_MODE out of range: {v}' + if (has(p = 'MODULO_MODE') && isValidInt(v, 0, 9, 2, p)) { + MODULO_MODE = v | 0; + } + r[p] = MODULO_MODE; - rws[2 * i] = carry & 0x1fff;carry = carry >>> 13; - rws[2 * i + 1] = carry & 0x1fff;carry = carry >>> 13; - } + // POW_PRECISION {number} Integer, 0 to MAX inclusive. + // 'config() POW_PRECISION not an integer: {v}' + // 'config() POW_PRECISION out of range: {v}' + if (has(p = 'POW_PRECISION') && isValidInt(v, 0, MAX, 2, p)) { + POW_PRECISION = v | 0; + } + r[p] = POW_PRECISION; - // Pad with zeroes - for (i = 2 * len; i < N; ++i) { - rws[i] = 0; - } + // FORMAT {object} + // 'config() FORMAT not an object: {v}' + if (has(p = 'FORMAT')) { - assert(carry === 0); - assert((carry & ~0x1fff) === 0); - }; + if ((typeof v === 'undefined' ? 'undefined' : _typeof(v)) == 'object') { + FORMAT = v; + } else if (ERRORS) { + raise(2, p + ' not an object', v); + } + } + r[p] = FORMAT; - FFTM.prototype.stub = function stub(N) { - var ph = new Array(N); - for (var i = 0; i < N; i++) { - ph[i] = 0; - } + return r; + }; - return ph; - }; + /* + * Return a new BigNumber whose value is the maximum of the arguments. + * + * arguments {number|string|BigNumber} + */ + BigNumber.max = function () { + return maxOrMin(arguments, P.lt); + }; - FFTM.prototype.mulp = function mulp(x, y, out) { - var N = 2 * this.guessLen13b(x.length, y.length); + /* + * Return a new BigNumber whose value is the minimum of the arguments. + * + * arguments {number|string|BigNumber} + */ + BigNumber.min = function () { + return maxOrMin(arguments, P.gt); + }; - var rbt = this.makeRBT(N); + /* + * Return a new BigNumber with a random value equal to or greater than 0 and less than 1, + * and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing + * zeros are produced). + * + * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. + * + * 'random() decimal places not an integer: {dp}' + * 'random() decimal places out of range: {dp}' + * 'random() crypto unavailable: {crypto}' + */ + BigNumber.random = function () { + var pow2_53 = 0x20000000000000; - var _ = this.stub(N); + // Return a 53 bit integer n, where 0 <= n < 9007199254740992. + // Check if Math.random() produces more than 32 bits of randomness. + // If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits. + // 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1. + var random53bitInt = Math.random() * pow2_53 & 0x1fffff ? function () { + return mathfloor(Math.random() * pow2_53); + } : function () { + return (Math.random() * 0x40000000 | 0) * 0x800000 + (Math.random() * 0x800000 | 0); + }; - var rws = new Array(N); - var rwst = new Array(N); - var iwst = new Array(N); + return function (dp) { + var a, + b, + e, + k, + v, + i = 0, + c = [], + rand = new BigNumber(ONE); - var nrws = new Array(N); - var nrwst = new Array(N); - var niwst = new Array(N); + dp = dp == null || !isValidInt(dp, 0, MAX, 14) ? DECIMAL_PLACES : dp | 0; + k = mathceil(dp / LOG_BASE); - var rmws = out.words; - rmws.length = N; + if (CRYPTO) { - this.convert13b(x.words, x.length, rws, N); - this.convert13b(y.words, y.length, nrws, N); + // Browsers supporting crypto.getRandomValues. + if (crypto.getRandomValues) { - this.transform(rws, _, rwst, iwst, N, rbt); - this.transform(nrws, _, nrwst, niwst, N, rbt); + a = crypto.getRandomValues(new Uint32Array(k *= 2)); - for (var i = 0; i < N; i++) { - var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; - iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; - rwst[i] = rx; - } + for (; i < k;) { - this.conjugate(rwst, iwst, N); - this.transform(rwst, iwst, rmws, _, N, rbt); - this.conjugate(rmws, _, N); - this.normalize13b(rmws, N); + // 53 bits: + // ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2) + // 11111 11111111 11111111 11111111 11100000 00000000 00000000 + // ((Math.pow(2, 32) - 1) >>> 11).toString(2) + // 11111 11111111 11111111 + // 0x20000 is 2^21. + v = a[i] * 0x20000 + (a[i + 1] >>> 11); - out.negative = x.negative ^ y.negative; - out.length = x.length + y.length; - return out.strip(); - }; + // Rejection sampling: + // 0 <= v < 9007199254740992 + // Probability that v >= 9e15, is + // 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251 + if (v >= 9e15) { + b = crypto.getRandomValues(new Uint32Array(2)); + a[i] = b[0]; + a[i + 1] = b[1]; + } else { - // Multiply `this` by `num` - BN.prototype.mul = function mul(num) { - var out = new BN(null); - out.words = new Array(this.length + num.length); - return this.mulTo(num, out); - }; + // 0 <= v <= 8999999999999999 + // 0 <= (v % 1e14) <= 99999999999999 + c.push(v % 1e14); + i += 2; + } + } + i = k / 2; - // Multiply employing FFT - BN.prototype.mulf = function mulf(num) { - var out = new BN(null); - out.words = new Array(this.length + num.length); - return jumboMulTo(this, num, out); - }; + // Node.js supporting crypto.randomBytes. + } else if (crypto.randomBytes) { - // In-place Multiplication - BN.prototype.imul = function imul(num) { - return this.clone().mulTo(num, this); - }; + // buffer + a = crypto.randomBytes(k *= 7); - BN.prototype.imuln = function imuln(num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); + for (; i < k;) { - // Carry - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = (this.words[i] | 0) * num; - var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); - carry >>= 26; - carry += w / 0x4000000 | 0; - // NOTE: lo is 27bit maximum - carry += lo >>> 26; - this.words[i] = lo & 0x3ffffff; - } + // 0x1000000000000 is 2^48, 0x10000000000 is 2^40 + // 0x100000000 is 2^32, 0x1000000 is 2^24 + // 11111 11111111 11111111 11111111 11111111 11111111 11111111 + // 0 <= v < 9007199254740992 + v = (a[i] & 31) * 0x1000000000000 + a[i + 1] * 0x10000000000 + a[i + 2] * 0x100000000 + a[i + 3] * 0x1000000 + (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6]; - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } + if (v >= 9e15) { + crypto.randomBytes(7).copy(a, i); + } else { - return this; - }; + // 0 <= (v % 1e14) <= 99999999999999 + c.push(v % 1e14); + i += 7; + } + } + i = k / 7; + } else { + CRYPTO = false; + if (ERRORS) raise(14, 'crypto unavailable', crypto); + } + } - BN.prototype.muln = function muln(num) { - return this.clone().imuln(num); - }; + // Use Math.random. + if (!CRYPTO) { - // `this` * `this` - BN.prototype.sqr = function sqr() { - return this.mul(this); - }; + for (; i < k;) { + v = random53bitInt(); + if (v < 9e15) c[i++] = v % 1e14; + } + } - // `this` * `this` in-place - BN.prototype.isqr = function isqr() { - return this.imul(this.clone()); - }; + k = c[--i]; + dp %= LOG_BASE; - // Math.pow(`this`, `num`) - BN.prototype.pow = function pow(num) { - var w = toBitArray(num); - if (w.length === 0) return new BN(1); + // Convert trailing digits to zeros according to dp. + if (k && dp) { + v = POWS_TEN[LOG_BASE - dp]; + c[i] = mathfloor(k / v) * v; + } - // Skip leading zeroes - var res = this; - for (var i = 0; i < w.length; i++, res = res.sqr()) { - if (w[i] !== 0) break; - } + // Remove trailing elements which are zero. + for (; c[i] === 0; c.pop(), i--) {} - if (++i < w.length) { - for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { - if (w[i] === 0) continue; + // Zero? + if (i < 0) { + c = [e = 0]; + } else { - res = res.mul(q); - } - } + // Remove leading elements which are zero and adjust exponent accordingly. + for (e = -1; c[0] === 0; c.splice(0, 1), e -= LOG_BASE) {} - return res; - }; + // Count the digits of the first element of c to determine leading zeros, and... + for (i = 1, v = c[0]; v >= 10; v /= 10, i++) {} - // Shift-left in-place - BN.prototype.iushln = function iushln(bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; - var carryMask = 0x3ffffff >>> 26 - r << 26 - r; - var i; + // adjust the exponent accordingly. + if (i < LOG_BASE) e -= LOG_BASE - i; + } - if (r !== 0) { - var carry = 0; + rand.e = e; + rand.c = c; + return rand; + }; + }(); - for (i = 0; i < this.length; i++) { - var newCarry = this.words[i] & carryMask; - var c = (this.words[i] | 0) - newCarry << r; - this.words[i] = c | carry; - carry = newCarry >>> 26 - r; - } + // PRIVATE FUNCTIONS - if (carry) { - this.words[i] = carry; - this.length++; - } - } - if (s !== 0) { - for (i = this.length - 1; i >= 0; i--) { - this.words[i + s] = this.words[i]; - } + // Convert a numeric string of baseIn to a numeric string of baseOut. + function convertBase(str, baseOut, baseIn, sign) { + var d, + e, + k, + r, + x, + xc, + y, + i = str.indexOf('.'), + dp = DECIMAL_PLACES, + rm = ROUNDING_MODE; - for (i = 0; i < s; i++) { - this.words[i] = 0; - } + if (baseIn < 37) str = str.toLowerCase(); - this.length += s; - } + // Non-integer. + if (i >= 0) { + k = POW_PRECISION; - return this.strip(); - }; + // Unlimited precision. + POW_PRECISION = 0; + str = str.replace('.', ''); + y = new BigNumber(baseIn); + x = y.pow(str.length - i); + POW_PRECISION = k; - BN.prototype.ishln = function ishln(bits) { - // TODO(indutny): implement me - assert(this.negative === 0); - return this.iushln(bits); - }; + // Convert str as if an integer, then restore the fraction part by dividing the + // result by its base raised to a power. + y.c = toBaseOut(toFixedPoint(coeffToString(x.c), x.e), 10, baseOut); + y.e = y.c.length; + } - // Shift-right in-place - // NOTE: `hint` is a lowest bit before trailing zeroes - // NOTE: if `extended` is present - it will be filled with destroyed bits - BN.prototype.iushrn = function iushrn(bits, hint, extended) { - assert(typeof bits === 'number' && bits >= 0); - var h; - if (hint) { - h = (hint - hint % 26) / 26; - } else { - h = 0; - } + // Convert the number as integer. + xc = toBaseOut(str, baseIn, baseOut); + e = k = xc.length; - var r = bits % 26; - var s = Math.min((bits - r) / 26, this.length); - var mask = 0x3ffffff ^ 0x3ffffff >>> r << r; - var maskedWords = extended; + // Remove trailing zeros. + for (; xc[--k] == 0; xc.pop()) {} + if (!xc[0]) return '0'; - h -= s; - h = Math.max(0, h); + if (i < 0) { + --e; + } else { + x.c = xc; + x.e = e; - // Extended mode, copy masked part - if (maskedWords) { - for (var i = 0; i < s; i++) { - maskedWords.words[i] = this.words[i]; - } - maskedWords.length = s; - } + // sign is needed for correct rounding. + x.s = sign; + x = div(x, y, dp, rm, baseOut); + xc = x.c; + r = x.r; + e = x.e; + } - if (s === 0) { - // No-op, we should not move anything at all - } else if (this.length > s) { - this.length -= s; - for (i = 0; i < this.length; i++) { - this.words[i] = this.words[i + s]; - } - } else { - this.words[0] = 0; - this.length = 1; - } + d = e + dp + 1; - var carry = 0; - for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { - var word = this.words[i] | 0; - this.words[i] = carry << 26 - r | word >>> r; - carry = word & mask; - } + // The rounding digit, i.e. the digit to the right of the digit that may be rounded up. + i = xc[d]; + k = baseOut / 2; + r = r || d < 0 || xc[d + 1] != null; - // Push carried bits as a mask - if (maskedWords && carry !== 0) { - maskedWords.words[maskedWords.length++] = carry; - } + r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : i > k || i == k && (rm == 4 || r || rm == 6 && xc[d - 1] & 1 || rm == (x.s < 0 ? 8 : 7)); - if (this.length === 0) { - this.words[0] = 0; - this.length = 1; - } + if (d < 1 || !xc[0]) { - return this.strip(); - }; + // 1^-dp or 0. + str = r ? toFixedPoint('1', -dp) : '0'; + } else { + xc.length = d; - BN.prototype.ishrn = function ishrn(bits, hint, extended) { - // TODO(indutny): implement me - assert(this.negative === 0); - return this.iushrn(bits, hint, extended); - }; + if (r) { - // Shift-left - BN.prototype.shln = function shln(bits) { - return this.clone().ishln(bits); - }; + // Rounding up may mean the previous digit has to be rounded up and so on. + for (--baseOut; ++xc[--d] > baseOut;) { + xc[d] = 0; - BN.prototype.ushln = function ushln(bits) { - return this.clone().iushln(bits); - }; + if (!d) { + ++e; + xc = [1].concat(xc); + } + } + } - // Shift-right - BN.prototype.shrn = function shrn(bits) { - return this.clone().ishrn(bits); - }; + // Determine trailing zeros. + for (k = xc.length; !xc[--k];) {} - BN.prototype.ushrn = function ushrn(bits) { - return this.clone().iushrn(bits); - }; + // E.g. [4, 11, 15] becomes 4bf. + for (i = 0, str = ''; i <= k; str += ALPHABET.charAt(xc[i++])) {} + str = toFixedPoint(str, e); + } - // Test if n bit is set - BN.prototype.testn = function testn(bit) { - assert(typeof bit === 'number' && bit >= 0); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; + // The caller will add the sign. + return str; + } - // Fast case: bit is much higher than all existing words - if (this.length <= s) return false; + // Perform division in the specified base. Called by div and convertBase. + div = function () { - // Check bit and return - var w = this.words[s]; + // Assume non-zero x and k. + function multiply(x, k, base) { + var m, + temp, + xlo, + xhi, + carry = 0, + i = x.length, + klo = k % SQRT_BASE, + khi = k / SQRT_BASE | 0; - return !!(w & q); - }; + for (x = x.slice(); i--;) { + xlo = x[i] % SQRT_BASE; + xhi = x[i] / SQRT_BASE | 0; + m = khi * xlo + xhi * klo; + temp = klo * xlo + m % SQRT_BASE * SQRT_BASE + carry; + carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi; + x[i] = temp % base; + } - // Return only lowers bits of number (in-place) - BN.prototype.imaskn = function imaskn(bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; + if (carry) x = [carry].concat(x); - assert(this.negative === 0, 'imaskn works only with positive numbers'); + return x; + } - if (this.length <= s) { - return this; - } + function compare(a, b, aL, bL) { + var i, cmp; - if (r !== 0) { - s++; - } - this.length = Math.min(s, this.length); + if (aL != bL) { + cmp = aL > bL ? 1 : -1; + } else { - if (r !== 0) { - var mask = 0x3ffffff ^ 0x3ffffff >>> r << r; - this.words[this.length - 1] &= mask; - } + for (i = cmp = 0; i < aL; i++) { - return this.strip(); - }; + if (a[i] != b[i]) { + cmp = a[i] > b[i] ? 1 : -1; + break; + } + } + } + return cmp; + } - // Return only lowers bits of number - BN.prototype.maskn = function maskn(bits) { - return this.clone().imaskn(bits); - }; + function subtract(a, b, aL, base) { + var i = 0; - // Add plain number `num` to `this` - BN.prototype.iaddn = function iaddn(num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); - if (num < 0) return this.isubn(-num); + // Subtract b from a. + for (; aL--;) { + a[aL] -= i; + i = a[aL] < b[aL] ? 1 : 0; + a[aL] = i * base + a[aL] - b[aL]; + } - // Possible sign change - if (this.negative !== 0) { - if (this.length === 1 && (this.words[0] | 0) < num) { - this.words[0] = num - (this.words[0] | 0); - this.negative = 0; - return this; - } + // Remove leading zeros. + for (; !a[0] && a.length > 1; a.splice(0, 1)) {} + } - this.negative = 0; - this.isubn(num); - this.negative = 1; - return this; - } + // x: dividend, y: divisor. + return function (x, y, dp, rm, base) { + var cmp, + e, + i, + more, + n, + prod, + prodL, + q, + qc, + rem, + remL, + rem0, + xi, + xL, + yc0, + yL, + yz, + s = x.s == y.s ? 1 : -1, + xc = x.c, + yc = y.c; - // Add without checks - return this._iaddn(num); - }; + // Either NaN, Infinity or 0? + if (!xc || !xc[0] || !yc || !yc[0]) { - BN.prototype._iaddn = function _iaddn(num) { - this.words[0] += num; + return new BigNumber( - // Carry - for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { - this.words[i] -= 0x4000000; - if (i === this.length - 1) { - this.words[i + 1] = 1; - } else { - this.words[i + 1]++; - } - } - this.length = Math.max(this.length, i + 1); + // Return NaN if either NaN, or both Infinity or 0. + !x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN : - return this; - }; + // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0. + xc && xc[0] == 0 || !yc ? s * 0 : s / 0); + } - // Subtract plain number `num` from `this` - BN.prototype.isubn = function isubn(num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); - if (num < 0) return this.iaddn(-num); + q = new BigNumber(s); + qc = q.c = []; + e = x.e - y.e; + s = dp + e + 1; - if (this.negative !== 0) { - this.negative = 0; - this.iaddn(num); - this.negative = 1; - return this; - } + if (!base) { + base = BASE; + e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE); + s = s / LOG_BASE | 0; + } - this.words[0] -= num; + // Result exponent may be one less then the current value of e. + // The coefficients of the BigNumbers from convertBase may have trailing zeros. + for (i = 0; yc[i] == (xc[i] || 0); i++) {} + if (yc[i] > (xc[i] || 0)) e--; - if (this.length === 1 && this.words[0] < 0) { - this.words[0] = -this.words[0]; - this.negative = 1; - } else { - // Carry - for (var i = 0; i < this.length && this.words[i] < 0; i++) { - this.words[i] += 0x4000000; - this.words[i + 1] -= 1; - } - } + if (s < 0) { + qc.push(1); + more = true; + } else { + xL = xc.length; + yL = yc.length; + i = 0; + s += 2; - return this.strip(); - }; + // Normalise xc and yc so highest order digit of yc is >= base / 2. - BN.prototype.addn = function addn(num) { - return this.clone().iaddn(num); - }; + n = mathfloor(base / (yc[0] + 1)); - BN.prototype.subn = function subn(num) { - return this.clone().isubn(num); - }; + // Not necessary, but to handle odd bases where yc[0] == ( base / 2 ) - 1. + // if ( n > 1 || n++ == 1 && yc[0] < base / 2 ) { + if (n > 1) { + yc = multiply(yc, n, base); + xc = multiply(xc, n, base); + yL = yc.length; + xL = xc.length; + } - BN.prototype.iabs = function iabs() { - this.negative = 0; + xi = yL; + rem = xc.slice(0, yL); + remL = rem.length; - return this; - }; + // Add zeros to make remainder as long as divisor. + for (; remL < yL; rem[remL++] = 0) {} + yz = yc.slice(); + yz = [0].concat(yz); + yc0 = yc[0]; + if (yc[1] >= base / 2) yc0++; + // Not necessary, but to prevent trial digit n > base, when using base 3. + // else if ( base == 3 && yc0 == 1 ) yc0 = 1 + 1e-15; - BN.prototype.abs = function abs() { - return this.clone().iabs(); - }; + do { + n = 0; - BN.prototype._ishlnsubmul = function _ishlnsubmul(num, mul, shift) { - var len = num.length + shift; - var i; + // Compare divisor and remainder. + cmp = compare(yc, rem, yL, remL); - this._expand(len); + // If divisor < remainder. + if (cmp < 0) { - var w; - var carry = 0; - for (i = 0; i < num.length; i++) { - w = (this.words[i + shift] | 0) + carry; - var right = (num.words[i] | 0) * mul; - w -= right & 0x3ffffff; - carry = (w >> 26) - (right / 0x4000000 | 0); - this.words[i + shift] = w & 0x3ffffff; - } - for (; i < this.length - shift; i++) { - w = (this.words[i + shift] | 0) + carry; - carry = w >> 26; - this.words[i + shift] = w & 0x3ffffff; - } + // Calculate trial digit, n. - if (carry === 0) return this.strip(); + rem0 = rem[0]; + if (yL != remL) rem0 = rem0 * base + (rem[1] || 0); - // Subtraction overflow - assert(carry === -1); - carry = 0; - for (i = 0; i < this.length; i++) { - w = -(this.words[i] | 0) + carry; - carry = w >> 26; - this.words[i] = w & 0x3ffffff; - } - this.negative = 1; + // n is how many times the divisor goes into the current remainder. + n = mathfloor(rem0 / yc0); - return this.strip(); - }; + // Algorithm: + // 1. product = divisor * trial digit (n) + // 2. if product > remainder: product -= divisor, n-- + // 3. remainder -= product + // 4. if product was < remainder at 2: + // 5. compare new remainder and divisor + // 6. If remainder > divisor: remainder -= divisor, n++ - BN.prototype._wordDiv = function _wordDiv(num, mode) { - var shift = this.length - num.length; + if (n > 1) { - var a = this.clone(); - var b = num; + // n may be > base only when base is 3. + if (n >= base) n = base - 1; - // Normalize - var bhi = b.words[b.length - 1] | 0; - var bhiBits = this._countBits(bhi); - shift = 26 - bhiBits; - if (shift !== 0) { - b = b.ushln(shift); - a.iushln(shift); - bhi = b.words[b.length - 1] | 0; - } + // product = divisor * trial digit. + prod = multiply(yc, n, base); + prodL = prod.length; + remL = rem.length; - // Initialize quotient - var m = a.length - b.length; - var q; + // Compare product and remainder. + // If product > remainder. + // Trial digit n too high. + // n is 1 too high about 5% of the time, and is not known to have + // ever been more than 1 too high. + while (compare(prod, rem, prodL, remL) == 1) { + n--; - if (mode !== 'mod') { - q = new BN(null); - q.length = m + 1; - q.words = new Array(q.length); - for (var i = 0; i < q.length; i++) { - q.words[i] = 0; - } - } + // Subtract divisor from product. + subtract(prod, yL < prodL ? yz : yc, prodL, base); + prodL = prod.length; + cmp = 1; + } + } else { - var diff = a.clone()._ishlnsubmul(b, 1, m); - if (diff.negative === 0) { - a = diff; - if (q) { - q.words[m] = 1; - } - } + // n is 0 or 1, cmp is -1. + // If n is 0, there is no need to compare yc and rem again below, + // so change cmp to 1 to avoid it. + // If n is 1, leave cmp as -1, so yc and rem are compared again. + if (n == 0) { - for (var j = m - 1; j >= 0; j--) { - var qj = (a.words[b.length + j] | 0) * 0x4000000 + (a.words[b.length + j - 1] | 0); + // divisor < remainder, so n must be at least 1. + cmp = n = 1; + } - // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max - // (0x7ffffff) - qj = Math.min(qj / bhi | 0, 0x3ffffff); + // product = divisor + prod = yc.slice(); + prodL = prod.length; + } - a._ishlnsubmul(b, qj, j); - while (a.negative !== 0) { - qj--; - a.negative = 0; - a._ishlnsubmul(b, 1, j); - if (!a.isZero()) { - a.negative ^= 1; - } - } - if (q) { - q.words[j] = qj; - } - } - if (q) { - q.strip(); - } - a.strip(); + if (prodL < remL) prod = [0].concat(prod); - // Denormalize - if (mode !== 'div' && shift !== 0) { - a.iushrn(shift); - } + // Subtract product from remainder. + subtract(rem, prod, remL, base); + remL = rem.length; - return { - div: q || null, - mod: a - }; - }; + // If product was < remainder. + if (cmp == -1) { - // NOTE: 1) `mode` can be set to `mod` to request mod only, - // to `div` to request div only, or be absent to - // request both div & mod - // 2) `positive` is true if unsigned mod is requested - BN.prototype.divmod = function divmod(num, mode, positive) { - assert(!num.isZero()); + // Compare divisor and new remainder. + // If divisor < new remainder, subtract divisor from remainder. + // Trial digit n too low. + // n is 1 too low about 5% of the time, and very rarely 2 too low. + while (compare(yc, rem, yL, remL) < 1) { + n++; - if (this.isZero()) { - return { - div: new BN(0), - mod: new BN(0) - }; - } + // Subtract divisor from remainder. + subtract(rem, yL < remL ? yz : yc, remL, base); + remL = rem.length; + } + } + } else if (cmp === 0) { + n++; + rem = [0]; + } // else cmp === 1 and n will be 0 - var div, mod, res; - if (this.negative !== 0 && num.negative === 0) { - res = this.neg().divmod(num, mode); + // Add the next digit, n, to the result array. + qc[i++] = n; - if (mode !== 'mod') { - div = res.div.neg(); - } + // Update the remainder. + if (rem[0]) { + rem[remL++] = xc[xi] || 0; + } else { + rem = [xc[xi]]; + remL = 1; + } + } while ((xi++ < xL || rem[0] != null) && s--); - if (mode !== 'div') { - mod = res.mod.neg(); - if (positive && mod.negative !== 0) { - mod.iadd(num); - } - } + more = rem[0] != null; - return { - div: div, - mod: mod - }; - } + // Leading zero? + if (!qc[0]) qc.splice(0, 1); + } - if (this.negative === 0 && num.negative !== 0) { - res = this.divmod(num.neg(), mode); + if (base == BASE) { - if (mode !== 'mod') { - div = res.div.neg(); - } + // To calculate q.e, first get the number of digits of qc[0]. + for (i = 1, s = qc[0]; s >= 10; s /= 10, i++) {} + round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more); - return { - div: div, - mod: res.mod - }; - } + // Caller is convertBase. + } else { + q.e = e; + q.r = +more; + } - if ((this.negative & num.negative) !== 0) { - res = this.neg().divmod(num.neg(), mode); + return q; + }; + }(); - if (mode !== 'div') { - mod = res.mod.neg(); - if (positive && mod.negative !== 0) { - mod.isub(num); - } - } + /* + * Return a string representing the value of BigNumber n in fixed-point or exponential + * notation rounded to the specified decimal places or significant digits. + * + * n is a BigNumber. + * i is the index of the last digit required (i.e. the digit that may be rounded up). + * rm is the rounding mode. + * caller is caller id: toExponential 19, toFixed 20, toFormat 21, toPrecision 24. + */ + function format(n, i, rm, caller) { + var c0, e, ne, len, str; - return { - div: res.div, - mod: mod - }; - } + rm = rm != null && isValidInt(rm, 0, 8, caller, roundingMode) ? rm | 0 : ROUNDING_MODE; - // Both numbers are positive at this point + if (!n.c) return n.toString(); + c0 = n.c[0]; + ne = n.e; - // Strip both numbers to approximate shift value - if (num.length > this.length || this.cmp(num) < 0) { - return { - div: new BN(0), - mod: this - }; - } + if (i == null) { + str = coeffToString(n.c); + str = caller == 19 || caller == 24 && ne <= TO_EXP_NEG ? toExponential(str, ne) : toFixedPoint(str, ne); + } else { + n = round(new BigNumber(n), i, rm); - // Very short reduction - if (num.length === 1) { - if (mode === 'div') { - return { - div: this.divn(num.words[0]), - mod: null - }; - } + // n.e may have changed if the value was rounded up. + e = n.e; - if (mode === 'mod') { - return { - div: null, - mod: new BN(this.modn(num.words[0])) - }; - } + str = coeffToString(n.c); + len = str.length; - return { - div: this.divn(num.words[0]), - mod: new BN(this.modn(num.words[0])) - }; - } + // toPrecision returns exponential notation if the number of significant digits + // specified is less than the number of digits necessary to represent the integer + // part of the value in fixed-point notation. - return this._wordDiv(num, mode); - }; + // Exponential notation. + if (caller == 19 || caller == 24 && (i <= e || e <= TO_EXP_NEG)) { - // Find `this` / `num` - BN.prototype.div = function div(num) { - return this.divmod(num, 'div', false).div; - }; + // Append zeros? + for (; len < i; str += '0', len++) {} + str = toExponential(str, e); - // Find `this` % `num` - BN.prototype.mod = function mod(num) { - return this.divmod(num, 'mod', false).mod; - }; + // Fixed-point notation. + } else { + i -= ne; + str = toFixedPoint(str, e); - BN.prototype.umod = function umod(num) { - return this.divmod(num, 'mod', true).mod; - }; + // Append zeros? + if (e + 1 > len) { + if (--i > 0) for (str += '.'; i--; str += '0') {} + } else { + i += e - len; + if (i > 0) { + if (e + 1 == len) str += '.'; + for (; i--; str += '0') {} + } + } + } + } - // Find Round(`this` / `num`) - BN.prototype.divRound = function divRound(num) { - var dm = this.divmod(num); + return n.s < 0 && c0 ? '-' + str : str; + } - // Fast case - exact division - if (dm.mod.isZero()) return dm.div; + // Handle BigNumber.max and BigNumber.min. + function maxOrMin(args, method) { + var m, + n, + i = 0; - var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; + if (isArray(args[0])) args = args[0]; + m = new BigNumber(args[0]); - var half = num.ushrn(1); - var r2 = num.andln(1); - var cmp = mod.cmp(half); + for (; ++i < args.length;) { + n = new BigNumber(args[i]); - // Round down - if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div; + // If any number is NaN, return NaN. + if (!n.s) { + m = n; + break; + } else if (method.call(m, n)) { + m = n; + } + } - // Round up - return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); - }; + return m; + } + + /* + * Return true if n is an integer in range, otherwise throw. + * Use for argument validation when ERRORS is true. + */ + function intValidatorWithErrors(n, min, max, caller, name) { + if (n < min || n > max || n != truncate(n)) { + raise(caller, (name || 'decimal places') + (n < min || n > max ? ' out of range' : ' not an integer'), n); + } - BN.prototype.modn = function modn(num) { - assert(num <= 0x3ffffff); - var p = (1 << 26) % num; + return true; + } - var acc = 0; - for (var i = this.length - 1; i >= 0; i--) { - acc = (p * acc + (this.words[i] | 0)) % num; - } + /* + * Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP. + * Called by minus, plus and times. + */ + function normalise(n, c, e) { + var i = 1, + j = c.length; - return acc; - }; + // Remove trailing zeros. + for (; !c[--j]; c.pop()) {} - // In-place division by number - BN.prototype.idivn = function idivn(num) { - assert(num <= 0x3ffffff); + // Calculate the base 10 exponent. First get the number of digits of c[0]. + for (j = c[0]; j >= 10; j /= 10, i++) {} - var carry = 0; - for (var i = this.length - 1; i >= 0; i--) { - var w = (this.words[i] | 0) + carry * 0x4000000; - this.words[i] = w / num | 0; - carry = w % num; - } + // Overflow? + if ((e = i + e * LOG_BASE - 1) > MAX_EXP) { - return this.strip(); - }; + // Infinity. + n.c = n.e = null; - BN.prototype.divn = function divn(num) { - return this.clone().idivn(num); - }; + // Underflow? + } else if (e < MIN_EXP) { - BN.prototype.egcd = function egcd(p) { - assert(p.negative === 0); - assert(!p.isZero()); + // Zero. + n.c = [n.e = 0]; + } else { + n.e = e; + n.c = c; + } - var x = this; - var y = p.clone(); + return n; + } - if (x.negative !== 0) { - x = x.umod(p); - } else { - x = x.clone(); - } + // Handle values that fail the validity test in BigNumber. + parseNumeric = function () { + var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, + dotAfter = /^([^.]+)\.$/, + dotBefore = /^\.([^.]+)$/, + isInfinityOrNaN = /^-?(Infinity|NaN)$/, + whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g; - // A * x + B * y = x - var A = new BN(1); - var B = new BN(0); + return function (x, str, num, b) { + var base, + s = num ? str : str.replace(whitespaceOrPlus, ''); - // C * x + D * y = y - var C = new BN(0); - var D = new BN(1); + // No exception on ±Infinity or NaN. + if (isInfinityOrNaN.test(s)) { + x.s = isNaN(s) ? null : s < 0 ? -1 : 1; + } else { + if (!num) { - var g = 0; + // basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i + s = s.replace(basePrefix, function (m, p1, p2) { + base = (p2 = p2.toLowerCase()) == 'x' ? 16 : p2 == 'b' ? 2 : 8; + return !b || b == base ? p1 : m; + }); - while (x.isEven() && y.isEven()) { - x.iushrn(1); - y.iushrn(1); - ++g; - } + if (b) { + base = b; - var yp = y.clone(); - var xp = x.clone(); + // E.g. '1.' to '1', '.1' to '0.1' + s = s.replace(dotAfter, '$1').replace(dotBefore, '0.$1'); + } - while (!x.isZero()) { - for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1) {} - if (i > 0) { - x.iushrn(i); - while (i-- > 0) { - if (A.isOdd() || B.isOdd()) { - A.iadd(yp); - B.isub(xp); - } + if (str != s) return new BigNumber(s, base); + } - A.iushrn(1); - B.iushrn(1); - } - } + // 'new BigNumber() not a number: {n}' + // 'new BigNumber() not a base {b} number: {n}' + if (ERRORS) raise(id, 'not a' + (b ? ' base ' + b : '') + ' number', str); + x.s = null; + } - for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1) {} - if (j > 0) { - y.iushrn(j); - while (j-- > 0) { - if (C.isOdd() || D.isOdd()) { - C.iadd(yp); - D.isub(xp); - } + x.c = x.e = null; + id = 0; + }; + }(); - C.iushrn(1); - D.iushrn(1); - } - } + // Throw a BigNumber Error. + function raise(caller, msg, val) { + var error = new Error(['new BigNumber', // 0 + 'cmp', // 1 + 'config', // 2 + 'div', // 3 + 'divToInt', // 4 + 'eq', // 5 + 'gt', // 6 + 'gte', // 7 + 'lt', // 8 + 'lte', // 9 + 'minus', // 10 + 'mod', // 11 + 'plus', // 12 + 'precision', // 13 + 'random', // 14 + 'round', // 15 + 'shift', // 16 + 'times', // 17 + 'toDigits', // 18 + 'toExponential', // 19 + 'toFixed', // 20 + 'toFormat', // 21 + 'toFraction', // 22 + 'pow', // 23 + 'toPrecision', // 24 + 'toString', // 25 + 'BigNumber' // 26 + ][caller] + '() ' + msg + ': ' + val); - if (x.cmp(y) >= 0) { - x.isub(y); - A.isub(C); - B.isub(D); - } else { - y.isub(x); - C.isub(A); - D.isub(B); - } - } + error.name = 'BigNumber Error'; + id = 0; + throw error; + } - return { - a: C, - b: D, - gcd: y.iushln(g) - }; - }; + /* + * Round x to sd significant digits using rounding mode rm. Check for over/under-flow. + * If r is truthy, it is known that there are more digits after the rounding digit. + */ + function round(x, sd, rm, r) { + var d, + i, + j, + k, + n, + ni, + rd, + xc = x.c, + pows10 = POWS_TEN; - // This is reduced incarnation of the binary EEA - // above, designated to invert members of the - // _prime_ fields F(p) at a maximal speed - BN.prototype._invmp = function _invmp(p) { - assert(p.negative === 0); - assert(!p.isZero()); + // if x is not Infinity or NaN... + if (xc) { - var a = this; - var b = p.clone(); + // rd is the rounding digit, i.e. the digit after the digit that may be rounded up. + // n is a base 1e14 number, the value of the element of array x.c containing rd. + // ni is the index of n within x.c. + // d is the number of digits of n. + // i is the index of rd within n including leading zeros. + // j is the actual index of rd within n (if < 0, rd is a leading zero). + out: { - if (a.negative !== 0) { - a = a.umod(p); - } else { - a = a.clone(); - } + // Get the number of digits of the first element of xc. + for (d = 1, k = xc[0]; k >= 10; k /= 10, d++) {} + i = sd - d; - var x1 = new BN(1); - var x2 = new BN(0); + // If the rounding digit is in the first element of xc... + if (i < 0) { + i += LOG_BASE; + j = sd; + n = xc[ni = 0]; - var delta = b.clone(); + // Get the rounding digit at index j of n. + rd = n / pows10[d - j - 1] % 10 | 0; + } else { + ni = mathceil((i + 1) / LOG_BASE); - while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { - for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1) {} - if (i > 0) { - a.iushrn(i); - while (i-- > 0) { - if (x1.isOdd()) { - x1.iadd(delta); - } + if (ni >= xc.length) { - x1.iushrn(1); - } - } + if (r) { - for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1) {} - if (j > 0) { - b.iushrn(j); - while (j-- > 0) { - if (x2.isOdd()) { - x2.iadd(delta); - } + // Needed by sqrt. + for (; xc.length <= ni; xc.push(0)) {} + n = rd = 0; + d = 1; + i %= LOG_BASE; + j = i - LOG_BASE + 1; + } else { + break out; + } + } else { + n = k = xc[ni]; - x2.iushrn(1); - } - } + // Get the number of digits of n. + for (d = 1; k >= 10; k /= 10, d++) {} - if (a.cmp(b) >= 0) { - a.isub(b); - x1.isub(x2); - } else { - b.isub(a); - x2.isub(x1); - } - } + // Get the index of rd within n. + i %= LOG_BASE; - var res; - if (a.cmpn(1) === 0) { - res = x1; - } else { - res = x2; - } + // Get the index of rd within n, adjusted for leading zeros. + // The number of leading zeros of n is given by LOG_BASE - d. + j = i - LOG_BASE + d; - if (res.cmpn(0) < 0) { - res.iadd(p); - } + // Get the rounding digit at index j of n. + rd = j < 0 ? 0 : n / pows10[d - j - 1] % 10 | 0; + } + } - return res; - }; + r = r || sd < 0 || - BN.prototype.gcd = function gcd(num) { - if (this.isZero()) return num.abs(); - if (num.isZero()) return this.abs(); + // Are there any non-zero digits after the rounding digit? + // The expression n % pows10[ d - j - 1 ] returns all digits of n to the right + // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714. + xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]); - var a = this.clone(); - var b = num.clone(); - a.negative = 0; - b.negative = 0; + r = rm < 4 ? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 && - // Remove common factor of two - for (var shift = 0; a.isEven() && b.isEven(); shift++) { - a.iushrn(1); - b.iushrn(1); - } + // Check whether the digit to the left of the rounding digit is odd. + (i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7)); - do { - while (a.isEven()) { - a.iushrn(1); - } - while (b.isEven()) { - b.iushrn(1); - } + if (sd < 1 || !xc[0]) { + xc.length = 0; - var r = a.cmp(b); - if (r < 0) { - // Swap `a` and `b` to make `a` always bigger than `b` - var t = a; - a = b; - b = t; - } else if (r === 0 || b.cmpn(1) === 0) { - break; - } + if (r) { - a.isub(b); - } while (true); + // Convert sd to decimal places. + sd -= x.e + 1; - return b.iushln(shift); - }; + // 1, 0.1, 0.01, 0.001, 0.0001 etc. + xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE]; + x.e = -sd || 0; + } else { - // Invert number in the field F(num) - BN.prototype.invm = function invm(num) { - return this.egcd(num).a.umod(num); - }; + // Zero. + xc[0] = x.e = 0; + } - BN.prototype.isEven = function isEven() { - return (this.words[0] & 1) === 0; - }; + return x; + } - BN.prototype.isOdd = function isOdd() { - return (this.words[0] & 1) === 1; - }; + // Remove excess digits. + if (i == 0) { + xc.length = ni; + k = 1; + ni--; + } else { + xc.length = ni + 1; + k = pows10[LOG_BASE - i]; - // And first word and num - BN.prototype.andln = function andln(num) { - return this.words[0] & num; - }; + // E.g. 56700 becomes 56000 if 7 is the rounding digit. + // j > 0 means i > number of leading zeros of n. + xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0; + } - // Increment at the bit position in-line - BN.prototype.bincn = function bincn(bit) { - assert(typeof bit === 'number'); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; + // Round up? + if (r) { - // Fast case: bit is much higher than all existing words - if (this.length <= s) { - this._expand(s + 1); - this.words[s] |= q; - return this; - } + for (;;) { - // Add bit and propagate, if needed - var carry = q; - for (var i = s; carry !== 0 && i < this.length; i++) { - var w = this.words[i] | 0; - w += carry; - carry = w >>> 26; - w &= 0x3ffffff; - this.words[i] = w; - } - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } - return this; - }; + // If the digit to be rounded up is in the first element of xc... + if (ni == 0) { - BN.prototype.isZero = function isZero() { - return this.length === 1 && this.words[0] === 0; - }; + // i will be the length of xc[0] before k is added. + for (i = 1, j = xc[0]; j >= 10; j /= 10, i++) {} + j = xc[0] += k; + for (k = 1; j >= 10; j /= 10, k++) {} - BN.prototype.cmpn = function cmpn(num) { - var negative = num < 0; + // if i != k the length has increased. + if (i != k) { + x.e++; + if (xc[0] == BASE) xc[0] = 1; + } - if (this.negative !== 0 && !negative) return -1; - if (this.negative === 0 && negative) return 1; + break; + } else { + xc[ni] += k; + if (xc[ni] != BASE) break; + xc[ni--] = 0; + k = 1; + } + } + } - this.strip(); + // Remove trailing zeros. + for (i = xc.length; xc[--i] === 0; xc.pop()) {} + } - var res; - if (this.length > 1) { - res = 1; - } else { - if (negative) { - num = -num; - } + // Overflow? Infinity. + if (x.e > MAX_EXP) { + x.c = x.e = null; - assert(num <= 0x3ffffff, 'Number is too big'); + // Underflow? Zero. + } else if (x.e < MIN_EXP) { + x.c = [x.e = 0]; + } + } - var w = this.words[0] | 0; - res = w === num ? 0 : w < num ? -1 : 1; - } - if (this.negative !== 0) return -res | 0; - return res; - }; + return x; + } - // Compare two numbers and return: - // 1 - if `this` > `num` - // 0 - if `this` == `num` - // -1 - if `this` < `num` - BN.prototype.cmp = function cmp(num) { - if (this.negative !== 0 && num.negative === 0) return -1; - if (this.negative === 0 && num.negative !== 0) return 1; + // PROTOTYPE/INSTANCE METHODS - var res = this.ucmp(num); - if (this.negative !== 0) return -res | 0; - return res; - }; - // Unsigned comparison - BN.prototype.ucmp = function ucmp(num) { - // At this point both numbers have the same sign - if (this.length > num.length) return 1; - if (this.length < num.length) return -1; + /* + * Return a new BigNumber whose value is the absolute value of this BigNumber. + */ + P.absoluteValue = P.abs = function () { + var x = new BigNumber(this); + if (x.s < 0) x.s = 1; + return x; + }; - var res = 0; - for (var i = this.length - 1; i >= 0; i--) { - var a = this.words[i] | 0; - var b = num.words[i] | 0; + /* + * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole + * number in the direction of Infinity. + */ + P.ceil = function () { + return round(new BigNumber(this), this.e + 1, 2); + }; - if (a === b) continue; - if (a < b) { - res = -1; - } else if (a > b) { - res = 1; - } - break; - } - return res; - }; + /* + * Return + * 1 if the value of this BigNumber is greater than the value of BigNumber(y, b), + * -1 if the value of this BigNumber is less than the value of BigNumber(y, b), + * 0 if they have the same value, + * or null if the value of either is NaN. + */ + P.comparedTo = P.cmp = function (y, b) { + id = 1; + return compare(this, new BigNumber(y, b)); + }; - BN.prototype.gtn = function gtn(num) { - return this.cmpn(num) === 1; - }; + /* + * Return the number of decimal places of the value of this BigNumber, or null if the value + * of this BigNumber is ±Infinity or NaN. + */ + P.decimalPlaces = P.dp = function () { + var n, + v, + c = this.c; - BN.prototype.gt = function gt(num) { - return this.cmp(num) === 1; - }; + if (!c) return null; + n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE; - BN.prototype.gten = function gten(num) { - return this.cmpn(num) >= 0; - }; + // Subtract the number of trailing zeros of the last number. + if (v = c[v]) for (; v % 10 == 0; v /= 10, n--) {} + if (n < 0) n = 0; - BN.prototype.gte = function gte(num) { - return this.cmp(num) >= 0; - }; + return n; + }; - BN.prototype.ltn = function ltn(num) { - return this.cmpn(num) === -1; - }; + /* + * n / 0 = I + * n / N = N + * n / I = 0 + * 0 / n = 0 + * 0 / 0 = N + * 0 / N = N + * 0 / I = 0 + * N / n = N + * N / 0 = N + * N / N = N + * N / I = N + * I / n = I + * I / 0 = I + * I / N = N + * I / I = N + * + * Return a new BigNumber whose value is the value of this BigNumber divided by the value of + * BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE. + */ + P.dividedBy = P.div = function (y, b) { + id = 3; + return div(this, new BigNumber(y, b), DECIMAL_PLACES, ROUNDING_MODE); + }; - BN.prototype.lt = function lt(num) { - return this.cmp(num) === -1; - }; + /* + * Return a new BigNumber whose value is the integer part of dividing the value of this + * BigNumber by the value of BigNumber(y, b). + */ + P.dividedToIntegerBy = P.divToInt = function (y, b) { + id = 4; + return div(this, new BigNumber(y, b), 0, 1); + }; - BN.prototype.lten = function lten(num) { - return this.cmpn(num) <= 0; - }; + /* + * Return true if the value of this BigNumber is equal to the value of BigNumber(y, b), + * otherwise returns false. + */ + P.equals = P.eq = function (y, b) { + id = 5; + return compare(this, new BigNumber(y, b)) === 0; + }; - BN.prototype.lte = function lte(num) { - return this.cmp(num) <= 0; - }; + /* + * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole + * number in the direction of -Infinity. + */ + P.floor = function () { + return round(new BigNumber(this), this.e + 1, 3); + }; - BN.prototype.eqn = function eqn(num) { - return this.cmpn(num) === 0; - }; + /* + * Return true if the value of this BigNumber is greater than the value of BigNumber(y, b), + * otherwise returns false. + */ + P.greaterThan = P.gt = function (y, b) { + id = 6; + return compare(this, new BigNumber(y, b)) > 0; + }; - BN.prototype.eq = function eq(num) { - return this.cmp(num) === 0; - }; + /* + * Return true if the value of this BigNumber is greater than or equal to the value of + * BigNumber(y, b), otherwise returns false. + */ + P.greaterThanOrEqualTo = P.gte = function (y, b) { + id = 7; + return (b = compare(this, new BigNumber(y, b))) === 1 || b === 0; + }; - // - // A reduce context, could be using montgomery or something better, depending - // on the `m` itself. - // - BN.red = function red(num) { - return new Red(num); - }; + /* + * Return true if the value of this BigNumber is a finite number, otherwise returns false. + */ + P.isFinite = function () { + return !!this.c; + }; - BN.prototype.toRed = function toRed(ctx) { - assert(!this.red, 'Already a number in reduction context'); - assert(this.negative === 0, 'red works only with positives'); - return ctx.convertTo(this)._forceRed(ctx); - }; + /* + * Return true if the value of this BigNumber is an integer, otherwise return false. + */ + P.isInteger = P.isInt = function () { + return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2; + }; - BN.prototype.fromRed = function fromRed() { - assert(this.red, 'fromRed works only with numbers in reduction context'); - return this.red.convertFrom(this); - }; + /* + * Return true if the value of this BigNumber is NaN, otherwise returns false. + */ + P.isNaN = function () { + return !this.s; + }; - BN.prototype._forceRed = function _forceRed(ctx) { - this.red = ctx; - return this; - }; + /* + * Return true if the value of this BigNumber is negative, otherwise returns false. + */ + P.isNegative = P.isNeg = function () { + return this.s < 0; + }; - BN.prototype.forceRed = function forceRed(ctx) { - assert(!this.red, 'Already a number in reduction context'); - return this._forceRed(ctx); - }; + /* + * Return true if the value of this BigNumber is 0 or -0, otherwise returns false. + */ + P.isZero = function () { + return !!this.c && this.c[0] == 0; + }; - BN.prototype.redAdd = function redAdd(num) { - assert(this.red, 'redAdd works only with red numbers'); - return this.red.add(this, num); - }; + /* + * Return true if the value of this BigNumber is less than the value of BigNumber(y, b), + * otherwise returns false. + */ + P.lessThan = P.lt = function (y, b) { + id = 8; + return compare(this, new BigNumber(y, b)) < 0; + }; - BN.prototype.redIAdd = function redIAdd(num) { - assert(this.red, 'redIAdd works only with red numbers'); - return this.red.iadd(this, num); - }; + /* + * Return true if the value of this BigNumber is less than or equal to the value of + * BigNumber(y, b), otherwise returns false. + */ + P.lessThanOrEqualTo = P.lte = function (y, b) { + id = 9; + return (b = compare(this, new BigNumber(y, b))) === -1 || b === 0; + }; - BN.prototype.redSub = function redSub(num) { - assert(this.red, 'redSub works only with red numbers'); - return this.red.sub(this, num); - }; + /* + * n - 0 = n + * n - N = N + * n - I = -I + * 0 - n = -n + * 0 - 0 = 0 + * 0 - N = N + * 0 - I = -I + * N - n = N + * N - 0 = N + * N - N = N + * N - I = N + * I - n = I + * I - 0 = I + * I - N = N + * I - I = N + * + * Return a new BigNumber whose value is the value of this BigNumber minus the value of + * BigNumber(y, b). + */ + P.minus = P.sub = function (y, b) { + var i, + j, + t, + xLTy, + x = this, + a = x.s; - BN.prototype.redISub = function redISub(num) { - assert(this.red, 'redISub works only with red numbers'); - return this.red.isub(this, num); - }; + id = 10; + y = new BigNumber(y, b); + b = y.s; - BN.prototype.redShl = function redShl(num) { - assert(this.red, 'redShl works only with red numbers'); - return this.red.shl(this, num); - }; + // Either NaN? + if (!a || !b) return new BigNumber(NaN); - BN.prototype.redMul = function redMul(num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.mul(this, num); - }; + // Signs differ? + if (a != b) { + y.s = -b; + return x.plus(y); + } - BN.prototype.redIMul = function redIMul(num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.imul(this, num); - }; + var xe = x.e / LOG_BASE, + ye = y.e / LOG_BASE, + xc = x.c, + yc = y.c; - BN.prototype.redSqr = function redSqr() { - assert(this.red, 'redSqr works only with red numbers'); - this.red._verify1(this); - return this.red.sqr(this); - }; + if (!xe || !ye) { - BN.prototype.redISqr = function redISqr() { - assert(this.red, 'redISqr works only with red numbers'); - this.red._verify1(this); - return this.red.isqr(this); - }; + // Either Infinity? + if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber(yc ? x : NaN); - // Square root over p - BN.prototype.redSqrt = function redSqrt() { - assert(this.red, 'redSqrt works only with red numbers'); - this.red._verify1(this); - return this.red.sqrt(this); - }; + // Either zero? + if (!xc[0] || !yc[0]) { - BN.prototype.redInvm = function redInvm() { - assert(this.red, 'redInvm works only with red numbers'); - this.red._verify1(this); - return this.red.invm(this); - }; + // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. + return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x : - // Return negative clone of `this` % `red modulo` - BN.prototype.redNeg = function redNeg() { - assert(this.red, 'redNeg works only with red numbers'); - this.red._verify1(this); - return this.red.neg(this); - }; + // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity + ROUNDING_MODE == 3 ? -0 : 0); + } + } - BN.prototype.redPow = function redPow(num) { - assert(this.red && !num.red, 'redPow(normalNum)'); - this.red._verify1(this); - return this.red.pow(this, num); - }; + xe = bitFloor(xe); + ye = bitFloor(ye); + xc = xc.slice(); - // Prime numbers with efficient reduction - var primes = { - k256: null, - p224: null, - p192: null, - p25519: null - }; + // Determine which is the bigger number. + if (a = xe - ye) { - // Pseudo-Mersenne prime - function MPrime(name, p) { - // P = 2 ^ N - K - this.name = name; - this.p = new BN(p, 16); - this.n = this.p.bitLength(); - this.k = new BN(1).iushln(this.n).isub(this.p); + if (xLTy = a < 0) { + a = -a; + t = xc; + } else { + ye = xe; + t = yc; + } - this.tmp = this._tmp(); - } + t.reverse(); - MPrime.prototype._tmp = function _tmp() { - var tmp = new BN(null); - tmp.words = new Array(Math.ceil(this.n / 13)); - return tmp; - }; + // Prepend zeros to equalise exponents. + for (b = a; b--; t.push(0)) {} + t.reverse(); + } else { - MPrime.prototype.ireduce = function ireduce(num) { - // Assumes that `num` is less than `P^2` - // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) - var r = num; - var rlen; + // Exponents equal. Check digit by digit. + j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b; - do { - this.split(r, this.tmp); - r = this.imulK(r); - r = r.iadd(this.tmp); - rlen = r.bitLength(); - } while (rlen > this.n); + for (a = b = 0; b < j; b++) { - var cmp = rlen < this.n ? -1 : r.ucmp(this.p); - if (cmp === 0) { - r.words[0] = 0; - r.length = 1; - } else if (cmp > 0) { - r.isub(this.p); - } else { - r.strip(); - } + if (xc[b] != yc[b]) { + xLTy = xc[b] < yc[b]; + break; + } + } + } - return r; - }; + // x < y? Point xc to the array of the bigger number. + if (xLTy) t = xc, xc = yc, yc = t, y.s = -y.s; - MPrime.prototype.split = function split(input, out) { - input.iushrn(this.n, 0, out); - }; + b = (j = yc.length) - (i = xc.length); - MPrime.prototype.imulK = function imulK(num) { - return num.imul(this.k); - }; + // Append zeros to xc if shorter. + // No need to add zeros to yc if shorter as subtract only needs to start at yc.length. + if (b > 0) for (; b--; xc[i++] = 0) {} + b = BASE - 1; - function K256() { - MPrime.call(this, 'k256', 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); - } - inherits(K256, MPrime); + // Subtract yc from xc. + for (; j > a;) { - K256.prototype.split = function split(input, output) { - // 256 = 9 * 26 + 22 - var mask = 0x3fffff; + if (xc[--j] < yc[j]) { + for (i = j; i && !xc[--i]; xc[i] = b) {} + --xc[i]; + xc[j] += BASE; + } - var outLen = Math.min(input.length, 9); - for (var i = 0; i < outLen; i++) { - output.words[i] = input.words[i]; - } - output.length = outLen; + xc[j] -= yc[j]; + } - if (input.length <= 9) { - input.words[0] = 0; - input.length = 1; - return; - } + // Remove leading zeros and adjust exponent accordingly. + for (; xc[0] == 0; xc.splice(0, 1), --ye) {} - // Shift by 9 limbs - var prev = input.words[9]; - output.words[output.length++] = prev & mask; + // Zero? + if (!xc[0]) { - for (i = 10; i < input.length; i++) { - var next = input.words[i] | 0; - input.words[i - 10] = (next & mask) << 4 | prev >>> 22; - prev = next; - } - prev >>>= 22; - input.words[i - 10] = prev; - if (prev === 0 && input.length > 10) { - input.length -= 10; - } else { - input.length -= 9; - } - }; + // Following IEEE 754 (2008) 6.3, + // n - n = +0 but n - n = -0 when rounding towards -Infinity. + y.s = ROUNDING_MODE == 3 ? -1 : 1; + y.c = [y.e = 0]; + return y; + } - K256.prototype.imulK = function imulK(num) { - // K = 0x1000003d1 = [ 0x40, 0x3d1 ] - num.words[num.length] = 0; - num.words[num.length + 1] = 0; - num.length += 2; + // No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity + // for finite x and y. + return normalise(y, xc, ye); + }; - // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 - var lo = 0; - for (var i = 0; i < num.length; i++) { - var w = num.words[i] | 0; - lo += w * 0x3d1; - num.words[i] = lo & 0x3ffffff; - lo = w * 0x40 + (lo / 0x4000000 | 0); - } + /* + * n % 0 = N + * n % N = N + * n % I = n + * 0 % n = 0 + * -0 % n = -0 + * 0 % 0 = N + * 0 % N = N + * 0 % I = 0 + * N % n = N + * N % 0 = N + * N % N = N + * N % I = N + * I % n = N + * I % 0 = N + * I % N = N + * I % I = N + * + * Return a new BigNumber whose value is the value of this BigNumber modulo the value of + * BigNumber(y, b). The result depends on the value of MODULO_MODE. + */ + P.modulo = P.mod = function (y, b) { + var q, + s, + x = this; - // Fast length reduction - if (num.words[num.length - 1] === 0) { - num.length--; - if (num.words[num.length - 1] === 0) { - num.length--; - } - } - return num; - }; + id = 11; + y = new BigNumber(y, b); - function P224() { - MPrime.call(this, 'p224', 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); - } - inherits(P224, MPrime); + // Return NaN if x is Infinity or NaN, or y is NaN or zero. + if (!x.c || !y.s || y.c && !y.c[0]) { + return new BigNumber(NaN); - function P192() { - MPrime.call(this, 'p192', 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); - } - inherits(P192, MPrime); + // Return x if y is Infinity or x is zero. + } else if (!y.c || x.c && !x.c[0]) { + return new BigNumber(x); + } - function P25519() { - // 2 ^ 255 - 19 - MPrime.call(this, '25519', '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); - } - inherits(P25519, MPrime); + if (MODULO_MODE == 9) { + + // Euclidian division: q = sign(y) * floor(x / abs(y)) + // r = x - qy where 0 <= r < abs(y) + s = y.s; + y.s = 1; + q = div(x, y, 0, 3); + y.s = s; + q.s *= s; + } else { + q = div(x, y, 0, MODULO_MODE); + } - P25519.prototype.imulK = function imulK(num) { - // K = 0x13 - var carry = 0; - for (var i = 0; i < num.length; i++) { - var hi = (num.words[i] | 0) * 0x13 + carry; - var lo = hi & 0x3ffffff; - hi >>>= 26; + return x.minus(q.times(y)); + }; - num.words[i] = lo; - carry = hi; - } - if (carry !== 0) { - num.words[num.length++] = carry; - } - return num; - }; + /* + * Return a new BigNumber whose value is the value of this BigNumber negated, + * i.e. multiplied by -1. + */ + P.negated = P.neg = function () { + var x = new BigNumber(this); + x.s = -x.s || null; + return x; + }; - // Exported mostly for testing purposes, use plain name instead - BN._prime = function prime(name) { - // Cached version of prime - if (primes[name]) return primes[name]; + /* + * n + 0 = n + * n + N = N + * n + I = I + * 0 + n = n + * 0 + 0 = 0 + * 0 + N = N + * 0 + I = I + * N + n = N + * N + 0 = N + * N + N = N + * N + I = N + * I + n = I + * I + 0 = I + * I + N = N + * I + I = I + * + * Return a new BigNumber whose value is the value of this BigNumber plus the value of + * BigNumber(y, b). + */ + P.plus = P.add = function (y, b) { + var t, + x = this, + a = x.s; - var prime; - if (name === 'k256') { - prime = new K256(); - } else if (name === 'p224') { - prime = new P224(); - } else if (name === 'p192') { - prime = new P192(); - } else if (name === 'p25519') { - prime = new P25519(); - } else { - throw new Error('Unknown prime ' + name); - } - primes[name] = prime; + id = 12; + y = new BigNumber(y, b); + b = y.s; - return prime; - }; + // Either NaN? + if (!a || !b) return new BigNumber(NaN); - // - // Base reduction engine - // - function Red(m) { - if (typeof m === 'string') { - var prime = BN._prime(m); - this.m = prime.p; - this.prime = prime; - } else { - assert(m.gtn(1), 'modulus must be greater than 1'); - this.m = m; - this.prime = null; - } - } + // Signs differ? + if (a != b) { + y.s = -b; + return x.minus(y); + } - Red.prototype._verify1 = function _verify1(a) { - assert(a.negative === 0, 'red works only with positives'); - assert(a.red, 'red works only with red numbers'); - }; + var xe = x.e / LOG_BASE, + ye = y.e / LOG_BASE, + xc = x.c, + yc = y.c; - Red.prototype._verify2 = function _verify2(a, b) { - assert((a.negative | b.negative) === 0, 'red works only with positives'); - assert(a.red && a.red === b.red, 'red works only with red numbers'); - }; + if (!xe || !ye) { - Red.prototype.imod = function imod(a) { - if (this.prime) return this.prime.ireduce(a)._forceRed(this); - return a.umod(this.m)._forceRed(this); - }; + // Return ±Infinity if either ±Infinity. + if (!xc || !yc) return new BigNumber(a / 0); - Red.prototype.neg = function neg(a) { - if (a.isZero()) { - return a.clone(); - } + // Either zero? + // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. + if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber(xc[0] ? x : a * 0); + } - return this.m.sub(a)._forceRed(this); - }; + xe = bitFloor(xe); + ye = bitFloor(ye); + xc = xc.slice(); - Red.prototype.add = function add(a, b) { - this._verify2(a, b); + // Prepend zeros to equalise exponents. Faster to use reverse then do unshifts. + if (a = xe - ye) { + if (a > 0) { + ye = xe; + t = yc; + } else { + a = -a; + t = xc; + } - var res = a.add(b); - if (res.cmp(this.m) >= 0) { - res.isub(this.m); - } - return res._forceRed(this); - }; + t.reverse(); + for (; a--; t.push(0)) {} + t.reverse(); + } - Red.prototype.iadd = function iadd(a, b) { - this._verify2(a, b); + a = xc.length; + b = yc.length; - var res = a.iadd(b); - if (res.cmp(this.m) >= 0) { - res.isub(this.m); - } - return res; - }; + // Point xc to the longer array, and b to the shorter length. + if (a - b < 0) t = yc, yc = xc, xc = t, b = a; - Red.prototype.sub = function sub(a, b) { - this._verify2(a, b); + // Only start adding at yc.length - 1 as the further digits of xc can be ignored. + for (a = 0; b;) { + a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0; + xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE; + } - var res = a.sub(b); - if (res.cmpn(0) < 0) { - res.iadd(this.m); - } - return res._forceRed(this); - }; + if (a) { + xc = [a].concat(xc); + ++ye; + } - Red.prototype.isub = function isub(a, b) { - this._verify2(a, b); + // No need to check for zero, as +x + +y != 0 && -x + -y != 0 + // ye = MAX_EXP + 1 possible + return normalise(y, xc, ye); + }; - var res = a.isub(b); - if (res.cmpn(0) < 0) { - res.iadd(this.m); - } - return res; - }; + /* + * Return the number of significant digits of the value of this BigNumber. + * + * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. + */ + P.precision = P.sd = function (z) { + var n, + v, + x = this, + c = x.c; - Red.prototype.shl = function shl(a, num) { - this._verify1(a); - return this.imod(a.ushln(num)); - }; + // 'precision() argument not a boolean or binary digit: {z}' + if (z != null && z !== !!z && z !== 1 && z !== 0) { + if (ERRORS) raise(13, 'argument' + notBool, z); + if (z != !!z) z = null; + } - Red.prototype.imul = function imul(a, b) { - this._verify2(a, b); - return this.imod(a.imul(b)); - }; + if (!c) return null; + v = c.length - 1; + n = v * LOG_BASE + 1; - Red.prototype.mul = function mul(a, b) { - this._verify2(a, b); - return this.imod(a.mul(b)); - }; + if (v = c[v]) { - Red.prototype.isqr = function isqr(a) { - return this.imul(a, a.clone()); - }; + // Subtract the number of trailing zeros of the last element. + for (; v % 10 == 0; v /= 10, n--) {} - Red.prototype.sqr = function sqr(a) { - return this.mul(a, a); - }; + // Add the number of digits of the first element. + for (v = c[0]; v >= 10; v /= 10, n++) {} + } - Red.prototype.sqrt = function sqrt(a) { - if (a.isZero()) return a.clone(); + if (z && x.e + 1 > n) n = x.e + 1; - var mod3 = this.m.andln(3); - assert(mod3 % 2 === 1); + return n; + }; - // Fast case - if (mod3 === 3) { - var pow = this.m.add(new BN(1)).iushrn(2); - return this.pow(a, pow); - } + /* + * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of + * dp decimal places using rounding mode rm, or to 0 and ROUNDING_MODE respectively if + * omitted. + * + * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'round() decimal places out of range: {dp}' + * 'round() decimal places not an integer: {dp}' + * 'round() rounding mode not an integer: {rm}' + * 'round() rounding mode out of range: {rm}' + */ + P.round = function (dp, rm) { + var n = new BigNumber(this); - // Tonelli-Shanks algorithm (Totally unoptimized and slow) - // - // Find Q and S, that Q * 2 ^ S = (P - 1) - var q = this.m.subn(1); - var s = 0; - while (!q.isZero() && q.andln(1) === 0) { - s++; - q.iushrn(1); - } - assert(!q.isZero()); + if (dp == null || isValidInt(dp, 0, MAX, 15)) { + round(n, ~~dp + this.e + 1, rm == null || !isValidInt(rm, 0, 8, 15, roundingMode) ? ROUNDING_MODE : rm | 0); + } - var one = new BN(1).toRed(this); - var nOne = one.redNeg(); + return n; + }; - // Find quadratic non-residue - // NOTE: Max is such because of generalized Riemann hypothesis. - var lpow = this.m.subn(1).iushrn(1); - var z = this.m.bitLength(); - z = new BN(2 * z * z).toRed(this); + /* + * Return a new BigNumber whose value is the value of this BigNumber shifted by k places + * (powers of 10). Shift to the right if n > 0, and to the left if n < 0. + * + * k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. + * + * If k is out of range and ERRORS is false, the result will be ±0 if k < 0, or ±Infinity + * otherwise. + * + * 'shift() argument not an integer: {k}' + * 'shift() argument out of range: {k}' + */ + P.shift = function (k) { + var n = this; + return isValidInt(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 16, 'argument') - while (this.pow(z, lpow).cmp(nOne) !== 0) { - z.redIAdd(nOne); - } + // k < 1e+21, or truncate(k) will produce exponential notation. + ? n.times('1e' + truncate(k)) : new BigNumber(n.c && n.c[0] && (k < -MAX_SAFE_INTEGER || k > MAX_SAFE_INTEGER) ? n.s * (k < 0 ? 0 : 1 / 0) : n); + }; - var c = this.pow(z, q); - var r = this.pow(a, q.addn(1).iushrn(1)); - var t = this.pow(a, q); - var m = s; - while (t.cmp(one) !== 0) { - var tmp = t; - for (var i = 0; tmp.cmp(one) !== 0; i++) { - tmp = tmp.redSqr(); - } - assert(i < m); - var b = this.pow(c, new BN(1).iushln(m - i - 1)); + /* + * sqrt(-n) = N + * sqrt( N) = N + * sqrt(-I) = N + * sqrt( I) = I + * sqrt( 0) = 0 + * sqrt(-0) = -0 + * + * Return a new BigNumber whose value is the square root of the value of this BigNumber, + * rounded according to DECIMAL_PLACES and ROUNDING_MODE. + */ + P.squareRoot = P.sqrt = function () { + var m, + n, + r, + rep, + t, + x = this, + c = x.c, + s = x.s, + e = x.e, + dp = DECIMAL_PLACES + 4, + half = new BigNumber('0.5'); - r = r.redMul(b); - c = b.redSqr(); - t = t.redMul(c); - m = i; - } + // Negative/NaN/Infinity/zero? + if (s !== 1 || !c || !c[0]) { + return new BigNumber(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0); + } - return r; - }; + // Initial estimate. + s = Math.sqrt(+x); - Red.prototype.invm = function invm(a) { - var inv = a._invmp(this.m); - if (inv.negative !== 0) { - inv.negative = 0; - return this.imod(inv).redNeg(); - } else { - return this.imod(inv); - } - }; + // Math.sqrt underflow/overflow? + // Pass x to Math.sqrt as integer, then adjust the exponent of the result. + if (s == 0 || s == 1 / 0) { + n = coeffToString(c); + if ((n.length + e) % 2 == 0) n += '0'; + s = Math.sqrt(n); + e = bitFloor((e + 1) / 2) - (e < 0 || e % 2); - Red.prototype.pow = function pow(a, num) { - if (num.isZero()) return new BN(1); - if (num.cmpn(1) === 0) return a.clone(); + if (s == 1 / 0) { + n = '1e' + e; + } else { + n = s.toExponential(); + n = n.slice(0, n.indexOf('e') + 1) + e; + } - var windowSize = 4; - var wnd = new Array(1 << windowSize); - wnd[0] = new BN(1).toRed(this); - wnd[1] = a; - for (var i = 2; i < wnd.length; i++) { - wnd[i] = this.mul(wnd[i - 1], a); - } + r = new BigNumber(n); + } else { + r = new BigNumber(s + ''); + } - var res = wnd[0]; - var current = 0; - var currentLen = 0; - var start = num.bitLength() % 26; - if (start === 0) { - start = 26; - } + // Check for zero. + // r could be zero if MIN_EXP is changed after the this value was created. + // This would cause a division by zero (x/t) and hence Infinity below, which would cause + // coeffToString to throw. + if (r.c[0]) { + e = r.e; + s = e + dp; + if (s < 3) s = 0; - for (i = num.length - 1; i >= 0; i--) { - var word = num.words[i]; - for (var j = start - 1; j >= 0; j--) { - var bit = word >> j & 1; - if (res !== wnd[0]) { - res = this.sqr(res); - } + // Newton-Raphson iteration. + for (;;) { + t = r; + r = half.times(t.plus(div(x, t, dp, 1))); - if (bit === 0 && current === 0) { - currentLen = 0; - continue; - } + if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) { - current <<= 1; - current |= bit; - currentLen++; - if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; + // The exponent of r may here be one less than the final result exponent, + // e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits + // are indexed correctly. + if (r.e < e) --s; + n = n.slice(s - 3, s + 1); - res = this.mul(res, wnd[current]); - currentLen = 0; - current = 0; - } - start = 26; - } + // The 4th rounding digit may be in error by -1 so if the 4 rounding digits + // are 9999 or 4999 (i.e. approaching a rounding boundary) continue the + // iteration. + if (n == '9999' || !rep && n == '4999') { - return res; - }; + // On the first iteration only, check to see if rounding up gives the + // exact result as the nines may infinitely repeat. + if (!rep) { + round(t, t.e + DECIMAL_PLACES + 2, 0); - Red.prototype.convertTo = function convertTo(num) { - var r = num.umod(this.m); + if (t.times(t).eq(x)) { + r = t; + break; + } + } - return r === num ? r.clone() : r; - }; + dp += 4; + s += 4; + rep = 1; + } else { - Red.prototype.convertFrom = function convertFrom(num) { - var res = num.clone(); - res.red = null; - return res; - }; + // If rounding digits are null, 0{0,4} or 50{0,3}, check for exact + // result. If not, then there are further digits and m will be truthy. + if (!+n || !+n.slice(1) && n.charAt(0) == '5') { - // - // Montgomery method engine - // + // Truncate to the first rounding digit. + round(r, r.e + DECIMAL_PLACES + 2, 1); + m = !r.times(r).eq(x); + } - BN.mont = function mont(num) { - return new Mont(num); - }; + break; + } + } + } + } - function Mont(m) { - Red.call(this, m); + return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m); + }; - this.shift = this.m.bitLength(); - if (this.shift % 26 !== 0) { - this.shift += 26 - this.shift % 26; - } + /* + * n * 0 = 0 + * n * N = N + * n * I = I + * 0 * n = 0 + * 0 * 0 = 0 + * 0 * N = N + * 0 * I = N + * N * n = N + * N * 0 = N + * N * N = N + * N * I = N + * I * n = I + * I * 0 = N + * I * N = N + * I * I = I + * + * Return a new BigNumber whose value is the value of this BigNumber times the value of + * BigNumber(y, b). + */ + P.times = P.mul = function (y, b) { + var c, + e, + i, + j, + k, + m, + xcL, + xlo, + xhi, + ycL, + ylo, + yhi, + zc, + base, + sqrtBase, + x = this, + xc = x.c, + yc = (id = 17, y = new BigNumber(y, b)).c; - this.r = new BN(1).iushln(this.shift); - this.r2 = this.imod(this.r.sqr()); - this.rinv = this.r._invmp(this.m); + // Either NaN, ±Infinity or ±0? + if (!xc || !yc || !xc[0] || !yc[0]) { - this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); - this.minv = this.minv.umod(this.r); - this.minv = this.r.sub(this.minv); - } - inherits(Mont, Red); + // Return NaN if either is NaN, or one is 0 and the other is Infinity. + if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) { + y.c = y.e = y.s = null; + } else { + y.s *= x.s; - Mont.prototype.convertTo = function convertTo(num) { - return this.imod(num.ushln(this.shift)); - }; + // Return ±Infinity if either is ±Infinity. + if (!xc || !yc) { + y.c = y.e = null; - Mont.prototype.convertFrom = function convertFrom(num) { - var r = this.imod(num.mul(this.rinv)); - r.red = null; - return r; - }; + // Return ±0 if either is ±0. + } else { + y.c = [0]; + y.e = 0; + } + } - Mont.prototype.imul = function imul(a, b) { - if (a.isZero() || b.isZero()) { - a.words[0] = 0; - a.length = 1; - return a; - } + return y; + } - var t = a.imul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).iushrn(this.shift); - var res = u; + e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE); + y.s *= x.s; + xcL = xc.length; + ycL = yc.length; - if (u.cmp(this.m) >= 0) { - res = u.isub(this.m); - } else if (u.cmpn(0) < 0) { - res = u.iadd(this.m); - } + // Ensure xc points to longer array and xcL to its length. + if (xcL < ycL) zc = xc, xc = yc, yc = zc, i = xcL, xcL = ycL, ycL = i; - return res._forceRed(this); - }; + // Initialise the result array with zeros. + for (i = xcL + ycL, zc = []; i--; zc.push(0)) {} - Mont.prototype.mul = function mul(a, b) { - if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); + base = BASE; + sqrtBase = SQRT_BASE; - var t = a.mul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).iushrn(this.shift); - var res = u; - if (u.cmp(this.m) >= 0) { - res = u.isub(this.m); - } else if (u.cmpn(0) < 0) { - res = u.iadd(this.m); - } + for (i = ycL; --i >= 0;) { + c = 0; + ylo = yc[i] % sqrtBase; + yhi = yc[i] / sqrtBase | 0; - return res._forceRed(this); - }; + for (k = xcL, j = i + k; j > i;) { + xlo = xc[--k] % sqrtBase; + xhi = xc[k] / sqrtBase | 0; + m = yhi * xlo + xhi * ylo; + xlo = ylo * xlo + m % sqrtBase * sqrtBase + zc[j] + c; + c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi; + zc[j--] = xlo % base; + } - Mont.prototype.invm = function invm(a) { - // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R - var res = this.imod(a._invmp(this.m).mul(this.r2)); - return res._forceRed(this); - }; -})(typeof module === 'undefined' || module, undefined); + zc[j] = c; + } -},{}],22:[function(require,module,exports){ -(function (global){ -'use strict'; + if (c) { + ++e; + } else { + zc.splice(0, 1); + } -// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js -// original notice: + return normalise(y, zc, e); + }; -/*! - * The buffer module from node.js, for the browser. - * - * @author Feross Aboukhadijeh - * @license MIT - */ + /* + * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of + * sd significant digits using rounding mode rm, or ROUNDING_MODE if rm is omitted. + * + * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'toDigits() precision out of range: {sd}' + * 'toDigits() precision not an integer: {sd}' + * 'toDigits() rounding mode not an integer: {rm}' + * 'toDigits() rounding mode out of range: {rm}' + */ + P.toDigits = function (sd, rm) { + var n = new BigNumber(this); + sd = sd == null || !isValidInt(sd, 1, MAX, 18, 'precision') ? null : sd | 0; + rm = rm == null || !isValidInt(rm, 0, 8, 18, roundingMode) ? ROUNDING_MODE : rm | 0; + return sd ? round(n, sd, rm) : n; + }; -var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + /* + * Return a string representing the value of this BigNumber in exponential notation and + * rounded using ROUNDING_MODE to dp fixed decimal places. + * + * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'toExponential() decimal places not an integer: {dp}' + * 'toExponential() decimal places out of range: {dp}' + * 'toExponential() rounding mode not an integer: {rm}' + * 'toExponential() rounding mode out of range: {rm}' + */ + P.toExponential = function (dp, rm) { + return format(this, dp != null && isValidInt(dp, 0, MAX, 19) ? ~~dp + 1 : null, rm, 19); + }; -function compare(a, b) { - if (a === b) { - return 0; - } + /* + * Return a string representing the value of this BigNumber in fixed-point notation rounding + * to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted. + * + * Note: as with JavaScript's number type, (-0).toFixed(0) is '0', + * but e.g. (-0.00001).toFixed(0) is '-0'. + * + * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'toFixed() decimal places not an integer: {dp}' + * 'toFixed() decimal places out of range: {dp}' + * 'toFixed() rounding mode not an integer: {rm}' + * 'toFixed() rounding mode out of range: {rm}' + */ + P.toFixed = function (dp, rm) { + return format(this, dp != null && isValidInt(dp, 0, MAX, 20) ? ~~dp + this.e + 1 : null, rm, 20); + }; - var x = a.length; - var y = b.length; + /* + * Return a string representing the value of this BigNumber in fixed-point notation rounded + * using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties + * of the FORMAT object (see BigNumber.config). + * + * FORMAT = { + * decimalSeparator : '.', + * groupSeparator : ',', + * groupSize : 3, + * secondaryGroupSize : 0, + * fractionGroupSeparator : '\xA0', // non-breaking space + * fractionGroupSize : 0 + * }; + * + * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'toFormat() decimal places not an integer: {dp}' + * 'toFormat() decimal places out of range: {dp}' + * 'toFormat() rounding mode not an integer: {rm}' + * 'toFormat() rounding mode out of range: {rm}' + */ + P.toFormat = function (dp, rm) { + var str = format(this, dp != null && isValidInt(dp, 0, MAX, 21) ? ~~dp + this.e + 1 : null, rm, 21); - for (var i = 0, len = Math.min(x, y); i < len; ++i) { - if (a[i] !== b[i]) { - x = a[i]; - y = b[i]; - break; - } - } + if (this.c) { + var i, + arr = str.split('.'), + g1 = +FORMAT.groupSize, + g2 = +FORMAT.secondaryGroupSize, + groupSeparator = FORMAT.groupSeparator, + intPart = arr[0], + fractionPart = arr[1], + isNeg = this.s < 0, + intDigits = isNeg ? intPart.slice(1) : intPart, + len = intDigits.length; - if (x < y) { - return -1; - } - if (y < x) { - return 1; - } - return 0; -} -function isBuffer(b) { - if (global.Buffer && typeof global.Buffer.isBuffer === 'function') { - return global.Buffer.isBuffer(b); - } - return !!(b != null && b._isBuffer); -} + if (g2) i = g1, g1 = g2, g2 = i, len -= i; -// based on node assert, original notice: + if (g1 > 0 && len > 0) { + i = len % g1 || g1; + intPart = intDigits.substr(0, i); -// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 -// -// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! -// -// Originally from narwhal.js (http://narwhaljs.org) -// Copyright (c) 2009 Thomas Robinson <280north.com> -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the 'Software'), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + for (; i < len; i += g1) { + intPart += groupSeparator + intDigits.substr(i, g1); + } -var util = require('util/'); -var hasOwn = Object.prototype.hasOwnProperty; -var pSlice = Array.prototype.slice; -var functionsHaveNames = function () { - return function foo() {}.name === 'foo'; -}(); -function pToString(obj) { - return Object.prototype.toString.call(obj); -} -function isView(arrbuf) { - if (isBuffer(arrbuf)) { - return false; - } - if (typeof global.ArrayBuffer !== 'function') { - return false; - } - if (typeof ArrayBuffer.isView === 'function') { - return ArrayBuffer.isView(arrbuf); - } - if (!arrbuf) { - return false; - } - if (arrbuf instanceof DataView) { - return true; - } - if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { - return true; - } - return false; -} -// 1. The assert module provides functions that throw -// AssertionError's when particular conditions are not met. The -// assert module must conform to the following interface. + if (g2 > 0) intPart += groupSeparator + intDigits.slice(i); + if (isNeg) intPart = '-' + intPart; + } -var assert = module.exports = ok; + str = fractionPart ? intPart + FORMAT.decimalSeparator + ((g2 = +FORMAT.fractionGroupSize) ? fractionPart.replace(new RegExp('\\d{' + g2 + '}\\B', 'g'), '$&' + FORMAT.fractionGroupSeparator) : fractionPart) : intPart; + } -// 2. The AssertionError is defined in assert. -// new assert.AssertionError({ message: message, -// actual: actual, -// expected: expected }) + return str; + }; -var regex = /\s*function\s+([^\(\s]*)\s*/; -// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js -function getName(func) { - if (!util.isFunction(func)) { - return; - } - if (functionsHaveNames) { - return func.name; - } - var str = func.toString(); - var match = str.match(regex); - return match && match[1]; -} -assert.AssertionError = function AssertionError(options) { - this.name = 'AssertionError'; - this.actual = options.actual; - this.expected = options.expected; - this.operator = options.operator; - if (options.message) { - this.message = options.message; - this.generatedMessage = false; - } else { - this.message = getMessage(this); - this.generatedMessage = true; - } - var stackStartFunction = options.stackStartFunction || fail; - if (Error.captureStackTrace) { - Error.captureStackTrace(this, stackStartFunction); - } else { - // non v8 browsers so we can have a stacktrace - var err = new Error(); - if (err.stack) { - var out = err.stack; + /* + * Return a string array representing the value of this BigNumber as a simple fraction with + * an integer numerator and an integer denominator. The denominator will be a positive + * non-zero value less than or equal to the specified maximum denominator. If a maximum + * denominator is not specified, the denominator will be the lowest value necessary to + * represent the number exactly. + * + * [md] {number|string|BigNumber} Integer >= 1 and < Infinity. The maximum denominator. + * + * 'toFraction() max denominator not an integer: {md}' + * 'toFraction() max denominator out of range: {md}' + */ + P.toFraction = function (md) { + var arr, + d0, + d2, + e, + exp, + n, + n0, + q, + s, + k = ERRORS, + x = this, + xc = x.c, + d = new BigNumber(ONE), + n1 = d0 = new BigNumber(ONE), + d1 = n0 = new BigNumber(ONE); - // try to strip useless frames - var fn_name = getName(stackStartFunction); - var idx = out.indexOf('\n' + fn_name); - if (idx >= 0) { - // once we have located the function frame - // we need to strip out everything before it (and its line) - var next_line = out.indexOf('\n', idx + 1); - out = out.substring(next_line + 1); - } + if (md != null) { + ERRORS = false; + n = new BigNumber(md); + ERRORS = k; - this.stack = out; - } - } -}; + if (!(k = n.isInt()) || n.lt(ONE)) { -// assert.AssertionError instanceof Error -util.inherits(assert.AssertionError, Error); + if (ERRORS) { + raise(22, 'max denominator ' + (k ? 'out of range' : 'not an integer'), md); + } -function truncate(s, n) { - if (typeof s === 'string') { - return s.length < n ? s : s.slice(0, n); - } else { - return s; - } -} -function inspect(something) { - if (functionsHaveNames || !util.isFunction(something)) { - return util.inspect(something); - } - var rawname = getName(something); - var name = rawname ? ': ' + rawname : ''; - return '[Function' + name + ']'; -} -function getMessage(self) { - return truncate(inspect(self.actual), 128) + ' ' + self.operator + ' ' + truncate(inspect(self.expected), 128); -} + // ERRORS is false: + // If md is a finite non-integer >= 1, round it to an integer and use it. + md = !k && n.c && round(n, n.e + 1, 1).gte(ONE) ? n : null; + } + } -// At present only the three keys mentioned above are used and -// understood by the spec. Implementations or sub modules can pass -// other keys to the AssertionError's constructor - they will be -// ignored. + if (!xc) return x.toString(); + s = coeffToString(xc); -// 3. All of the following functions must throw an AssertionError -// when a corresponding condition is not met, with a message that -// may be undefined if not provided. All assertion methods provide -// both the actual and expected values to the assertion error for -// display purposes. + // Determine initial denominator. + // d is a power of 10 and the minimum max denominator that specifies the value exactly. + e = d.e = s.length - x.e - 1; + d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp]; + md = !md || n.cmp(d) > 0 ? e > 0 ? d : n1 : n; -function fail(actual, expected, message, operator, stackStartFunction) { - throw new assert.AssertionError({ - message: message, - actual: actual, - expected: expected, - operator: operator, - stackStartFunction: stackStartFunction - }); -} + exp = MAX_EXP; + MAX_EXP = 1 / 0; + n = new BigNumber(s); -// EXTENSION! allows for well behaved errors defined elsewhere. -assert.fail = fail; + // n0 = d1 = 0 + n0.c[0] = 0; -// 4. Pure assertion tests whether a value is truthy, as determined -// by !!guard. -// assert.ok(guard, message_opt); -// This statement is equivalent to assert.equal(true, !!guard, -// message_opt);. To test strictly for the value true, use -// assert.strictEqual(true, guard, message_opt);. + for (;;) { + q = div(n, d, 0, 1); + d2 = d0.plus(q.times(d1)); + if (d2.cmp(md) == 1) break; + d0 = d1; + d1 = d2; + n1 = n0.plus(q.times(d2 = n1)); + n0 = d2; + d = n.minus(q.times(d2 = d)); + n = d2; + } -function ok(value, message) { - if (!value) fail(value, true, message, '==', assert.ok); -} -assert.ok = ok; + d2 = div(md.minus(d0), d1, 0, 1); + n0 = n0.plus(d2.times(n1)); + d0 = d0.plus(d2.times(d1)); + n0.s = n1.s = x.s; + e *= 2; -// 5. The equality assertion tests shallow, coercive equality with -// ==. -// assert.equal(actual, expected, message_opt); + // Determine which fraction is closer to x, n0/d0 or n1/d1 + arr = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().cmp(div(n0, d0, e, ROUNDING_MODE).minus(x).abs()) < 1 ? [n1.toString(), d1.toString()] : [n0.toString(), d0.toString()]; -assert.equal = function equal(actual, expected, message) { - if (actual != expected) fail(actual, expected, message, '==', assert.equal); -}; + MAX_EXP = exp; + return arr; + }; -// 6. The non-equality assertion tests for whether two objects are not equal -// with != assert.notEqual(actual, expected, message_opt); + /* + * Return the value of this BigNumber converted to a number primitive. + */ + P.toNumber = function () { + return +this; + }; -assert.notEqual = function notEqual(actual, expected, message) { - if (actual == expected) { - fail(actual, expected, message, '!=', assert.notEqual); - } -}; + /* + * Return a BigNumber whose value is the value of this BigNumber raised to the power n. + * If m is present, return the result modulo m. + * If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE. + * If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using + * ROUNDING_MODE. + * + * The modular power operation works efficiently when x, n, and m are positive integers, + * otherwise it is equivalent to calculating x.toPower(n).modulo(m) (with POW_PRECISION 0). + * + * n {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. + * [m] {number|string|BigNumber} The modulus. + * + * 'pow() exponent not an integer: {n}' + * 'pow() exponent out of range: {n}' + * + * Performs 54 loop iterations for n of 9007199254740991. + */ + P.toPower = P.pow = function (n, m) { + var k, + y, + z, + i = mathfloor(n < 0 ? -n : +n), + x = this; -// 7. The equivalence assertion tests a deep equality relation. -// assert.deepEqual(actual, expected, message_opt); + if (m != null) { + id = 23; + m = new BigNumber(m); + } -assert.deepEqual = function deepEqual(actual, expected, message) { - if (!_deepEqual(actual, expected, false)) { - fail(actual, expected, message, 'deepEqual', assert.deepEqual); - } -}; + // Pass ±Infinity to Math.pow if exponent is out of range. + if (!isValidInt(n, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 23, 'exponent') && (!isFinite(n) || i > MAX_SAFE_INTEGER && (n /= 0) || parseFloat(n) != n && !(n = NaN)) || n == 0) { + k = Math.pow(+x, n); + return new BigNumber(m ? k % m : k); + } -assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { - if (!_deepEqual(actual, expected, true)) { - fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); - } -}; + if (m) { + if (n > 1 && x.gt(ONE) && x.isInt() && m.gt(ONE) && m.isInt()) { + x = x.mod(m); + } else { + z = m; -function _deepEqual(actual, expected, strict, memos) { - // 7.1. All identical values are equivalent, as determined by ===. - if (actual === expected) { - return true; - } else if (isBuffer(actual) && isBuffer(expected)) { - return compare(actual, expected) === 0; + // Nullify m so only a single mod operation is performed at the end. + m = null; + } + } else if (POW_PRECISION) { - // 7.2. If the expected value is a Date object, the actual value is - // equivalent if it is also a Date object that refers to the same time. - } else if (util.isDate(actual) && util.isDate(expected)) { - return actual.getTime() === expected.getTime(); + // Truncating each coefficient array to a length of k after each multiplication + // equates to truncating significant digits to POW_PRECISION + [28, 41], + // i.e. there will be a minimum of 28 guard digits retained. + // (Using + 1.5 would give [9, 21] guard digits.) + k = mathceil(POW_PRECISION / LOG_BASE + 2); + } - // 7.3 If the expected value is a RegExp object, the actual value is - // equivalent if it is also a RegExp object with the same source and - // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). - } else if (util.isRegExp(actual) && util.isRegExp(expected)) { - return actual.source === expected.source && actual.global === expected.global && actual.multiline === expected.multiline && actual.lastIndex === expected.lastIndex && actual.ignoreCase === expected.ignoreCase; + y = new BigNumber(ONE); - // 7.4. Other pairs that do not both pass typeof value == 'object', - // equivalence is determined by ==. - } else if ((actual === null || (typeof actual === 'undefined' ? 'undefined' : _typeof(actual)) !== 'object') && (expected === null || (typeof expected === 'undefined' ? 'undefined' : _typeof(expected)) !== 'object')) { - return strict ? actual === expected : actual == expected; + for (;;) { + if (i % 2) { + y = y.times(x); + if (!y.c) break; + if (k) { + if (y.c.length > k) y.c.length = k; + } else if (m) { + y = y.mod(m); + } + } - // If both values are instances of typed arrays, wrap their underlying - // ArrayBuffers in a Buffer each to increase performance - // This optimization requires the arrays to have the same type as checked by - // Object.prototype.toString (aka pToString). Never perform binary - // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their - // bit patterns are not identical. - } else if (isView(actual) && isView(expected) && pToString(actual) === pToString(expected) && !(actual instanceof Float32Array || actual instanceof Float64Array)) { - return compare(new Uint8Array(actual.buffer), new Uint8Array(expected.buffer)) === 0; + i = mathfloor(i / 2); + if (!i) break; + x = x.times(x); + if (k) { + if (x.c && x.c.length > k) x.c.length = k; + } else if (m) { + x = x.mod(m); + } + } - // 7.5 For all other Object pairs, including Array objects, equivalence is - // determined by having the same number of owned properties (as verified - // with Object.prototype.hasOwnProperty.call), the same set of keys - // (although not necessarily the same order), equivalent values for every - // corresponding key, and an identical 'prototype' property. Note: this - // accounts for both named and indexed properties on Arrays. - } else if (isBuffer(actual) !== isBuffer(expected)) { - return false; - } else { - memos = memos || { actual: [], expected: [] }; + if (m) return y; + if (n < 0) y = ONE.div(y); - var actualIndex = memos.actual.indexOf(actual); - if (actualIndex !== -1) { - if (actualIndex === memos.expected.indexOf(expected)) { - return true; - } - } + return z ? y.mod(z) : k ? round(y, POW_PRECISION, ROUNDING_MODE) : y; + }; - memos.actual.push(actual); - memos.expected.push(expected); + /* + * Return a string representing the value of this BigNumber rounded to sd significant digits + * using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits + * necessary to represent the integer part of the value in fixed-point notation, then use + * exponential notation. + * + * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. + * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. + * + * 'toPrecision() precision not an integer: {sd}' + * 'toPrecision() precision out of range: {sd}' + * 'toPrecision() rounding mode not an integer: {rm}' + * 'toPrecision() rounding mode out of range: {rm}' + */ + P.toPrecision = function (sd, rm) { + return format(this, sd != null && isValidInt(sd, 1, MAX, 24, 'precision') ? sd | 0 : null, rm, 24); + }; - return objEquiv(actual, expected, strict, memos); - } -} + /* + * Return a string representing the value of this BigNumber in base b, or base 10 if b is + * omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and + * ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent + * that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than + * TO_EXP_NEG, return exponential notation. + * + * [b] {number} Integer, 2 to 64 inclusive. + * + * 'toString() base not an integer: {b}' + * 'toString() base out of range: {b}' + */ + P.toString = function (b) { + var str, + n = this, + s = n.s, + e = n.e; -function isArguments(object) { - return Object.prototype.toString.call(object) == '[object Arguments]'; -} + // Infinity or NaN? + if (e === null) { -function objEquiv(a, b, strict, actualVisitedObjects) { - if (a === null || a === undefined || b === null || b === undefined) return false; - // if one is a primitive, the other must be same - if (util.isPrimitive(a) || util.isPrimitive(b)) return a === b; - if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; - var aIsArgs = isArguments(a); - var bIsArgs = isArguments(b); - if (aIsArgs && !bIsArgs || !aIsArgs && bIsArgs) return false; - if (aIsArgs) { - a = pSlice.call(a); - b = pSlice.call(b); - return _deepEqual(a, b, strict); - } - var ka = objectKeys(a); - var kb = objectKeys(b); - var key, i; - // having the same number of owned properties (keys incorporates - // hasOwnProperty) - if (ka.length !== kb.length) return false; - //the same set of keys (although not necessarily the same order), - ka.sort(); - kb.sort(); - //~~~cheap key test - for (i = ka.length - 1; i >= 0; i--) { - if (ka[i] !== kb[i]) return false; - } - //equivalent values for every corresponding key, and - //~~~possibly expensive deep test - for (i = ka.length - 1; i >= 0; i--) { - key = ka[i]; - if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) return false; - } - return true; -} + if (s) { + str = 'Infinity'; + if (s < 0) str = '-' + str; + } else { + str = 'NaN'; + } + } else { + str = coeffToString(n.c); -// 8. The non-equivalence assertion tests for any deep inequality. -// assert.notDeepEqual(actual, expected, message_opt); + if (b == null || !isValidInt(b, 2, 64, 25, 'base')) { + str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(str, e) : toFixedPoint(str, e); + } else { + str = convertBase(toFixedPoint(str, e), b | 0, 10, s); + } -assert.notDeepEqual = function notDeepEqual(actual, expected, message) { - if (_deepEqual(actual, expected, false)) { - fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); - } -}; + if (s < 0 && n.c[0]) str = '-' + str; + } -assert.notDeepStrictEqual = notDeepStrictEqual; -function notDeepStrictEqual(actual, expected, message) { - if (_deepEqual(actual, expected, true)) { - fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); - } -} + return str; + }; -// 9. The strict equality assertion tests strict equality, as determined by ===. -// assert.strictEqual(actual, expected, message_opt); + /* + * Return a new BigNumber whose value is the value of this BigNumber truncated to a whole + * number. + */ + P.truncated = P.trunc = function () { + return round(new BigNumber(this), this.e + 1, 1); + }; -assert.strictEqual = function strictEqual(actual, expected, message) { - if (actual !== expected) { - fail(actual, expected, message, '===', assert.strictEqual); - } -}; + /* + * Return as toString, but do not accept a base argument, and include the minus sign for + * negative zero. + */ + P.valueOf = P.toJSON = function () { + var str, + n = this, + e = n.e; -// 10. The strict non-equality assertion tests for strict inequality, as -// determined by !==. assert.notStrictEqual(actual, expected, message_opt); + if (e === null) return n.toString(); -assert.notStrictEqual = function notStrictEqual(actual, expected, message) { - if (actual === expected) { - fail(actual, expected, message, '!==', assert.notStrictEqual); - } -}; + str = coeffToString(n.c); -function expectedException(actual, expected) { - if (!actual || !expected) { - return false; - } + str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(str, e) : toFixedPoint(str, e); - if (Object.prototype.toString.call(expected) == '[object RegExp]') { - return expected.test(actual); - } + return n.s < 0 ? '-' + str : str; + }; - try { - if (actual instanceof expected) { - return true; - } - } catch (e) { - // Ignore. The instanceof check doesn't work for arrow functions. - } + P.isBigNumber = true; - if (Error.isPrototypeOf(expected)) { - return false; - } + if (config != null) BigNumber.config(config); - return expected.call({}, actual) === true; -} + return BigNumber; + } -function _tryBlock(block) { - var error; - try { - block(); - } catch (e) { - error = e; - } - return error; -} + // PRIVATE HELPER FUNCTIONS -function _throws(shouldThrow, block, expected, message) { - var actual; - if (typeof block !== 'function') { - throw new TypeError('"block" argument must be a function'); - } + function bitFloor(n) { + var i = n | 0; + return n > 0 || n === i ? i : i - 1; + } - if (typeof expected === 'string') { - message = expected; - expected = null; - } + // Return a coefficient array as a string of base 10 digits. + function coeffToString(a) { + var s, + z, + i = 1, + j = a.length, + r = a[0] + ''; - actual = _tryBlock(block); + for (; i < j;) { + s = a[i++] + ''; + z = LOG_BASE - s.length; + for (; z--; s = '0' + s) {} + r += s; + } - message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + (message ? ' ' + message : '.'); + // Determine trailing zeros. + for (j = r.length; r.charCodeAt(--j) === 48;) {} + return r.slice(0, j + 1 || 1); + } - if (shouldThrow && !actual) { - fail(actual, expected, 'Missing expected exception' + message); - } + // Compare the value of BigNumbers x and y. + function compare(x, y) { + var a, + b, + xc = x.c, + yc = y.c, + i = x.s, + j = y.s, + k = x.e, + l = y.e; - var userProvidedMessage = typeof message === 'string'; - var isUnwantedException = !shouldThrow && util.isError(actual); - var isUnexpectedException = !shouldThrow && actual && !expected; + // Either NaN? + if (!i || !j) return null; - if (isUnwantedException && userProvidedMessage && expectedException(actual, expected) || isUnexpectedException) { - fail(actual, expected, 'Got unwanted exception' + message); - } + a = xc && !xc[0]; + b = yc && !yc[0]; - if (shouldThrow && actual && expected && !expectedException(actual, expected) || !shouldThrow && actual) { - throw actual; - } -} + // Either zero? + if (a || b) return a ? b ? 0 : -j : i; -// 11. Expected to throw an error: -// assert.throws(block, Error_opt, message_opt); + // Signs differ? + if (i != j) return i; -assert.throws = function (block, /*optional*/error, /*optional*/message) { - _throws(true, block, error, message); -}; + a = i < 0; + b = k == l; -// EXTENSION! This is annoying to write outside this module. -assert.doesNotThrow = function (block, /*optional*/error, /*optional*/message) { - _throws(false, block, error, message); -}; + // Either Infinity? + if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1; -assert.ifError = function (err) { - if (err) throw err; -}; + // Compare exponents. + if (!b) return k > l ^ a ? 1 : -1; -var objectKeys = Object.keys || function (obj) { - var keys = []; - for (var key in obj) { - if (hasOwn.call(obj, key)) keys.push(key); - } - return keys; -}; + j = (k = xc.length) < (l = yc.length) ? k : l; -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"util/":349}],23:[function(require,module,exports){ -'use strict'; + // Compare digit by digit. + for (i = 0; i < j; i++) { + if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1; + } // Compare lengths. + return k == l ? 0 : k > l ^ a ? 1 : -1; + } -exports.byteLength = byteLength; -exports.toByteArray = toByteArray; -exports.fromByteArray = fromByteArray; + /* + * Return true if n is a valid number in range, otherwise false. + * Use for argument validation when ERRORS is false. + * Note: parseInt('1e+1') == 1 but parseFloat('1e+1') == 10. + */ + function intValidatorNoErrors(n, min, max) { + return (n = truncate(n)) >= min && n <= max; + } -var lookup = []; -var revLookup = []; -var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array; + function isArray(obj) { + return Object.prototype.toString.call(obj) == '[object Array]'; + } -var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; -for (var i = 0, len = code.length; i < len; ++i) { - lookup[i] = code[i]; - revLookup[code.charCodeAt(i)] = i; -} + /* + * Convert string of baseIn to an array of numbers of baseOut. + * Eg. convertBase('255', 10, 16) returns [15, 15]. + * Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. + */ + function toBaseOut(str, baseIn, baseOut) { + var j, + arr = [0], + arrL, + i = 0, + len = str.length; -revLookup['-'.charCodeAt(0)] = 62; -revLookup['_'.charCodeAt(0)] = 63; + for (; i < len;) { + for (arrL = arr.length; arrL--; arr[arrL] *= baseIn) {} + arr[j = 0] += ALPHABET.indexOf(str.charAt(i++)); -function placeHoldersCount(b64) { - var len = b64.length; - if (len % 4 > 0) { - throw new Error('Invalid string. Length must be a multiple of 4'); - } + for (; j < arr.length; j++) { - // the number of equal signs (place holders) - // if there are two placeholders, than the two characters before it - // represent one byte - // if there is only one, then the three characters before it represent 2 bytes - // this is just a cheap hack to not do indexOf twice - return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0; -} + if (arr[j] > baseOut - 1) { + if (arr[j + 1] == null) arr[j + 1] = 0; + arr[j + 1] += arr[j] / baseOut | 0; + arr[j] %= baseOut; + } + } + } -function byteLength(b64) { - // base64 is 4/3 + up to two characters of the original data - return b64.length * 3 / 4 - placeHoldersCount(b64); -} + return arr.reverse(); + } -function toByteArray(b64) { - var i, j, l, tmp, placeHolders, arr; - var len = b64.length; - placeHolders = placeHoldersCount(b64); + function toExponential(str, e) { + return (str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str) + (e < 0 ? 'e' : 'e+') + e; + } - arr = new Arr(len * 3 / 4 - placeHolders); + function toFixedPoint(str, e) { + var len, z; - // if there are placeholders, only get up to the last complete 4 chars - l = placeHolders > 0 ? len - 4 : len; + // Negative exponent? + if (e < 0) { - var L = 0; + // Prepend zeros. + for (z = '0.'; ++e; z += '0') {} + str = z + str; - for (i = 0, j = 0; i < l; i += 4, j += 3) { - tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)]; - arr[L++] = tmp >> 16 & 0xFF; - arr[L++] = tmp >> 8 & 0xFF; - arr[L++] = tmp & 0xFF; - } + // Positive exponent + } else { + len = str.length; - if (placeHolders === 2) { - tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4; - arr[L++] = tmp & 0xFF; - } else if (placeHolders === 1) { - tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2; - arr[L++] = tmp >> 8 & 0xFF; - arr[L++] = tmp & 0xFF; - } + // Append zeros. + if (++e > len) { + for (z = '0', e -= len; --e; z += '0') {} + str += z; + } else if (e < len) { + str = str.slice(0, e) + '.' + str.slice(e); + } + } - return arr; -} + return str; + } -function tripletToBase64(num) { - return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; -} + function truncate(n) { + n = parseFloat(n); + return n < 0 ? mathceil(n) : mathfloor(n); + } -function encodeChunk(uint8, start, end) { - var tmp; - var output = []; - for (var i = start; i < end; i += 3) { - tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2]; - output.push(tripletToBase64(tmp)); - } - return output.join(''); -} + // EXPORT -function fromByteArray(uint8) { - var tmp; - var len = uint8.length; - var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes - var output = ''; - var parts = []; - var maxChunkLength = 16383; // must be multiple of 3 - // go through the array every three bytes, we'll deal with trailing stuff later - for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { - parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength)); - } + BigNumber = constructorFactory(); + BigNumber['default'] = BigNumber.BigNumber = BigNumber; - // pad the end with zeros, but make sure to not forget the extra bytes - if (extraBytes === 1) { - tmp = uint8[len - 1]; - output += lookup[tmp >> 2]; - output += lookup[tmp << 4 & 0x3F]; - output += '=='; - } else if (extraBytes === 2) { - tmp = (uint8[len - 2] << 8) + uint8[len - 1]; - output += lookup[tmp >> 10]; - output += lookup[tmp >> 4 & 0x3F]; - output += lookup[tmp << 2 & 0x3F]; - output += '='; - } + // AMD. + if (typeof define == 'function' && define.amd) { + define(function () { + return BigNumber; + }); - parts.push(output); + // Node.js and other environments that support module.exports. + } else if (typeof module != 'undefined' && module.exports) { + module.exports = BigNumber; - return parts.join(''); -} + // Browser. + } else { + if (!globalObj) globalObj = typeof self != 'undefined' ? self : Function('return this')(); + globalObj.BigNumber = BigNumber; + } +})(undefined); },{}],24:[function(require,module,exports){ -(function (Buffer){ 'use strict'; // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] // NOTE: SIGHASH byte ignored AND restricted, truncate before use +var Buffer = require('safe-buffer').Buffer; + function check(buffer) { if (buffer.length < 8) return false; if (buffer.length > 72) return false; @@ -6664,7 +6037,7 @@ function encode(r, s) { if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80)) throw new Error('R value excessively padded'); if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80)) throw new Error('S value excessively padded'); - var signature = new Buffer(6 + lenR + lenS); + var signature = Buffer.allocUnsafe(6 + lenR + lenS); // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] signature[0] = 0x30; @@ -6685,8 +6058,7 @@ module.exports = { encode: encode }; -}).call(this,require("buffer").Buffer) -},{"buffer":155}],25:[function(require,module,exports){ +},{"safe-buffer":312}],25:[function(require,module,exports){ (function (global,Buffer){ 'use strict'; @@ -6757,7 +6129,7 @@ bitcore.deps._ = require('lodash'); bitcore.Transaction.sighash = require('./lib/transaction/sighash'); }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) -},{"./lib/address":26,"./lib/block":29,"./lib/block/blockheader":28,"./lib/block/merkleblock":30,"./lib/crypto/bn":31,"./lib/crypto/ecdsa":32,"./lib/crypto/hash":33,"./lib/crypto/point":34,"./lib/crypto/random":35,"./lib/crypto/signature":36,"./lib/encoding/base58":37,"./lib/encoding/base58check":38,"./lib/encoding/bufferreader":39,"./lib/encoding/bufferwriter":40,"./lib/encoding/varint":41,"./lib/errors":42,"./lib/hdprivatekey.js":44,"./lib/hdpublickey.js":45,"./lib/networks":46,"./lib/opcode":47,"./lib/privatekey":48,"./lib/publickey":49,"./lib/script":50,"./lib/transaction":53,"./lib/transaction/sighash":61,"./lib/unit":65,"./lib/uri":66,"./lib/util/buffer":67,"./lib/util/js":68,"./lib/util/preconditions":69,"./package.json":96,"bn.js":70,"bs58":71,"buffer":155,"elliptic":73,"lodash":95}],26:[function(require,module,exports){ +},{"./lib/address":26,"./lib/block":29,"./lib/block/blockheader":28,"./lib/block/merkleblock":30,"./lib/crypto/bn":31,"./lib/crypto/ecdsa":32,"./lib/crypto/hash":33,"./lib/crypto/point":34,"./lib/crypto/random":35,"./lib/crypto/signature":36,"./lib/encoding/base58":37,"./lib/encoding/base58check":38,"./lib/encoding/bufferreader":39,"./lib/encoding/bufferwriter":40,"./lib/encoding/varint":41,"./lib/errors":42,"./lib/hdprivatekey.js":44,"./lib/hdpublickey.js":45,"./lib/networks":46,"./lib/opcode":47,"./lib/privatekey":48,"./lib/publickey":49,"./lib/script":50,"./lib/transaction":53,"./lib/transaction/sighash":61,"./lib/unit":65,"./lib/uri":66,"./lib/util/buffer":67,"./lib/util/js":68,"./lib/util/preconditions":69,"./package.json":96,"bn.js":70,"bs58":71,"buffer":152,"elliptic":73,"lodash":95}],26:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -7256,7 +6628,7 @@ module.exports = Address; var Script = require('./script'); }).call(this,require("buffer").Buffer) -},{"./crypto/hash":33,"./encoding/base58check":38,"./errors":42,"./networks":46,"./publickey":49,"./script":50,"./util/js":68,"./util/preconditions":69,"buffer":155,"lodash":95}],27:[function(require,module,exports){ +},{"./crypto/hash":33,"./encoding/base58check":38,"./errors":42,"./networks":46,"./publickey":49,"./script":50,"./util/js":68,"./util/preconditions":69,"buffer":152,"lodash":95}],27:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -7541,7 +6913,7 @@ Block.Values = { module.exports = Block; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../transaction":53,"../util/buffer":67,"../util/preconditions":69,"./blockheader":28,"buffer":155,"lodash":95}],28:[function(require,module,exports){ +},{"../crypto/bn":31,"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../transaction":53,"../util/buffer":67,"../util/preconditions":69,"./blockheader":28,"buffer":152,"lodash":95}],28:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -7838,7 +7210,7 @@ BlockHeader.Constants = { module.exports = BlockHeader; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":155,"lodash":95}],29:[function(require,module,exports){ +},{"../crypto/bn":31,"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":152,"lodash":95}],29:[function(require,module,exports){ 'use strict'; module.exports = require('./block'); @@ -8121,7 +7493,7 @@ MerkleBlock.fromObject = function fromObject(obj) { module.exports = MerkleBlock; }).call(this,require("buffer").Buffer) -},{"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../transaction":53,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./blockheader":28,"buffer":155,"lodash":95}],31:[function(require,module,exports){ +},{"../crypto/hash":33,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../transaction":53,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./blockheader":28,"buffer":152,"lodash":95}],31:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -8326,7 +7698,7 @@ BN.pad = function (buf, natlen, size) { module.exports = BN; }).call(this,require("buffer").Buffer) -},{"../util/preconditions":69,"bn.js":70,"buffer":155,"lodash":95}],32:[function(require,module,exports){ +},{"../util/preconditions":69,"bn.js":70,"buffer":152,"lodash":95}],32:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -8624,7 +7996,7 @@ ECDSA.verify = function (hashbuf, sig, pubkey, endian) { module.exports = ECDSA; }).call(this,require("buffer").Buffer) -},{"../publickey":49,"../util/buffer":67,"../util/preconditions":69,"./bn":31,"./hash":33,"./point":34,"./random":35,"./signature":36,"buffer":155,"lodash":95}],33:[function(require,module,exports){ +},{"../publickey":49,"../util/buffer":67,"../util/preconditions":69,"./bn":31,"./hash":33,"./point":34,"./random":35,"./signature":36,"buffer":152,"lodash":95}],33:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -8713,7 +8085,7 @@ Hash.sha512hmac = function (data, key) { }; }).call(this,require("buffer").Buffer) -},{"../util/buffer":67,"../util/preconditions":69,"buffer":155,"crypto":180}],34:[function(require,module,exports){ +},{"../util/buffer":67,"../util/preconditions":69,"buffer":152,"crypto":178}],34:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -8861,7 +8233,7 @@ Point.pointToCompressed = function pointToCompressed(point) { module.exports = Point; }).call(this,require("buffer").Buffer) -},{"../util/buffer":67,"./bn":31,"buffer":155,"elliptic":73}],35:[function(require,module,exports){ +},{"../util/buffer":67,"./bn":31,"buffer":152,"elliptic":73}],35:[function(require,module,exports){ (function (process,Buffer){ 'use strict'; @@ -8913,7 +8285,7 @@ Random.getPseudoRandomBuffer = function (size) { module.exports = Random; }).call(this,require('_process'),require("buffer").Buffer) -},{"_process":280,"buffer":155,"crypto":180}],36:[function(require,module,exports){ +},{"_process":284,"buffer":152,"crypto":178}],36:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9225,7 +8597,7 @@ Signature.SIGHASH_ANYONECANPAY = 0x80; module.exports = Signature; }).call(this,require("buffer").Buffer) -},{"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./bn":31,"buffer":155,"lodash":95}],37:[function(require,module,exports){ +},{"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./bn":31,"buffer":152,"lodash":95}],37:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9301,7 +8673,7 @@ Base58.prototype.toString = function () { module.exports = Base58; }).call(this,require("buffer").Buffer) -},{"bs58":71,"buffer":155,"lodash":95}],38:[function(require,module,exports){ +},{"bs58":71,"buffer":152,"lodash":95}],38:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9395,7 +8767,7 @@ Base58Check.prototype.toString = function () { module.exports = Base58Check; }).call(this,require("buffer").Buffer) -},{"../crypto/hash":33,"./base58":37,"buffer":155,"lodash":95}],39:[function(require,module,exports){ +},{"../crypto/hash":33,"./base58":37,"buffer":152,"lodash":95}],39:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9596,7 +8968,7 @@ BufferReader.prototype.readReverse = function (len) { module.exports = BufferReader; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"../util/buffer":67,"../util/preconditions":69,"buffer":155,"lodash":95}],40:[function(require,module,exports){ +},{"../crypto/bn":31,"../util/buffer":67,"../util/preconditions":69,"buffer":152,"lodash":95}],40:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9747,7 +9119,7 @@ BufferWriter.varintBufBN = function (bn) { module.exports = BufferWriter; }).call(this,require("buffer").Buffer) -},{"../util/buffer":67,"assert":22,"buffer":155}],41:[function(require,module,exports){ +},{"../util/buffer":67,"assert":21,"buffer":152}],41:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -9822,7 +9194,7 @@ Varint.prototype.toNumber = function () { module.exports = Varint; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"./bufferreader":39,"./bufferwriter":40,"buffer":155}],42:[function(require,module,exports){ +},{"../crypto/bn":31,"./bufferreader":39,"./bufferwriter":40,"buffer":152}],42:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); @@ -10699,7 +10071,7 @@ assert(HDPrivateKey.ChecksumEnd === HDPrivateKey.SerializedByteSize); module.exports = HDPrivateKey; }).call(this,require("buffer").Buffer) -},{"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./crypto/random":35,"./encoding/base58":37,"./encoding/base58check":38,"./errors":42,"./hdpublickey":45,"./networks":46,"./privatekey":48,"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"assert":22,"buffer":155,"lodash":95}],45:[function(require,module,exports){ +},{"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./crypto/random":35,"./encoding/base58":37,"./encoding/base58check":38,"./errors":42,"./hdpublickey":45,"./networks":46,"./privatekey":48,"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"assert":21,"buffer":152,"lodash":95}],45:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -11193,7 +10565,7 @@ assert(HDPublicKey.ChecksumEnd === HDPublicKey.SerializedByteSize); module.exports = HDPublicKey; }).call(this,require("buffer").Buffer) -},{"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./encoding/base58":37,"./encoding/base58check":38,"./errors":42,"./hdprivatekey":44,"./networks":46,"./publickey":49,"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"assert":22,"buffer":155,"lodash":95}],46:[function(require,module,exports){ +},{"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./encoding/base58":37,"./encoding/base58check":38,"./errors":42,"./hdprivatekey":44,"./networks":46,"./publickey":49,"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"assert":21,"buffer":152,"lodash":95}],46:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); @@ -11705,7 +11077,7 @@ Opcode.prototype.inspect = function () { module.exports = Opcode; }).call(this,require("buffer").Buffer) -},{"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"buffer":155,"lodash":95}],48:[function(require,module,exports){ +},{"./util/buffer":67,"./util/js":68,"./util/preconditions":69,"buffer":152,"lodash":95}],48:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -12105,7 +11477,7 @@ PrivateKey.prototype.inspect = function () { module.exports = PrivateKey; }).call(this,require("buffer").Buffer) -},{"./address":26,"./crypto/bn":31,"./crypto/point":34,"./crypto/random":35,"./encoding/base58check":38,"./networks":46,"./publickey":49,"./util/js":68,"./util/preconditions":69,"buffer":155,"lodash":95}],49:[function(require,module,exports){ +},{"./address":26,"./crypto/bn":31,"./crypto/point":34,"./crypto/random":35,"./encoding/base58check":38,"./networks":46,"./publickey":49,"./util/js":68,"./util/preconditions":69,"buffer":152,"lodash":95}],49:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -12500,7 +11872,7 @@ PublicKey.prototype.inspect = function () { module.exports = PublicKey; }).call(this,require("buffer").Buffer) -},{"./address":26,"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./networks":46,"./privatekey":48,"./util/js":68,"./util/preconditions":69,"buffer":155,"lodash":95}],50:[function(require,module,exports){ +},{"./address":26,"./crypto/bn":31,"./crypto/hash":33,"./crypto/point":34,"./networks":46,"./privatekey":48,"./util/js":68,"./util/preconditions":69,"buffer":152,"lodash":95}],50:[function(require,module,exports){ 'use strict'; module.exports = require('./script'); @@ -13751,7 +13123,7 @@ Interpreter.prototype.step = function () { }; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"../crypto/hash":33,"../crypto/signature":36,"../opcode":47,"../publickey":49,"../transaction":53,"./script":52,"buffer":155,"lodash":95}],52:[function(require,module,exports){ +},{"../crypto/bn":31,"../crypto/hash":33,"../crypto/signature":36,"../opcode":47,"../publickey":49,"../transaction":53,"./script":52,"buffer":152,"lodash":95}],52:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -14777,7 +14149,7 @@ Script.prototype.getSignatureOperationsCount = function (accurate) { module.exports = Script; }).call(this,require("buffer").Buffer) -},{"../address":26,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../errors":42,"../networks":46,"../opcode":47,"../publickey":49,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":155,"lodash":95}],53:[function(require,module,exports){ +},{"../address":26,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../errors":42,"../networks":46,"../opcode":47,"../publickey":49,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":152,"lodash":95}],53:[function(require,module,exports){ 'use strict'; module.exports = require('./transaction'); @@ -14984,7 +14356,7 @@ Input.prototype._estimateSize = function () { module.exports = Input; -},{"../../encoding/bufferwriter":40,"../../errors":42,"../../script":50,"../../util/buffer":67,"../../util/js":68,"../../util/preconditions":69,"../output":60,"../sighash":61,"buffer":155,"lodash":95}],56:[function(require,module,exports){ +},{"../../encoding/bufferwriter":40,"../../errors":42,"../../script":50,"../../util/buffer":67,"../../util/js":68,"../../util/preconditions":69,"../output":60,"../sighash":61,"buffer":152,"lodash":95}],56:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); @@ -15675,7 +15047,7 @@ Output.prototype.toBufferWriter = function (writer) { module.exports = Output; -},{"../crypto/bn":31,"../encoding/bufferwriter":40,"../errors":42,"../script":50,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":155,"lodash":95}],61:[function(require,module,exports){ +},{"../crypto/bn":31,"../encoding/bufferwriter":40,"../errors":42,"../script":50,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":152,"lodash":95}],61:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -15810,7 +15182,7 @@ module.exports = { }; }).call(this,require("buffer").Buffer) -},{"../crypto/bn":31,"../crypto/ecdsa":32,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../script":50,"../util/preconditions":69,"./input":54,"./output":60,"./transaction":63,"buffer":155,"lodash":95}],62:[function(require,module,exports){ +},{"../crypto/bn":31,"../crypto/ecdsa":32,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../script":50,"../util/preconditions":69,"./input":54,"./output":60,"./transaction":63,"buffer":152,"lodash":95}],62:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -15898,7 +15270,7 @@ TransactionSignature.fromObject = function (object) { module.exports = TransactionSignature; }).call(this,require("buffer").Buffer) -},{"../crypto/signature":36,"../errors":42,"../publickey":49,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":155,"inherits":94,"lodash":95}],63:[function(require,module,exports){ +},{"../crypto/signature":36,"../errors":42,"../publickey":49,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"buffer":152,"inherits":94,"lodash":95}],63:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -17098,7 +16470,7 @@ Transaction.prototype.enableRBF = function () { module.exports = Transaction; }).call(this,require("buffer").Buffer) -},{"../address":26,"../crypto/bn":31,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../errors":42,"../privatekey":48,"../script":50,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./input":54,"./output":60,"./sighash":61,"./unspentoutput":64,"buffer":155,"buffer-compare":72,"lodash":95}],64:[function(require,module,exports){ +},{"../address":26,"../crypto/bn":31,"../crypto/hash":33,"../crypto/signature":36,"../encoding/bufferreader":39,"../encoding/bufferwriter":40,"../errors":42,"../privatekey":48,"../script":50,"../util/buffer":67,"../util/js":68,"../util/preconditions":69,"./input":54,"./output":60,"./sighash":61,"./unspentoutput":64,"buffer":152,"buffer-compare":72,"lodash":95}],64:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); @@ -17666,7 +17038,7 @@ URI.prototype.inspect = function () { module.exports = URI; -},{"./address":26,"./unit":65,"lodash":95,"url":343}],67:[function(require,module,exports){ +},{"./address":26,"./unit":65,"lodash":95,"url":350}],67:[function(require,module,exports){ (function (Buffer){ 'use strict'; @@ -17847,7 +17219,7 @@ module.exports.NULL_HASH = module.exports.fill(new Buffer(32), 0); module.exports.EMPTY_BUFFER = new Buffer(0); }).call(this,require("buffer").Buffer) -},{"./js":68,"./preconditions":69,"assert":22,"buffer":155}],68:[function(require,module,exports){ +},{"./js":68,"./preconditions":69,"assert":21,"buffer":152}],68:[function(require,module,exports){ 'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; @@ -23465,44 +22837,29 @@ exports.shr64_lo = shr64_lo; },{"inherits":94}],93:[function(require,module,exports){ module.exports={ - "_args": [ - [ - { - "raw": "elliptic@https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "scope": null, - "escapedName": "elliptic", - "name": "elliptic", - "rawSpec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "spec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "type": "remote" - }, - "/home/chris/dev/eth-lightwallet/node_modules/bitcore-lib" - ] - ], "_from": "elliptic@=3.0.3", "_id": "elliptic@3.0.3", - "_inCache": true, + "_inBundle": false, + "_integrity": "sha1-hlybQgv75VAGuflp+XoNLESWZZU=", "_location": "/bitcore-lib/elliptic", "_phantomChildren": { "inherits": "2.0.1" }, "_requested": { + "type": "remote", "raw": "elliptic@https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "scope": null, - "escapedName": "elliptic", "name": "elliptic", + "escapedName": "elliptic", "rawSpec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "spec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "type": "remote" + "saveSpec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", + "fetchSpec": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz" }, "_requiredBy": [ "/bitcore-lib" ], "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "_shasum": "865c9b420bfbe55006b9f969f97a0d2c44966595", - "_shrinkwrap": null, "_spec": "elliptic@https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz", - "_where": "/home/chris/dev/eth-lightwallet/node_modules/bitcore-lib", + "_where": "/Users/me/dltlab/eth-lightwallet/node_modules/bitcore-lib", "author": { "name": "Fedor Indutny", "email": "fedor@indutny.com" @@ -23510,12 +22867,14 @@ module.exports={ "bugs": { "url": "https://github.com/indutny/elliptic/issues" }, + "bundleDependencies": false, "dependencies": { "bn.js": "^2.0.0", "brorand": "^1.0.1", "hash.js": "^1.0.0", "inherits": "^2.0.1" }, + "deprecated": false, "description": "EC cryptography", "devDependencies": { "browserify": "^3.44.2", @@ -23534,9 +22893,6 @@ module.exports={ "license": "MIT", "main": "lib/elliptic.js", "name": "elliptic", - "optionalDependencies": {}, - "readme": "# Elliptic [![Build Status](https://secure.travis-ci.org/indutny/elliptic.png)](http://travis-ci.org/indutny/elliptic)\n\nFast elliptic-curve cryptography in a plain javascript implementation.\n\nNOTE: Please take a look at http://safecurves.cr.yp.to/ before choosing a curve\nfor your cryptography operations.\n\n## Incentive\n\nECC is much slower than regular RSA cryptography, the JS implementations are\neven more slower.\n\n## Benchmarks\n\n```bash\n$ node benchmarks/index.js\nBenchmarking: sign\nelliptic#sign x 262 ops/sec ±0.51% (177 runs sampled)\neccjs#sign x 55.91 ops/sec ±0.90% (144 runs sampled)\n------------------------\nFastest is elliptic#sign\n========================\nBenchmarking: verify\nelliptic#verify x 113 ops/sec ±0.50% (166 runs sampled)\neccjs#verify x 48.56 ops/sec ±0.36% (125 runs sampled)\n------------------------\nFastest is elliptic#verify\n========================\nBenchmarking: gen\nelliptic#gen x 294 ops/sec ±0.43% (176 runs sampled)\neccjs#gen x 62.25 ops/sec ±0.63% (129 runs sampled)\n------------------------\nFastest is elliptic#gen\n========================\nBenchmarking: ecdh\nelliptic#ecdh x 136 ops/sec ±0.85% (156 runs sampled)\n------------------------\nFastest is elliptic#ecdh\n========================\n```\n\n## API\n\n### ECDSA\n\n```javascript\nvar EC = require('elliptic').ec;\n\n// Create and initialize EC context\n// (better do it once and reuse it)\nvar ec = new EC('secp256k1');\n\n// Generate keys\nvar key = ec.genKeyPair();\n\n// Sign message (must be an array, or it'll be treated as a hex sequence)\nvar msg = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];\nvar signature = key.sign(msg);\n\n// Export DER encoded signature in Array\nvar derSign = signature.toDER();\n\n// Verify signature\nconsole.log(key.verify(msg, derSign));\n```\n\n### ECDH\n\n```javascript\n// Generate keys\nvar key1 = ec.genKeyPair();\nvar key2 = ec.genKeyPair();\n\nvar shared1 = key1.derive(key2.getPublic());\nvar shared2 = key2.derive(key1.getPublic());\n\nconsole.log('Both shared secrets are BN instances');\nconsole.log(shared1.toString(16));\nconsole.log(shared2.toString(16));\n```\n\nNOTE: `.derive()` returns a [BN][1] instance.\n\n## Supported curves\n\nElliptic.js support following curve types:\n\n* Short Weierstrass\n* Montgomery\n* Edwards\n* Twisted Edwards\n\nFollowing curve 'presets' are embedded into the library:\n\n* `secp256k1`\n* `p192`\n* `p224`\n* `p256`\n* `curve25519`\n* `ed25519`\n\nNOTE: That `curve25519` could not be used for ECDSA, use `ed25519` instead.\n\n### Implementation details\n\nECDSA is using deterministic `k` value generation as per [RFC6979][0]. Most of\nthe curve operations are performed on non-affine coordinates (either projective\nor extended), various windowing techniques are used for different cases.\n\nAll operations are performed in reduction context using [bn.js][1], hashing is\nprovided by [hash.js][2]\n\n### Related projects\n\n* [eccrypto][3]: isomorphic implementation of ECDSA, ECDH and ECIES for both\n browserify and node (uses `elliptic` for browser and [secp256k1-node][4] for\n node)\n\n#### LICENSE\n\nThis software is licensed under the MIT License.\n\nCopyright Fedor Indutny, 2014.\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to permit\npersons to whom the Software is furnished to do so, subject to the\nfollowing conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\nNO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\nDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\nOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\nUSE OR OTHER DEALINGS IN THE SOFTWARE.\n\n[0]: http://tools.ietf.org/html/rfc6979\n[1]: https://github.com/indutny/bn.js\n[2]: https://github.com/indutny/hash.js\n[3]: https://github.com/bitchan/eccrypto\n[4]: https://github.com/wanderer/secp256k1-node\n", - "readmeFilename": "README.md", "repository": { "type": "git", "url": "git+ssh://git@github.com/indutny/elliptic.git" @@ -30664,6682 +30020,7612 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol * values are sorted in ascending order. Otherwise, a value is sorted in * ascending order if its corresponding order is "asc", and descending if "desc". * - * If a property name is provided for an iteratee the created `_.property` - * style callback returns the property value of the given element. + * If a property name is provided for an iteratee the created `_.property` + * style callback returns the property value of the given element. + * + * If an object is provided for an iteratee the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by. + * @param {boolean[]} [orders] The sort orders of `iteratees`. + * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. + * @returns {Array} Returns the new sorted array. + * @example + * + * var users = [ + * { 'user': 'fred', 'age': 48 }, + * { 'user': 'barney', 'age': 34 }, + * { 'user': 'fred', 'age': 42 }, + * { 'user': 'barney', 'age': 36 } + * ]; + * + * // sort by `user` in ascending order and by `age` in descending order + * _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values); + * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] + */ + function sortByOrder(collection, iteratees, orders, guard) { + if (collection == null) { + return []; + } + if (guard && isIterateeCall(iteratees, orders, guard)) { + orders = undefined; + } + if (!isArray(iteratees)) { + iteratees = iteratees == null ? [] : [iteratees]; + } + if (!isArray(orders)) { + orders = orders == null ? [] : [orders]; + } + return baseSortByOrder(collection, iteratees, orders); + } + + /** + * Performs a deep comparison between each element in `collection` and the + * source object, returning an array of all elements that have equivalent + * property values. + * + * **Note:** This method supports comparing arrays, booleans, `Date` objects, + * numbers, `Object` objects, regexes, and strings. Objects are compared by + * their own, not inherited, enumerable properties. For comparing a single + * own or inherited property value see `_.matchesProperty`. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object|string} collection The collection to search. + * @param {Object} source The object of property values to match. + * @returns {Array} Returns the new filtered array. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] }, + * { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] } + * ]; + * + * _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user'); + * // => ['barney'] + * + * _.pluck(_.where(users, { 'pets': ['dino'] }), 'user'); + * // => ['fred'] + */ + function where(collection, source) { + return filter(collection, baseMatches(source)); + } + + /*------------------------------------------------------------------------*/ + + /** + * Gets the number of milliseconds that have elapsed since the Unix epoch + * (1 January 1970 00:00:00 UTC). + * + * @static + * @memberOf _ + * @category Date + * @example + * + * _.defer(function(stamp) { + * console.log(_.now() - stamp); + * }, _.now()); + * // => logs the number of milliseconds it took for the deferred function to be invoked + */ + var now = nativeNow || function () { + return new Date().getTime(); + }; + + /*------------------------------------------------------------------------*/ + + /** + * The opposite of `_.before`; this method creates a function that invokes + * `func` once it is called `n` or more times. + * + * @static + * @memberOf _ + * @category Function + * @param {number} n The number of calls before `func` is invoked. + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * var saves = ['profile', 'settings']; + * + * var done = _.after(saves.length, function() { + * console.log('done saving!'); + * }); + * + * _.forEach(saves, function(type) { + * asyncSave({ 'type': type, 'complete': done }); + * }); + * // => logs 'done saving!' after the two async saves have completed + */ + function after(n, func) { + if (typeof func != 'function') { + if (typeof n == 'function') { + var temp = n; + n = func; + func = temp; + } else { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + n = nativeIsFinite(n = +n) ? n : 0; + return function () { + if (--n < 1) { + return func.apply(this, arguments); + } + }; + } + + /** + * Creates a function that accepts up to `n` arguments ignoring any + * additional arguments. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to cap arguments for. + * @param {number} [n=func.length] The arity cap. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {Function} Returns the new function. + * @example + * + * _.map(['6', '8', '10'], _.ary(parseInt, 1)); + * // => [6, 8, 10] + */ + function ary(func, n, guard) { + if (guard && isIterateeCall(func, n, guard)) { + n = undefined; + } + n = func && n == null ? func.length : nativeMax(+n || 0, 0); + return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n); + } + + /** + * Creates a function that invokes `func`, with the `this` binding and arguments + * of the created function, while it is called less than `n` times. Subsequent + * calls to the created function return the result of the last `func` invocation. + * + * @static + * @memberOf _ + * @category Function + * @param {number} n The number of calls at which `func` is no longer invoked. + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * jQuery('#add').on('click', _.before(5, addContactToList)); + * // => allows adding up to 4 contacts to the list + */ + function before(n, func) { + var result; + if (typeof func != 'function') { + if (typeof n == 'function') { + var temp = n; + n = func; + func = temp; + } else { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + return function () { + if (--n > 0) { + result = func.apply(this, arguments); + } + if (n <= 1) { + func = undefined; + } + return result; + }; + } + + /** + * Creates a function that invokes `func` with the `this` binding of `thisArg` + * and prepends any additional `_.bind` arguments to those provided to the + * bound function. + * + * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds, + * may be used as a placeholder for partially applied arguments. + * + * **Note:** Unlike native `Function#bind` this method does not set the "length" + * property of bound functions. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to bind. + * @param {*} thisArg The `this` binding of `func`. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new bound function. + * @example + * + * var greet = function(greeting, punctuation) { + * return greeting + ' ' + this.user + punctuation; + * }; + * + * var object = { 'user': 'fred' }; + * + * var bound = _.bind(greet, object, 'hi'); + * bound('!'); + * // => 'hi fred!' + * + * // using placeholders + * var bound = _.bind(greet, object, _, '!'); + * bound('hi'); + * // => 'hi fred!' + */ + var bind = restParam(function (func, thisArg, partials) { + var bitmask = BIND_FLAG; + if (partials.length) { + var holders = replaceHolders(partials, bind.placeholder); + bitmask |= PARTIAL_FLAG; + } + return createWrapper(func, bitmask, thisArg, partials, holders); + }); + + /** + * Binds methods of an object to the object itself, overwriting the existing + * method. Method names may be specified as individual arguments or as arrays + * of method names. If no method names are provided all enumerable function + * properties, own and inherited, of `object` are bound. + * + * **Note:** This method does not set the "length" property of bound functions. + * + * @static + * @memberOf _ + * @category Function + * @param {Object} object The object to bind and assign the bound methods to. + * @param {...(string|string[])} [methodNames] The object method names to bind, + * specified as individual method names or arrays of method names. + * @returns {Object} Returns `object`. + * @example + * + * var view = { + * 'label': 'docs', + * 'onClick': function() { + * console.log('clicked ' + this.label); + * } + * }; + * + * _.bindAll(view); + * jQuery('#docs').on('click', view.onClick); + * // => logs 'clicked docs' when the element is clicked + */ + var bindAll = restParam(function (object, methodNames) { + methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object); + + var index = -1, + length = methodNames.length; + + while (++index < length) { + var key = methodNames[index]; + object[key] = createWrapper(object[key], BIND_FLAG, object); + } + return object; + }); + + /** + * Creates a function that invokes the method at `object[key]` and prepends + * any additional `_.bindKey` arguments to those provided to the bound function. + * + * This method differs from `_.bind` by allowing bound functions to reference + * methods that may be redefined or don't yet exist. + * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern) + * for more details. + * + * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for partially applied arguments. + * + * @static + * @memberOf _ + * @category Function + * @param {Object} object The object the method belongs to. + * @param {string} key The key of the method. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new bound function. + * @example + * + * var object = { + * 'user': 'fred', + * 'greet': function(greeting, punctuation) { + * return greeting + ' ' + this.user + punctuation; + * } + * }; + * + * var bound = _.bindKey(object, 'greet', 'hi'); + * bound('!'); + * // => 'hi fred!' + * + * object.greet = function(greeting, punctuation) { + * return greeting + 'ya ' + this.user + punctuation; + * }; + * + * bound('!'); + * // => 'hiya fred!' + * + * // using placeholders + * var bound = _.bindKey(object, 'greet', _, '!'); + * bound('hi'); + * // => 'hiya fred!' + */ + var bindKey = restParam(function (object, key, partials) { + var bitmask = BIND_FLAG | BIND_KEY_FLAG; + if (partials.length) { + var holders = replaceHolders(partials, bindKey.placeholder); + bitmask |= PARTIAL_FLAG; + } + return createWrapper(key, bitmask, object, partials, holders); + }); + + /** + * Creates a function that accepts one or more arguments of `func` that when + * called either invokes `func` returning its result, if all `func` arguments + * have been provided, or returns a function that accepts one or more of the + * remaining `func` arguments, and so on. The arity of `func` may be specified + * if `func.length` is not sufficient. + * + * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds, + * may be used as a placeholder for provided arguments. + * + * **Note:** This method does not set the "length" property of curried functions. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to curry. + * @param {number} [arity=func.length] The arity of `func`. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {Function} Returns the new curried function. + * @example + * + * var abc = function(a, b, c) { + * return [a, b, c]; + * }; + * + * var curried = _.curry(abc); + * + * curried(1)(2)(3); + * // => [1, 2, 3] + * + * curried(1, 2)(3); + * // => [1, 2, 3] + * + * curried(1, 2, 3); + * // => [1, 2, 3] + * + * // using placeholders + * curried(1)(_, 3)(2); + * // => [1, 2, 3] + */ + var curry = createCurry(CURRY_FLAG); + + /** + * This method is like `_.curry` except that arguments are applied to `func` + * in the manner of `_.partialRight` instead of `_.partial`. + * + * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for provided arguments. + * + * **Note:** This method does not set the "length" property of curried functions. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to curry. + * @param {number} [arity=func.length] The arity of `func`. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {Function} Returns the new curried function. + * @example + * + * var abc = function(a, b, c) { + * return [a, b, c]; + * }; + * + * var curried = _.curryRight(abc); + * + * curried(3)(2)(1); + * // => [1, 2, 3] + * + * curried(2, 3)(1); + * // => [1, 2, 3] + * + * curried(1, 2, 3); + * // => [1, 2, 3] + * + * // using placeholders + * curried(3)(1, _)(2); + * // => [1, 2, 3] + */ + var curryRight = createCurry(CURRY_RIGHT_FLAG); + + /** + * Creates a debounced function that delays invoking `func` until after `wait` + * milliseconds have elapsed since the last time the debounced function was + * invoked. The debounced function comes with a `cancel` method to cancel + * delayed invocations. Provide an options object to indicate that `func` + * should be invoked on the leading and/or trailing edge of the `wait` timeout. + * Subsequent calls to the debounced function return the result of the last + * `func` invocation. + * + * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked + * on the trailing edge of the timeout only if the the debounced function is + * invoked more than once during the `wait` timeout. + * + * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation) + * for details over the differences between `_.debounce` and `_.throttle`. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to debounce. + * @param {number} [wait=0] The number of milliseconds to delay. + * @param {Object} [options] The options object. + * @param {boolean} [options.leading=false] Specify invoking on the leading + * edge of the timeout. + * @param {number} [options.maxWait] The maximum time `func` is allowed to be + * delayed before it is invoked. + * @param {boolean} [options.trailing=true] Specify invoking on the trailing + * edge of the timeout. + * @returns {Function} Returns the new debounced function. + * @example + * + * // avoid costly calculations while the window size is in flux + * jQuery(window).on('resize', _.debounce(calculateLayout, 150)); + * + * // invoke `sendMail` when the click event is fired, debouncing subsequent calls + * jQuery('#postbox').on('click', _.debounce(sendMail, 300, { + * 'leading': true, + * 'trailing': false + * })); * - * If an object is provided for an iteratee the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. + * // ensure `batchLog` is invoked once after 1 second of debounced calls + * var source = new EventSource('/stream'); + * jQuery(source).on('message', _.debounce(batchLog, 250, { + * 'maxWait': 1000 + * })); * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by. - * @param {boolean[]} [orders] The sort orders of `iteratees`. - * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. - * @returns {Array} Returns the new sorted array. - * @example + * // cancel a debounced call + * var todoChanges = _.debounce(batchLog, 1000); + * Object.observe(models.todo, todoChanges); * - * var users = [ - * { 'user': 'fred', 'age': 48 }, - * { 'user': 'barney', 'age': 34 }, - * { 'user': 'fred', 'age': 42 }, - * { 'user': 'barney', 'age': 36 } - * ]; + * Object.observe(models, function(changes) { + * if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) { + * todoChanges.cancel(); + * } + * }, ['delete']); * - * // sort by `user` in ascending order and by `age` in descending order - * _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values); - * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] + * // ...at some point `models.todo` is changed + * models.todo.completed = true; + * + * // ...before 1 second has passed `models.todo` is deleted + * // which cancels the debounced `todoChanges` call + * delete models.todo; */ - function sortByOrder(collection, iteratees, orders, guard) { - if (collection == null) { - return []; + function debounce(func, wait, options) { + var args, + maxTimeoutId, + result, + stamp, + thisArg, + timeoutId, + trailingCall, + lastCalled = 0, + maxWait = false, + trailing = true; + + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); } - if (guard && isIterateeCall(iteratees, orders, guard)) { - orders = undefined; + wait = wait < 0 ? 0 : +wait || 0; + if (options === true) { + var leading = true; + trailing = false; + } else if (isObject(options)) { + leading = !!options.leading; + maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait); + trailing = 'trailing' in options ? !!options.trailing : trailing; } - if (!isArray(iteratees)) { - iteratees = iteratees == null ? [] : [iteratees]; + + function cancel() { + if (timeoutId) { + clearTimeout(timeoutId); + } + if (maxTimeoutId) { + clearTimeout(maxTimeoutId); + } + lastCalled = 0; + maxTimeoutId = timeoutId = trailingCall = undefined; } - if (!isArray(orders)) { - orders = orders == null ? [] : [orders]; + + function complete(isCalled, id) { + if (id) { + clearTimeout(id); + } + maxTimeoutId = timeoutId = trailingCall = undefined; + if (isCalled) { + lastCalled = now(); + result = func.apply(thisArg, args); + if (!timeoutId && !maxTimeoutId) { + args = thisArg = undefined; + } + } } - return baseSortByOrder(collection, iteratees, orders); + + function delayed() { + var remaining = wait - (now() - stamp); + if (remaining <= 0 || remaining > wait) { + complete(trailingCall, maxTimeoutId); + } else { + timeoutId = setTimeout(delayed, remaining); + } + } + + function maxDelayed() { + complete(trailing, timeoutId); + } + + function debounced() { + args = arguments; + stamp = now(); + thisArg = this; + trailingCall = trailing && (timeoutId || !leading); + + if (maxWait === false) { + var leadingCall = leading && !timeoutId; + } else { + if (!maxTimeoutId && !leading) { + lastCalled = stamp; + } + var remaining = maxWait - (stamp - lastCalled), + isCalled = remaining <= 0 || remaining > maxWait; + + if (isCalled) { + if (maxTimeoutId) { + maxTimeoutId = clearTimeout(maxTimeoutId); + } + lastCalled = stamp; + result = func.apply(thisArg, args); + } else if (!maxTimeoutId) { + maxTimeoutId = setTimeout(maxDelayed, remaining); + } + } + if (isCalled && timeoutId) { + timeoutId = clearTimeout(timeoutId); + } else if (!timeoutId && wait !== maxWait) { + timeoutId = setTimeout(delayed, wait); + } + if (leadingCall) { + isCalled = true; + result = func.apply(thisArg, args); + } + if (isCalled && !timeoutId && !maxTimeoutId) { + args = thisArg = undefined; + } + return result; + } + debounced.cancel = cancel; + return debounced; } /** - * Performs a deep comparison between each element in `collection` and the - * source object, returning an array of all elements that have equivalent - * property values. - * - * **Note:** This method supports comparing arrays, booleans, `Date` objects, - * numbers, `Object` objects, regexes, and strings. Objects are compared by - * their own, not inherited, enumerable properties. For comparing a single - * own or inherited property value see `_.matchesProperty`. + * Defers invoking the `func` until the current call stack has cleared. Any + * additional arguments are provided to `func` when it is invoked. * * @static * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to search. - * @param {Object} source The object of property values to match. - * @returns {Array} Returns the new filtered array. + * @category Function + * @param {Function} func The function to defer. + * @param {...*} [args] The arguments to invoke the function with. + * @returns {number} Returns the timer id. * @example * - * var users = [ - * { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] }, - * { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] } - * ]; - * - * _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user'); - * // => ['barney'] - * - * _.pluck(_.where(users, { 'pets': ['dino'] }), 'user'); - * // => ['fred'] + * _.defer(function(text) { + * console.log(text); + * }, 'deferred'); + * // logs 'deferred' after one or more milliseconds */ - function where(collection, source) { - return filter(collection, baseMatches(source)); - } - - /*------------------------------------------------------------------------*/ + var defer = restParam(function (func, args) { + return baseDelay(func, 1, args); + }); /** - * Gets the number of milliseconds that have elapsed since the Unix epoch - * (1 January 1970 00:00:00 UTC). + * Invokes `func` after `wait` milliseconds. Any additional arguments are + * provided to `func` when it is invoked. * * @static * @memberOf _ - * @category Date + * @category Function + * @param {Function} func The function to delay. + * @param {number} wait The number of milliseconds to delay invocation. + * @param {...*} [args] The arguments to invoke the function with. + * @returns {number} Returns the timer id. * @example * - * _.defer(function(stamp) { - * console.log(_.now() - stamp); - * }, _.now()); - * // => logs the number of milliseconds it took for the deferred function to be invoked + * _.delay(function(text) { + * console.log(text); + * }, 1000, 'later'); + * // => logs 'later' after one second */ - var now = nativeNow || function () { - return new Date().getTime(); - }; - - /*------------------------------------------------------------------------*/ + var delay = restParam(function (func, wait, args) { + return baseDelay(func, wait, args); + }); /** - * The opposite of `_.before`; this method creates a function that invokes - * `func` once it is called `n` or more times. + * Creates a function that returns the result of invoking the provided + * functions with the `this` binding of the created function, where each + * successive invocation is supplied the return value of the previous. * * @static * @memberOf _ * @category Function - * @param {number} n The number of calls before `func` is invoked. - * @param {Function} func The function to restrict. - * @returns {Function} Returns the new restricted function. + * @param {...Function} [funcs] Functions to invoke. + * @returns {Function} Returns the new function. * @example * - * var saves = ['profile', 'settings']; - * - * var done = _.after(saves.length, function() { - * console.log('done saving!'); - * }); + * function square(n) { + * return n * n; + * } * - * _.forEach(saves, function(type) { - * asyncSave({ 'type': type, 'complete': done }); - * }); - * // => logs 'done saving!' after the two async saves have completed + * var addSquare = _.flow(_.add, square); + * addSquare(1, 2); + * // => 9 */ - function after(n, func) { - if (typeof func != 'function') { - if (typeof n == 'function') { - var temp = n; - n = func; - func = temp; - } else { - throw new TypeError(FUNC_ERROR_TEXT); - } - } - n = nativeIsFinite(n = +n) ? n : 0; - return function () { - if (--n < 1) { - return func.apply(this, arguments); - } - }; - } + var flow = createFlow(); /** - * Creates a function that accepts up to `n` arguments ignoring any - * additional arguments. + * This method is like `_.flow` except that it creates a function that + * invokes the provided functions from right to left. * * @static * @memberOf _ + * @alias backflow, compose * @category Function - * @param {Function} func The function to cap arguments for. - * @param {number} [n=func.length] The arity cap. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @param {...Function} [funcs] Functions to invoke. * @returns {Function} Returns the new function. * @example * - * _.map(['6', '8', '10'], _.ary(parseInt, 1)); - * // => [6, 8, 10] + * function square(n) { + * return n * n; + * } + * + * var addSquare = _.flowRight(square, _.add); + * addSquare(1, 2); + * // => 9 */ - function ary(func, n, guard) { - if (guard && isIterateeCall(func, n, guard)) { - n = undefined; - } - n = func && n == null ? func.length : nativeMax(+n || 0, 0); - return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n); - } + var flowRight = createFlow(true); /** - * Creates a function that invokes `func`, with the `this` binding and arguments - * of the created function, while it is called less than `n` times. Subsequent - * calls to the created function return the result of the last `func` invocation. + * Creates a function that memoizes the result of `func`. If `resolver` is + * provided it determines the cache key for storing the result based on the + * arguments provided to the memoized function. By default, the first argument + * provided to the memoized function is coerced to a string and used as the + * cache key. The `func` is invoked with the `this` binding of the memoized + * function. + * + * **Note:** The cache is exposed as the `cache` property on the memoized + * function. Its creation may be customized by replacing the `_.memoize.Cache` + * constructor with one whose instances implement the [`Map`](http://ecma-international.org/ecma-262/6.0/#sec-properties-of-the-map-prototype-object) + * method interface of `get`, `has`, and `set`. * * @static * @memberOf _ * @category Function - * @param {number} n The number of calls at which `func` is no longer invoked. - * @param {Function} func The function to restrict. - * @returns {Function} Returns the new restricted function. + * @param {Function} func The function to have its output memoized. + * @param {Function} [resolver] The function to resolve the cache key. + * @returns {Function} Returns the new memoizing function. * @example * - * jQuery('#add').on('click', _.before(5, addContactToList)); - * // => allows adding up to 4 contacts to the list + * var upperCase = _.memoize(function(string) { + * return string.toUpperCase(); + * }); + * + * upperCase('fred'); + * // => 'FRED' + * + * // modifying the result cache + * upperCase.cache.set('fred', 'BARNEY'); + * upperCase('fred'); + * // => 'BARNEY' + * + * // replacing `_.memoize.Cache` + * var object = { 'user': 'fred' }; + * var other = { 'user': 'barney' }; + * var identity = _.memoize(_.identity); + * + * identity(object); + * // => { 'user': 'fred' } + * identity(other); + * // => { 'user': 'fred' } + * + * _.memoize.Cache = WeakMap; + * var identity = _.memoize(_.identity); + * + * identity(object); + * // => { 'user': 'fred' } + * identity(other); + * // => { 'user': 'barney' } */ - function before(n, func) { - var result; - if (typeof func != 'function') { - if (typeof n == 'function') { - var temp = n; - n = func; - func = temp; - } else { - throw new TypeError(FUNC_ERROR_TEXT); - } + function memoize(func, resolver) { + if (typeof func != 'function' || resolver && typeof resolver != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); } - return function () { - if (--n > 0) { - result = func.apply(this, arguments); - } - if (n <= 1) { - func = undefined; + var memoized = function memoized() { + var args = arguments, + key = resolver ? resolver.apply(this, args) : args[0], + cache = memoized.cache; + + if (cache.has(key)) { + return cache.get(key); } + var result = func.apply(this, args); + memoized.cache = cache.set(key, result); return result; }; + memoized.cache = new memoize.Cache(); + return memoized; } /** - * Creates a function that invokes `func` with the `this` binding of `thisArg` - * and prepends any additional `_.bind` arguments to those provided to the - * bound function. - * - * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds, - * may be used as a placeholder for partially applied arguments. - * - * **Note:** Unlike native `Function#bind` this method does not set the "length" - * property of bound functions. + * Creates a function that runs each argument through a corresponding + * transform function. * * @static * @memberOf _ * @category Function - * @param {Function} func The function to bind. - * @param {*} thisArg The `this` binding of `func`. - * @param {...*} [partials] The arguments to be partially applied. - * @returns {Function} Returns the new bound function. + * @param {Function} func The function to wrap. + * @param {...(Function|Function[])} [transforms] The functions to transform + * arguments, specified as individual functions or arrays of functions. + * @returns {Function} Returns the new function. * @example * - * var greet = function(greeting, punctuation) { - * return greeting + ' ' + this.user + punctuation; - * }; + * function doubled(n) { + * return n * 2; + * } * - * var object = { 'user': 'fred' }; + * function square(n) { + * return n * n; + * } * - * var bound = _.bind(greet, object, 'hi'); - * bound('!'); - * // => 'hi fred!' + * var modded = _.modArgs(function(x, y) { + * return [x, y]; + * }, square, doubled); * - * // using placeholders - * var bound = _.bind(greet, object, _, '!'); - * bound('hi'); - * // => 'hi fred!' + * modded(1, 2); + * // => [1, 4] + * + * modded(5, 10); + * // => [25, 20] */ - var bind = restParam(function (func, thisArg, partials) { - var bitmask = BIND_FLAG; - if (partials.length) { - var holders = replaceHolders(partials, bind.placeholder); - bitmask |= PARTIAL_FLAG; + var modArgs = restParam(function (func, transforms) { + transforms = baseFlatten(transforms); + if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) { + throw new TypeError(FUNC_ERROR_TEXT); } - return createWrapper(func, bitmask, thisArg, partials, holders); + var length = transforms.length; + return restParam(function (args) { + var index = nativeMin(args.length, length); + while (index--) { + args[index] = transforms[index](args[index]); + } + return func.apply(this, args); + }); }); /** - * Binds methods of an object to the object itself, overwriting the existing - * method. Method names may be specified as individual arguments or as arrays - * of method names. If no method names are provided all enumerable function - * properties, own and inherited, of `object` are bound. - * - * **Note:** This method does not set the "length" property of bound functions. + * Creates a function that negates the result of the predicate `func`. The + * `func` predicate is invoked with the `this` binding and arguments of the + * created function. * * @static * @memberOf _ * @category Function - * @param {Object} object The object to bind and assign the bound methods to. - * @param {...(string|string[])} [methodNames] The object method names to bind, - * specified as individual method names or arrays of method names. - * @returns {Object} Returns `object`. + * @param {Function} predicate The predicate to negate. + * @returns {Function} Returns the new function. * @example * - * var view = { - * 'label': 'docs', - * 'onClick': function() { - * console.log('clicked ' + this.label); - * } - * }; + * function isEven(n) { + * return n % 2 == 0; + * } * - * _.bindAll(view); - * jQuery('#docs').on('click', view.onClick); - * // => logs 'clicked docs' when the element is clicked + * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven)); + * // => [1, 3, 5] */ - var bindAll = restParam(function (object, methodNames) { - methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object); - - var index = -1, - length = methodNames.length; - - while (++index < length) { - var key = methodNames[index]; - object[key] = createWrapper(object[key], BIND_FLAG, object); + function negate(predicate) { + if (typeof predicate != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); } - return object; - }); + return function () { + return !predicate.apply(this, arguments); + }; + } /** - * Creates a function that invokes the method at `object[key]` and prepends - * any additional `_.bindKey` arguments to those provided to the bound function. + * Creates a function that is restricted to invoking `func` once. Repeat calls + * to the function return the value of the first call. The `func` is invoked + * with the `this` binding and arguments of the created function. * - * This method differs from `_.bind` by allowing bound functions to reference - * methods that may be redefined or don't yet exist. - * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern) - * for more details. + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * var initialize = _.once(createApplication); + * initialize(); + * initialize(); + * // `initialize` invokes `createApplication` once + */ + function once(func) { + return before(2, func); + } + + /** + * Creates a function that invokes `func` with `partial` arguments prepended + * to those provided to the new function. This method is like `_.bind` except + * it does **not** alter the `this` binding. * - * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic + * The `_.partial.placeholder` value, which defaults to `_` in monolithic * builds, may be used as a placeholder for partially applied arguments. * + * **Note:** This method does not set the "length" property of partially + * applied functions. + * * @static * @memberOf _ * @category Function - * @param {Object} object The object the method belongs to. - * @param {string} key The key of the method. + * @param {Function} func The function to partially apply arguments to. * @param {...*} [partials] The arguments to be partially applied. - * @returns {Function} Returns the new bound function. + * @returns {Function} Returns the new partially applied function. * @example * - * var object = { - * 'user': 'fred', - * 'greet': function(greeting, punctuation) { - * return greeting + ' ' + this.user + punctuation; - * } - * }; - * - * var bound = _.bindKey(object, 'greet', 'hi'); - * bound('!'); - * // => 'hi fred!' - * - * object.greet = function(greeting, punctuation) { - * return greeting + 'ya ' + this.user + punctuation; + * var greet = function(greeting, name) { + * return greeting + ' ' + name; * }; * - * bound('!'); - * // => 'hiya fred!' + * var sayHelloTo = _.partial(greet, 'hello'); + * sayHelloTo('fred'); + * // => 'hello fred' * * // using placeholders - * var bound = _.bindKey(object, 'greet', _, '!'); - * bound('hi'); - * // => 'hiya fred!' + * var greetFred = _.partial(greet, _, 'fred'); + * greetFred('hi'); + * // => 'hi fred' */ - var bindKey = restParam(function (object, key, partials) { - var bitmask = BIND_FLAG | BIND_KEY_FLAG; - if (partials.length) { - var holders = replaceHolders(partials, bindKey.placeholder); - bitmask |= PARTIAL_FLAG; - } - return createWrapper(key, bitmask, object, partials, holders); - }); + var partial = createPartial(PARTIAL_FLAG); /** - * Creates a function that accepts one or more arguments of `func` that when - * called either invokes `func` returning its result, if all `func` arguments - * have been provided, or returns a function that accepts one or more of the - * remaining `func` arguments, and so on. The arity of `func` may be specified - * if `func.length` is not sufficient. + * This method is like `_.partial` except that partially applied arguments + * are appended to those provided to the new function. * - * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds, - * may be used as a placeholder for provided arguments. + * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for partially applied arguments. * - * **Note:** This method does not set the "length" property of curried functions. + * **Note:** This method does not set the "length" property of partially + * applied functions. * * @static * @memberOf _ * @category Function - * @param {Function} func The function to curry. - * @param {number} [arity=func.length] The arity of `func`. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. - * @returns {Function} Returns the new curried function. + * @param {Function} func The function to partially apply arguments to. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new partially applied function. * @example * - * var abc = function(a, b, c) { - * return [a, b, c]; + * var greet = function(greeting, name) { + * return greeting + ' ' + name; * }; * - * var curried = _.curry(abc); + * var greetFred = _.partialRight(greet, 'fred'); + * greetFred('hi'); + * // => 'hi fred' * - * curried(1)(2)(3); - * // => [1, 2, 3] + * // using placeholders + * var sayHelloTo = _.partialRight(greet, 'hello', _); + * sayHelloTo('fred'); + * // => 'hello fred' + */ + var partialRight = createPartial(PARTIAL_RIGHT_FLAG); + + /** + * Creates a function that invokes `func` with arguments arranged according + * to the specified indexes where the argument value at the first index is + * provided as the first argument, the argument value at the second index is + * provided as the second argument, and so on. * - * curried(1, 2)(3); - * // => [1, 2, 3] + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to rearrange arguments for. + * @param {...(number|number[])} indexes The arranged argument indexes, + * specified as individual indexes or arrays of indexes. + * @returns {Function} Returns the new function. + * @example * - * curried(1, 2, 3); - * // => [1, 2, 3] + * var rearged = _.rearg(function(a, b, c) { + * return [a, b, c]; + * }, 2, 0, 1); * - * // using placeholders - * curried(1)(_, 3)(2); - * // => [1, 2, 3] + * rearged('b', 'c', 'a') + * // => ['a', 'b', 'c'] + * + * var map = _.rearg(_.map, [1, 0]); + * map(function(n) { + * return n * 3; + * }, [1, 2, 3]); + * // => [3, 6, 9] */ - var curry = createCurry(CURRY_FLAG); + var rearg = restParam(function (func, indexes) { + return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes)); + }); /** - * This method is like `_.curry` except that arguments are applied to `func` - * in the manner of `_.partialRight` instead of `_.partial`. - * - * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic - * builds, may be used as a placeholder for provided arguments. + * Creates a function that invokes `func` with the `this` binding of the + * created function and arguments from `start` and beyond provided as an array. * - * **Note:** This method does not set the "length" property of curried functions. + * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters). * * @static * @memberOf _ * @category Function - * @param {Function} func The function to curry. - * @param {number} [arity=func.length] The arity of `func`. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. - * @returns {Function} Returns the new curried function. + * @param {Function} func The function to apply a rest parameter to. + * @param {number} [start=func.length-1] The start position of the rest parameter. + * @returns {Function} Returns the new function. * @example * - * var abc = function(a, b, c) { - * return [a, b, c]; - * }; + * var say = _.restParam(function(what, names) { + * return what + ' ' + _.initial(names).join(', ') + + * (_.size(names) > 1 ? ', & ' : '') + _.last(names); + * }); * - * var curried = _.curryRight(abc); + * say('hello', 'fred', 'barney', 'pebbles'); + * // => 'hello fred, barney, & pebbles' + */ + function restParam(func, start) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + start = nativeMax(start === undefined ? func.length - 1 : +start || 0, 0); + return function () { + var args = arguments, + index = -1, + length = nativeMax(args.length - start, 0), + rest = Array(length); + + while (++index < length) { + rest[index] = args[start + index]; + } + switch (start) { + case 0: + return func.call(this, rest); + case 1: + return func.call(this, args[0], rest); + case 2: + return func.call(this, args[0], args[1], rest); + } + var otherArgs = Array(start + 1); + index = -1; + while (++index < start) { + otherArgs[index] = args[index]; + } + otherArgs[start] = rest; + return func.apply(this, otherArgs); + }; + } + + /** + * Creates a function that invokes `func` with the `this` binding of the created + * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3). * - * curried(3)(2)(1); - * // => [1, 2, 3] + * **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator). * - * curried(2, 3)(1); - * // => [1, 2, 3] + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to spread arguments over. + * @returns {Function} Returns the new function. + * @example * - * curried(1, 2, 3); - * // => [1, 2, 3] + * var say = _.spread(function(who, what) { + * return who + ' says ' + what; + * }); * - * // using placeholders - * curried(3)(1, _)(2); - * // => [1, 2, 3] + * say(['fred', 'hello']); + * // => 'fred says hello' + * + * // with a Promise + * var numbers = Promise.all([ + * Promise.resolve(40), + * Promise.resolve(36) + * ]); + * + * numbers.then(_.spread(function(x, y) { + * return x + y; + * })); + * // => a Promise of 76 */ - var curryRight = createCurry(CURRY_RIGHT_FLAG); + function spread(func) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + return function (array) { + return func.apply(this, array); + }; + } /** - * Creates a debounced function that delays invoking `func` until after `wait` - * milliseconds have elapsed since the last time the debounced function was - * invoked. The debounced function comes with a `cancel` method to cancel - * delayed invocations. Provide an options object to indicate that `func` - * should be invoked on the leading and/or trailing edge of the `wait` timeout. - * Subsequent calls to the debounced function return the result of the last - * `func` invocation. + * Creates a throttled function that only invokes `func` at most once per + * every `wait` milliseconds. The throttled function comes with a `cancel` + * method to cancel delayed invocations. Provide an options object to indicate + * that `func` should be invoked on the leading and/or trailing edge of the + * `wait` timeout. Subsequent calls to the throttled function return the + * result of the last `func` call. * * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked - * on the trailing edge of the timeout only if the the debounced function is + * on the trailing edge of the timeout only if the the throttled function is * invoked more than once during the `wait` timeout. * * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation) - * for details over the differences between `_.debounce` and `_.throttle`. + * for details over the differences between `_.throttle` and `_.debounce`. * * @static * @memberOf _ * @category Function - * @param {Function} func The function to debounce. - * @param {number} [wait=0] The number of milliseconds to delay. + * @param {Function} func The function to throttle. + * @param {number} [wait=0] The number of milliseconds to throttle invocations to. * @param {Object} [options] The options object. - * @param {boolean} [options.leading=false] Specify invoking on the leading + * @param {boolean} [options.leading=true] Specify invoking on the leading * edge of the timeout. - * @param {number} [options.maxWait] The maximum time `func` is allowed to be - * delayed before it is invoked. * @param {boolean} [options.trailing=true] Specify invoking on the trailing * edge of the timeout. - * @returns {Function} Returns the new debounced function. + * @returns {Function} Returns the new throttled function. * @example * - * // avoid costly calculations while the window size is in flux - * jQuery(window).on('resize', _.debounce(calculateLayout, 150)); + * // avoid excessively updating the position while scrolling + * jQuery(window).on('scroll', _.throttle(updatePosition, 100)); * - * // invoke `sendMail` when the click event is fired, debouncing subsequent calls - * jQuery('#postbox').on('click', _.debounce(sendMail, 300, { - * 'leading': true, + * // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes + * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, { * 'trailing': false * })); * - * // ensure `batchLog` is invoked once after 1 second of debounced calls - * var source = new EventSource('/stream'); - * jQuery(source).on('message', _.debounce(batchLog, 250, { - * 'maxWait': 1000 - * })); - * - * // cancel a debounced call - * var todoChanges = _.debounce(batchLog, 1000); - * Object.observe(models.todo, todoChanges); - * - * Object.observe(models, function(changes) { - * if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) { - * todoChanges.cancel(); - * } - * }, ['delete']); - * - * // ...at some point `models.todo` is changed - * models.todo.completed = true; - * - * // ...before 1 second has passed `models.todo` is deleted - * // which cancels the debounced `todoChanges` call - * delete models.todo; + * // cancel a trailing throttled call + * jQuery(window).on('popstate', throttled.cancel); */ - function debounce(func, wait, options) { - var args, - maxTimeoutId, - result, - stamp, - thisArg, - timeoutId, - trailingCall, - lastCalled = 0, - maxWait = false, + function throttle(func, wait, options) { + var leading = true, trailing = true; if (typeof func != 'function') { throw new TypeError(FUNC_ERROR_TEXT); } - wait = wait < 0 ? 0 : +wait || 0; - if (options === true) { - var leading = true; - trailing = false; + if (options === false) { + leading = false; } else if (isObject(options)) { - leading = !!options.leading; - maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait); + leading = 'leading' in options ? !!options.leading : leading; trailing = 'trailing' in options ? !!options.trailing : trailing; } + return debounce(func, wait, { 'leading': leading, 'maxWait': +wait, 'trailing': trailing }); + } - function cancel() { - if (timeoutId) { - clearTimeout(timeoutId); - } - if (maxTimeoutId) { - clearTimeout(maxTimeoutId); - } - lastCalled = 0; - maxTimeoutId = timeoutId = trailingCall = undefined; - } - - function complete(isCalled, id) { - if (id) { - clearTimeout(id); - } - maxTimeoutId = timeoutId = trailingCall = undefined; - if (isCalled) { - lastCalled = now(); - result = func.apply(thisArg, args); - if (!timeoutId && !maxTimeoutId) { - args = thisArg = undefined; - } - } - } + /** + * Creates a function that provides `value` to the wrapper function as its + * first argument. Any additional arguments provided to the function are + * appended to those provided to the wrapper function. The wrapper is invoked + * with the `this` binding of the created function. + * + * @static + * @memberOf _ + * @category Function + * @param {*} value The value to wrap. + * @param {Function} wrapper The wrapper function. + * @returns {Function} Returns the new function. + * @example + * + * var p = _.wrap(_.escape, function(func, text) { + * return '

' + func(text) + '

'; + * }); + * + * p('fred, barney, & pebbles'); + * // => '

fred, barney, & pebbles

' + */ + function wrap(value, wrapper) { + wrapper = wrapper == null ? identity : wrapper; + return createWrapper(wrapper, PARTIAL_FLAG, undefined, [value], []); + } - function delayed() { - var remaining = wait - (now() - stamp); - if (remaining <= 0 || remaining > wait) { - complete(trailingCall, maxTimeoutId); - } else { - timeoutId = setTimeout(delayed, remaining); - } - } + /*------------------------------------------------------------------------*/ - function maxDelayed() { - complete(trailing, timeoutId); + /** + * Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned, + * otherwise they are assigned by reference. If `customizer` is provided it is + * invoked to produce the cloned values. If `customizer` returns `undefined` + * cloning is handled by the method instead. The `customizer` is bound to + * `thisArg` and invoked with two argument; (value [, index|key, object]). + * + * **Note:** This method is loosely based on the + * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). + * The enumerable properties of `arguments` objects and objects created by + * constructors other than `Object` are cloned to plain `Object` objects. An + * empty object is returned for uncloneable values such as functions, DOM nodes, + * Maps, Sets, and WeakMaps. + * + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to clone. + * @param {boolean} [isDeep] Specify a deep clone. + * @param {Function} [customizer] The function to customize cloning values. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {*} Returns the cloned value. + * @example + * + * var users = [ + * { 'user': 'barney' }, + * { 'user': 'fred' } + * ]; + * + * var shallow = _.clone(users); + * shallow[0] === users[0]; + * // => true + * + * var deep = _.clone(users, true); + * deep[0] === users[0]; + * // => false + * + * // using a customizer callback + * var el = _.clone(document.body, function(value) { + * if (_.isElement(value)) { + * return value.cloneNode(false); + * } + * }); + * + * el === document.body + * // => false + * el.nodeName + * // => BODY + * el.childNodes.length; + * // => 0 + */ + function clone(value, isDeep, customizer, thisArg) { + if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) { + isDeep = false; + } else if (typeof isDeep == 'function') { + thisArg = customizer; + customizer = isDeep; + isDeep = false; } + return typeof customizer == 'function' ? baseClone(value, isDeep, bindCallback(customizer, thisArg, 1)) : baseClone(value, isDeep); + } - function debounced() { - args = arguments; - stamp = now(); - thisArg = this; - trailingCall = trailing && (timeoutId || !leading); - - if (maxWait === false) { - var leadingCall = leading && !timeoutId; - } else { - if (!maxTimeoutId && !leading) { - lastCalled = stamp; - } - var remaining = maxWait - (stamp - lastCalled), - isCalled = remaining <= 0 || remaining > maxWait; + /** + * Creates a deep clone of `value`. If `customizer` is provided it is invoked + * to produce the cloned values. If `customizer` returns `undefined` cloning + * is handled by the method instead. The `customizer` is bound to `thisArg` + * and invoked with two argument; (value [, index|key, object]). + * + * **Note:** This method is loosely based on the + * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). + * The enumerable properties of `arguments` objects and objects created by + * constructors other than `Object` are cloned to plain `Object` objects. An + * empty object is returned for uncloneable values such as functions, DOM nodes, + * Maps, Sets, and WeakMaps. + * + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to deep clone. + * @param {Function} [customizer] The function to customize cloning values. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {*} Returns the deep cloned value. + * @example + * + * var users = [ + * { 'user': 'barney' }, + * { 'user': 'fred' } + * ]; + * + * var deep = _.cloneDeep(users); + * deep[0] === users[0]; + * // => false + * + * // using a customizer callback + * var el = _.cloneDeep(document.body, function(value) { + * if (_.isElement(value)) { + * return value.cloneNode(true); + * } + * }); + * + * el === document.body + * // => false + * el.nodeName + * // => BODY + * el.childNodes.length; + * // => 20 + */ + function cloneDeep(value, customizer, thisArg) { + return typeof customizer == 'function' ? baseClone(value, true, bindCallback(customizer, thisArg, 1)) : baseClone(value, true); + } - if (isCalled) { - if (maxTimeoutId) { - maxTimeoutId = clearTimeout(maxTimeoutId); - } - lastCalled = stamp; - result = func.apply(thisArg, args); - } else if (!maxTimeoutId) { - maxTimeoutId = setTimeout(maxDelayed, remaining); - } - } - if (isCalled && timeoutId) { - timeoutId = clearTimeout(timeoutId); - } else if (!timeoutId && wait !== maxWait) { - timeoutId = setTimeout(delayed, wait); - } - if (leadingCall) { - isCalled = true; - result = func.apply(thisArg, args); - } - if (isCalled && !timeoutId && !maxTimeoutId) { - args = thisArg = undefined; - } - return result; - } - debounced.cancel = cancel; - return debounced; + /** + * Checks if `value` is greater than `other`. + * + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is greater than `other`, else `false`. + * @example + * + * _.gt(3, 1); + * // => true + * + * _.gt(3, 3); + * // => false + * + * _.gt(1, 3); + * // => false + */ + function gt(value, other) { + return value > other; } /** - * Defers invoking the `func` until the current call stack has cleared. Any - * additional arguments are provided to `func` when it is invoked. + * Checks if `value` is greater than or equal to `other`. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to defer. - * @param {...*} [args] The arguments to invoke the function with. - * @returns {number} Returns the timer id. + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is greater than or equal to `other`, else `false`. * @example * - * _.defer(function(text) { - * console.log(text); - * }, 'deferred'); - * // logs 'deferred' after one or more milliseconds + * _.gte(3, 1); + * // => true + * + * _.gte(3, 3); + * // => true + * + * _.gte(1, 3); + * // => false */ - var defer = restParam(function (func, args) { - return baseDelay(func, 1, args); - }); + function gte(value, other) { + return value >= other; + } /** - * Invokes `func` after `wait` milliseconds. Any additional arguments are - * provided to `func` when it is invoked. + * Checks if `value` is classified as an `arguments` object. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to delay. - * @param {number} wait The number of milliseconds to delay invocation. - * @param {...*} [args] The arguments to invoke the function with. - * @returns {number} Returns the timer id. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * _.delay(function(text) { - * console.log(text); - * }, 1000, 'later'); - * // => logs 'later' after one second + * _.isArguments(function() { return arguments; }()); + * // => true + * + * _.isArguments([1, 2, 3]); + * // => false */ - var delay = restParam(function (func, wait, args) { - return baseDelay(func, wait, args); - }); + function isArguments(value) { + return isObjectLike(value) && isArrayLike(value) && hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee'); + } /** - * Creates a function that returns the result of invoking the provided - * functions with the `this` binding of the created function, where each - * successive invocation is supplied the return value of the previous. + * Checks if `value` is classified as an `Array` object. * * @static * @memberOf _ - * @category Function - * @param {...Function} [funcs] Functions to invoke. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * function square(n) { - * return n * n; - * } + * _.isArray([1, 2, 3]); + * // => true * - * var addSquare = _.flow(_.add, square); - * addSquare(1, 2); - * // => 9 + * _.isArray(function() { return arguments; }()); + * // => false */ - var flow = createFlow(); + var isArray = nativeIsArray || function (value) { + return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag; + }; /** - * This method is like `_.flow` except that it creates a function that - * invokes the provided functions from right to left. + * Checks if `value` is classified as a boolean primitive or object. * * @static * @memberOf _ - * @alias backflow, compose - * @category Function - * @param {...Function} [funcs] Functions to invoke. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * function square(n) { - * return n * n; - * } + * _.isBoolean(false); + * // => true * - * var addSquare = _.flowRight(square, _.add); - * addSquare(1, 2); - * // => 9 + * _.isBoolean(null); + * // => false */ - var flowRight = createFlow(true); + function isBoolean(value) { + return value === true || value === false || isObjectLike(value) && objToString.call(value) == boolTag; + } /** - * Creates a function that memoizes the result of `func`. If `resolver` is - * provided it determines the cache key for storing the result based on the - * arguments provided to the memoized function. By default, the first argument - * provided to the memoized function is coerced to a string and used as the - * cache key. The `func` is invoked with the `this` binding of the memoized - * function. + * Checks if `value` is classified as a `Date` object. * - * **Note:** The cache is exposed as the `cache` property on the memoized - * function. Its creation may be customized by replacing the `_.memoize.Cache` - * constructor with one whose instances implement the [`Map`](http://ecma-international.org/ecma-262/6.0/#sec-properties-of-the-map-prototype-object) - * method interface of `get`, `has`, and `set`. + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @example + * + * _.isDate(new Date); + * // => true + * + * _.isDate('Mon April 23 2012'); + * // => false + */ + function isDate(value) { + return isObjectLike(value) && objToString.call(value) == dateTag; + } + + /** + * Checks if `value` is a DOM element. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to have its output memoized. - * @param {Function} [resolver] The function to resolve the cache key. - * @returns {Function} Returns the new memoizing function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a DOM element, else `false`. * @example * - * var upperCase = _.memoize(function(string) { - * return string.toUpperCase(); - * }); + * _.isElement(document.body); + * // => true * - * upperCase('fred'); - * // => 'FRED' + * _.isElement(''); + * // => false + */ + function isElement(value) { + return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value); + } + + /** + * Checks if `value` is empty. A value is considered empty unless it is an + * `arguments` object, array, string, or jQuery-like collection with a length + * greater than `0` or an object with own enumerable properties. * - * // modifying the result cache - * upperCase.cache.set('fred', 'BARNEY'); - * upperCase('fred'); - * // => 'BARNEY' + * @static + * @memberOf _ + * @category Lang + * @param {Array|Object|string} value The value to inspect. + * @returns {boolean} Returns `true` if `value` is empty, else `false`. + * @example * - * // replacing `_.memoize.Cache` - * var object = { 'user': 'fred' }; - * var other = { 'user': 'barney' }; - * var identity = _.memoize(_.identity); + * _.isEmpty(null); + * // => true * - * identity(object); - * // => { 'user': 'fred' } - * identity(other); - * // => { 'user': 'fred' } + * _.isEmpty(true); + * // => true * - * _.memoize.Cache = WeakMap; - * var identity = _.memoize(_.identity); + * _.isEmpty(1); + * // => true * - * identity(object); - * // => { 'user': 'fred' } - * identity(other); - * // => { 'user': 'barney' } + * _.isEmpty([1, 2, 3]); + * // => false + * + * _.isEmpty({ 'a': 1 }); + * // => false */ - function memoize(func, resolver) { - if (typeof func != 'function' || resolver && typeof resolver != 'function') { - throw new TypeError(FUNC_ERROR_TEXT); + function isEmpty(value) { + if (value == null) { + return true; } - var memoized = function memoized() { - var args = arguments, - key = resolver ? resolver.apply(this, args) : args[0], - cache = memoized.cache; - - if (cache.has(key)) { - return cache.get(key); - } - var result = func.apply(this, args); - memoized.cache = cache.set(key, result); - return result; - }; - memoized.cache = new memoize.Cache(); - return memoized; + if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) || isObjectLike(value) && isFunction(value.splice))) { + return !value.length; + } + return !keys(value).length; } /** - * Creates a function that runs each argument through a corresponding - * transform function. + * Performs a deep comparison between two values to determine if they are + * equivalent. If `customizer` is provided it is invoked to compare values. + * If `customizer` returns `undefined` comparisons are handled by the method + * instead. The `customizer` is bound to `thisArg` and invoked with three + * arguments: (value, other [, index|key]). + * + * **Note:** This method supports comparing arrays, booleans, `Date` objects, + * numbers, `Object` objects, regexes, and strings. Objects are compared by + * their own, not inherited, enumerable properties. Functions and DOM nodes + * are **not** supported. Provide a customizer function to extend support + * for comparing other values. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to wrap. - * @param {...(Function|Function[])} [transforms] The functions to transform - * arguments, specified as individual functions or arrays of functions. - * @returns {Function} Returns the new function. + * @alias eq + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @param {Function} [customizer] The function to customize value comparisons. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {boolean} Returns `true` if the values are equivalent, else `false`. * @example * - * function doubled(n) { - * return n * 2; - * } + * var object = { 'user': 'fred' }; + * var other = { 'user': 'fred' }; * - * function square(n) { - * return n * n; - * } + * object == other; + * // => false * - * var modded = _.modArgs(function(x, y) { - * return [x, y]; - * }, square, doubled); + * _.isEqual(object, other); + * // => true * - * modded(1, 2); - * // => [1, 4] + * // using a customizer callback + * var array = ['hello', 'goodbye']; + * var other = ['hi', 'goodbye']; * - * modded(5, 10); - * // => [25, 20] + * _.isEqual(array, other, function(value, other) { + * if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) { + * return true; + * } + * }); + * // => true */ - var modArgs = restParam(function (func, transforms) { - transforms = baseFlatten(transforms); - if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - var length = transforms.length; - return restParam(function (args) { - var index = nativeMin(args.length, length); - while (index--) { - args[index] = transforms[index](args[index]); - } - return func.apply(this, args); - }); - }); + function isEqual(value, other, customizer, thisArg) { + customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined; + var result = customizer ? customizer(value, other) : undefined; + return result === undefined ? baseIsEqual(value, other, customizer) : !!result; + } /** - * Creates a function that negates the result of the predicate `func`. The - * `func` predicate is invoked with the `this` binding and arguments of the - * created function. + * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`, + * `SyntaxError`, `TypeError`, or `URIError` object. * * @static * @memberOf _ - * @category Function - * @param {Function} predicate The predicate to negate. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an error object, else `false`. * @example * - * function isEven(n) { - * return n % 2 == 0; - * } + * _.isError(new Error); + * // => true * - * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven)); - * // => [1, 3, 5] + * _.isError(Error); + * // => false */ - function negate(predicate) { - if (typeof predicate != 'function') { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function () { - return !predicate.apply(this, arguments); - }; + function isError(value) { + return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag; } /** - * Creates a function that is restricted to invoking `func` once. Repeat calls - * to the function return the value of the first call. The `func` is invoked - * with the `this` binding and arguments of the created function. + * Checks if `value` is a finite primitive number. + * + * **Note:** This method is based on [`Number.isFinite`](http://ecma-international.org/ecma-262/6.0/#sec-number.isfinite). * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to restrict. - * @returns {Function} Returns the new restricted function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a finite number, else `false`. * @example * - * var initialize = _.once(createApplication); - * initialize(); - * initialize(); - * // `initialize` invokes `createApplication` once + * _.isFinite(10); + * // => true + * + * _.isFinite('10'); + * // => false + * + * _.isFinite(true); + * // => false + * + * _.isFinite(Object(10)); + * // => false + * + * _.isFinite(Infinity); + * // => false */ - function once(func) { - return before(2, func); + function isFinite(value) { + return typeof value == 'number' && nativeIsFinite(value); } /** - * Creates a function that invokes `func` with `partial` arguments prepended - * to those provided to the new function. This method is like `_.bind` except - * it does **not** alter the `this` binding. + * Checks if `value` is classified as a `Function` object. * - * The `_.partial.placeholder` value, which defaults to `_` in monolithic - * builds, may be used as a placeholder for partially applied arguments. + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @example * - * **Note:** This method does not set the "length" property of partially - * applied functions. + * _.isFunction(_); + * // => true + * + * _.isFunction(/abc/); + * // => false + */ + function isFunction(value) { + // The use of `Object#toString` avoids issues with the `typeof` operator + // in older versions of Chrome and Safari which return 'function' for regexes + // and Safari 8 equivalents which return 'object' for typed array constructors. + return isObject(value) && objToString.call(value) == funcTag; + } + + /** + * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`. + * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to partially apply arguments to. - * @param {...*} [partials] The arguments to be partially applied. - * @returns {Function} Returns the new partially applied function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an object, else `false`. * @example * - * var greet = function(greeting, name) { - * return greeting + ' ' + name; - * }; + * _.isObject({}); + * // => true * - * var sayHelloTo = _.partial(greet, 'hello'); - * sayHelloTo('fred'); - * // => 'hello fred' + * _.isObject([1, 2, 3]); + * // => true * - * // using placeholders - * var greetFred = _.partial(greet, _, 'fred'); - * greetFred('hi'); - * // => 'hi fred' + * _.isObject(1); + * // => false */ - var partial = createPartial(PARTIAL_FLAG); + function isObject(value) { + // Avoid a V8 JIT bug in Chrome 19-20. + // See https://code.google.com/p/v8/issues/detail?id=2291 for more details. + var type = typeof value === 'undefined' ? 'undefined' : _typeof(value); + return !!value && (type == 'object' || type == 'function'); + } /** - * This method is like `_.partial` except that partially applied arguments - * are appended to those provided to the new function. + * Performs a deep comparison between `object` and `source` to determine if + * `object` contains equivalent property values. If `customizer` is provided + * it is invoked to compare values. If `customizer` returns `undefined` + * comparisons are handled by the method instead. The `customizer` is bound + * to `thisArg` and invoked with three arguments: (value, other, index|key). * - * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic - * builds, may be used as a placeholder for partially applied arguments. + * **Note:** This method supports comparing properties of arrays, booleans, + * `Date` objects, numbers, `Object` objects, regexes, and strings. Functions + * and DOM nodes are **not** supported. Provide a customizer function to extend + * support for comparing other values. * - * **Note:** This method does not set the "length" property of partially - * applied functions. + * @static + * @memberOf _ + * @category Lang + * @param {Object} object The object to inspect. + * @param {Object} source The object of property values to match. + * @param {Function} [customizer] The function to customize value comparisons. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {boolean} Returns `true` if `object` is a match, else `false`. + * @example + * + * var object = { 'user': 'fred', 'age': 40 }; + * + * _.isMatch(object, { 'age': 40 }); + * // => true + * + * _.isMatch(object, { 'age': 36 }); + * // => false + * + * // using a customizer callback + * var object = { 'greeting': 'hello' }; + * var source = { 'greeting': 'hi' }; + * + * _.isMatch(object, source, function(value, other) { + * return _.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/) || undefined; + * }); + * // => true + */ + function isMatch(object, source, customizer, thisArg) { + customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined; + return baseIsMatch(object, getMatchData(source), customizer); + } + + /** + * Checks if `value` is `NaN`. + * + * **Note:** This method is not the same as [`isNaN`](https://es5.github.io/#x15.1.2.4) + * which returns `true` for `undefined` and other non-numeric values. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to partially apply arguments to. - * @param {...*} [partials] The arguments to be partially applied. - * @returns {Function} Returns the new partially applied function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`. * @example * - * var greet = function(greeting, name) { - * return greeting + ' ' + name; - * }; + * _.isNaN(NaN); + * // => true * - * var greetFred = _.partialRight(greet, 'fred'); - * greetFred('hi'); - * // => 'hi fred' + * _.isNaN(new Number(NaN)); + * // => true * - * // using placeholders - * var sayHelloTo = _.partialRight(greet, 'hello', _); - * sayHelloTo('fred'); - * // => 'hello fred' + * isNaN(undefined); + * // => true + * + * _.isNaN(undefined); + * // => false */ - var partialRight = createPartial(PARTIAL_RIGHT_FLAG); + function isNaN(value) { + // An `NaN` primitive is the only value that is not equal to itself. + // Perform the `toStringTag` check first to avoid errors with some host objects in IE. + return isNumber(value) && value != +value; + } /** - * Creates a function that invokes `func` with arguments arranged according - * to the specified indexes where the argument value at the first index is - * provided as the first argument, the argument value at the second index is - * provided as the second argument, and so on. + * Checks if `value` is a native function. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to rearrange arguments for. - * @param {...(number|number[])} indexes The arranged argument indexes, - * specified as individual indexes or arrays of indexes. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a native function, else `false`. * @example * - * var rearged = _.rearg(function(a, b, c) { - * return [a, b, c]; - * }, 2, 0, 1); - * - * rearged('b', 'c', 'a') - * // => ['a', 'b', 'c'] + * _.isNative(Array.prototype.push); + * // => true * - * var map = _.rearg(_.map, [1, 0]); - * map(function(n) { - * return n * 3; - * }, [1, 2, 3]); - * // => [3, 6, 9] + * _.isNative(_); + * // => false */ - var rearg = restParam(function (func, indexes) { - return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes)); - }); + function isNative(value) { + if (value == null) { + return false; + } + if (isFunction(value)) { + return reIsNative.test(fnToString.call(value)); + } + return isObjectLike(value) && reIsHostCtor.test(value); + } /** - * Creates a function that invokes `func` with the `this` binding of the - * created function and arguments from `start` and beyond provided as an array. - * - * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters). + * Checks if `value` is `null`. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to apply a rest parameter to. - * @param {number} [start=func.length-1] The start position of the rest parameter. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `null`, else `false`. * @example * - * var say = _.restParam(function(what, names) { - * return what + ' ' + _.initial(names).join(', ') + - * (_.size(names) > 1 ? ', & ' : '') + _.last(names); - * }); + * _.isNull(null); + * // => true * - * say('hello', 'fred', 'barney', 'pebbles'); - * // => 'hello fred, barney, & pebbles' + * _.isNull(void 0); + * // => false */ - function restParam(func, start) { - if (typeof func != 'function') { - throw new TypeError(FUNC_ERROR_TEXT); - } - start = nativeMax(start === undefined ? func.length - 1 : +start || 0, 0); - return function () { - var args = arguments, - index = -1, - length = nativeMax(args.length - start, 0), - rest = Array(length); - - while (++index < length) { - rest[index] = args[start + index]; - } - switch (start) { - case 0: - return func.call(this, rest); - case 1: - return func.call(this, args[0], rest); - case 2: - return func.call(this, args[0], args[1], rest); - } - var otherArgs = Array(start + 1); - index = -1; - while (++index < start) { - otherArgs[index] = args[index]; - } - otherArgs[start] = rest; - return func.apply(this, otherArgs); - }; + function isNull(value) { + return value === null; } /** - * Creates a function that invokes `func` with the `this` binding of the created - * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3). + * Checks if `value` is classified as a `Number` primitive or object. * - * **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator). + * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are classified + * as numbers, use the `_.isFinite` method. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to spread arguments over. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * var say = _.spread(function(who, what) { - * return who + ' says ' + what; - * }); - * - * say(['fred', 'hello']); - * // => 'fred says hello' + * _.isNumber(8.4); + * // => true * - * // with a Promise - * var numbers = Promise.all([ - * Promise.resolve(40), - * Promise.resolve(36) - * ]); + * _.isNumber(NaN); + * // => true * - * numbers.then(_.spread(function(x, y) { - * return x + y; - * })); - * // => a Promise of 76 + * _.isNumber('8.4'); + * // => false */ - function spread(func) { - if (typeof func != 'function') { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function (array) { - return func.apply(this, array); - }; + function isNumber(value) { + return typeof value == 'number' || isObjectLike(value) && objToString.call(value) == numberTag; } /** - * Creates a throttled function that only invokes `func` at most once per - * every `wait` milliseconds. The throttled function comes with a `cancel` - * method to cancel delayed invocations. Provide an options object to indicate - * that `func` should be invoked on the leading and/or trailing edge of the - * `wait` timeout. Subsequent calls to the throttled function return the - * result of the last `func` call. - * - * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked - * on the trailing edge of the timeout only if the the throttled function is - * invoked more than once during the `wait` timeout. + * Checks if `value` is a plain object, that is, an object created by the + * `Object` constructor or one with a `[[Prototype]]` of `null`. * - * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation) - * for details over the differences between `_.throttle` and `_.debounce`. + * **Note:** This method assumes objects created by the `Object` constructor + * have no inherited enumerable properties. * * @static * @memberOf _ - * @category Function - * @param {Function} func The function to throttle. - * @param {number} [wait=0] The number of milliseconds to throttle invocations to. - * @param {Object} [options] The options object. - * @param {boolean} [options.leading=true] Specify invoking on the leading - * edge of the timeout. - * @param {boolean} [options.trailing=true] Specify invoking on the trailing - * edge of the timeout. - * @returns {Function} Returns the new throttled function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. * @example * - * // avoid excessively updating the position while scrolling - * jQuery(window).on('scroll', _.throttle(updatePosition, 100)); + * function Foo() { + * this.a = 1; + * } * - * // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes - * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, { - * 'trailing': false - * })); + * _.isPlainObject(new Foo); + * // => false * - * // cancel a trailing throttled call - * jQuery(window).on('popstate', throttled.cancel); + * _.isPlainObject([1, 2, 3]); + * // => false + * + * _.isPlainObject({ 'x': 0, 'y': 0 }); + * // => true + * + * _.isPlainObject(Object.create(null)); + * // => true */ - function throttle(func, wait, options) { - var leading = true, - trailing = true; + function isPlainObject(value) { + var Ctor; - if (typeof func != 'function') { - throw new TypeError(FUNC_ERROR_TEXT); - } - if (options === false) { - leading = false; - } else if (isObject(options)) { - leading = 'leading' in options ? !!options.leading : leading; - trailing = 'trailing' in options ? !!options.trailing : trailing; + // Exit early for non `Object` objects. + if (!(isObjectLike(value) && objToString.call(value) == objectTag && !isArguments(value)) || !hasOwnProperty.call(value, 'constructor') && (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor))) { + return false; } - return debounce(func, wait, { 'leading': leading, 'maxWait': +wait, 'trailing': trailing }); + // IE < 9 iterates inherited properties before own properties. If the first + // iterated property is an object's own property then there are no inherited + // enumerable properties. + var result; + // In most environments an object's own properties are iterated before + // its inherited properties. If the last iterated property is an object's + // own property then there are no inherited enumerable properties. + baseForIn(value, function (subValue, key) { + result = key; + }); + return result === undefined || hasOwnProperty.call(value, result); } /** - * Creates a function that provides `value` to the wrapper function as its - * first argument. Any additional arguments provided to the function are - * appended to those provided to the wrapper function. The wrapper is invoked - * with the `this` binding of the created function. + * Checks if `value` is classified as a `RegExp` object. * * @static * @memberOf _ - * @category Function - * @param {*} value The value to wrap. - * @param {Function} wrapper The wrapper function. - * @returns {Function} Returns the new function. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * var p = _.wrap(_.escape, function(func, text) { - * return '

' + func(text) + '

'; - * }); + * _.isRegExp(/abc/); + * // => true * - * p('fred, barney, & pebbles'); - * // => '

fred, barney, & pebbles

' + * _.isRegExp('/abc/'); + * // => false */ - function wrap(value, wrapper) { - wrapper = wrapper == null ? identity : wrapper; - return createWrapper(wrapper, PARTIAL_FLAG, undefined, [value], []); + function isRegExp(value) { + return isObject(value) && objToString.call(value) == regexpTag; } - /*------------------------------------------------------------------------*/ - /** - * Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned, - * otherwise they are assigned by reference. If `customizer` is provided it is - * invoked to produce the cloned values. If `customizer` returns `undefined` - * cloning is handled by the method instead. The `customizer` is bound to - * `thisArg` and invoked with two argument; (value [, index|key, object]). - * - * **Note:** This method is loosely based on the - * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). - * The enumerable properties of `arguments` objects and objects created by - * constructors other than `Object` are cloned to plain `Object` objects. An - * empty object is returned for uncloneable values such as functions, DOM nodes, - * Maps, Sets, and WeakMaps. + * Checks if `value` is classified as a `String` primitive or object. * * @static * @memberOf _ * @category Lang - * @param {*} value The value to clone. - * @param {boolean} [isDeep] Specify a deep clone. - * @param {Function} [customizer] The function to customize cloning values. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {*} Returns the cloned value. + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * var users = [ - * { 'user': 'barney' }, - * { 'user': 'fred' } - * ]; - * - * var shallow = _.clone(users); - * shallow[0] === users[0]; + * _.isString('abc'); * // => true * - * var deep = _.clone(users, true); - * deep[0] === users[0]; - * // => false - * - * // using a customizer callback - * var el = _.clone(document.body, function(value) { - * if (_.isElement(value)) { - * return value.cloneNode(false); - * } - * }); - * - * el === document.body + * _.isString(1); * // => false - * el.nodeName - * // => BODY - * el.childNodes.length; - * // => 0 */ - function clone(value, isDeep, customizer, thisArg) { - if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) { - isDeep = false; - } else if (typeof isDeep == 'function') { - thisArg = customizer; - customizer = isDeep; - isDeep = false; - } - return typeof customizer == 'function' ? baseClone(value, isDeep, bindCallback(customizer, thisArg, 1)) : baseClone(value, isDeep); + function isString(value) { + return typeof value == 'string' || isObjectLike(value) && objToString.call(value) == stringTag; } /** - * Creates a deep clone of `value`. If `customizer` is provided it is invoked - * to produce the cloned values. If `customizer` returns `undefined` cloning - * is handled by the method instead. The `customizer` is bound to `thisArg` - * and invoked with two argument; (value [, index|key, object]). - * - * **Note:** This method is loosely based on the - * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). - * The enumerable properties of `arguments` objects and objects created by - * constructors other than `Object` are cloned to plain `Object` objects. An - * empty object is returned for uncloneable values such as functions, DOM nodes, - * Maps, Sets, and WeakMaps. + * Checks if `value` is classified as a typed array. * * @static * @memberOf _ * @category Lang - * @param {*} value The value to deep clone. - * @param {Function} [customizer] The function to customize cloning values. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {*} Returns the deep cloned value. + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. * @example * - * var users = [ - * { 'user': 'barney' }, - * { 'user': 'fred' } - * ]; + * _.isTypedArray(new Uint8Array); + * // => true * - * var deep = _.cloneDeep(users); - * deep[0] === users[0]; + * _.isTypedArray([]); * // => false + */ + function isTypedArray(value) { + return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)]; + } + + /** + * Checks if `value` is `undefined`. * - * // using a customizer callback - * var el = _.cloneDeep(document.body, function(value) { - * if (_.isElement(value)) { - * return value.cloneNode(true); - * } - * }); + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`. + * @example * - * el === document.body + * _.isUndefined(void 0); + * // => true + * + * _.isUndefined(null); * // => false - * el.nodeName - * // => BODY - * el.childNodes.length; - * // => 20 */ - function cloneDeep(value, customizer, thisArg) { - return typeof customizer == 'function' ? baseClone(value, true, bindCallback(customizer, thisArg, 1)) : baseClone(value, true); + function isUndefined(value) { + return value === undefined; } /** - * Checks if `value` is greater than `other`. + * Checks if `value` is less than `other`. * * @static * @memberOf _ * @category Lang * @param {*} value The value to compare. * @param {*} other The other value to compare. - * @returns {boolean} Returns `true` if `value` is greater than `other`, else `false`. + * @returns {boolean} Returns `true` if `value` is less than `other`, else `false`. * @example * - * _.gt(3, 1); + * _.lt(1, 3); * // => true * - * _.gt(3, 3); + * _.lt(3, 3); * // => false * - * _.gt(1, 3); + * _.lt(3, 1); * // => false */ - function gt(value, other) { - return value > other; + function lt(value, other) { + return value < other; } /** - * Checks if `value` is greater than or equal to `other`. + * Checks if `value` is less than or equal to `other`. * * @static * @memberOf _ * @category Lang * @param {*} value The value to compare. * @param {*} other The other value to compare. - * @returns {boolean} Returns `true` if `value` is greater than or equal to `other`, else `false`. + * @returns {boolean} Returns `true` if `value` is less than or equal to `other`, else `false`. * @example * - * _.gte(3, 1); + * _.lte(1, 3); * // => true * - * _.gte(3, 3); + * _.lte(3, 3); * // => true * - * _.gte(1, 3); + * _.lte(3, 1); * // => false */ - function gte(value, other) { - return value >= other; + function lte(value, other) { + return value <= other; } /** - * Checks if `value` is classified as an `arguments` object. + * Converts `value` to an array. * * @static * @memberOf _ * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @param {*} value The value to convert. + * @returns {Array} Returns the converted array. * @example * - * _.isArguments(function() { return arguments; }()); - * // => true - * - * _.isArguments([1, 2, 3]); - * // => false + * (function() { + * return _.toArray(arguments).slice(1); + * }(1, 2, 3)); + * // => [2, 3] */ - function isArguments(value) { - return isObjectLike(value) && isArrayLike(value) && hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee'); + function toArray(value) { + var length = value ? getLength(value) : 0; + if (!isLength(length)) { + return values(value); + } + if (!length) { + return []; + } + return arrayCopy(value); } /** - * Checks if `value` is classified as an `Array` object. + * Converts `value` to a plain object flattening inherited enumerable + * properties of `value` to own properties of the plain object. * * @static * @memberOf _ * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @param {*} value The value to convert. + * @returns {Object} Returns the converted plain object. + * @example + * + * function Foo() { + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.assign({ 'a': 1 }, new Foo); + * // => { 'a': 1, 'b': 2 } + * + * _.assign({ 'a': 1 }, _.toPlainObject(new Foo)); + * // => { 'a': 1, 'b': 2, 'c': 3 } + */ + function toPlainObject(value) { + return baseCopy(value, keysIn(value)); + } + + /*------------------------------------------------------------------------*/ + + /** + * Recursively merges own enumerable properties of the source object(s), that + * don't resolve to `undefined` into the destination object. Subsequent sources + * overwrite property assignments of previous sources. If `customizer` is + * provided it is invoked to produce the merged values of the destination and + * source properties. If `customizer` returns `undefined` merging is handled + * by the method instead. The `customizer` is bound to `thisArg` and invoked + * with five arguments: (objectValue, sourceValue, key, object, source). + * + * @static + * @memberOf _ + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @param {Function} [customizer] The function to customize assigned values. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {Object} Returns `object`. + * @example + * + * var users = { + * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }] + * }; + * + * var ages = { + * 'data': [{ 'age': 36 }, { 'age': 40 }] + * }; + * + * _.merge(users, ages); + * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] } + * + * // using a customizer callback + * var object = { + * 'fruits': ['apple'], + * 'vegetables': ['beet'] + * }; + * + * var other = { + * 'fruits': ['banana'], + * 'vegetables': ['carrot'] + * }; + * + * _.merge(object, other, function(a, b) { + * if (_.isArray(a)) { + * return a.concat(b); + * } + * }); + * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] } + */ + var merge = createAssigner(baseMerge); + + /** + * Assigns own enumerable properties of source object(s) to the destination + * object. Subsequent sources overwrite property assignments of previous sources. + * If `customizer` is provided it is invoked to produce the assigned values. + * The `customizer` is bound to `thisArg` and invoked with five arguments: + * (objectValue, sourceValue, key, object, source). + * + * **Note:** This method mutates `object` and is based on + * [`Object.assign`](http://ecma-international.org/ecma-262/6.0/#sec-object.assign). + * + * @static + * @memberOf _ + * @alias extend + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @param {Function} [customizer] The function to customize assigned values. + * @param {*} [thisArg] The `this` binding of `customizer`. + * @returns {Object} Returns `object`. * @example * - * _.isArray([1, 2, 3]); - * // => true + * _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' }); + * // => { 'user': 'fred', 'age': 40 } * - * _.isArray(function() { return arguments; }()); - * // => false + * // using a customizer callback + * var defaults = _.partialRight(_.assign, function(value, other) { + * return _.isUndefined(value) ? other : value; + * }); + * + * defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' }); + * // => { 'user': 'barney', 'age': 36 } */ - var isArray = nativeIsArray || function (value) { - return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag; - }; + var assign = createAssigner(function (object, source, customizer) { + return customizer ? assignWith(object, source, customizer) : baseAssign(object, source); + }); /** - * Checks if `value` is classified as a boolean primitive or object. + * Creates an object that inherits from the given `prototype` object. If a + * `properties` object is provided its own enumerable properties are assigned + * to the created object. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} prototype The object to inherit from. + * @param {Object} [properties] The properties to assign to the object. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {Object} Returns the new object. * @example * - * _.isBoolean(false); + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype, { + * 'constructor': Circle + * }); + * + * var circle = new Circle; + * circle instanceof Circle; * // => true * - * _.isBoolean(null); - * // => false + * circle instanceof Shape; + * // => true */ - function isBoolean(value) { - return value === true || value === false || isObjectLike(value) && objToString.call(value) == boolTag; + function create(prototype, properties, guard) { + var result = baseCreate(prototype); + if (guard && isIterateeCall(prototype, properties, guard)) { + properties = undefined; + } + return properties ? baseAssign(result, properties) : result; } /** - * Checks if `value` is classified as a `Date` object. + * Assigns own enumerable properties of source object(s) to the destination + * object for all destination properties that resolve to `undefined`. Once a + * property is set, additional values of the same property are ignored. + * + * **Note:** This method mutates `object`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. * @example * - * _.isDate(new Date); - * // => true - * - * _.isDate('Mon April 23 2012'); - * // => false + * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' }); + * // => { 'user': 'barney', 'age': 36 } */ - function isDate(value) { - return isObjectLike(value) && objToString.call(value) == dateTag; - } + var defaults = createDefaults(assign, assignDefaults); /** - * Checks if `value` is a DOM element. + * This method is like `_.defaults` except that it recursively assigns + * default properties. + * + * **Note:** This method mutates `object`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a DOM element, else `false`. + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. * @example * - * _.isElement(document.body); - * // => true + * _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } }); + * // => { 'user': { 'name': 'barney', 'age': 36 } } * - * _.isElement(''); - * // => false */ - function isElement(value) { - return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value); - } + var defaultsDeep = createDefaults(merge, mergeDefaults); /** - * Checks if `value` is empty. A value is considered empty unless it is an - * `arguments` object, array, string, or jQuery-like collection with a length - * greater than `0` or an object with own enumerable properties. + * This method is like `_.find` except that it returns the key of the first + * element `predicate` returns truthy for instead of the element itself. + * + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. * * @static * @memberOf _ - * @category Lang - * @param {Array|Object|string} value The value to inspect. - * @returns {boolean} Returns `true` if `value` is empty, else `false`. + * @category Object + * @param {Object} object The object to search. + * @param {Function|Object|string} [predicate=_.identity] The function invoked + * per iteration. + * @param {*} [thisArg] The `this` binding of `predicate`. + * @returns {string|undefined} Returns the key of the matched element, else `undefined`. * @example * - * _.isEmpty(null); - * // => true + * var users = { + * 'barney': { 'age': 36, 'active': true }, + * 'fred': { 'age': 40, 'active': false }, + * 'pebbles': { 'age': 1, 'active': true } + * }; * - * _.isEmpty(true); - * // => true + * _.findKey(users, function(chr) { + * return chr.age < 40; + * }); + * // => 'barney' (iteration order is not guaranteed) * - * _.isEmpty(1); - * // => true + * // using the `_.matches` callback shorthand + * _.findKey(users, { 'age': 1, 'active': true }); + * // => 'pebbles' * - * _.isEmpty([1, 2, 3]); - * // => false + * // using the `_.matchesProperty` callback shorthand + * _.findKey(users, 'active', false); + * // => 'fred' * - * _.isEmpty({ 'a': 1 }); - * // => false + * // using the `_.property` callback shorthand + * _.findKey(users, 'active'); + * // => 'barney' */ - function isEmpty(value) { - if (value == null) { - return true; - } - if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) || isObjectLike(value) && isFunction(value.splice))) { - return !value.length; - } - return !keys(value).length; - } + var findKey = createFindKey(baseForOwn); /** - * Performs a deep comparison between two values to determine if they are - * equivalent. If `customizer` is provided it is invoked to compare values. - * If `customizer` returns `undefined` comparisons are handled by the method - * instead. The `customizer` is bound to `thisArg` and invoked with three - * arguments: (value, other [, index|key]). + * This method is like `_.findKey` except that it iterates over elements of + * a collection in the opposite order. * - * **Note:** This method supports comparing arrays, booleans, `Date` objects, - * numbers, `Object` objects, regexes, and strings. Objects are compared by - * their own, not inherited, enumerable properties. Functions and DOM nodes - * are **not** supported. Provide a customizer function to extend support - * for comparing other values. + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. * * @static * @memberOf _ - * @alias eq - * @category Lang - * @param {*} value The value to compare. - * @param {*} other The other value to compare. - * @param {Function} [customizer] The function to customize value comparisons. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {boolean} Returns `true` if the values are equivalent, else `false`. + * @category Object + * @param {Object} object The object to search. + * @param {Function|Object|string} [predicate=_.identity] The function invoked + * per iteration. + * @param {*} [thisArg] The `this` binding of `predicate`. + * @returns {string|undefined} Returns the key of the matched element, else `undefined`. * @example * - * var object = { 'user': 'fred' }; - * var other = { 'user': 'fred' }; + * var users = { + * 'barney': { 'age': 36, 'active': true }, + * 'fred': { 'age': 40, 'active': false }, + * 'pebbles': { 'age': 1, 'active': true } + * }; * - * object == other; - * // => false + * _.findLastKey(users, function(chr) { + * return chr.age < 40; + * }); + * // => returns `pebbles` assuming `_.findKey` returns `barney` * - * _.isEqual(object, other); - * // => true + * // using the `_.matches` callback shorthand + * _.findLastKey(users, { 'age': 36, 'active': true }); + * // => 'barney' * - * // using a customizer callback - * var array = ['hello', 'goodbye']; - * var other = ['hi', 'goodbye']; + * // using the `_.matchesProperty` callback shorthand + * _.findLastKey(users, 'active', false); + * // => 'fred' * - * _.isEqual(array, other, function(value, other) { - * if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) { - * return true; - * } - * }); - * // => true + * // using the `_.property` callback shorthand + * _.findLastKey(users, 'active'); + * // => 'pebbles' */ - function isEqual(value, other, customizer, thisArg) { - customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined; - var result = customizer ? customizer(value, other) : undefined; - return result === undefined ? baseIsEqual(value, other, customizer) : !!result; - } + var findLastKey = createFindKey(baseForOwnRight); /** - * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`, - * `SyntaxError`, `TypeError`, or `URIError` object. + * Iterates over own and inherited enumerable properties of an object invoking + * `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked + * with three arguments: (value, key, object). Iteratee functions may exit + * iteration early by explicitly returning `false`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is an error object, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns `object`. * @example * - * _.isError(new Error); - * // => true + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * _.isError(Error); - * // => false + * Foo.prototype.c = 3; + * + * _.forIn(new Foo, function(value, key) { + * console.log(key); + * }); + * // => logs 'a', 'b', and 'c' (iteration order is not guaranteed) */ - function isError(value) { - return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag; - } + var forIn = createForIn(baseFor); /** - * Checks if `value` is a finite primitive number. - * - * **Note:** This method is based on [`Number.isFinite`](http://ecma-international.org/ecma-262/6.0/#sec-number.isfinite). + * This method is like `_.forIn` except that it iterates over properties of + * `object` in the opposite order. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a finite number, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns `object`. * @example * - * _.isFinite(10); - * // => true - * - * _.isFinite('10'); - * // => false - * - * _.isFinite(true); - * // => false + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * _.isFinite(Object(10)); - * // => false + * Foo.prototype.c = 3; * - * _.isFinite(Infinity); - * // => false + * _.forInRight(new Foo, function(value, key) { + * console.log(key); + * }); + * // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c' */ - function isFinite(value) { - return typeof value == 'number' && nativeIsFinite(value); - } + var forInRight = createForIn(baseForRight); /** - * Checks if `value` is classified as a `Function` object. + * Iterates over own enumerable properties of an object invoking `iteratee` + * for each property. The `iteratee` is bound to `thisArg` and invoked with + * three arguments: (value, key, object). Iteratee functions may exit iteration + * early by explicitly returning `false`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns `object`. * @example * - * _.isFunction(_); - * // => true + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * _.isFunction(/abc/); - * // => false + * Foo.prototype.c = 3; + * + * _.forOwn(new Foo, function(value, key) { + * console.log(key); + * }); + * // => logs 'a' and 'b' (iteration order is not guaranteed) */ - function isFunction(value) { - // The use of `Object#toString` avoids issues with the `typeof` operator - // in older versions of Chrome and Safari which return 'function' for regexes - // and Safari 8 equivalents which return 'object' for typed array constructors. - return isObject(value) && objToString.call(value) == funcTag; - } + var forOwn = createForOwn(baseForOwn); /** - * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`. - * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) + * This method is like `_.forOwn` except that it iterates over properties of + * `object` in the opposite order. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is an object, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns `object`. * @example * - * _.isObject({}); - * // => true + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * _.isObject([1, 2, 3]); - * // => true + * Foo.prototype.c = 3; * - * _.isObject(1); - * // => false + * _.forOwnRight(new Foo, function(value, key) { + * console.log(key); + * }); + * // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b' */ - function isObject(value) { - // Avoid a V8 JIT bug in Chrome 19-20. - // See https://code.google.com/p/v8/issues/detail?id=2291 for more details. - var type = typeof value === 'undefined' ? 'undefined' : _typeof(value); - return !!value && (type == 'object' || type == 'function'); - } + var forOwnRight = createForOwn(baseForOwnRight); /** - * Performs a deep comparison between `object` and `source` to determine if - * `object` contains equivalent property values. If `customizer` is provided - * it is invoked to compare values. If `customizer` returns `undefined` - * comparisons are handled by the method instead. The `customizer` is bound - * to `thisArg` and invoked with three arguments: (value, other, index|key). - * - * **Note:** This method supports comparing properties of arrays, booleans, - * `Date` objects, numbers, `Object` objects, regexes, and strings. Functions - * and DOM nodes are **not** supported. Provide a customizer function to extend - * support for comparing other values. + * Creates an array of function property names from all enumerable properties, + * own and inherited, of `object`. * * @static * @memberOf _ - * @category Lang + * @alias methods + * @category Object * @param {Object} object The object to inspect. - * @param {Object} source The object of property values to match. - * @param {Function} [customizer] The function to customize value comparisons. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {boolean} Returns `true` if `object` is a match, else `false`. + * @returns {Array} Returns the new array of property names. * @example * - * var object = { 'user': 'fred', 'age': 40 }; - * - * _.isMatch(object, { 'age': 40 }); - * // => true - * - * _.isMatch(object, { 'age': 36 }); - * // => false - * - * // using a customizer callback - * var object = { 'greeting': 'hello' }; - * var source = { 'greeting': 'hi' }; - * - * _.isMatch(object, source, function(value, other) { - * return _.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/) || undefined; - * }); - * // => true + * _.functions(_); + * // => ['after', 'ary', 'assign', ...] */ - function isMatch(object, source, customizer, thisArg) { - customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined; - return baseIsMatch(object, getMatchData(source), customizer); + function functions(object) { + return baseFunctions(object, keysIn(object)); } /** - * Checks if `value` is `NaN`. - * - * **Note:** This method is not the same as [`isNaN`](https://es5.github.io/#x15.1.2.4) - * which returns `true` for `undefined` and other non-numeric values. + * Gets the property value at `path` of `object`. If the resolved value is + * `undefined` the `defaultValue` is used in its place. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`. + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to get. + * @param {*} [defaultValue] The value returned if the resolved value is `undefined`. + * @returns {*} Returns the resolved value. * @example * - * _.isNaN(NaN); - * // => true + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; * - * _.isNaN(new Number(NaN)); - * // => true + * _.get(object, 'a[0].b.c'); + * // => 3 * - * isNaN(undefined); - * // => true + * _.get(object, ['a', '0', 'b', 'c']); + * // => 3 * - * _.isNaN(undefined); - * // => false + * _.get(object, 'a.b.c', 'default'); + * // => 'default' */ - function isNaN(value) { - // An `NaN` primitive is the only value that is not equal to itself. - // Perform the `toStringTag` check first to avoid errors with some host objects in IE. - return isNumber(value) && value != +value; + function get(object, path, defaultValue) { + var result = object == null ? undefined : baseGet(object, toPath(path), path + ''); + return result === undefined ? defaultValue : result; } /** - * Checks if `value` is a native function. + * Checks if `path` is a direct property. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a native function, else `false`. + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path to check. + * @returns {boolean} Returns `true` if `path` is a direct property, else `false`. * @example * - * _.isNative(Array.prototype.push); + * var object = { 'a': { 'b': { 'c': 3 } } }; + * + * _.has(object, 'a'); * // => true * - * _.isNative(_); - * // => false + * _.has(object, 'a.b.c'); + * // => true + * + * _.has(object, ['a', 'b', 'c']); + * // => true */ - function isNative(value) { - if (value == null) { + function has(object, path) { + if (object == null) { return false; } - if (isFunction(value)) { - return reIsNative.test(fnToString.call(value)); + var result = hasOwnProperty.call(object, path); + if (!result && !isKey(path)) { + path = toPath(path); + object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); + if (object == null) { + return false; + } + path = last(path); + result = hasOwnProperty.call(object, path); } - return isObjectLike(value) && reIsHostCtor.test(value); + return result || isLength(object.length) && isIndex(path, object.length) && (isArray(object) || isArguments(object)); } /** - * Checks if `value` is `null`. + * Creates an object composed of the inverted keys and values of `object`. + * If `object` contains duplicate values, subsequent values overwrite property + * assignments of previous values unless `multiValue` is `true`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is `null`, else `false`. + * @category Object + * @param {Object} object The object to invert. + * @param {boolean} [multiValue] Allow multiple values per key. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {Object} Returns the new inverted object. * @example * - * _.isNull(null); - * // => true + * var object = { 'a': 1, 'b': 2, 'c': 1 }; + * + * _.invert(object); + * // => { '1': 'c', '2': 'b' } * - * _.isNull(void 0); - * // => false + * // with `multiValue` + * _.invert(object, true); + * // => { '1': ['a', 'c'], '2': ['b'] } */ - function isNull(value) { - return value === null; + function invert(object, multiValue, guard) { + if (guard && isIterateeCall(object, multiValue, guard)) { + multiValue = undefined; + } + var index = -1, + props = keys(object), + length = props.length, + result = {}; + + while (++index < length) { + var key = props[index], + value = object[key]; + + if (multiValue) { + if (hasOwnProperty.call(result, value)) { + result[value].push(key); + } else { + result[value] = [key]; + } + } else { + result[value] = key; + } + } + return result; } /** - * Checks if `value` is classified as a `Number` primitive or object. + * Creates an array of the own enumerable property names of `object`. * - * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are classified - * as numbers, use the `_.isFinite` method. + * **Note:** Non-object values are coerced to objects. See the + * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys) + * for more details. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. * @example * - * _.isNumber(8.4); - * // => true + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * _.isNumber(NaN); - * // => true + * Foo.prototype.c = 3; * - * _.isNumber('8.4'); - * // => false + * _.keys(new Foo); + * // => ['a', 'b'] (iteration order is not guaranteed) + * + * _.keys('hi'); + * // => ['0', '1'] */ - function isNumber(value) { - return typeof value == 'number' || isObjectLike(value) && objToString.call(value) == numberTag; - } + var keys = !nativeKeys ? shimKeys : function (object) { + var Ctor = object == null ? undefined : object.constructor; + if (typeof Ctor == 'function' && Ctor.prototype === object || typeof object != 'function' && isArrayLike(object)) { + return shimKeys(object); + } + return isObject(object) ? nativeKeys(object) : []; + }; /** - * Checks if `value` is a plain object, that is, an object created by the - * `Object` constructor or one with a `[[Prototype]]` of `null`. + * Creates an array of the own and inherited enumerable property names of `object`. * - * **Note:** This method assumes objects created by the `Object` constructor - * have no inherited enumerable properties. + * **Note:** Non-object values are coerced to objects. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. * @example * * function Foo() { * this.a = 1; + * this.b = 2; * } * - * _.isPlainObject(new Foo); - * // => false - * - * _.isPlainObject([1, 2, 3]); - * // => false - * - * _.isPlainObject({ 'x': 0, 'y': 0 }); - * // => true + * Foo.prototype.c = 3; * - * _.isPlainObject(Object.create(null)); - * // => true + * _.keysIn(new Foo); + * // => ['a', 'b', 'c'] (iteration order is not guaranteed) */ - function isPlainObject(value) { - var Ctor; + function keysIn(object) { + if (object == null) { + return []; + } + if (!isObject(object)) { + object = Object(object); + } + var length = object.length; + length = length && isLength(length) && (isArray(object) || isArguments(object)) && length || 0; - // Exit early for non `Object` objects. - if (!(isObjectLike(value) && objToString.call(value) == objectTag && !isArguments(value)) || !hasOwnProperty.call(value, 'constructor') && (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor))) { - return false; + var Ctor = object.constructor, + index = -1, + isProto = typeof Ctor == 'function' && Ctor.prototype === object, + result = Array(length), + skipIndexes = length > 0; + + while (++index < length) { + result[index] = index + ''; } - // IE < 9 iterates inherited properties before own properties. If the first - // iterated property is an object's own property then there are no inherited - // enumerable properties. - var result; - // In most environments an object's own properties are iterated before - // its inherited properties. If the last iterated property is an object's - // own property then there are no inherited enumerable properties. - baseForIn(value, function (subValue, key) { - result = key; - }); - return result === undefined || hasOwnProperty.call(value, result); + for (var key in object) { + if (!(skipIndexes && isIndex(key, length)) && !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) { + result.push(key); + } + } + return result; } /** - * Checks if `value` is classified as a `RegExp` object. + * The opposite of `_.mapValues`; this method creates an object with the + * same values as `object` and keys generated by running each own enumerable + * property of `object` through `iteratee`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function|Object|string} [iteratee=_.identity] The function invoked + * per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns the new mapped object. * @example * - * _.isRegExp(/abc/); - * // => true - * - * _.isRegExp('/abc/'); - * // => false + * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + * return key + value; + * }); + * // => { 'a1': 1, 'b2': 2 } */ - function isRegExp(value) { - return isObject(value) && objToString.call(value) == regexpTag; - } + var mapKeys = createObjectMapper(true); /** - * Checks if `value` is classified as a `String` primitive or object. + * Creates an object with the same keys as `object` and values generated by + * running each own enumerable property of `object` through `iteratee`. The + * iteratee function is bound to `thisArg` and invoked with three arguments: + * (value, key, object). + * + * If a property name is provided for `iteratee` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `iteratee` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function|Object|string} [iteratee=_.identity] The function invoked + * per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns the new mapped object. * @example * - * _.isString('abc'); - * // => true + * _.mapValues({ 'a': 1, 'b': 2 }, function(n) { + * return n * 3; + * }); + * // => { 'a': 3, 'b': 6 } * - * _.isString(1); - * // => false + * var users = { + * 'fred': { 'user': 'fred', 'age': 40 }, + * 'pebbles': { 'user': 'pebbles', 'age': 1 } + * }; + * + * // using the `_.property` callback shorthand + * _.mapValues(users, 'age'); + * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed) */ - function isString(value) { - return typeof value == 'string' || isObjectLike(value) && objToString.call(value) == stringTag; - } + var mapValues = createObjectMapper(); /** - * Checks if `value` is classified as a typed array. + * The opposite of `_.pick`; this method creates an object composed of the + * own and inherited enumerable properties of `object` that are not omitted. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`. + * @category Object + * @param {Object} object The source object. + * @param {Function|...(string|string[])} [predicate] The function invoked per + * iteration or property names to omit, specified as individual property + * names or arrays of property names. + * @param {*} [thisArg] The `this` binding of `predicate`. + * @returns {Object} Returns the new object. * @example * - * _.isTypedArray(new Uint8Array); - * // => true + * var object = { 'user': 'fred', 'age': 40 }; * - * _.isTypedArray([]); - * // => false + * _.omit(object, 'age'); + * // => { 'user': 'fred' } + * + * _.omit(object, _.isNumber); + * // => { 'user': 'fred' } */ - function isTypedArray(value) { - return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)]; - } + var omit = restParam(function (object, props) { + if (object == null) { + return {}; + } + if (typeof props[0] != 'function') { + var props = arrayMap(baseFlatten(props), String); + return pickByArray(object, baseDifference(keysIn(object), props)); + } + var predicate = bindCallback(props[0], props[1], 3); + return pickByCallback(object, function (value, key, object) { + return !predicate(value, key, object); + }); + }); /** - * Checks if `value` is `undefined`. + * Creates a two dimensional array of the key-value pairs for `object`, + * e.g. `[[key1, value1], [key2, value2]]`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`. + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the new array of key-value pairs. * @example * - * _.isUndefined(void 0); - * // => true - * - * _.isUndefined(null); - * // => false + * _.pairs({ 'barney': 36, 'fred': 40 }); + * // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed) */ - function isUndefined(value) { - return value === undefined; + function pairs(object) { + object = toObject(object); + + var index = -1, + props = keys(object), + length = props.length, + result = Array(length); + + while (++index < length) { + var key = props[index]; + result[index] = [key, object[key]]; + } + return result; } /** - * Checks if `value` is less than `other`. + * Creates an object composed of the picked `object` properties. Property + * names may be specified as individual arguments or as arrays of property + * names. If `predicate` is provided it is invoked for each property of `object` + * picking the properties `predicate` returns truthy for. The predicate is + * bound to `thisArg` and invoked with three arguments: (value, key, object). * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to compare. - * @param {*} other The other value to compare. - * @returns {boolean} Returns `true` if `value` is less than `other`, else `false`. + * @category Object + * @param {Object} object The source object. + * @param {Function|...(string|string[])} [predicate] The function invoked per + * iteration or property names to pick, specified as individual property + * names or arrays of property names. + * @param {*} [thisArg] The `this` binding of `predicate`. + * @returns {Object} Returns the new object. * @example * - * _.lt(1, 3); - * // => true + * var object = { 'user': 'fred', 'age': 40 }; * - * _.lt(3, 3); - * // => false + * _.pick(object, 'user'); + * // => { 'user': 'fred' } * - * _.lt(3, 1); - * // => false + * _.pick(object, _.isString); + * // => { 'user': 'fred' } */ - function lt(value, other) { - return value < other; - } + var pick = restParam(function (object, props) { + if (object == null) { + return {}; + } + return typeof props[0] == 'function' ? pickByCallback(object, bindCallback(props[0], props[1], 3)) : pickByArray(object, baseFlatten(props)); + }); /** - * Checks if `value` is less than or equal to `other`. + * This method is like `_.get` except that if the resolved value is a function + * it is invoked with the `this` binding of its parent object and its result + * is returned. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to compare. - * @param {*} other The other value to compare. - * @returns {boolean} Returns `true` if `value` is less than or equal to `other`, else `false`. + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to resolve. + * @param {*} [defaultValue] The value returned if the resolved value is `undefined`. + * @returns {*} Returns the resolved value. * @example * - * _.lte(1, 3); - * // => true + * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] }; * - * _.lte(3, 3); - * // => true + * _.result(object, 'a[0].b.c1'); + * // => 3 * - * _.lte(3, 1); - * // => false + * _.result(object, 'a[0].b.c2'); + * // => 4 + * + * _.result(object, 'a.b.c', 'default'); + * // => 'default' + * + * _.result(object, 'a.b.c', _.constant('default')); + * // => 'default' */ - function lte(value, other) { - return value <= other; + function result(object, path, defaultValue) { + var result = object == null ? undefined : object[path]; + if (result === undefined) { + if (object != null && !isKey(path, object)) { + path = toPath(path); + object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); + result = object == null ? undefined : object[last(path)]; + } + result = result === undefined ? defaultValue : result; + } + return isFunction(result) ? result.call(object) : result; } /** - * Converts `value` to an array. + * Sets the property value of `path` on `object`. If a portion of `path` + * does not exist it is created. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to convert. - * @returns {Array} Returns the converted array. + * @category Object + * @param {Object} object The object to augment. + * @param {Array|string} path The path of the property to set. + * @param {*} value The value to set. + * @returns {Object} Returns `object`. * @example * - * (function() { - * return _.toArray(arguments).slice(1); - * }(1, 2, 3)); - * // => [2, 3] + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; + * + * _.set(object, 'a[0].b.c', 4); + * console.log(object.a[0].b.c); + * // => 4 + * + * _.set(object, 'x[0].y.z', 5); + * console.log(object.x[0].y.z); + * // => 5 */ - function toArray(value) { - var length = value ? getLength(value) : 0; - if (!isLength(length)) { - return values(value); + function set(object, path, value) { + if (object == null) { + return object; } - if (!length) { - return []; + var pathKey = path + ''; + path = object[pathKey] != null || isKey(path, object) ? [pathKey] : toPath(path); + + var index = -1, + length = path.length, + lastIndex = length - 1, + nested = object; + + while (nested != null && ++index < length) { + var key = path[index]; + if (isObject(nested)) { + if (index == lastIndex) { + nested[key] = value; + } else if (nested[key] == null) { + nested[key] = isIndex(path[index + 1]) ? [] : {}; + } + } + nested = nested[key]; } - return arrayCopy(value); + return object; } /** - * Converts `value` to a plain object flattening inherited enumerable - * properties of `value` to own properties of the plain object. + * An alternative to `_.reduce`; this method transforms `object` to a new + * `accumulator` object which is the result of running each of its own enumerable + * properties through `iteratee`, with each invocation potentially mutating + * the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked + * with four arguments: (accumulator, value, key, object). Iteratee functions + * may exit iteration early by explicitly returning `false`. * * @static * @memberOf _ - * @category Lang - * @param {*} value The value to convert. - * @returns {Object} Returns the converted plain object. + * @category Object + * @param {Array|Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [accumulator] The custom accumulator value. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {*} Returns the accumulated value. * @example * - * function Foo() { - * this.b = 2; - * } - * - * Foo.prototype.c = 3; - * - * _.assign({ 'a': 1 }, new Foo); - * // => { 'a': 1, 'b': 2 } + * _.transform([2, 3, 4], function(result, n) { + * result.push(n *= n); + * return n % 2 == 0; + * }); + * // => [4, 9] * - * _.assign({ 'a': 1 }, _.toPlainObject(new Foo)); - * // => { 'a': 1, 'b': 2, 'c': 3 } + * _.transform({ 'a': 1, 'b': 2 }, function(result, n, key) { + * result[key] = n * 3; + * }); + * // => { 'a': 3, 'b': 6 } */ - function toPlainObject(value) { - return baseCopy(value, keysIn(value)); - } + function transform(object, iteratee, accumulator, thisArg) { + var isArr = isArray(object) || isTypedArray(object); + iteratee = getCallback(iteratee, thisArg, 4); - /*------------------------------------------------------------------------*/ + if (accumulator == null) { + if (isArr || isObject(object)) { + var Ctor = object.constructor; + if (isArr) { + accumulator = isArray(object) ? new Ctor() : []; + } else { + accumulator = baseCreate(isFunction(Ctor) ? Ctor.prototype : undefined); + } + } else { + accumulator = {}; + } + } + (isArr ? arrayEach : baseForOwn)(object, function (value, index, object) { + return iteratee(accumulator, value, index, object); + }); + return accumulator; + } /** - * Recursively merges own enumerable properties of the source object(s), that - * don't resolve to `undefined` into the destination object. Subsequent sources - * overwrite property assignments of previous sources. If `customizer` is - * provided it is invoked to produce the merged values of the destination and - * source properties. If `customizer` returns `undefined` merging is handled - * by the method instead. The `customizer` is bound to `thisArg` and invoked - * with five arguments: (objectValue, sourceValue, key, object, source). + * Creates an array of the own enumerable property values of `object`. + * + * **Note:** Non-object values are coerced to objects. * * @static * @memberOf _ * @category Object - * @param {Object} object The destination object. - * @param {...Object} [sources] The source objects. - * @param {Function} [customizer] The function to customize assigned values. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {Object} Returns `object`. + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property values. * @example * - * var users = { - * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }] - * }; - * - * var ages = { - * 'data': [{ 'age': 36 }, { 'age': 40 }] - * }; - * - * _.merge(users, ages); - * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] } + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * // using a customizer callback - * var object = { - * 'fruits': ['apple'], - * 'vegetables': ['beet'] - * }; + * Foo.prototype.c = 3; * - * var other = { - * 'fruits': ['banana'], - * 'vegetables': ['carrot'] - * }; + * _.values(new Foo); + * // => [1, 2] (iteration order is not guaranteed) * - * _.merge(object, other, function(a, b) { - * if (_.isArray(a)) { - * return a.concat(b); - * } - * }); - * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] } + * _.values('hi'); + * // => ['h', 'i'] */ - var merge = createAssigner(baseMerge); + function values(object) { + return baseValues(object, keys(object)); + } /** - * Assigns own enumerable properties of source object(s) to the destination - * object. Subsequent sources overwrite property assignments of previous sources. - * If `customizer` is provided it is invoked to produce the assigned values. - * The `customizer` is bound to `thisArg` and invoked with five arguments: - * (objectValue, sourceValue, key, object, source). + * Creates an array of the own and inherited enumerable property values + * of `object`. * - * **Note:** This method mutates `object` and is based on - * [`Object.assign`](http://ecma-international.org/ecma-262/6.0/#sec-object.assign). + * **Note:** Non-object values are coerced to objects. * * @static * @memberOf _ - * @alias extend * @category Object - * @param {Object} object The destination object. - * @param {...Object} [sources] The source objects. - * @param {Function} [customizer] The function to customize assigned values. - * @param {*} [thisArg] The `this` binding of `customizer`. - * @returns {Object} Returns `object`. + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property values. * @example * - * _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' }); - * // => { 'user': 'fred', 'age': 40 } + * function Foo() { + * this.a = 1; + * this.b = 2; + * } * - * // using a customizer callback - * var defaults = _.partialRight(_.assign, function(value, other) { - * return _.isUndefined(value) ? other : value; - * }); + * Foo.prototype.c = 3; * - * defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' }); - * // => { 'user': 'barney', 'age': 36 } + * _.valuesIn(new Foo); + * // => [1, 2, 3] (iteration order is not guaranteed) */ - var assign = createAssigner(function (object, source, customizer) { - return customizer ? assignWith(object, source, customizer) : baseAssign(object, source); - }); + function valuesIn(object) { + return baseValues(object, keysIn(object)); + } + + /*------------------------------------------------------------------------*/ /** - * Creates an object that inherits from the given `prototype` object. If a - * `properties` object is provided its own enumerable properties are assigned - * to the created object. + * Checks if `n` is between `start` and up to but not including, `end`. If + * `end` is not specified it is set to `start` with `start` then set to `0`. * * @static * @memberOf _ - * @category Object - * @param {Object} prototype The object to inherit from. - * @param {Object} [properties] The properties to assign to the object. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. - * @returns {Object} Returns the new object. + * @category Number + * @param {number} n The number to check. + * @param {number} [start=0] The start of the range. + * @param {number} end The end of the range. + * @returns {boolean} Returns `true` if `n` is in the range, else `false`. * @example * - * function Shape() { - * this.x = 0; - * this.y = 0; - * } + * _.inRange(3, 2, 4); + * // => true * - * function Circle() { - * Shape.call(this); - * } + * _.inRange(4, 8); + * // => true * - * Circle.prototype = _.create(Shape.prototype, { - * 'constructor': Circle - * }); + * _.inRange(4, 2); + * // => false * - * var circle = new Circle; - * circle instanceof Circle; - * // => true + * _.inRange(2, 2); + * // => false * - * circle instanceof Shape; + * _.inRange(1.2, 2); * // => true + * + * _.inRange(5.2, 4); + * // => false */ - function create(prototype, properties, guard) { - var result = baseCreate(prototype); - if (guard && isIterateeCall(prototype, properties, guard)) { - properties = undefined; + function inRange(value, start, end) { + start = +start || 0; + if (end === undefined) { + end = start; + start = 0; + } else { + end = +end || 0; } - return properties ? baseAssign(result, properties) : result; + return value >= nativeMin(start, end) && value < nativeMax(start, end); } /** - * Assigns own enumerable properties of source object(s) to the destination - * object for all destination properties that resolve to `undefined`. Once a - * property is set, additional values of the same property are ignored. - * - * **Note:** This method mutates `object`. + * Produces a random number between `min` and `max` (inclusive). If only one + * argument is provided a number between `0` and the given number is returned. + * If `floating` is `true`, or either `min` or `max` are floats, a floating-point + * number is returned instead of an integer. * * @static * @memberOf _ - * @category Object - * @param {Object} object The destination object. - * @param {...Object} [sources] The source objects. - * @returns {Object} Returns `object`. + * @category Number + * @param {number} [min=0] The minimum possible value. + * @param {number} [max=1] The maximum possible value. + * @param {boolean} [floating] Specify returning a floating-point number. + * @returns {number} Returns the random number. * @example * - * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' }); - * // => { 'user': 'barney', 'age': 36 } + * _.random(0, 5); + * // => an integer between 0 and 5 + * + * _.random(5); + * // => also an integer between 0 and 5 + * + * _.random(5, true); + * // => a floating-point number between 0 and 5 + * + * _.random(1.2, 5.2); + * // => a floating-point number between 1.2 and 5.2 */ - var defaults = createDefaults(assign, assignDefaults); + function random(min, max, floating) { + if (floating && isIterateeCall(min, max, floating)) { + max = floating = undefined; + } + var noMin = min == null, + noMax = max == null; + + if (floating == null) { + if (noMax && typeof min == 'boolean') { + floating = min; + min = 1; + } else if (typeof max == 'boolean') { + floating = max; + noMax = true; + } + } + if (noMin && noMax) { + max = 1; + noMax = false; + } + min = +min || 0; + if (noMax) { + max = min; + min = 0; + } else { + max = +max || 0; + } + if (floating || min % 1 || max % 1) { + var rand = nativeRandom(); + return nativeMin(min + rand * (max - min + parseFloat('1e-' + ((rand + '').length - 1))), max); + } + return baseRandom(min, max); + } + + /*------------------------------------------------------------------------*/ /** - * This method is like `_.defaults` except that it recursively assigns - * default properties. - * - * **Note:** This method mutates `object`. + * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase). * * @static * @memberOf _ - * @category Object - * @param {Object} object The destination object. - * @param {...Object} [sources] The source objects. - * @returns {Object} Returns `object`. + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the camel cased string. * @example * - * _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } }); - * // => { 'user': { 'name': 'barney', 'age': 36 } } + * _.camelCase('Foo Bar'); + * // => 'fooBar' * + * _.camelCase('--foo-bar'); + * // => 'fooBar' + * + * _.camelCase('__foo_bar__'); + * // => 'fooBar' */ - var defaultsDeep = createDefaults(merge, mergeDefaults); + var camelCase = createCompounder(function (result, word, index) { + word = word.toLowerCase(); + return result + (index ? word.charAt(0).toUpperCase() + word.slice(1) : word); + }); /** - * This method is like `_.find` except that it returns the key of the first - * element `predicate` returns truthy for instead of the element itself. - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. + * Capitalizes the first character of `string`. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to search. - * @param {Function|Object|string} [predicate=_.identity] The function invoked - * per iteration. - * @param {*} [thisArg] The `this` binding of `predicate`. - * @returns {string|undefined} Returns the key of the matched element, else `undefined`. + * @category String + * @param {string} [string=''] The string to capitalize. + * @returns {string} Returns the capitalized string. * @example * - * var users = { - * 'barney': { 'age': 36, 'active': true }, - * 'fred': { 'age': 40, 'active': false }, - * 'pebbles': { 'age': 1, 'active': true } - * }; - * - * _.findKey(users, function(chr) { - * return chr.age < 40; - * }); - * // => 'barney' (iteration order is not guaranteed) - * - * // using the `_.matches` callback shorthand - * _.findKey(users, { 'age': 1, 'active': true }); - * // => 'pebbles' - * - * // using the `_.matchesProperty` callback shorthand - * _.findKey(users, 'active', false); - * // => 'fred' - * - * // using the `_.property` callback shorthand - * _.findKey(users, 'active'); - * // => 'barney' + * _.capitalize('fred'); + * // => 'Fred' */ - var findKey = createFindKey(baseForOwn); + function capitalize(string) { + string = baseToString(string); + return string && string.charAt(0).toUpperCase() + string.slice(1); + } /** - * This method is like `_.findKey` except that it iterates over elements of - * a collection in the opposite order. - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. + * Deburrs `string` by converting [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table) + * to basic latin letters and removing [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to search. - * @param {Function|Object|string} [predicate=_.identity] The function invoked - * per iteration. - * @param {*} [thisArg] The `this` binding of `predicate`. - * @returns {string|undefined} Returns the key of the matched element, else `undefined`. + * @category String + * @param {string} [string=''] The string to deburr. + * @returns {string} Returns the deburred string. * @example * - * var users = { - * 'barney': { 'age': 36, 'active': true }, - * 'fred': { 'age': 40, 'active': false }, - * 'pebbles': { 'age': 1, 'active': true } - * }; - * - * _.findLastKey(users, function(chr) { - * return chr.age < 40; - * }); - * // => returns `pebbles` assuming `_.findKey` returns `barney` - * - * // using the `_.matches` callback shorthand - * _.findLastKey(users, { 'age': 36, 'active': true }); - * // => 'barney' - * - * // using the `_.matchesProperty` callback shorthand - * _.findLastKey(users, 'active', false); - * // => 'fred' - * - * // using the `_.property` callback shorthand - * _.findLastKey(users, 'active'); - * // => 'pebbles' + * _.deburr('déjà vu'); + * // => 'deja vu' */ - var findLastKey = createFindKey(baseForOwnRight); + function deburr(string) { + string = baseToString(string); + return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, ''); + } /** - * Iterates over own and inherited enumerable properties of an object invoking - * `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked - * with three arguments: (value, key, object). Iteratee functions may exit - * iteration early by explicitly returning `false`. + * Checks if `string` ends with the given target string. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function} [iteratee=_.identity] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns `object`. + * @category String + * @param {string} [string=''] The string to search. + * @param {string} [target] The string to search for. + * @param {number} [position=string.length] The position to search from. + * @returns {boolean} Returns `true` if `string` ends with `target`, else `false`. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } + * _.endsWith('abc', 'c'); + * // => true * - * Foo.prototype.c = 3; + * _.endsWith('abc', 'b'); + * // => false * - * _.forIn(new Foo, function(value, key) { - * console.log(key); - * }); - * // => logs 'a', 'b', and 'c' (iteration order is not guaranteed) + * _.endsWith('abc', 'b', 2); + * // => true */ - var forIn = createForIn(baseFor); + function endsWith(string, target, position) { + string = baseToString(string); + target = target + ''; + + var length = string.length; + position = position === undefined ? length : nativeMin(position < 0 ? 0 : +position || 0, length); + + position -= target.length; + return position >= 0 && string.indexOf(target, position) == position; + } /** - * This method is like `_.forIn` except that it iterates over properties of - * `object` in the opposite order. + * Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to + * their corresponding HTML entities. + * + * **Note:** No other characters are escaped. To escape additional characters + * use a third-party library like [_he_](https://mths.be/he). + * + * Though the ">" character is escaped for symmetry, characters like + * ">" and "/" don't need escaping in HTML and have no special meaning + * unless they're part of a tag or unquoted attribute value. + * See [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands) + * (under "semi-related fun fact") for more details. + * + * Backticks are escaped because in Internet Explorer < 9, they can break out + * of attribute values or HTML comments. See [#59](https://html5sec.org/#59), + * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and + * [#133](https://html5sec.org/#133) of the [HTML5 Security Cheatsheet](https://html5sec.org/) + * for more details. + * + * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping) + * to reduce XSS vectors. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function} [iteratee=_.identity] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns `object`. + * @category String + * @param {string} [string=''] The string to escape. + * @returns {string} Returns the escaped string. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } + * _.escape('fred, barney, & pebbles'); + * // => 'fred, barney, & pebbles' + */ + function escape(string) { + // Reset `lastIndex` because in IE < 9 `String#replace` does not. + string = baseToString(string); + return string && reHasUnescapedHtml.test(string) ? string.replace(reUnescapedHtml, escapeHtmlChar) : string; + } + + /** + * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?", + * "*", "+", "(", ")", "[", "]", "{" and "}" in `string`. * - * Foo.prototype.c = 3; + * @static + * @memberOf _ + * @category String + * @param {string} [string=''] The string to escape. + * @returns {string} Returns the escaped string. + * @example * - * _.forInRight(new Foo, function(value, key) { - * console.log(key); - * }); - * // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c' + * _.escapeRegExp('[lodash](https://lodash.com/)'); + * // => '\[lodash\]\(https:\/\/lodash\.com\/\)' */ - var forInRight = createForIn(baseForRight); + function escapeRegExp(string) { + string = baseToString(string); + return string && reHasRegExpChars.test(string) ? string.replace(reRegExpChars, escapeRegExpChar) : string || '(?:)'; + } /** - * Iterates over own enumerable properties of an object invoking `iteratee` - * for each property. The `iteratee` is bound to `thisArg` and invoked with - * three arguments: (value, key, object). Iteratee functions may exit iteration - * early by explicitly returning `false`. + * Converts `string` to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles). * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function} [iteratee=_.identity] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns `object`. + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the kebab cased string. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } + * _.kebabCase('Foo Bar'); + * // => 'foo-bar' * - * Foo.prototype.c = 3; + * _.kebabCase('fooBar'); + * // => 'foo-bar' * - * _.forOwn(new Foo, function(value, key) { - * console.log(key); - * }); - * // => logs 'a' and 'b' (iteration order is not guaranteed) + * _.kebabCase('__foo_bar__'); + * // => 'foo-bar' */ - var forOwn = createForOwn(baseForOwn); + var kebabCase = createCompounder(function (result, word, index) { + return result + (index ? '-' : '') + word.toLowerCase(); + }); /** - * This method is like `_.forOwn` except that it iterates over properties of - * `object` in the opposite order. + * Pads `string` on the left and right sides if it's shorter than `length`. + * Padding characters are truncated if they can't be evenly divided by `length`. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function} [iteratee=_.identity] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns `object`. + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } + * _.pad('abc', 8); + * // => ' abc ' * - * Foo.prototype.c = 3; + * _.pad('abc', 8, '_-'); + * // => '_-abc_-_' * - * _.forOwnRight(new Foo, function(value, key) { - * console.log(key); - * }); - * // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b' + * _.pad('abc', 3); + * // => 'abc' */ - var forOwnRight = createForOwn(baseForOwnRight); + function pad(string, length, chars) { + string = baseToString(string); + length = +length; + + var strLength = string.length; + if (strLength >= length || !nativeIsFinite(length)) { + return string; + } + var mid = (length - strLength) / 2, + leftLength = nativeFloor(mid), + rightLength = nativeCeil(mid); + + chars = createPadding('', rightLength, chars); + return chars.slice(0, leftLength) + string + chars; + } /** - * Creates an array of function property names from all enumerable properties, - * own and inherited, of `object`. + * Pads `string` on the left side if it's shorter than `length`. Padding + * characters are truncated if they exceed `length`. * * @static * @memberOf _ - * @alias methods - * @category Object - * @param {Object} object The object to inspect. - * @returns {Array} Returns the new array of property names. + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. * @example * - * _.functions(_); - * // => ['after', 'ary', 'assign', ...] + * _.padLeft('abc', 6); + * // => ' abc' + * + * _.padLeft('abc', 6, '_-'); + * // => '_-_abc' + * + * _.padLeft('abc', 3); + * // => 'abc' */ - function functions(object) { - return baseFunctions(object, keysIn(object)); - } + var padLeft = createPadDir(); /** - * Gets the property value at `path` of `object`. If the resolved value is - * `undefined` the `defaultValue` is used in its place. + * Pads `string` on the right side if it's shorter than `length`. Padding + * characters are truncated if they exceed `length`. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to query. - * @param {Array|string} path The path of the property to get. - * @param {*} [defaultValue] The value returned if the resolved value is `undefined`. - * @returns {*} Returns the resolved value. + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. * @example * - * var object = { 'a': [{ 'b': { 'c': 3 } }] }; - * - * _.get(object, 'a[0].b.c'); - * // => 3 + * _.padRight('abc', 6); + * // => 'abc ' * - * _.get(object, ['a', '0', 'b', 'c']); - * // => 3 + * _.padRight('abc', 6, '_-'); + * // => 'abc_-_' * - * _.get(object, 'a.b.c', 'default'); - * // => 'default' + * _.padRight('abc', 3); + * // => 'abc' */ - function get(object, path, defaultValue) { - var result = object == null ? undefined : baseGet(object, toPath(path), path + ''); - return result === undefined ? defaultValue : result; - } + var padRight = createPadDir(true); /** - * Checks if `path` is a direct property. + * Converts `string` to an integer of the specified radix. If `radix` is + * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal, + * in which case a `radix` of `16` is used. + * + * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E) + * of `parseInt`. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to query. - * @param {Array|string} path The path to check. - * @returns {boolean} Returns `true` if `path` is a direct property, else `false`. + * @category String + * @param {string} string The string to convert. + * @param {number} [radix] The radix to interpret `value` by. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {number} Returns the converted integer. * @example * - * var object = { 'a': { 'b': { 'c': 3 } } }; - * - * _.has(object, 'a'); - * // => true - * - * _.has(object, 'a.b.c'); - * // => true + * _.parseInt('08'); + * // => 8 * - * _.has(object, ['a', 'b', 'c']); - * // => true + * _.map(['6', '08', '10'], _.parseInt); + * // => [6, 8, 10] */ - function has(object, path) { - if (object == null) { - return false; - } - var result = hasOwnProperty.call(object, path); - if (!result && !isKey(path)) { - path = toPath(path); - object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); - if (object == null) { - return false; - } - path = last(path); - result = hasOwnProperty.call(object, path); + function parseInt(string, radix, guard) { + // Firefox < 21 and Opera < 15 follow ES3 for `parseInt`. + // Chrome fails to trim leading whitespace characters. + // See https://code.google.com/p/v8/issues/detail?id=3109 for more details. + if (guard ? isIterateeCall(string, radix, guard) : radix == null) { + radix = 0; + } else if (radix) { + radix = +radix; } - return result || isLength(object.length) && isIndex(path, object.length) && (isArray(object) || isArguments(object)); + string = trim(string); + return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10)); } /** - * Creates an object composed of the inverted keys and values of `object`. - * If `object` contains duplicate values, subsequent values overwrite property - * assignments of previous values unless `multiValue` is `true`. + * Repeats the given string `n` times. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to invert. - * @param {boolean} [multiValue] Allow multiple values per key. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. - * @returns {Object} Returns the new inverted object. + * @category String + * @param {string} [string=''] The string to repeat. + * @param {number} [n=0] The number of times to repeat the string. + * @returns {string} Returns the repeated string. * @example * - * var object = { 'a': 1, 'b': 2, 'c': 1 }; + * _.repeat('*', 3); + * // => '***' * - * _.invert(object); - * // => { '1': 'c', '2': 'b' } + * _.repeat('abc', 2); + * // => 'abcabc' * - * // with `multiValue` - * _.invert(object, true); - * // => { '1': ['a', 'c'], '2': ['b'] } + * _.repeat('abc', 0); + * // => '' */ - function invert(object, multiValue, guard) { - if (guard && isIterateeCall(object, multiValue, guard)) { - multiValue = undefined; + function repeat(string, n) { + var result = ''; + string = baseToString(string); + n = +n; + if (n < 1 || !string || !nativeIsFinite(n)) { + return result; } - var index = -1, - props = keys(object), - length = props.length, - result = {}; - - while (++index < length) { - var key = props[index], - value = object[key]; - - if (multiValue) { - if (hasOwnProperty.call(result, value)) { - result[value].push(key); - } else { - result[value] = [key]; - } - } else { - result[value] = key; + // Leverage the exponentiation by squaring algorithm for a faster repeat. + // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details. + do { + if (n % 2) { + result += string; } - } + n = nativeFloor(n / 2); + string += string; + } while (n); + return result; } /** - * Creates an array of the own enumerable property names of `object`. - * - * **Note:** Non-object values are coerced to objects. See the - * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys) - * for more details. + * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case). * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to query. - * @returns {Array} Returns the array of property names. + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the snake cased string. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } - * - * Foo.prototype.c = 3; + * _.snakeCase('Foo Bar'); + * // => 'foo_bar' * - * _.keys(new Foo); - * // => ['a', 'b'] (iteration order is not guaranteed) + * _.snakeCase('fooBar'); + * // => 'foo_bar' * - * _.keys('hi'); - * // => ['0', '1'] + * _.snakeCase('--foo-bar'); + * // => 'foo_bar' */ - var keys = !nativeKeys ? shimKeys : function (object) { - var Ctor = object == null ? undefined : object.constructor; - if (typeof Ctor == 'function' && Ctor.prototype === object || typeof object != 'function' && isArrayLike(object)) { - return shimKeys(object); - } - return isObject(object) ? nativeKeys(object) : []; - }; + var snakeCase = createCompounder(function (result, word, index) { + return result + (index ? '_' : '') + word.toLowerCase(); + }); /** - * Creates an array of the own and inherited enumerable property names of `object`. - * - * **Note:** Non-object values are coerced to objects. + * Converts `string` to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage). * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to query. - * @returns {Array} Returns the array of property names. + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the start cased string. * @example * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } + * _.startCase('--foo-bar'); + * // => 'Foo Bar' * - * Foo.prototype.c = 3; + * _.startCase('fooBar'); + * // => 'Foo Bar' * - * _.keysIn(new Foo); - * // => ['a', 'b', 'c'] (iteration order is not guaranteed) + * _.startCase('__foo_bar__'); + * // => 'Foo Bar' */ - function keysIn(object) { - if (object == null) { - return []; - } - if (!isObject(object)) { - object = Object(object); - } - var length = object.length; - length = length && isLength(length) && (isArray(object) || isArguments(object)) && length || 0; - - var Ctor = object.constructor, - index = -1, - isProto = typeof Ctor == 'function' && Ctor.prototype === object, - result = Array(length), - skipIndexes = length > 0; - - while (++index < length) { - result[index] = index + ''; - } - for (var key in object) { - if (!(skipIndexes && isIndex(key, length)) && !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) { - result.push(key); - } - } - return result; - } + var startCase = createCompounder(function (result, word, index) { + return result + (index ? ' ' : '') + (word.charAt(0).toUpperCase() + word.slice(1)); + }); /** - * The opposite of `_.mapValues`; this method creates an object with the - * same values as `object` and keys generated by running each own enumerable - * property of `object` through `iteratee`. + * Checks if `string` starts with the given target string. * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function|Object|string} [iteratee=_.identity] The function invoked - * per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns the new mapped object. + * @category String + * @param {string} [string=''] The string to search. + * @param {string} [target] The string to search for. + * @param {number} [position=0] The position to search from. + * @returns {boolean} Returns `true` if `string` starts with `target`, else `false`. * @example * - * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { - * return key + value; - * }); - * // => { 'a1': 1, 'b2': 2 } + * _.startsWith('abc', 'a'); + * // => true + * + * _.startsWith('abc', 'b'); + * // => false + * + * _.startsWith('abc', 'b', 1); + * // => true */ - var mapKeys = createObjectMapper(true); + function startsWith(string, target, position) { + string = baseToString(string); + position = position == null ? 0 : nativeMin(position < 0 ? 0 : +position || 0, string.length); + + return string.lastIndexOf(target, position) == position; + } /** - * Creates an object with the same keys as `object` and values generated by - * running each own enumerable property of `object` through `iteratee`. The - * iteratee function is bound to `thisArg` and invoked with three arguments: - * (value, key, object). + * Creates a compiled template function that can interpolate data properties + * in "interpolate" delimiters, HTML-escape interpolated data properties in + * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data + * properties may be accessed as free variables in the template. If a setting + * object is provided it takes precedence over `_.templateSettings` values. * - * If a property name is provided for `iteratee` the created `_.property` - * style callback returns the property value of the given element. + * **Note:** In the development build `_.template` utilizes + * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) + * for easier debugging. * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. + * For more information on precompiling templates see + * [lodash's custom builds documentation](https://lodash.com/custom-builds). * - * If an object is provided for `iteratee` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. + * For more information on Chrome extension sandboxes see + * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval). * * @static * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function|Object|string} [iteratee=_.identity] The function invoked - * per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {Object} Returns the new mapped object. + * @category String + * @param {string} [string=''] The template string. + * @param {Object} [options] The options object. + * @param {RegExp} [options.escape] The HTML "escape" delimiter. + * @param {RegExp} [options.evaluate] The "evaluate" delimiter. + * @param {Object} [options.imports] An object to import into the template as free variables. + * @param {RegExp} [options.interpolate] The "interpolate" delimiter. + * @param {string} [options.sourceURL] The sourceURL of the template's compiled source. + * @param {string} [options.variable] The data object variable name. + * @param- {Object} [otherOptions] Enables the legacy `options` param signature. + * @returns {Function} Returns the compiled template function. * @example * - * _.mapValues({ 'a': 1, 'b': 2 }, function(n) { - * return n * 3; - * }); - * // => { 'a': 3, 'b': 6 } + * // using the "interpolate" delimiter to create a compiled template + * var compiled = _.template('hello <%= user %>!'); + * compiled({ 'user': 'fred' }); + * // => 'hello fred!' * - * var users = { - * 'fred': { 'user': 'fred', 'age': 40 }, - * 'pebbles': { 'user': 'pebbles', 'age': 1 } - * }; + * // using the HTML "escape" delimiter to escape data property values + * var compiled = _.template('<%- value %>'); + * compiled({ 'value': '