ethjs-util
Advanced tools
| # Contributing to ethjs-util | ||
| Love ethjs-util and want to help? Thanks so much, there's something to do for everybody! | ||
| Please take a moment to review this document in order to make the contribution process easy and effective for everyone involved. | ||
| Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue or assessing patches and features. | ||
| ## Using the issue tracker | ||
| The [issue tracker](https://github.com/ethjs/ethjs-util/issues) is | ||
| the preferred channel for [bug reports](#bugs), [features requests](#features) | ||
| and [submitting pull requests](#pull-requests). | ||
| <a name="bugs"></a> | ||
| ## Bug reports | ||
| A bug is a _demonstrable problem_ that is caused by the code in the repository. | ||
| Good bug reports are extremely helpful - thank you! | ||
| Guidelines for bug reports: | ||
| 1. **Use the GitHub issue search** — check if the issue has already been reported. | ||
| 2. **Check if the issue has been fixed** — try to reproduce it using the latest `master` or development branch in the repository. | ||
| 3. **Isolate the problem** — | ||
| A good bug report shouldn't leave others needing to chase you up for more information. Please try to be as detailed as possible in your report. What is your environment? What steps will reproduce the issue? What browser(s) and OS | ||
| experience the problem? What would you expect to be the outcome? All these details will help people to fix any potential bugs. | ||
| Example: | ||
| > Short and descriptive example bug report title | ||
| > | ||
| > A summary of the issue and the browser/OS environment in which it occurs. If | ||
| > suitable, include the steps required to reproduce the bug. | ||
| > | ||
| > 1. This is the first step | ||
| > 2. This is the second step | ||
| > 3. Further steps, etc. | ||
| > | ||
| > `<url>` - a link to the reduced test case | ||
| > | ||
| > Any other information you want to share that is relevant to the issue being | ||
| > reported. This might include the lines of code that you have identified as | ||
| > causing the bug, and potential solutions (and your opinions on their | ||
| > merits). | ||
| <a name="features"></a> | ||
| ## Feature requests | ||
| Feature requests are welcome. But take a moment to find out whether your idea fits with the scope and aims of the project. It's up to *you* to make a strong case to convince the project's developers of the merits of this feature. Please provide as much detail and context as possible. | ||
| <a name="pull-requests"></a> | ||
| ## Pull requests | ||
| Good pull requests - patches, improvements, new features - are a fantastic | ||
| help. They should remain focused in scope and avoid containing unrelated | ||
| commits. | ||
| **Please ask first** before embarking on any significant pull request (e.g. | ||
| implementing features, refactoring code, porting to a different language), | ||
| otherwise you risk spending a lot of time working on something that the | ||
| project's developers might not want to merge into the project. | ||
| Please adhere to the coding conventions used throughout a project (indentation, | ||
| accurate comments, etc.) and any other requirements (such as test coverage). | ||
| Adhering to the following process is the best way to get your work | ||
| included in the project: | ||
| 1. [Fork](https://help.github.com/articles/fork-a-repo/) the project, clone your fork, and configure the remotes: | ||
| ```bash | ||
| # Clone your fork of the repo into the current directory | ||
| git clone https://github.com/<your-username>/default.git | ||
| # Navigate to the newly cloned directory | ||
| cd default | ||
| # Assign the original repo to a remote called "upstream" | ||
| git remote add upstream https://github.com/ethjs/ethjs-util.git | ||
| ``` | ||
| 2. If you cloned a while ago, get the latest changes from upstream: | ||
| ```bash | ||
| git checkout master | ||
| git pull upstream master | ||
| ``` | ||
| 3. Create a new topic branch (off the main project development branch) to contain your feature, change, or fix: | ||
| ```bash | ||
| git checkout -b <topic-branch-name> | ||
| ``` | ||
| 4. Commit your changes in logical chunks. Please adhere to these [git commit message guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) or your code is unlikely be merged into the main project. Use Git's [interactive rebase](https://help.github.com/articles/about-git-rebase/) feature to tidy up your commits before making them public. | ||
| 5. Locally merge (or rebase) the upstream development branch into your topic branch: | ||
| ```bash | ||
| git pull [--rebase] upstream master | ||
| ``` | ||
| 6. Push your topic branch up to your fork: | ||
| ```bash | ||
| git push origin <topic-branch-name> | ||
| ``` | ||
| 7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/) | ||
| with a clear title and description. | ||
| **DESIGN NOTE**: default follows the UNIX programming philosophy. Please consider this before contributing, keep your commits/modules concise and to the point. | ||
| Read more here: | ||
| http://www.catb.org/esr/writings/taoup/html/ch01s06.html | ||
| **IMPORTANT**: By submitting a patch, you agree to allow the project | ||
| owners to license your work under the terms of the [MIT License](LICENSE.txt). |
| # ethjs-util | ||
| Before opening a new issue, please take a moment to review our [**community guidelines**](https://github.com/ethjs/ethjs-util/blob/master/.github/CONTRIBUTING.md) to make the contribution process easy and effective for everyone involved. | ||
| **Before opening a new issue, you may find an answer in already closed issues**: | ||
| https://github.com/ethjs/ethjs-util/issues?q=is%3Aissue+is%3Aclosed | ||
| ## Issue Type | ||
| - [ ] Bug (https://github.com/ethjs/ethjs-util/blob/master/.github/CONTRIBUTING.md#bug-reports) | ||
| - [ ] Feature (https://github.com/ethjs/ethjs-util/blob/master/.github/CONTRIBUTING.md#feature-requests) | ||
| ## Description | ||
| (Add images if possible) | ||
| ## Steps to reproduce | ||
| (Add link to a demo on https://jsfiddle.net or similar if possible) | ||
| # Versions | ||
| - Node/NPM: | ||
| - Browser: |
| ## ethjs-util | ||
| Thank you for contributing! Please take a moment to review our [**contributing guidelines**](https://github.com/ethjs/ethjs-util/blob/master/.github/CONTRIBUTING.md) | ||
| to make the process easy and effective for everyone involved. | ||
| **Please open an issue** before embarking on any significant pull request, especially those that | ||
| add a new library or change existing tests, otherwise you risk spending a lot of time working | ||
| on something that might not end up being merged into the project. | ||
| Before opening a pull request, please ensure: | ||
| - [ ] You have followed our [**contributing guidelines**](https://github.com/ethjs/ethjs-util/blob/master/.github/CONTRIBUTING.md) | ||
| - [ ] Pull request has tests (we are going for 100% coverage!) | ||
| - [ ] Code is well-commented, linted and follows project conventions | ||
| - [ ] Documentation is updated (if necessary) | ||
| - [ ] Internal code generators and templates are updated (if necessary) | ||
| - [ ] Description explains the issue/use-case resolved and auto-closes related issues | ||
| Be kind to code reviewers, please try to keep pull requests as small and focused as possible :) | ||
| **IMPORTANT**: By submitting a patch, you agree to allow the project | ||
| owners to license your work under the terms of the [MIT License](https://github.com/ethjs/ethjs-util/blob/master/LICENSE.md). |
+4
-0
@@ -0,1 +1,5 @@ | ||
| # 0.0.4 -- remove unused deps | ||
| 1. Removed one unused dep | ||
| # 0.0.3 -- added `some` property to arrayContainsArray | ||
@@ -2,0 +6,0 @@ |
+12
-9
| { | ||
| "name": "ethjs-util", | ||
| "version": "0.0.3", | ||
| "version": "0.0.4", | ||
| "description": "A simple set of Ethereum JS utilties.", | ||
@@ -15,7 +15,8 @@ "main": "./src/index.js", | ||
| "lint:staged": "lint-staged", | ||
| "coveralls": "cat ./coverage/lcov/lcov.info | coveralls" | ||
| "test-travis": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- src/tests/**/*.js -R spec --timeout 2000000", | ||
| "coveralls": "npm run test-travis && cat ./coverage/lcov.info | coveralls" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+ssh://git@github.com/silentcicero/ethjs-format.git" | ||
| "url": "git+ssh://git@github.com/ethjs/ethjs-format.git" | ||
| }, | ||
@@ -40,9 +41,10 @@ "keywords": [ | ||
| "bugs": { | ||
| "url": "https://github.com/silentcicero/ethjs-util/issues" | ||
| "url": "https://github.com/ethjs/ethjs-util/issues" | ||
| }, | ||
| "homepage": "https://github.com/silentcicero/ethjs-util#readme", | ||
| "homepage": "https://github.com/ethjs/ethjs-util#readme", | ||
| "devDependencies": { | ||
| "chai": "^3.5.0", | ||
| "mocha": "^3.1.2", | ||
| "babel-eslint": "7.1.0", | ||
| "bignumber.js": "3.0.1", | ||
| "bn.js": "4.11.6", | ||
| "chai": "3.5.0", | ||
| "coveralls": "2.11.9", | ||
@@ -56,3 +58,5 @@ "eslint": "2.10.1", | ||
| "eventsource-polyfill": "0.9.6", | ||
| "istanbul": "0.4.5", | ||
| "lint-staged": "1.0.1", | ||
| "mocha": "3.1.2", | ||
| "pre-commit": "1.1.3" | ||
@@ -62,4 +66,3 @@ }, | ||
| "assert": "1.4.1", | ||
| "ethjs-filter": "0.0.1", | ||
| "utf8": "^2.1.2" | ||
| "utf8": "2.1.2" | ||
| }, | ||
@@ -66,0 +69,0 @@ "babel": { |
+21
-17
@@ -5,4 +5,4 @@ ## ethjs-util | ||
| <!-- Dependency Status --> | ||
| <a href="https://david-dm.org/SilentCicero/ethjs-util"> | ||
| <img src="https://david-dm.org/SilentCicero/ethjs-util.svg" | ||
| <a href="https://david-dm.org/ethjs/ethjs-util"> | ||
| <img src="https://david-dm.org/ethjs/ethjs-util.svg" | ||
| alt="Dependency Status" /> | ||
@@ -12,9 +12,9 @@ </a> | ||
| <!-- devDependency Status --> | ||
| <a href="https://david-dm.org/SilentCicero/ethjs-util#info=devDependencies"> | ||
| <img src="https://david-dm.org/SilentCicero/ethjs-util/dev-status.svg" alt="devDependency Status" /> | ||
| <a href="https://david-dm.org/ethjs/ethjs-util#info=devDependencies"> | ||
| <img src="https://david-dm.org/ethjs/ethjs-util/dev-status.svg" alt="devDependency Status" /> | ||
| </a> | ||
| <!-- Build Status --> | ||
| <a href="https://travis-ci.org/SilentCicero/ethjs-util"> | ||
| <img src="https://travis-ci.org/SilentCicero/ethjs-util.svg" | ||
| <a href="https://travis-ci.org/ethjs/ethjs-util"> | ||
| <img src="https://travis-ci.org/ethjs/ethjs-util.svg" | ||
| alt="Build Status" /> | ||
@@ -29,2 +29,7 @@ </a> | ||
| <!-- Test Coverage --> | ||
| <a href="https://coveralls.io/r/ethjs/ethjs-util"> | ||
| <img src="https://coveralls.io/repos/github/ethjs/ethjs-util/badge.svg" alt="Test Coverage" /> | ||
| </a> | ||
| <!-- Javascript Style --> | ||
@@ -63,11 +68,10 @@ <a href="http://airbnb.io/javascript/"> | ||
| ``` | ||
| arrayContainsArray | ||
| getBinarySize | ||
| toBuffer | ||
| intToBuffer | ||
| getBinarySize | ||
| isHexPrefixed | ||
| stripHexPrefix | ||
| padToEven | ||
| intToHex | ||
| arrayContainsArray <Function (Array, Array) : (Boolean)> | ||
| getBinarySize <Function (String) : (Number)> | ||
| toBuffer <Function (String) : (Buffer)> | ||
| intToBuffer <Function (Number) : (Buffer)> | ||
| isHexPrefixed <Function (String) : (Boolean)> | ||
| stripHexPrefix <Function (String) : (String)> | ||
| padToEven <Function (String) : (String)> | ||
| intToHex <Function (Number) : (String)> | ||
| ``` | ||
@@ -102,3 +106,3 @@ | ||
| We communicate via [issues](https://github.com/SilentCicero/ethjs-util/issues) and [pull requests](https://github.com/SilentCicero/ethjs-util/pulls). | ||
| We communicate via [issues](https://github.com/ethjs/ethjs-util/issues) and [pull requests](https://github.com/ethjs/ethjs-util/pulls). | ||
@@ -109,3 +113,3 @@ ## Important documents | ||
| - [Code of Conduct](CODE_OF_CONDUCT.md) | ||
| - [License](https://raw.githubusercontent.com/SilentCicero/ethjs-util/master/LICENSE) | ||
| - [License](https://raw.githubusercontent.com/ethjs/ethjs-util/master/LICENSE) | ||
@@ -112,0 +116,0 @@ ## Licence |
+31
-4
@@ -12,2 +12,6 @@ const assert = require('assert'); | ||
| function isHexPrefixed(str) { | ||
| if (typeof str !== 'string') { | ||
| throw new Error(`value must be string, is currently ${typeof str}, while checking isHexPrefixed.`); | ||
| } | ||
| return str.slice(0, 2) === '0x'; | ||
@@ -31,4 +35,4 @@ } | ||
| * Pads a `String` to have an even length | ||
| * @param {String} a | ||
| * @return {String} | ||
| * @param {String} value | ||
| * @return {String} output | ||
| */ | ||
@@ -38,2 +42,6 @@ function padToEven(value) { | ||
| if (typeof a !== 'string') { | ||
| throw new Error(`value must be string, is currently ${typeof a}, while padToEven.`); | ||
| } | ||
| if (a.length % 2) { | ||
@@ -80,2 +88,6 @@ a = `0${a}`; | ||
| function getBinarySize(str) { | ||
| if (typeof str !== 'string') { | ||
| throw new Error(`method getBinarySize requires input 'str' to be type String, got '${typeof str}'.`); | ||
| } | ||
| return Buffer.byteLength(str, 'utf8'); | ||
@@ -107,2 +119,5 @@ } | ||
| v = Buffer.from(v.toArray()); | ||
| } else if (v.dividedToIntegerBy) { | ||
| // converts a BigNumber to a Buffer | ||
| v = Buffer.from(padToEven(v.toString(16)), 'hex'); | ||
| } else { | ||
@@ -126,2 +141,5 @@ throw new Error('invalid type'); | ||
| function arrayContainsArray(superset, subset, some) { | ||
| if (Array.isArray(superset) !== true) { throw new Error(`method arrayContainsArray requires input 'superset' to be an array got type '${typeof superset}'`); } | ||
| if (Array.isArray(subset) !== true) { throw new Error(`method arrayContainsArray requires input 'subset' to be an array got type '${typeof subset}'`); } | ||
| return subset[Boolean(some) && 'some' || 'every']((value) => (superset.indexOf(value) >= 0)); | ||
@@ -223,5 +241,14 @@ } | ||
| // getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3] | ||
| /** | ||
| * getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3] | ||
| * | ||
| * @method getKeys get specific key from inner object array of objects | ||
| * @param {String} params | ||
| * @param {String} key | ||
| * @param {Boolean} allowEmpty | ||
| * @returns {Array} output just a simple array of output keys | ||
| */ | ||
| function getKeys(params, key, allowEmpty) { | ||
| if (!Array.isArray(params)) { throw new Error('invalid params'); } | ||
| if (!Array.isArray(params)) { throw new Error(`method getKeys expecting type Array as 'params' input, got '${typeof params}'`); } | ||
| if (typeof key !== 'string') { throw new Error(`method getKeys expecting type String for input 'key' got '${typeof key}'.`); } | ||
@@ -228,0 +255,0 @@ var result = []; // eslint-disable-line |
+422
-0
| const util = require('../index.js'); | ||
| const assert = require('chai').assert; | ||
| const BN = require('bn.js'); | ||
| const BigNumber = require('bignumber.js'); | ||
@@ -25,2 +27,422 @@ describe('check all exports', () => { | ||
| }); | ||
| it('should isHexPrefixed check if hex is prefixed', () => { | ||
| assert.equal(util.isHexPrefixed('0xsdffsd'), true); | ||
| assert.equal(util.isHexPrefixed('0x'), true); | ||
| assert.equal(util.isHexPrefixed('0x3982349284'), true); | ||
| assert.equal(util.isHexPrefixed('0x824723894jshdksjdhks'), true); | ||
| }); | ||
| it('should isHexPrefixed check if hex is prefixed not prefixed', () => { | ||
| assert.equal(util.isHexPrefixed('sdffsd'), false); | ||
| assert.equal(util.isHexPrefixed(''), false); | ||
| assert.equal(util.isHexPrefixed('3982349284'), false); | ||
| assert.equal(util.isHexPrefixed('824723894jshdksjdhks'), false); | ||
| }); | ||
| it('should isHexPrefixed throw as expected string got buffer', () => { | ||
| try { | ||
| util.isHexPrefixed(new Buffer()); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should isHexPrefixed throw as expected string got empty object', () => { | ||
| try { | ||
| util.isHexPrefixed({}); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should isHexPrefixed throw as expected string got number', () => { | ||
| try { | ||
| util.isHexPrefixed(823947243994); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should isHexPrefixed throw as expected string got undefined', () => { | ||
| try { | ||
| util.isHexPrefixed(undefined); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should isHexPrefixed throw as expected string got null', () => { | ||
| try { | ||
| util.isHexPrefixed(null); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should stripHexPrefix strip prefix of valid strings', () => { | ||
| assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xksfdkj'), 'ksfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xkdsfdkj'), 'kdsfdkj'); | ||
| assert.equal(util.stripHexPrefix('0x23442sfdkj'), '23442sfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xaaaasfdkj'), 'aaaasfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xkdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj'); | ||
| assert.equal(util.stripHexPrefix('0x111dssdddj'), '111dssdddj'); | ||
| }); | ||
| it('should stripHexPrefix strip prefix of mix hexed strings', () => { | ||
| assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj'); | ||
| assert.equal(util.stripHexPrefix('ksfdkj'), 'ksfdkj'); | ||
| assert.equal(util.stripHexPrefix('kdsfdkj'), 'kdsfdkj'); | ||
| assert.equal(util.stripHexPrefix('23442sfdkj'), '23442sfdkj'); | ||
| assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj'); | ||
| assert.equal(util.stripHexPrefix('aaaasfdkj'), 'aaaasfdkj'); | ||
| assert.equal(util.stripHexPrefix('kdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj'); | ||
| assert.equal(util.stripHexPrefix('111dssdddj'), '111dssdddj'); | ||
| }); | ||
| it('should stripHexPrefix bypass if not string', () => { | ||
| assert.equal(util.stripHexPrefix(null), null); | ||
| assert.equal(util.stripHexPrefix(undefined), undefined); | ||
| assert.equal(util.stripHexPrefix(242423), 242423); | ||
| assert.deepEqual(util.stripHexPrefix({}), {}); | ||
| assert.deepEqual(util.stripHexPrefix([]), []); | ||
| assert.equal(util.stripHexPrefix(true), true); | ||
| }); | ||
| it('valid padToEven should pad to even', () => { | ||
| assert.equal(String(util.padToEven('0')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('111')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('22222')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('ddd')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('aa')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('aaaaaa')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('sdssd')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('eee')).length % 2, 0); | ||
| assert.equal(String(util.padToEven('w')).length % 2, 0); | ||
| }); | ||
| it('valid padToEven should pad to even check string prefix 0', () => { | ||
| assert.equal(String(util.padToEven('0')), '00'); | ||
| assert.equal(String(util.padToEven('111')), '0111'); | ||
| assert.equal(String(util.padToEven('22222')), '022222'); | ||
| assert.equal(String(util.padToEven('ddd')), '0ddd'); | ||
| assert.equal(String(util.padToEven('aa')), 'aa'); | ||
| assert.equal(String(util.padToEven('aaaaaa')), 'aaaaaa'); | ||
| assert.equal(String(util.padToEven('sdssd')), '0sdssd'); | ||
| assert.equal(String(util.padToEven('eee')), '0eee'); | ||
| assert.equal(String(util.padToEven('w')), '0w'); | ||
| }); | ||
| it('should padToEven throw as expected string got null', () => { | ||
| try { | ||
| util.padToEven(null); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should padToEven throw as expected string got undefined', () => { | ||
| try { | ||
| util.padToEven(undefined); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should padToEven throw as expected string got {}', () => { | ||
| try { | ||
| util.padToEven({}); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should padToEven throw as expected string got new Buffer()', () => { | ||
| try { | ||
| util.padToEven(new Buffer()); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('should padToEven throw as expected string got number', () => { | ||
| try { | ||
| util.padToEven(24423232); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method getKeys should throw as expected array for params got number', () => { | ||
| try { | ||
| util.getKeys(2482822); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method invalid getKeys with allow empty and no defined value', () => { | ||
| try { | ||
| util.getKeys([{ type: undefined }], 'type', true); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method valid getKeys with allow empty and false', () => { | ||
| try { | ||
| util.getKeys([{ type: true }], 'type', true); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method getKeys should throw as expected array for params got number', () => { | ||
| try { | ||
| util.getKeys(2482822, 293849824); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method getKeys should throw as expected array for params got object', () => { | ||
| try { | ||
| util.getKeys({}, []); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method getKeys should throw as expected array for params got null', () => { | ||
| try { | ||
| util.getKeys(null); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method getKeys should throw as expected array for params got false', () => { | ||
| try { | ||
| util.getKeys(false); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('valid getKeys should get keys from object in array', () => { | ||
| assert.deepEqual(util.getKeys([{ type: 'sfd' }, { type: 'something' }], 'type'), ['sfd', 'something']); | ||
| assert.deepEqual(util.getKeys([{ cool: 'something' }, { cool: 'fdsdfsfd' }], 'cool'), ['something', 'fdsdfsfd']); | ||
| assert.deepEqual(util.getKeys([{ type: '234424' }, { type: '243234242432' }], 'type'), ['234424', '243234242432']); | ||
| assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }], 'type'), ['something', 'something']); | ||
| assert.deepEqual(util.getKeys([{ type: 'something' }], 'type'), ['something']); | ||
| assert.deepEqual(util.getKeys([], 'type'), []); | ||
| assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }, { type: 'something' }], 'type'), ['something', 'something', 'something']); | ||
| }); | ||
| it('valid isHexString tests', () => { | ||
| assert.equal(util.isHexString('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x1e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x6e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0xecfaa1a0c4372a2ac5cca1e164510ec8df04f681fc960797f1419802ec00b225'), true); | ||
| assert.equal(util.isHexString('0x6e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x620e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x1e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x2e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true); | ||
| assert.equal(util.isHexString('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), true); | ||
| assert.equal(util.isHexString('0x2bb303f0ae65c64ef80a3bb3ee8ceef5d50065bd'), true); | ||
| assert.equal(util.isHexString('0x6e026d45820d91256fc73d7ff2bdef353ebfe7e9'), true); | ||
| }); | ||
| it('invalid isHexString tests', () => { | ||
| assert.equal(util.isHexString(' 0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), false); | ||
| assert.equal(util.isHexString('fdsjfsd'), false); | ||
| assert.equal(util.isHexString(' 0xfdsjfsd'), false); | ||
| assert.equal(util.isHexString('0xfds*jfsd'), false); | ||
| assert.equal(util.isHexString('0xfds$jfsd'), false); | ||
| assert.equal(util.isHexString('0xf@dsjfsd'), false); | ||
| assert.equal(util.isHexString('0xfdsjf!sd'), false); | ||
| assert.equal(util.isHexString('fds@@jfsd'), false); | ||
| assert.equal(util.isHexString(24223), false); | ||
| assert.equal(util.isHexString(null), false); | ||
| assert.equal(util.isHexString(undefined), false); | ||
| assert.equal(util.isHexString(false), false); | ||
| assert.equal(util.isHexString({}), false); | ||
| assert.equal(util.isHexString([]), false); | ||
| }); | ||
| it('valid arrayContainsArray should array contain every array', () => { | ||
| assert.equal(util.arrayContainsArray([1, 2, 3], [1, 2]), true); | ||
| assert.equal(util.arrayContainsArray([3, 3], [3, 3]), true); | ||
| assert.equal(util.arrayContainsArray([1, 2, 'h'], [1, 2, 'h']), true); | ||
| assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [1, 2, 'fsffds']), true); | ||
| assert.equal(util.arrayContainsArray([1], [1]), true); | ||
| assert.equal(util.arrayContainsArray([], []), true); | ||
| assert.equal(util.arrayContainsArray([1, 3333], [1, 3333]), true); | ||
| }); | ||
| it('valid getBinarySize should get binary size of string', () => { | ||
| assert.equal(util.getBinarySize('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), 42); | ||
| assert.equal(util.getBinarySize('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), 66); | ||
| }); | ||
| it('invalid getBinarySize should throw invalid type Boolean', () => { | ||
| try { | ||
| util.getBinarySize(false); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('invalid getBinarySize should throw invalid type object', () => { | ||
| try { | ||
| util.getBinarySize({}); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('invalid getBinarySize should throw invalid type Array', () => { | ||
| try { | ||
| util.getBinarySize([]); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('valid arrayContainsArray should array some every array', () => { | ||
| assert.equal(util.arrayContainsArray([1, 2], [1], true), true); | ||
| assert.equal(util.arrayContainsArray([3, 3], [3, 2323], true), true); | ||
| assert.equal(util.arrayContainsArray([1, 2, 'h'], [2332, 2, 'h'], true), true); | ||
| assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [3232, 2, 'fsffds'], true), true); | ||
| assert.equal(util.arrayContainsArray([1], [1], true), true); | ||
| assert.equal(util.arrayContainsArray([1, 3333], [1, 323232], true), true); | ||
| }); | ||
| it('method arrayContainsArray should throw as expected array for params got false', () => { | ||
| try { | ||
| util.arrayContainsArray(false); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method arrayContainsArray should throw as expected array for params got false', () => { | ||
| try { | ||
| util.arrayContainsArray([], false); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| it('method arrayContainsArray should throw as expected array for params got {}', () => { | ||
| try { | ||
| util.arrayContainsArray({}, false); | ||
| } catch (error) { | ||
| assert.equal(typeof error, 'object'); | ||
| } | ||
| }); | ||
| const fromAsciiTests = [ | ||
| { value: 'myString', expected: '0x6d79537472696e67' }, | ||
| { value: 'myString\x00', expected: '0x6d79537472696e6700' }, | ||
| { value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|ξ\u001a7«\u00052\u0011(ÐY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076* \bñùC1ÉUÀé2\u001aÓB', | ||
| expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c' }, | ||
| ]; | ||
| describe('fromAscii', () => { | ||
| fromAsciiTests.forEach((test) => { | ||
| it(`should turn ${test.value} to ${test.expected} `, () => { | ||
| assert.strictEqual(util.fromAscii(test.value), test.expected); | ||
| }); | ||
| }); | ||
| }); | ||
| const fromUtf8Tests = [ | ||
| { value: 'myString', expected: '0x6d79537472696e67' }, | ||
| { value: 'myString\x00', expected: '0x6d79537472696e67' }, | ||
| { value: 'expected value\u0000\u0000\u0000', expected: '0x65787065637465642076616c7565' }, | ||
| ]; | ||
| describe('fromUtf8', () => { | ||
| fromUtf8Tests.forEach((test) => { | ||
| it(`should turn ${test.value} to ${test.expected} `, () => { | ||
| assert.strictEqual(util.fromUtf8(test.value), test.expected); | ||
| }); | ||
| }); | ||
| }); | ||
| const toUtf8Tests = [ | ||
| { value: '0x6d79537472696e67', expected: 'myString' }, | ||
| { value: '0x6d79537472696e6700', expected: 'myString' }, | ||
| { value: '0x65787065637465642076616c7565000000000000000000000000000000000000', expected: 'expected value' }, | ||
| ]; | ||
| describe('toUtf8', () => { | ||
| toUtf8Tests.forEach((test) => { | ||
| it(`should turn ${test.value} to ${test.expected} `, () => { | ||
| assert.strictEqual(util.toUtf8(test.value), test.expected); | ||
| }); | ||
| }); | ||
| }); | ||
| const toAsciiTests = [ | ||
| { value: '0x6d79537472696e67', expected: 'myString' }, | ||
| { value: '0x6d79537472696e6700', expected: 'myString\u0000' }, | ||
| { value: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c', | ||
| expected: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|ξ\u001a7«\u00052\u0011(ÐY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076* \bñùC1ÉUÀé2\u001aÓB' }, | ||
| ]; | ||
| describe('toAsciiTests', () => { | ||
| toAsciiTests.forEach((test) => { | ||
| it(`should turn ${test.value} to ${test.expected} `, () => { | ||
| assert.strictEqual(util.toAscii(test.value), test.expected); | ||
| }); | ||
| }); | ||
| }); | ||
| describe('intToHex', () => { | ||
| it('should convert a int to hex', () => { | ||
| const i = 6003400; | ||
| const hex = util.intToHex(i); | ||
| assert.equal(hex, '0x5b9ac8'); | ||
| }); | ||
| }); | ||
| describe('intToBuffer', () => { | ||
| it('should convert a int to a buffer', () => { | ||
| const i = 6003400; | ||
| const buf = util.intToBuffer(i); | ||
| assert.equal(buf.toString('hex'), '5b9ac8'); | ||
| }); | ||
| }); | ||
| describe('toBuffer', () => { | ||
| it('should work', () => { | ||
| // Buffer | ||
| assert.deepEqual(util.toBuffer(Buffer.allocUnsafe(0)), Buffer.allocUnsafe(0)); | ||
| // Array | ||
| assert.deepEqual(util.toBuffer([]), Buffer.allocUnsafe(0)); | ||
| // String | ||
| assert.deepEqual(util.toBuffer('11'), Buffer.from([49, 49])); | ||
| assert.deepEqual(util.toBuffer('0x11'), Buffer.from([17])); | ||
| assert.deepEqual(util.toBuffer('1234').toString('hex'), '31323334'); | ||
| assert.deepEqual(util.toBuffer('0x1234').toString('hex'), '1234'); | ||
| assert.deepEqual(util.toBuffer(new BN(34)).toString('hex'), '22'); | ||
| assert.deepEqual(util.toBuffer(new BigNumber(34)).toString('hex'), '22'); | ||
| // Number | ||
| assert.deepEqual(util.toBuffer(1), Buffer.from([1])); | ||
| // null | ||
| assert.deepEqual(util.toBuffer(null), Buffer.allocUnsafe(0)); | ||
| // undefined | ||
| assert.deepEqual(util.toBuffer(), Buffer.allocUnsafe(0)); | ||
| }); | ||
| it('should fail', () => { | ||
| assert.throws(() => { | ||
| util.toBuffer({ test: 1 }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Sorry, the diff of this file is not supported yet
44089
136.64%2
-33.33%14
27.27%643
161.38%137
3.01%16
23.08%- Removed
- Removed
Updated