From 1b2e9728ceead929d6c8dfac3e869ac3a9fcefa8 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 11 Dec 2014 19:12:26 -0500 Subject: [PATCH 1/3] Opcode: Modify from methods to be static, and an instance be immutable. --- lib/opcode.js | 55 ++++++++++++++++++++++++-------------------------- test/opcode.js | 36 ++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/opcode.js b/lib/opcode.js index c48bbc1b8..149d4e969 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -8,43 +8,42 @@ function Opcode(num) { return new Opcode(num); } - if (typeof num === 'number') { - this.num = num; - } else if (typeof num === 'string') { - var str = num; - this.num = Opcode.map[str]; - } else if (num) { - var obj = num; - this.set(obj); + var value; + + if (_.isNumber(num)) { + value = num; + } else if (_.isString(num)) { + value = Opcode.map[num]; + } else { + throw new TypeError('Unrecognized num type: "' + typeof(num) + '" for Opcode'); } + + Object.defineProperty(this, 'num', { + configurable: false, + value: value + }); + + return this; } -Opcode.prototype.set = function(obj) { - $.checkArgument(_.isObject(obj)); - this.num = typeof obj.num !== 'undefined' ? obj.num : this.num; - return this; +Opcode.fromNumber = function(num) { + $.checkArgument(_.isNumber(num)); + return new Opcode(num); }; -Opcode.prototype.fromNumber = function(num) { - $.checkArgument(_.isNumber(num)); - this.num = num; - return this; +Opcode.fromString = function(str) { + $.checkArgument(_.isString(str)); + var value = Opcode.map[str]; + if (typeof value === 'undefined') { + throw new TypeError('Invalid opcodestr'); + } + return new Opcode(value); }; Opcode.prototype.toNumber = function() { return this.num; }; -Opcode.prototype.fromString = function(str) { - $.checkArgument(_.isString(str)); - var num = Opcode.map[str]; - if (typeof num === 'undefined') { - throw new Error('Invalid opcodestr'); - } - this.num = num; - return this; -}; - Opcode.prototype.toString = function() { var str = Opcode.reverseMap[this.num]; if (typeof str === 'undefined') { @@ -203,9 +202,7 @@ Opcode.map = { Opcode.reverseMap = []; for (var k in Opcode.map) { - if (Opcode.map.hasOwnProperty(k)) { - Opcode.reverseMap[Opcode.map[k]] = k; - } + Opcode.reverseMap[Opcode.map[k]] = k; } // Easier access to opcodes diff --git a/test/opcode.js b/test/opcode.js index 9d06b6973..b2b8054c7 100644 --- a/test/opcode.js +++ b/test/opcode.js @@ -1,7 +1,9 @@ 'use strict'; var _ = require('lodash'); -var should = require('chai').should(); +var chai = require('chai'); +var should = chai.should(); +var expect = chai.expect; var bitcore = require('..'); var Opcode = bitcore.Opcode; @@ -26,46 +28,52 @@ describe('Opcode', function() { describe('#fromNumber', function() { it('should work for 0', function() { - Opcode().fromNumber(0).num.should.equal(0); + Opcode.fromNumber(0).num.should.equal(0); }); it('should fail for non-number', function() { - Opcode().fromNumber.bind(null, 'a string').should.throw('Invalid Argument'); + Opcode.fromNumber.bind(null, 'a string').should.throw('Invalid Argument'); }); }); describe('#set', function() { it('should work for object', function() { - Opcode().set({ - num: 42 - }).num.should.equal(42); + Opcode(42).num.should.equal(42); }); - it('should fail for non-object', function() { - Opcode().set.bind(null, 'non-object').should.throw('Invalid Argument'); + it('should fail for empty-object', function() { + expect(function() { + Opcode(); + }).to.throw(TypeError); }); }); describe('#toNumber', function() { it('should work for 0', function() { - Opcode().fromNumber(0).toNumber().should.equal(0); + Opcode.fromNumber(0).toNumber().should.equal(0); }); }); describe('#fromString', function() { it('should work for OP_0', function() { - Opcode().fromString('OP_0').num.should.equal(0); + Opcode.fromString('OP_0').num.should.equal(0); }); it('should fail for invalid string', function() { - Opcode().fromString.bind(null, 'OP_SATOSHI').should.throw('Invalid opcodestr'); - Opcode().fromString.bind(null, 'BANANA').should.throw('Invalid opcodestr'); + Opcode.fromString.bind(null, 'OP_SATOSHI').should.throw('Invalid opcodestr'); + Opcode.fromString.bind(null, 'BANANA').should.throw('Invalid opcodestr'); }); it('should fail for non-string', function() { - Opcode().fromString.bind(null, 123).should.throw('Invalid Argument'); + Opcode.fromString.bind(null, 123).should.throw('Invalid Argument'); }); }); describe('#toString', function() { it('should work for OP_0', function() { - Opcode().fromString('OP_0').toString().should.equal('OP_0'); + Opcode.fromString('OP_0').toString().should.equal('OP_0'); + }); + + it('should not work for non-opcode', function() { + expect(function(){ + Opcode('OP_NOTACODE').toString(); + }).to.throw('Opcode does not have a string representation'); }); }); From 6bb9990a2bbdef0b6a423e25bf3bf1633635f6aa Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 11 Dec 2014 20:37:54 -0500 Subject: [PATCH 2/3] Opcode: JS-Beautify --- lib/opcode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/opcode.js b/lib/opcode.js index 149d4e969..48f0f72cb 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -219,4 +219,4 @@ Opcode.isSmallIntOp = function(opcode) { ((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16))); }; -module.exports = Opcode; +module.exports = Opcode; \ No newline at end of file From 77be24a52f5cc22677f01d48d801d88556f1227b Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 11 Dec 2014 22:27:37 -0500 Subject: [PATCH 3/3] Bump --- lib/opcode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/opcode.js b/lib/opcode.js index 48f0f72cb..149d4e969 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -219,4 +219,4 @@ Opcode.isSmallIntOp = function(opcode) { ((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16))); }; -module.exports = Opcode; \ No newline at end of file +module.exports = Opcode;