From 55fca2d7d54295bcbf5b015488342463971b482f Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Wed, 20 May 2015 18:47:05 -0700 Subject: [PATCH] Lint --- src/binformat.js | 14 ++-- src/serializedobject.js | 96 ++++++++++++----------- test/serializedobject-test.js | 144 +++++++++++++++------------------- 3 files changed, 121 insertions(+), 133 deletions(-) diff --git a/src/binformat.js b/src/binformat.js index c93ad184..257ddd1c 100644 --- a/src/binformat.js +++ b/src/binformat.js @@ -48,7 +48,7 @@ exports.types = [ * Mapping of field type id to field type name. */ -var FIELDS_MAP = exports.fields = { +const FIELDS_MAP = exports.fields = { // Common types 1: { // Int16 1: 'LedgerEntryType', @@ -202,7 +202,7 @@ var FIELDS_MAP = exports.fields = { } }; -var INVERSE_FIELDS_MAP = exports.fieldsInverseMap = { }; +let INVERSE_FIELDS_MAP = exports.fieldsInverseMap = { }; Object.keys(FIELDS_MAP).forEach(function(k1) { Object.keys(FIELDS_MAP[k1]).forEach(function(k2) { @@ -210,11 +210,11 @@ Object.keys(FIELDS_MAP).forEach(function(k1) { }); }); -var REQUIRED = exports.REQUIRED = 0, - OPTIONAL = exports.OPTIONAL = 1, - DEFAULT = exports.DEFAULT = 2; +const REQUIRED = exports.REQUIRED = 0, + OPTIONAL = exports.OPTIONAL = 1, + DEFAULT = exports.DEFAULT = 2; -var base = [ +const base = [ [ 'TransactionType' , REQUIRED ], [ 'Flags' , OPTIONAL ], [ 'SourceTag' , OPTIONAL ], @@ -299,7 +299,7 @@ exports.tx = { ]) }; -var sleBase = [ +const sleBase = [ ['LedgerIndex', OPTIONAL], ['LedgerEntryType', REQUIRED], ['Flags', REQUIRED] diff --git a/src/serializedobject.js b/src/serializedobject.js index e97248c6..56d12a2c 100644 --- a/src/serializedobject.js +++ b/src/serializedobject.js @@ -1,37 +1,37 @@ 'use strict'; -var _ = require('lodash'); -var assert = require('assert'); -var extend = require('extend'); -var binformat = require('./binformat'); -var stypes = require('./serializedtypes'); -var utils = require('./utils'); -var UInt256 = require('./uint256').UInt256; +const _ = require('lodash'); +const assert = require('assert'); +const extend = require('extend'); +const binformat = require('./binformat'); +const stypes = require('./serializedtypes'); +const utils = require('./utils'); +const UInt256 = require('./uint256').UInt256; -var sjcl = utils.sjcl; +const sjcl = utils.sjcl; -var TRANSACTION_TYPES = { }; +const TRANSACTION_TYPES = { }; Object.keys(binformat.tx).forEach(function(key) { TRANSACTION_TYPES[binformat.tx[key][0]] = key; }); -var LEDGER_ENTRY_TYPES = {}; +const LEDGER_ENTRY_TYPES = {}; Object.keys(binformat.ledger).forEach(function(key) { LEDGER_ENTRY_TYPES[binformat.ledger[key][0]] = key; }); -var TRANSACTION_RESULTS = {}; +const TRANSACTION_RESULTS = {}; Object.keys(binformat.ter).forEach(function(key) { TRANSACTION_RESULTS[binformat.ter[key]] = key; }); function normalize_sjcl_bn_hex(string) { - var hex = string.slice(2); // remove '0x' prefix + const hex = string.slice(2); // remove '0x' prefix // now strip leading zeros - var i = _.findIndex(hex, function(c) { + const i = _.findIndex(hex, function(c) { return c !== '0'; }); return i >= 0 ? hex.slice(i) : '0'; @@ -50,11 +50,12 @@ function SerializedObject(buf) { this.pointer = 0; } -SerializedObject.from_json = function(obj) { +SerializedObject.from_json = function(obj_) { // Create a copy of the object so we don't modify it - obj = extend(true, {}, obj); - var so = new SerializedObject(); - var typedef; + const obj = extend(true, {}, obj_); + + let so = new SerializedObject(); + let typedef; if (typeof obj.TransactionType === 'number') { obj.TransactionType = SerializedObject.lookup_type_tx(obj.TransactionType); @@ -104,14 +105,14 @@ SerializedObject.from_json = function(obj) { }; SerializedObject.check_fields = function(typedef, obj) { - let missingFields = []; - let unknownFields = []; - let fieldsMap = {}; + const missingFields = []; + const unknownFields = []; + const fieldsMap = {}; // Get missing required fields typedef.forEach(function(field) { - var fieldName = field[0]; - var isRequired = field[1] === binformat.REQUIRED; + const fieldName = field[0]; + const isRequired = field[1] === binformat.REQUIRED; if (isRequired && obj[fieldName] === undefined) { missingFields.push(fieldName); @@ -132,7 +133,7 @@ SerializedObject.check_fields = function(typedef, obj) { return; } - var errorMessage; + let errorMessage; if (obj.TransactionType !== undefined) { errorMessage = SerializedObject.lookup_type_tx(obj.TransactionType); @@ -146,17 +147,15 @@ SerializedObject.check_fields = function(typedef, obj) { errorMessage += ' is missing fields: ' + JSON.stringify(missingFields); } if (unknownFields.length > 0) { - errorMessage += (missingFields.length ? ' and' : '') + errorMessage += (missingFields.length ? ' and' : '') + ' has unknown fields: ' + JSON.stringify(unknownFields); } throw new Error(errorMessage); }; -SerializedObject.prototype.append = function(bytes) { - if (bytes instanceof SerializedObject) { - bytes = bytes.buffer; - } +SerializedObject.prototype.append = function(bytes_) { + const bytes = bytes_ instanceof SerializedObject ? bytes_.buffer : bytes_; // Make sure both buffer and bytes are Array. Either could potentially be a // Buffer. @@ -178,14 +177,14 @@ SerializedObject.prototype.resetPointer = function() { function readOrPeek(advance) { return function(bytes) { - var start = this.pointer; - var end = start + bytes; + const start = this.pointer; + const end = start + bytes; if (end > this.buffer.length) { throw new Error('Buffer length exceeded'); } - var result = this.buffer.slice(start, end); + const result = this.buffer.slice(start, end); if (advance) { this.pointer = end; @@ -208,14 +207,16 @@ SerializedObject.prototype.to_hex = function() { }; SerializedObject.prototype.to_json = function() { - var old_pointer = this.pointer; + const old_pointer = this.pointer; + this.resetPointer(); - var output = { }; + + let output = { }; while (this.pointer < this.buffer.length) { - var key_and_value = stypes.parse(this); - var key = key_and_value[0]; - var value = key_and_value[1]; + const key_and_value = stypes.parse(this); + const key = key_and_value[0]; + const value = key_and_value[1]; output[key] = SerializedObject.jsonify_structure(value, key); } @@ -225,7 +226,7 @@ SerializedObject.prototype.to_json = function() { }; SerializedObject.jsonify_structure = function(structure, field_name) { - var output; + let output; switch (typeof structure) { case 'number': @@ -259,10 +260,10 @@ SerializedObject.jsonify_structure = function(structure, field_name) { // new Array or Object output = new structure.constructor(); - var keys = Object.keys(structure); + let keys = Object.keys(structure); - for (var i = 0, l = keys.length; i < l; i++) { - var key = keys[i]; + for (let i = 0, l = keys.length; i < l; i++) { + let key = keys[i]; output[key] = SerializedObject.jsonify_structure(structure[key], key); } } @@ -284,14 +285,14 @@ SerializedObject.prototype.serialize = function(typedef, obj) { typedef = SerializedObject.sort_typedef(typedef); // Serialize fields - for (var i=0, l=typedef.length; i 0xF) { buffer.push(type_id & 0xFF); diff --git a/test/serializedobject-test.js b/test/serializedobject-test.js index b5bd84dd..1b45eb22 100644 --- a/test/serializedobject-test.js +++ b/test/serializedobject-test.js @@ -1,26 +1,16 @@ 'use strict'; /* eslint-disable max-len*/ -/* eslint-disable key-spacing*/ -/* eslint-disable no-spaced-func*/ -/* eslint-disable space-before-blocks*/ -/* eslint-disable space-unary-ops*/ -/* eslint-disable no-multi-spaces*/ -/* eslint-disable no-void*/ -/* eslint-disable semi*/ -/* eslint-disable no-unused-vars*/ -/* eslint-disable quotes*/ -var assert = require('assert'); -var lodash = require('lodash'); -var SerializedObject = require('ripple-lib').SerializedObject; -var Amount = require('ripple-lib').Amount; -var sjcl = require('ripple-lib').sjcl; +const assert = require('assert'); +const lodash = require('lodash'); +const SerializedObject = require('ripple-lib').SerializedObject; +const Amount = require('ripple-lib').Amount; +const sjcl = require('ripple-lib').sjcl; // Shortcuts -var hex = sjcl.codec.hex; -var bytes = sjcl.codec.bytes; -var utf8 = sjcl.codec.utf8String; +const hex = sjcl.codec.hex; +const utf8 = sjcl.codec.utf8String; describe('Serialized object', function() { @@ -28,13 +18,9 @@ describe('Serialized object', function() { return hex.fromBits(utf8.toBits(string)).toUpperCase(); } - function convertHexToString(hexString) { - return utf8.fromBits(hex.toBits(hexString)); - } - - describe('#from_json(v).to_json() == v', function(){ + describe('#from_json(v).to_json() == v', function() { it('outputs same as passed to from_json', function() { - var input_json = { + let input_json = { Account: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', Amount: '274579388', Destination: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', @@ -42,36 +28,36 @@ describe('Serialized object', function() { Flags: 0, Paths: [[ { - account: 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV', - currency: 'USD', - issuer: 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV', + account: 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV', + currency: 'USD', + issuer: 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV', type: 49, - type_hex: "0000000000000031" + type_hex: '0000000000000031' }, { currency: 'XRP', type: 16, - type_hex: "0000000000000010" + type_hex: '0000000000000010' } ]], SendMax: { - currency: 'USD', - issuer: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', - value: '2.74579388' + currency: 'USD', + issuer: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', + value: '2.74579388' }, Sequence: 351, SigningPubKey: '02854B06CE8F3E65323F89260E9E19B33DA3E01B30EA4CA172612DE77973FAC58A', TransactionType: 'Payment', TxnSignature: '30450221009DA3A42DD25E3B22EC45AD8BA8FC7A954264264A816D300B2DF69F814D7D4DD2022072C9627F97EEC6DA13DE841E06E2CD985EF06A0FBB15DDBF0800D0730C8986BF' }; - var output_json = SerializedObject.from_json(input_json).to_json(); + let output_json = SerializedObject.from_json(input_json).to_json(); assert.deepEqual(input_json, output_json); }); }); - describe('#from_json(v).to_json() == v -- invalid amount', function(){ + describe('#from_json(v).to_json() == v -- invalid amount', function() { it('outputs same as passed to from_json', function() { - var input_json = { + let input_json = { Account: 'rUR9gTCcrUY9fMkz9rwcM9urPREh3LKXoW', Fee: '10', Flags: 0, @@ -93,9 +79,9 @@ describe('Serialized object', function() { }); }); - describe('#from_json(v).to_json() == v -- invalid amount, strict_mode = false', function(){ + describe('#from_json(v).to_json() == v -- invalid amount, strict_mode = false', function() { it('outputs same as passed to from_json', function() { - var input_json = { + let input_json = { Account: 'rUR9gTCcrUY9fMkz9rwcM9urPREh3LKXoW', Fee: '10', Flags: 0, @@ -111,9 +97,9 @@ describe('Serialized object', function() { TxnSignature: 'FFFFFF210085C6AE945643150E6D450CF796E45D74FB24B4E03E964A29CC6AFFEB346C77C80221009BE1B6678CF6C2E61F8F2696144C75AFAF66DF4FC0733DF9118EDEFEEFE33243' }; - var strictMode = Amount.strict_mode; + let strictMode = Amount.strict_mode; Amount.strict_mode = false; - var output_json = SerializedObject.from_json(input_json).to_json(); + let output_json = SerializedObject.from_json(input_json).to_json(); assert.deepEqual(input_json, output_json); Amount.strict_mode = strictMode; }); @@ -121,7 +107,7 @@ describe('Serialized object', function() { describe('#from_json', function() { it('understands TransactionType as a Number', function() { - var input_json = { + let input_json = { // no non required fields Account: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', Amount: '274579388', @@ -131,28 +117,28 @@ describe('Serialized object', function() { SigningPubKey: '02',// VL field ;) TransactionType: 0 // }; - var output_json = SerializedObject.from_json(input_json).to_json(); + let output_json = SerializedObject.from_json(input_json).to_json(); assert.equal(0, input_json.TransactionType); - assert.equal("Payment", output_json.TransactionType); + assert.equal('Payment', output_json.TransactionType); }); it('understands LedgerEntryType as a Number', function() { - var input_json = { + let input_json = { // no, non required fields - "LedgerEntryType": 100, - "Flags": 0, - "Indexes": [], - "RootIndex": "000360186E008422E06B72D5B275E29EE3BE9D87A370F424E0E7BF613C465909" - } + LedgerEntryType: 100, + Flags: 0, + Indexes: [], + RootIndex: '000360186E008422E06B72D5B275E29EE3BE9D87A370F424E0E7BF613C465909' + }; - var output_json = SerializedObject.from_json(input_json).to_json(); + let output_json = SerializedObject.from_json(input_json).to_json(); assert.equal(100, input_json.LedgerEntryType); - assert.equal("DirectoryNode", output_json.LedgerEntryType); + assert.equal('DirectoryNode', output_json.LedgerEntryType); }); it('checks for missing required fields', function() { - var input_json = { + let input_json = { TransactionType: 'Payment', // no non required fields Account: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', @@ -164,7 +150,7 @@ describe('Serialized object', function() { }; Object.keys(input_json).slice(1).forEach(function(k) { - var bad_json = lodash.merge({}, input_json); + let bad_json = lodash.merge({}, input_json); delete bad_json[k]; assert.strictEqual(bad_json[k], undefined); @@ -175,7 +161,7 @@ describe('Serialized object', function() { }); }); it('checks for unknown fields', function() { - var input_json = { + let input_json = { TransactionType: 'Payment', // no non required fields Account: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', @@ -187,7 +173,7 @@ describe('Serialized object', function() { }; Object.keys(input_json).slice(1).forEach(function(k) { - var bad_json = lodash.merge({}, input_json); + let bad_json = lodash.merge({}, input_json); bad_json[k + 'z'] = bad_json[k]; assert.throws(function() { @@ -199,9 +185,9 @@ describe('Serialized object', function() { describe('Format validation', function() { // Peercover actually had a problem submitting transactions without a `Fee` - // and rippled was only informing of "transaction is invalid" + // and rippled was only informing of 'transaction is invalid' it('should throw an Error when there is a missing field', function() { - var input_json = { + let input_json = { Account: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', Amount: '274579388', Destination: 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', @@ -210,7 +196,7 @@ describe('Serialized object', function() { TransactionType: 'Payment', TxnSignature: '30450221009DA3A42DD25E3B22EC45AD8BA8FC7A954264264A816D300B2DF69F814D7D4DD2022072C9627F97EEC6DA13DE841E06E2CD985EF06A0FBB15DDBF0800D0730C8986BF' }; - assert.throws ( + assert.throws( function() { SerializedObject.from_json(input_json); }, @@ -220,20 +206,20 @@ describe('Serialized object', function() { }); describe('Memos', function() { - var input_json; + let input_json; - beforeEach(function() { + beforeEach(function() { input_json = { - "Flags": 2147483648, - "TransactionType": "Payment", - "Account": "rhXzSyt1q9J8uiFXpK3qSugAAPJKXLtnrF", - "Amount": "1", - "Destination": "radqi6ppXFxVhJdjzaATRBxdrPcVTf1Ung", - "Sequence": 281, - "SigningPubKey": "03D642E6457B8AB4D140E2C66EB4C484FAFB1BF267CB578EC4815FE6CD06379C51", - "Fee": "12000", - "LastLedgerSequence": 10074214, - "TxnSignature": "304402201180636F2CE215CE97A29CD302618FAE60D63EBFC8903DE17A356E857A449C430220290F4A54F9DE4AC79034C8BEA5F1F8757F7505F1A6FF04D2E19B6D62E867256B" + 'Flags': 2147483648, + 'TransactionType': 'Payment', + 'Account': 'rhXzSyt1q9J8uiFXpK3qSugAAPJKXLtnrF', + 'Amount': '1', + 'Destination': 'radqi6ppXFxVhJdjzaATRBxdrPcVTf1Ung', + 'Sequence': 281, + 'SigningPubKey': '03D642E6457B8AB4D140E2C66EB4C484FAFB1BF267CB578EC4815FE6CD06379C51', + 'Fee': '12000', + 'LastLedgerSequence': 10074214, + 'TxnSignature': '304402201180636F2CE215CE97A29CD302618FAE60D63EBFC8903DE17A356E857A449C430220290F4A54F9DE4AC79034C8BEA5F1F8757F7505F1A6FF04D2E19B6D62E867256B' }; }); @@ -248,7 +234,7 @@ describe('Serialized object', function() { } ]; - var so = SerializedObject.from_json(input_json).to_json(); + let so = SerializedObject.from_json(input_json).to_json(); input_json.Memos[0].Memo.parsed_memo_type = 'test'; input_json.Memos[0].Memo.parsed_memo_format = 'text'; input_json.Memos[0].Memo.parsed_memo_data = 'some data'; @@ -262,7 +248,7 @@ describe('Serialized object', function() { it('should serialize and parse - full memo, all strings, invalid MemoFormat', function() { input_json.Memos = [ { - "Memo": + 'Memo': { MemoType: '74657374', MemoFormat: '6170706C69636174696F6E2F6A736F6E', @@ -271,7 +257,7 @@ describe('Serialized object', function() { } ]; - var so = SerializedObject.from_json(input_json).to_json(); + let so = SerializedObject.from_json(input_json).to_json(); input_json.Memos[0].Memo.parsed_memo_type = 'test'; input_json.Memos[0].Memo.parsed_memo_format = 'application/json'; input_json.Memos[0].Memo.MemoType = convertStringToHex('test'); @@ -279,7 +265,7 @@ describe('Serialized object', function() { input_json.Memos[0].Memo.MemoData = convertStringToHex('some data'); assert.deepEqual(so, input_json); - assert.strictEqual(input_json.Memos[0].Memo.parsed_memo_data, void(0)); + assert.strictEqual(input_json.Memos[0].Memo.parsed_memo_data, undefined); }); it('should serialize and parse - full memo, json data, valid MemoFormat, ignored field', function() { @@ -288,26 +274,26 @@ describe('Serialized object', function() { Memo: { MemoType: '74657374', MemoFormat: '6A736F6E', - ignored : 'ignored', + ignored: 'ignored', MemoData: '7B22737472696E67223A22736F6D655F737472696E67222C22626F6F6C65616E223A747275657D' } } ]; - var so = SerializedObject.from_json(input_json).to_json(); + let so = SerializedObject.from_json(input_json).to_json(); delete input_json.Memos[0].Memo.ignored; input_json.Memos[0].Memo.parsed_memo_type = 'test'; input_json.Memos[0].Memo.parsed_memo_format = 'json'; input_json.Memos[0].Memo.parsed_memo_data = { - "string" : "some_string", - "boolean" : true + 'string': 'some_string', + 'boolean': true }; input_json.Memos[0].Memo.MemoType = convertStringToHex('test'); input_json.Memos[0].Memo.MemoFormat = convertStringToHex('json'); input_json.Memos[0].Memo.MemoData = convertStringToHex(JSON.stringify( { - "string" : "some_string", - "boolean" : true + 'string': 'some_string', + 'boolean': true } )); @@ -352,7 +338,7 @@ describe('Serialized object', function() { TxnSignature: '304402206B53EDFA6EFCF6FE5BA76C81BABB60A3B55E9DE8A1462DEDC5F387879575E498022015AE7B59AA49E735D7F2E252802C4406CD00689BCE5057C477FE979D38D2DAC9' }; - var serializedHex = '12000022800000002400000126201B009EC2FF614000000000000001684000000000002EE0732103D642E6457B8AB4D140E2C66EB4C484FAFB1BF267CB578EC4815FE6CD06379C517446304402206B53EDFA6EFCF6FE5BA76C81BABB60A3B55E9DE8A1462DEDC5F387879575E498022015AE7B59AA49E735D7F2E252802C4406CD00689BCE5057C477FE979D38D2DAC9811426C4CFB3BD05A9AA23936F2E81634C66A9820C9483143DD06317D19C6110CAFF150AE528F58843BE2CA1F9EA7C05696D616765E1F1'; + let serializedHex = '12000022800000002400000126201B009EC2FF614000000000000001684000000000002EE0732103D642E6457B8AB4D140E2C66EB4C484FAFB1BF267CB578EC4815FE6CD06379C517446304402206B53EDFA6EFCF6FE5BA76C81BABB60A3B55E9DE8A1462DEDC5F387879575E498022015AE7B59AA49E735D7F2E252802C4406CD00689BCE5057C477FE979D38D2DAC9811426C4CFB3BD05A9AA23936F2E81634C66A9820C9483143DD06317D19C6110CAFF150AE528F58843BE2CA1F9EA7C05696D616765E1F1'; assert.strictEqual(SerializedObject.from_json(input_json).to_hex(), serializedHex); });