Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

remove lodash dependency #275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18,720 changes: 935 additions & 17,785 deletions bitcore-lib.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ bitcore.deps.bnjs = require('bn.js');
bitcore.deps.bs58 = require('bs58');
bitcore.deps.Buffer = Buffer;
bitcore.deps.elliptic = require('elliptic');
bitcore.deps._ = require('lodash');

// Internal usage, exposed for testing/advanced tweaking
bitcore.Transaction.sighash = require('./lib/transaction/sighash');
5 changes: 2 additions & 3 deletions lib/address.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var $ = require('./util/preconditions');
var errors = require('./errors');
var Base58Check = require('./encoding/base58check');
Expand Down Expand Up @@ -51,7 +50,7 @@ function Address(data, network, type) {
return new Address(data, network, type);
}

if (_.isArray(data) && _.isNumber(network)) {
if (Array.isArray(data) && typeof network === 'number') {
return Address.createMultisig(data, network, type);
}

Expand Down Expand Up @@ -105,7 +104,7 @@ Address.prototype._classifyArguments = function(data, network, type) {
return Address._transformScript(data, network);
} else if (typeof(data) === 'string') {
return Address._transformString(data, network, type);
} else if (_.isObject(data)) {
} else if (JSUtil.isObject(data)) {
return Address._transformObject(data);
} else {
throw new TypeError('First argument is an unrecognized data format.');
Expand Down
8 changes: 4 additions & 4 deletions lib/block/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var BlockHeader = require('./blockheader');
var BN = require('../crypto/bn');
var BufferUtil = require('../util/buffer');
Expand All @@ -9,6 +8,7 @@ var BufferWriter = require('../encoding/bufferwriter');
var Hash = require('../crypto/hash');
var Transaction = require('../transaction');
var $ = require('../util/preconditions');
var JSUtil = require('../util/js');

/**
* Instantiate a Block from a Buffer, JSON object, or Object with
Expand All @@ -22,7 +22,7 @@ function Block(arg) {
if (!(this instanceof Block)) {
return new Block(arg);
}
_.extend(this, Block._from(arg));
Object.assign(this, Block._from(arg));
return this;
}

Expand All @@ -39,7 +39,7 @@ Block._from = function _from(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = Block._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (JSUtil.isObject(arg)) {
info = Block._fromObject(arg);
} else {
throw new TypeError('Unrecognized argument for Block');
Expand Down Expand Up @@ -261,7 +261,7 @@ var idProperty = {
}
return this._id;
},
set: _.noop
set: function() {}
};
Object.defineProperty(Block.prototype, 'id', idProperty);
Object.defineProperty(Block.prototype, 'hash', idProperty);
Expand Down
9 changes: 4 additions & 5 deletions lib/block/blockheader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var BN = require('../crypto/bn');
var BufferUtil = require('../util/buffer');
var BufferReader = require('../encoding/bufferreader');
Expand Down Expand Up @@ -52,7 +51,7 @@ BlockHeader._from = function _from(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = BlockHeader._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (JSUtil.isObject(arg)) {
info = BlockHeader._fromObject(arg);
} else {
throw new TypeError('Unrecognized argument for BlockHeader');
Expand All @@ -69,10 +68,10 @@ BlockHeader._fromObject = function _fromObject(data) {
$.checkArgument(data, 'data is required');
var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot;
if (_.isString(data.prevHash)) {
if (typeof data.prevHash === 'string') {
prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex'));
}
if (_.isString(data.merkleRoot)) {
if (typeof data.merkleRoot === 'string') {
merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex'));
}
var info = {
Expand Down Expand Up @@ -251,7 +250,7 @@ var idProperty = {
}
return this._id;
},
set: _.noop
set: function() {}
};
Object.defineProperty(BlockHeader.prototype, 'id', idProperty);
Object.defineProperty(BlockHeader.prototype, 'hash', idProperty);
Expand Down
15 changes: 7 additions & 8 deletions lib/block/merkleblock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var BlockHeader = require('./blockheader');
var BufferUtil = require('../util/buffer');
var BufferReader = require('../encoding/bufferreader');
Expand Down Expand Up @@ -29,7 +28,7 @@ function MerkleBlock(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = MerkleBlock._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (JSUtil.isObject(arg)) {
var header;
if(arg.header instanceof BlockHeader) {
header = arg.header;
Expand Down Expand Up @@ -61,7 +60,7 @@ function MerkleBlock(arg) {
} else {
throw new TypeError('Unrecognized argument for MerkleBlock');
}
_.extend(this,info);
Object.assign(this, info);
this._flagBitsUsed = 0;
this._hashesUsed = 0;

Expand Down Expand Up @@ -129,8 +128,8 @@ MerkleBlock.prototype.toObject = MerkleBlock.prototype.toJSON = function toObjec
* @returns {Boolean} - True/False whether this MerkleBlock is Valid
*/
MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
$.checkState(Array.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(Array.isArray(this.hashes), 'MerkleBlock hashes is not an array');

// Can't have more hashes than numTransactions
if(this.hashes.length > this.numTransactions) {
Expand All @@ -156,8 +155,8 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
* @returns {Array} - txs hash that match the filter
*/
MerkleBlock.prototype.filterdTxsHash = function filterdTxsHash() {
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
$.checkState(Array.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(Array.isArray(this.hashes), 'MerkleBlock hashes is not an array');

// Can't have more hashes than numTransactions
if(this.hashes.length > this.numTransactions) {
Expand Down Expand Up @@ -263,7 +262,7 @@ MerkleBlock.prototype._calcTreeHeight = function calcTreeHeight() {
* @private
*/
MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
$.checkArgument(!_.isUndefined(tx), 'tx cannot be undefined');
$.checkArgument(typeof tx !== 'undefined', 'tx cannot be undefined');
$.checkArgument(tx instanceof Transaction || typeof tx === 'string',
'Invalid tx given, tx must be a "string" or "Transaction"');

Expand Down
5 changes: 2 additions & 3 deletions lib/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var BN = require('bn.js');
var $ = require('../util/preconditions');
var _ = require('lodash');

var reversebuf = function(buf) {
var buf2 = Buffer.alloc(buf.length);
Expand All @@ -17,12 +16,12 @@ BN.One = new BN(1);
BN.Minus1 = new BN(-1);

BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n));
$.checkArgument(typeof n === 'number');
return new BN(n);
};

BN.fromString = function(str, base) {
$.checkArgument(_.isString(str));
$.checkArgument(typeof str === 'string');
return new BN(str, base);
};

Expand Down
3 changes: 1 addition & 2 deletions lib/crypto/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var PublicKey = require('../publickey');
var Random = require('./random');
var Hash = require('./hash');
var BufferUtil = require('../util/buffer');
var _ = require('lodash');
var $ = require('../util/preconditions');

var ECDSA = function ECDSA(obj) {
Expand Down Expand Up @@ -78,7 +77,7 @@ ECDSA.prototype.deterministicK = function(badrs) {
// if r or s were invalid when this function was used in signing,
// we do not want to actually compute r, s here for efficiency, so,
// we can increment badrs. explained at end of RFC 6979 section 3.2
if (_.isUndefined(badrs)) {
if (typeof badrs === 'undefined') {
badrs = 0;
}
var v = Buffer.alloc(32);
Expand Down
3 changes: 1 addition & 2 deletions lib/crypto/signature.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var BN = require('./bn');
var _ = require('lodash');
var $ = require('../util/preconditions');
var BufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
Expand Down Expand Up @@ -90,7 +89,7 @@ Signature.fromString = function(str) {
*/
Signature.parseDER = function(buf, strict) {
$.checkArgument(BufferUtil.isBuffer(buf), new Error('DER formatted signature should be a buffer'));
if (_.isUndefined(strict)) {
if (typeof strict === 'undefined') {
strict = true;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/encoding/base58.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var bs58 = require('bs58');
var buffer = require('buffer');

Expand All @@ -26,7 +25,9 @@ Base58.validCharacters = function validCharacters(chars) {
if (buffer.Buffer.isBuffer(chars)) {
chars = chars.toString();
}
return _.every(_.map(chars, function(char) { return _.includes(ALPHABET, char); }));
return chars.split('').every(function(char){
return ALPHABET.includes(char);
});
};

Base58.prototype.set = function(obj) {
Expand Down
5 changes: 2 additions & 3 deletions lib/encoding/base58check.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var Base58 = require('./base58');
var buffer = require('buffer');
var sha256sha256 = require('../crypto/hash').sha256sha256;
Expand All @@ -25,10 +24,10 @@ Base58Check.prototype.set = function(obj) {
};

Base58Check.validChecksum = function validChecksum(data, checksum) {
if (_.isString(data)) {
if (typeof data === 'string') {
data = new buffer.Buffer(Base58.decode(data));
}
if (_.isString(checksum)) {
if (typeof checksum === 'string') {
checksum = new buffer.Buffer(Base58.decode(checksum));
}
if (!checksum) {
Expand Down
12 changes: 6 additions & 6 deletions lib/encoding/bufferreader.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
'use strict';

var _ = require('lodash');
var $ = require('../util/preconditions');
var BufferUtil = require('../util/buffer');
var BN = require('../crypto/bn');
var JSUtil = require('../util/js');

var BufferReader = function BufferReader(buf) {
if (!(this instanceof BufferReader)) {
return new BufferReader(buf);
}
if (_.isUndefined(buf)) {
if (typeof buf === 'undefined') {
return;
}
if (Buffer.isBuffer(buf)) {
this.set({
buf: buf
});
} else if (_.isString(buf)) {
} else if (typeof buf === 'string') {
this.set({
buf: Buffer.from(buf, 'hex'),
});
} else if (_.isObject(buf)) {
} else if (JSUtil.isObject(buf)) {
var obj = buf;
this.set(obj);
} else {
Expand All @@ -41,7 +41,7 @@ BufferReader.prototype.eof = function() {
BufferReader.prototype.finished = BufferReader.prototype.eof;

BufferReader.prototype.read = function(len) {
$.checkArgument(!_.isUndefined(len), 'Must specify a length');
$.checkArgument(typeof len !== 'undefined', 'Must specify a length');
var buf = this.buf.slice(this.pos, this.pos + len);
this.pos = this.pos + len;
return buf;
Expand Down Expand Up @@ -187,7 +187,7 @@ BufferReader.prototype.reverse = function() {
};

BufferReader.prototype.readReverse = function(len) {
if (_.isUndefined(len)) {
if (typeof len === 'undefined') {
len = this.buf.length;
}
var buf = this.buf.slice(this.pos, this.pos + len);
Expand Down
8 changes: 3 additions & 5 deletions lib/errors/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var _ = require('lodash');

function format(message, args) {
return message
.replace('{0}', args[0])
Expand All @@ -10,9 +8,9 @@ function format(message, args) {
}
var traverseNode = function(parent, errorDefinition) {
var NodeError = function() {
if (_.isString(errorDefinition.message)) {
if (typeof errorDefinition.message === 'string') {
this.message = format(errorDefinition.message, arguments);
} else if (_.isFunction(errorDefinition.message)) {
} else if (typeof errorDefinition.message === 'function') {
this.message = errorDefinition.message.apply(null, arguments);
} else {
throw new Error('Invalid error definition for ' + errorDefinition.name);
Expand All @@ -30,7 +28,7 @@ var traverseNode = function(parent, errorDefinition) {

/* jshint latedef: false */
var childDefinitions = function(parent, childDefinitions) {
_.each(childDefinitions, function(childDefinition) {
childDefinitions.forEach(function (childDefinition) {
traverseNode(parent, childDefinition);
});
};
Expand Down
Loading