From b4ce46a85b5e99556a01bbf1127ff7b7678e8be0 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Tue, 6 Nov 2018 23:33:59 +0100 Subject: [PATCH 1/2] rewrote tests to typescript, improved typings --- package.json | 8 +++++--- src/index.ts | 5 ++++- src/types.ts | 2 +- test/{index.js => index.ts} | 30 ++++++++++++++++-------------- 4 files changed, 26 insertions(+), 19 deletions(-) rename test/{index.js => index.ts} (94%) diff --git a/package.json b/package.json index 8660164..9c2b98b 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,10 @@ }, "scripts": { "build": "tsc", - "coverage": "istanbul cover _mocha", + "coverage": "istanbul cover _mocha -- --reporter spec --require ts-node/register 'test/index.ts'", "coveralls": "npm run coverage && coveralls input).length === 0) { return Buffer.from([]); @@ -259,5 +261,6 @@ function isNumber(input: RLPInput): input is number { // Check if an input is a BigNumber function isBN(input: RLPInput): input is BN { - return !!input.hasOwnProperty('toArray'); + if (!input) return false; + return !!input.hasOwnProperty('toArray'); } \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index b4ad4bb..91554fe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,6 @@ import BN from 'bn.js'; -export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray; +export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray | null; export interface RLPArray extends Array {}; interface RLPObject { diff --git a/test/index.js b/test/index.ts similarity index 94% rename from test/index.js rename to test/index.ts index 3b12bd5..5d1cf6d 100644 --- a/test/index.js +++ b/test/index.ts @@ -1,7 +1,7 @@ -const assert = require('assert') -const RLP = require('../dist/index.js') -const BN = require('bn.js') -const testing = require('ethereumjs-testing') +import * as assert from "assert"; +import * as RLP from "../src"; +const BN = require('bn.js'); +const testing = require('ethereumjs-testing'); describe('invalid rlps', function () { it('should not crash on an invalid rlp', function () { @@ -113,11 +113,12 @@ describe('RLP decoding (int):', function () { }) describe('strings over 55 bytes long', function () { - var testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion' - testString = Buffer.from(testString) - var encoded = null + const testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion' + const testBuffer = Buffer.from(testString) + let encoded:Buffer + it('should encode it', function () { - encoded = RLP.encode(testString) + encoded = RLP.encode(testBuffer) assert.equal(encoded[0], 184) assert.equal(encoded[1], 86) }) @@ -129,17 +130,18 @@ describe('strings over 55 bytes long', function () { }) describe('list over 55 bytes long', function () { - var testString = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9'] - var encoded = null + const testString = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9'] + let encoded: Buffer it('should encode it', function () { encoded = RLP.encode(testString) }) it('should decode', function () { - var decoded = RLP.decode(encoded) - for (var i = 0; i < decoded.length; i++) { - decoded[i] = decoded[i].toString() + const decodedBuffer = RLP.decode(encoded) + const decoded: string[] = [] + for (var i = 0; i < decodedBuffer.length; i++) { + decoded[i] = decodedBuffer[i].toString() } assert.deepEqual(decoded, testString) }) @@ -169,7 +171,7 @@ describe('nested lists:', function () { ] ] ] - var encoded + var encoded: Buffer it('encode a nested list', function () { encoded = RLP.encode(nestedList) assert.deepEqual(encoded, Buffer.from([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0])) From 664057b3f1cbd0af3e60033cb0a01185854e5dac Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Nov 2018 23:39:48 +0100 Subject: [PATCH 2/2] migrate to NYC --- .gitignore | 1 + .nycrc | 13 +++++++++++++ package.json | 8 ++++---- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 .nycrc diff --git a/.gitignore b/.gitignore index 95e028f..a2a7c7d 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ package-lock.json dist +.nyc_output diff --git a/.nycrc b/.nycrc new file mode 100644 index 0000000..fbcdf94 --- /dev/null +++ b/.nycrc @@ -0,0 +1,13 @@ +{ + "reporter": [ + "lcov", + "text" + ], + "include": [ + "src/**/*.ts" + ], + "extension": [ + ".ts" + ], + "all": true +} diff --git a/package.json b/package.json index 9c2b98b..caa4f90 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,10 @@ }, "scripts": { "build": "tsc", - "coverage": "istanbul cover _mocha -- --reporter spec --require ts-node/register 'test/index.ts'", - "coveralls": "npm run coverage && coveralls