Skip to content

Commit ad58a2a

Browse files
author
Koray Koska
authored
Merge branch '1.x' into fix/metamask-issue
2 parents 09cc191 + ee8bec9 commit ad58a2a

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Released with 1.0.0-beta.37 code base.
294294

295295
- Add `web3-eth2-core` package (#3743)
296296
- Add `web3-eth2-beaconchain` package (#3743)
297+
- Add `stripHexPrefix` method to `web3-utils` package (#3776)
297298

298299
### Changed
299300

docs/web3-utils.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,41 @@ Example
666666
web3.utils.toHex('I have 100€');
667667
> "0x49206861766520313030e282ac"
668668
669+
------------------------------------------------------------------------------
670+
671+
stripHexPrefix
672+
=====================
673+
674+
.. code-block:: javascript
675+
web3.utils.stripHexPrefix(str)
676+
Returns provided string without 0x prefix.
677+
678+
----------
679+
Parameters
680+
----------
681+
682+
1. ``str`` - ``string``: Input string
683+
684+
-------
685+
Returns
686+
-------
687+
688+
``String``: The input string without 0x prefix.
689+
690+
-------
691+
Example
692+
-------
693+
694+
.. code-block:: javascript
695+
696+
web3.utils.stripHexPrefix('234');
697+
> "234"
698+
699+
web3.utils.stripHexPrefix('0x234');
700+
> "234"
701+
702+
web3.utils.stripHexPrefix(42);
703+
> 42
669704
670705
------------------------------------------------------------------------------
671706

packages/web3-utils/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ module.exports = {
411411
hexToUtf8: utils.hexToUtf8,
412412
hexToString: utils.hexToUtf8,
413413
toUtf8: utils.hexToUtf8,
414+
stripHexPrefix: utils.stripHexPrefix,
414415

415416
utf8ToHex: utils.utf8ToHex,
416417
stringToHex: utils.utf8ToHex,

packages/web3-utils/src/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ var isHex = function (hex) {
388388
return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex));
389389
};
390390

391+
/**
392+
* Remove 0x prefix from string
393+
*
394+
* @method stripHexPrefix
395+
* @param {String} str to be checked
396+
* @returns {String}
397+
*/
398+
var stripHexPrefix = function (str) {
399+
if (str !== 0 && isHex(str))
400+
return str.replace(/^(-)?0x/i, '$1')
401+
return str;
402+
};
391403

392404
/**
393405
* Returns true if given string is a valid Ethereum block header bloom.
@@ -534,6 +546,7 @@ module.exports = {
534546
bytesToHex: bytesToHex,
535547
isHex: isHex,
536548
isHexStrict: isHexStrict,
549+
stripHexPrefix: stripHexPrefix,
537550
leftPad: leftPad,
538551
rightPad: rightPad,
539552
toTwosComplement: toTwosComplement,

test/utils.stripHexPrefix.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var chai = require('chai');
2+
var utils = require('../packages/web3-utils');
3+
4+
var BigNumber = require('bignumber.js');
5+
var BN = require('bn.js');
6+
7+
var assert = chai.assert;
8+
9+
var tests = [
10+
{ value: '1', expected: '1' },
11+
{ value: '0x1', expected: '1'},
12+
{ value: '0xf', expected: 'f'},
13+
{ value: '-1', expected: '-1'},
14+
{ value: '-0x1', expected: '-1'},
15+
{ value: '-15', expected: '-15'},
16+
{ value: '-0xf', expected: '-f'},
17+
{ value: '0x657468657265756d', expected: '657468657265756d'},
18+
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
19+
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
20+
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
21+
{ value: 0, expected: 0},
22+
{ value: '0', expected: '0'},
23+
{ value: '0x0', expected: '0'},
24+
{ value: -0, expected: -0},
25+
{ value: '-0', expected: '-0'},
26+
{ value: '-0x0', expected: '-0'},
27+
{ value: [1,2,3,{test: 'data'}], expected: [1,2,3,{test: 'data'}]},
28+
{ value: {test: 'test'}, expected: {test: 'test'}},
29+
{ value: '{"test": "test"}', expected: '{"test": "test"}'},
30+
{ value: 'myString', expected: 'myString'},
31+
{ value: 'myString 34534!', expected: 'myString 34534!'},
32+
{ value: new BN(15), expected: new BN(15)},
33+
{ value: new BigNumber(15), expected: new BigNumber(15)},
34+
{ value: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/', expected: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/'},
35+
{ value: true, expected: true},
36+
{ value: false, expected: false},
37+
{ 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Œ',
38+
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Œ'},
39+
{ 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Œ',
40+
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Œ'},
41+
{ value: '내가 제일 잘 나가', expected: '내가 제일 잘 나가'},
42+
{ value: Buffer.from('100'), expected: Buffer.from('100')},
43+
{ value: '0xfffff0x0fffffffffff0xffffff0x', expected: '0xfffff0x0fffffffffff0xffffff0x'}
44+
];
45+
46+
describe('lib/utils/utils', function () {
47+
describe('stripHexPrefix', function () {
48+
tests.forEach(function (test) {
49+
it('should turn ' + test.value + ' to ' + test.expected, function () {
50+
assert.deepEqual(utils.stripHexPrefix(test.value), test.expected);
51+
});
52+
});
53+
});
54+
});

test/utils_methods.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ describe('utils', function() {
1414
u.methodExists(utils, 'isAddress');
1515
u.methodExists(utils, 'soliditySha3');
1616
u.methodExists(utils, 'encodePacked');
17+
u.methodExists(utils, 'stripHexPrefix');
1718
});
1819
});

0 commit comments

Comments
 (0)