Skip to content

Commit

Permalink
Merge branch '1.x' into fix/metamask-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Koray Koska committed Nov 13, 2020
2 parents 09cc191 + ee8bec9 commit ad58a2a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Released with 1.0.0-beta.37 code base.

- Add `web3-eth2-core` package (#3743)
- Add `web3-eth2-beaconchain` package (#3743)
- Add `stripHexPrefix` method to `web3-utils` package (#3776)

### Changed

Expand Down
35 changes: 35 additions & 0 deletions docs/web3-utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,41 @@ Example
web3.utils.toHex('I have 100€');
> "0x49206861766520313030e282ac"
------------------------------------------------------------------------------

stripHexPrefix
=====================

.. code-block:: javascript
web3.utils.stripHexPrefix(str)
Returns provided string without 0x prefix.

----------
Parameters
----------

1. ``str`` - ``string``: Input string

-------
Returns
-------

``String``: The input string without 0x prefix.

-------
Example
-------

.. code-block:: javascript
web3.utils.stripHexPrefix('234');
> "234"
web3.utils.stripHexPrefix('0x234');
> "234"
web3.utils.stripHexPrefix(42);
> 42
------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ module.exports = {
hexToUtf8: utils.hexToUtf8,
hexToString: utils.hexToUtf8,
toUtf8: utils.hexToUtf8,
stripHexPrefix: utils.stripHexPrefix,

utf8ToHex: utils.utf8ToHex,
stringToHex: utils.utf8ToHex,
Expand Down
13 changes: 13 additions & 0 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ var isHex = function (hex) {
return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex));
};

/**
* Remove 0x prefix from string
*
* @method stripHexPrefix
* @param {String} str to be checked
* @returns {String}
*/
var stripHexPrefix = function (str) {
if (str !== 0 && isHex(str))
return str.replace(/^(-)?0x/i, '$1')
return str;
};

/**
* Returns true if given string is a valid Ethereum block header bloom.
Expand Down Expand Up @@ -534,6 +546,7 @@ module.exports = {
bytesToHex: bytesToHex,
isHex: isHex,
isHexStrict: isHexStrict,
stripHexPrefix: stripHexPrefix,
leftPad: leftPad,
rightPad: rightPad,
toTwosComplement: toTwosComplement,
Expand Down
54 changes: 54 additions & 0 deletions test/utils.stripHexPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');

var BigNumber = require('bignumber.js');
var BN = require('bn.js');

var assert = chai.assert;

var tests = [
{ value: '1', expected: '1' },
{ value: '0x1', expected: '1'},
{ value: '0xf', expected: 'f'},
{ value: '-1', expected: '-1'},
{ value: '-0x1', expected: '-1'},
{ value: '-15', expected: '-15'},
{ value: '-0xf', expected: '-f'},
{ value: '0x657468657265756d', expected: '657468657265756d'},
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: 0, expected: 0},
{ value: '0', expected: '0'},
{ value: '0x0', expected: '0'},
{ value: -0, expected: -0},
{ value: '-0', expected: '-0'},
{ value: '-0x0', expected: '-0'},
{ value: [1,2,3,{test: 'data'}], expected: [1,2,3,{test: 'data'}]},
{ value: {test: 'test'}, expected: {test: 'test'}},
{ value: '{"test": "test"}', expected: '{"test": "test"}'},
{ value: 'myString', expected: 'myString'},
{ value: 'myString 34534!', expected: 'myString 34534!'},
{ value: new BN(15), expected: new BN(15)},
{ value: new BigNumber(15), expected: new BigNumber(15)},
{ value: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/', expected: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/'},
{ value: true, expected: true},
{ value: false, expected: false},
{ value: 'ff\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: 'ff\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Œ'},
{ 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: '\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Œ'},
{ value: '내가 제일 잘 나가', expected: '내가 제일 잘 나가'},
{ value: Buffer.from('100'), expected: Buffer.from('100')},
{ value: '0xfffff0x0fffffffffff0xffffff0x', expected: '0xfffff0x0fffffffffff0xffffff0x'}
];

describe('lib/utils/utils', function () {
describe('stripHexPrefix', function () {
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.deepEqual(utils.stripHexPrefix(test.value), test.expected);
});
});
});
});
1 change: 1 addition & 0 deletions test/utils_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ describe('utils', function() {
u.methodExists(utils, 'isAddress');
u.methodExists(utils, 'soliditySha3');
u.methodExists(utils, 'encodePacked');
u.methodExists(utils, 'stripHexPrefix');
});
});

0 comments on commit ad58a2a

Please sign in to comment.