diff --git a/CHANGELOG.md b/CHANGELOG.md index c425d3d9aea..f9519c9c037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/web3-utils.rst b/docs/web3-utils.rst index ef148d8564c..46ab23a07fb 100644 --- a/docs/web3-utils.rst +++ b/docs/web3-utils.rst @@ -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 ------------------------------------------------------------------------------ diff --git a/packages/web3-utils/src/index.js b/packages/web3-utils/src/index.js index 0c30545723a..fb26c6e1187 100644 --- a/packages/web3-utils/src/index.js +++ b/packages/web3-utils/src/index.js @@ -411,6 +411,7 @@ module.exports = { hexToUtf8: utils.hexToUtf8, hexToString: utils.hexToUtf8, toUtf8: utils.hexToUtf8, + stripHexPrefix: utils.stripHexPrefix, utf8ToHex: utils.utf8ToHex, stringToHex: utils.utf8ToHex, diff --git a/packages/web3-utils/src/utils.js b/packages/web3-utils/src/utils.js index 3cc225d47cf..63afa1c7dee 100644 --- a/packages/web3-utils/src/utils.js +++ b/packages/web3-utils/src/utils.js @@ -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. @@ -534,6 +546,7 @@ module.exports = { bytesToHex: bytesToHex, isHex: isHex, isHexStrict: isHexStrict, + stripHexPrefix: stripHexPrefix, leftPad: leftPad, rightPad: rightPad, toTwosComplement: toTwosComplement, diff --git a/test/utils.stripHexPrefix.js b/test/utils.stripHexPrefix.js new file mode 100644 index 00000000000..0636daaaede --- /dev/null +++ b/test/utils.stripHexPrefix.js @@ -0,0 +1,53 @@ +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')} +]; + +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); + }); + }); + }); +}); diff --git a/test/utils_methods.js b/test/utils_methods.js index 038cd24646a..4fb8c676c67 100644 --- a/test/utils_methods.js +++ b/test/utils_methods.js @@ -14,5 +14,6 @@ describe('utils', function() { u.methodExists(utils, 'isAddress'); u.methodExists(utils, 'soliditySha3'); u.methodExists(utils, 'encodePacked'); + u.methodExists(utils, 'stripHexPrefix'); }); });