From 532c526bc7e7a06e42ae50ac78e29d4af16bb766 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 20 Feb 2018 14:01:02 +0100 Subject: [PATCH] Add basic u8aToString (#92) --- package.json | 2 +- packages/util/docs/u8a.md | 18 +++++++ packages/util/src/u8a/index.js | 2 + packages/util/src/u8a/toString.js | 29 +++++++++++ packages/util/src/u8a/toString.spec.js | 27 ++++++++++ yarn.lock | 68 +++++++++++++------------- 6 files changed, 112 insertions(+), 34 deletions(-) create mode 100644 packages/util/src/u8a/toString.js create mode 100644 packages/util/src/u8a/toString.spec.js diff --git a/package.json b/package.json index bf4054361..2238ba12d 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.16.7", + "@polkadot/dev": "^0.16.10", "lerna": "^2.5.1" } } diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index 8289c1456..f85ba150f 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -12,6 +12,7 @@ Utility methods to convert to and from `Uint8Array` objects - [u8aToBn](#u8atobn) Creates a BN from a Uint8Array object. - [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string. - [u8aToHex](#u8atohex) Creates a hex string from a Uint8Array object. +- [u8aToString](#u8atostring) Creates a string from a Uint8Array object. - [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object. ## u8aConcat @@ -174,6 +175,23 @@ import { u8aToHex } from '@polkadot/util'; u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f ``` +## u8aToString + +Creates a string from a Uint8Array object. + +```js +u8aToString (value?: UInt8Array): string +``` + + +`UInt8Array` input values return the actual decoded string. `null` or `undefined` values returns an empty string. + +```js +import { u8aToString } from '@polkadot/util'; + +u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello +``` + ## u8aToUtf8 Creates a utf-8 string from a Uint8Array object. diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index ed2f66e0a..148790c95 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -13,6 +13,7 @@ const u8aFromString = require('./fromString'); const u8aToBn = require('./toBn'); const u8aToBuffer = require('./toBuffer'); const u8aToHex = require('./toHex'); +const u8aToString = require('./toString'); const u8aToUtf8 = require('./toUtf8'); /** @@ -29,5 +30,6 @@ module.exports = { u8aToBn, u8aToBuffer, u8aToHex, + u8aToString, u8aToUtf8 }; diff --git a/packages/util/src/u8a/toString.js b/packages/util/src/u8a/toString.js new file mode 100644 index 000000000..92aff563b --- /dev/null +++ b/packages/util/src/u8a/toString.js @@ -0,0 +1,29 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +/** + @name u8aToString + @signature u8aToString (value?: UInt8Array): string + @summary Creates a string from a Uint8Array object. + @description + `UInt8Array` input values return the actual decoded string. `null` or `undefined` values returns an empty string. + @example + import { u8aToString } from '@polkadot/util'; + + u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello +*/ +module.exports = function u8aToString (value?: Uint8Array): string { + if (!value || !value.length) { + return ''; + } + + return value + .reduce((result, ch) => { + result.push(String.fromCharCode(ch)); + + return result; + }, []) + .join(''); +}; diff --git a/packages/util/src/u8a/toString.spec.js b/packages/util/src/u8a/toString.spec.js new file mode 100644 index 000000000..5521bbb0f --- /dev/null +++ b/packages/util/src/u8a/toString.spec.js @@ -0,0 +1,27 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +const { u8aToString } = require('./index'); + +describe('u8aToString', () => { + it('decodes to an empty string for undefined', () => { + expect( + u8aToString() + ).toEqual(''); + }); + + it('decodes to an empty string for empty buffer', () => { + expect( + u8aToString(new Uint8Array([])) + ).toEqual(''); + }); + + it('decodes the buffer correctly', () => { + expect( + u8aToString( + new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]) + ) + ).toEqual('hello'); + }); +}); diff --git a/yarn.lock b/yarn.lock index 77808a9d4..d8396ae7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -69,9 +69,9 @@ dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.16.7": - version "0.16.7" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.16.7.tgz#072ca840f133d237767e2a737cfcf903ca0e6c6d" +"@polkadot/dev@^0.16.10": + version "0.16.10" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.16.10.tgz#3bd0ba75436ff9f8c2e9a62a185d208220d57e47" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -83,17 +83,17 @@ babel-preset-env "^1.6.1" coveralls "^3.0.0" dox "^0.9.0" - eslint "^4.14.0" + eslint "^4.18.0" eslint-config-semistandard "^12.0.0" - eslint-config-standard "^11.0.0-beta.0" + eslint-config-standard "^11.0.0" eslint-config-standard-jsx "^5.0.0" - eslint-config-standard-react "^5.0.0" + eslint-config-standard-react "^6.0.0" eslint-plugin-flowtype "^2.39.1" eslint-plugin-import "^2.8.0" eslint-plugin-jest "^21.3.2" eslint-plugin-node "^6.0.0" eslint-plugin-promise "^3.6.0" - eslint-plugin-react "^7.4.0" + eslint-plugin-react "^7.7.0" eslint-plugin-standard "^3.0.1" flow-bin "^0.66.0" flow-copy-source "^1.2.1" @@ -104,7 +104,7 @@ postcss-sass "^0.3.0" rimraf "^2.6.2" stylelint "^9.0.0" - stylelint-config-standard "^18.0.0" + stylelint-config-standard "^18.1.0" "@types/node@*": version "8.5.2" @@ -1668,12 +1668,18 @@ doctrine@1.5.0: esutils "^2.0.2" isarray "^1.0.0" -doctrine@^2.0.0, doctrine@^2.0.2: +doctrine@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.0.2.tgz#68f96ce8efc56cc42651f1faadb4f175273b0075" dependencies: esutils "^2.0.2" +doctrine@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d" + dependencies: + esutils "^2.0.2" + dom-serializer@0: version "0.1.0" resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82" @@ -1801,23 +1807,19 @@ eslint-config-semistandard@^12.0.0: version "12.0.0" resolved "https://registry.yarnpkg.com/eslint-config-semistandard/-/eslint-config-semistandard-12.0.0.tgz#e6482de68e05c2dbdddaf45c3078f6a97fe23f1a" -eslint-config-standard-jsx@^4.0.0: - version "4.0.2" - resolved "https://registry.yarnpkg.com/eslint-config-standard-jsx/-/eslint-config-standard-jsx-4.0.2.tgz#009e53c4ddb1e9ee70b4650ffe63a7f39f8836e1" - eslint-config-standard-jsx@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/eslint-config-standard-jsx/-/eslint-config-standard-jsx-5.0.0.tgz#4abfac554f38668e0078c664569e7b2384e5d2aa" -eslint-config-standard-react@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/eslint-config-standard-react/-/eslint-config-standard-react-5.0.0.tgz#64c7b8140172852be810a53d48ee87649ff178e3" +eslint-config-standard-react@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/eslint-config-standard-react/-/eslint-config-standard-react-6.0.0.tgz#d366d6c3c092426fd3ae794a4ca0b3cb131f2964" dependencies: - eslint-config-standard-jsx "^4.0.0" + eslint-config-standard-jsx "^5.0.0" -eslint-config-standard@^11.0.0-beta.0: - version "11.0.0-beta.0" - resolved "https://registry.yarnpkg.com/eslint-config-standard/-/eslint-config-standard-11.0.0-beta.0.tgz#f8afe69803d95c685a4b8392b8793188eb03cbb3" +eslint-config-standard@^11.0.0: + version "11.0.0" + resolved "https://registry.yarnpkg.com/eslint-config-standard/-/eslint-config-standard-11.0.0.tgz#87ee0d3c9d95382dc761958cbb23da9eea31e0ba" eslint-import-resolver-node@^0.3.1: version "0.3.1" @@ -1871,13 +1873,13 @@ eslint-plugin-promise@^3.6.0: version "3.6.0" resolved "https://registry.yarnpkg.com/eslint-plugin-promise/-/eslint-plugin-promise-3.6.0.tgz#54b7658c8f454813dc2a870aff8152ec4969ba75" -eslint-plugin-react@^7.4.0: - version "7.5.1" - resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.5.1.tgz#52e56e8d80c810de158859ef07b880d2f56ee30b" +eslint-plugin-react@^7.7.0: + version "7.7.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.7.0.tgz#f606c719dbd8a1a2b3d25c16299813878cca0160" dependencies: - doctrine "^2.0.0" + doctrine "^2.0.2" has "^1.0.1" - jsx-ast-utils "^2.0.0" + jsx-ast-utils "^2.0.1" prop-types "^15.6.0" eslint-plugin-standard@^3.0.1: @@ -1895,9 +1897,9 @@ eslint-visitor-keys@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#3f3180fb2e291017716acb4c9d6d5b5c34a6a81d" -eslint@^4.14.0: - version "4.14.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.14.0.tgz#96609768d1dd23304faba2d94b7fefe5a5447a82" +eslint@^4.18.0: + version "4.18.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.18.0.tgz#ebd0ba795af6dc59aa5cee17938160af5950e051" dependencies: ajv "^5.3.0" babel-code-frame "^6.22.0" @@ -1905,7 +1907,7 @@ eslint@^4.14.0: concat-stream "^1.6.0" cross-spawn "^5.1.0" debug "^3.1.0" - doctrine "^2.0.2" + doctrine "^2.1.0" eslint-scope "^3.7.1" eslint-visitor-keys "^1.0.0" espree "^3.5.2" @@ -3348,7 +3350,7 @@ jsprim@^1.2.2: json-schema "0.2.3" verror "1.10.0" -jsx-ast-utils@^2.0.0: +jsx-ast-utils@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-2.0.1.tgz#e801b1b39985e20fffc87b40e3748080e2dcac7f" dependencies: @@ -4963,9 +4965,9 @@ stylelint-config-recommended@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/stylelint-config-recommended/-/stylelint-config-recommended-2.0.0.tgz#560a028e81ea3ca8894b9a8eef4c0e05ac60e090" -stylelint-config-standard@^18.0.0: - version "18.0.0" - resolved "https://registry.yarnpkg.com/stylelint-config-standard/-/stylelint-config-standard-18.0.0.tgz#0d872b40fafdcddcf4188fb5b64ddb3887e8aefc" +stylelint-config-standard@^18.1.0: + version "18.1.0" + resolved "https://registry.yarnpkg.com/stylelint-config-standard/-/stylelint-config-standard-18.1.0.tgz#e4d977063f5bca77970033d2f76941c3cc19fd9c" dependencies: stylelint-config-recommended "^2.0.0"