From f20035fe972ecb49a951602eaef7282707e98fa5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 11 Jan 2018 17:48:38 +0100 Subject: [PATCH] Move util function from @polkadot/client (#62) --- docs/u8a.md | 36 ++++++++++++++++++++++++++++ package.json | 5 ++-- src/syncify.js | 32 +++++++++++++++++++++++++ src/syncify.spec.js | 25 +++++++++++++++++++ src/u8a/fromUtf8.js | 26 ++++++++++++++++++++ src/u8a/fromUtf8.spec.js | 19 +++++++++++++++ src/u8a/index.js | 6 ++++- src/u8a/polyfill/textDecoder.js | 7 ++++++ src/u8a/polyfill/textDecoder.spec.js | 27 +++++++++++++++++++++ src/u8a/polyfill/textEncoder.js | 7 ++++++ src/u8a/polyfill/textEncoder.spec.js | 27 +++++++++++++++++++++ src/u8a/toUtf8.js | 25 +++++++++++++++++++ src/u8a/toUtf8.spec.js | 25 +++++++++++++++++++ yarn.lock | 27 ++++++++++++++------- 14 files changed, 283 insertions(+), 11 deletions(-) create mode 100644 src/syncify.js create mode 100644 src/syncify.spec.js create mode 100644 src/u8a/fromUtf8.js create mode 100644 src/u8a/fromUtf8.spec.js create mode 100644 src/u8a/polyfill/textDecoder.js create mode 100644 src/u8a/polyfill/textDecoder.spec.js create mode 100644 src/u8a/polyfill/textEncoder.js create mode 100644 src/u8a/polyfill/textEncoder.spec.js create mode 100644 src/u8a/toUtf8.js create mode 100644 src/u8a/toUtf8.spec.js diff --git a/docs/u8a.md b/docs/u8a.md index e2f92bbda..f1e2df16d 100644 --- a/docs/u8a.md +++ b/docs/u8a.md @@ -3,7 +3,9 @@ Utility methods to convert to and from `Uint8Array` objects - [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object. +- [u8aFromUtf8](#u8afromutf8) Creates a Uint8Array object from a utf-8 string. - [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string. +- [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object. ## u8aFromBuffer @@ -17,6 +19,23 @@ u8aFromBuffer (value?: Buffer): number +## u8aFromUtf8 + +Creates a Uint8Array object from a utf-8 string. + +```js +u8aFromUtf8 (value?: string): UInt8Array +``` + + +String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array. + +```js +import { u8aFromUtf8 } from '@polkadot/util'; + +u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] +``` + ## u8aToBuffer Creates a Buffer object from a hex string. @@ -32,4 +51,21 @@ u8aToBuffer (value?: UInt8Array): Buffer import { u8aToBuffer } from '@polkadot/util'; console.log('Buffer', u8aToBuffer('0x123480001f')); +``` + +## u8aToUtf8 + +Creates a utf-8 string from a Uint8Array object. + +```js +u8aToUtf8 (value?: UInt8Array): string +``` + + +`UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string. + +```js +import { u8aToString } from '@polkadot/util'; + +u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello ``` \ No newline at end of file diff --git a/package.json b/package.json index 006557013..c8569caa0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.11.4", + "version": "0.12.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ @@ -30,11 +30,12 @@ }, "devDependencies": { "@polkadot/api-jsonrpc": "^0.6.9", - "@polkadot/dev": "^0.14.6" + "@polkadot/dev": "^0.14.8" }, "dependencies": { "babel-runtime": "^6.26.0", "bn.js": "^4.11.8", + "deasync": "^0.1.12", "ip-regex": "^2.1.0", "keccak": "^1.3.0" } diff --git a/src/syncify.js b/src/syncify.js new file mode 100644 index 000000000..f613122d5 --- /dev/null +++ b/src/syncify.js @@ -0,0 +1,32 @@ +// ISC, Copyright 2017-2018 Jaco Greeff +// @flow + +const deasync = require('deasync'); + +const isError = require('./is/error'); +const isUndefined = require('./is/undefined'); + +// FIXME: This is currently for Node environments only - for Browser environments deasync is not available, we need a different solution. + +// flowlint-next-line unclear-type:off +module.exports = function syncify (promise: Promise): any { + let result; + + promise + .catch((error) => error) + .then((_result) => { + result = _result; + }); + + let ms = 0; + + while (isUndefined(result)) { + deasync.sleep(ms++); + } + + if (isError(result)) { + throw result; + } + + return result; +}; diff --git a/src/syncify.spec.js b/src/syncify.spec.js new file mode 100644 index 000000000..6eb5090ac --- /dev/null +++ b/src/syncify.spec.js @@ -0,0 +1,25 @@ +// ISC, Copyright 2017-2018 Jaco Greeff + +const syncify = require('./syncify'); + +describe('syncify', () => { + it('returns the result of the promise', () => { + expect( + syncify( + new Promise((resolve) => { + setTimeout(() => resolve(12345), 100); + }) + ) + ).toEqual(12345); + }); + + it('throws promise exceptions', () => { + expect( + () => syncify( + new Promise((resolve, reject) => { + setTimeout(() => reject(new Error('test reject error')), 100); + }) + ) + ).toThrow(/test reject error/); + }); +}); diff --git a/src/u8a/fromUtf8.js b/src/u8a/fromUtf8.js new file mode 100644 index 000000000..5bec16aa6 --- /dev/null +++ b/src/u8a/fromUtf8.js @@ -0,0 +1,26 @@ +// ISC, Copyright 2017-2018 Jaco Greeff +// @flow + +require('./polyfill/textEncoder'); + +const encoder = new TextEncoder(); + +/** + @name u8aFromUtf8 + @signature u8aFromUtf8 (value?: string): UInt8Array + @summary Creates a Uint8Array object from a utf-8 string. + @description + String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array. + @example + import { u8aFromUtf8 } from '@polkadot/util'; + + u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] +*/ +module.exports = function u8aFromUtf8 (value?: string): Uint8Array { + // flowlint-next-line sketchy-null-string:off + if (!value) { + return new Uint8Array([]); + } + + return encoder.encode(value); +}; diff --git a/src/u8a/fromUtf8.spec.js b/src/u8a/fromUtf8.spec.js new file mode 100644 index 000000000..f561218f1 --- /dev/null +++ b/src/u8a/fromUtf8.spec.js @@ -0,0 +1,19 @@ +// ISC, Copyright 2017-2018 Jaco Greeff + +const { u8aFromUtf8 } = require('./index'); + +describe('u8aFromUtf8', () => { + it('decodes to an empty string for undefined', () => { + expect( + u8aFromUtf8() + ).toEqual(new Uint8Array([])); + }); + + it('encodes the string correctly', () => { + expect( + u8aFromUtf8('Привет, мир!') + ).toEqual( + new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]) + ); + }); +}); diff --git a/src/u8a/index.js b/src/u8a/index.js index d4e731166..ebf9c0dc1 100644 --- a/src/u8a/index.js +++ b/src/u8a/index.js @@ -2,12 +2,16 @@ // @flow const u8aFromBuffer = require('./fromBuffer'); +const u8aFromUtf8 = require('./fromUtf8'); const u8aToBuffer = require('./toBuffer'); +const u8aToUtf8 = require('./toUtf8'); /** @summary Utility methods to convert to and from `Uint8Array` objects */ module.exports = { u8aFromBuffer, - u8aToBuffer + u8aFromUtf8, + u8aToBuffer, + u8aToUtf8 }; diff --git a/src/u8a/polyfill/textDecoder.js b/src/u8a/polyfill/textDecoder.js new file mode 100644 index 000000000..9c91806fc --- /dev/null +++ b/src/u8a/polyfill/textDecoder.js @@ -0,0 +1,7 @@ +// ISC, Copyright 2017-2018 Jaco Greeff +// @flow + +if (typeof TextDecoder === 'undefined') { + // $FlowFixMe trust me flow, it is there + global.TextDecoder = require('util').TextDecoder; +} diff --git a/src/u8a/polyfill/textDecoder.spec.js b/src/u8a/polyfill/textDecoder.spec.js new file mode 100644 index 000000000..1277d278a --- /dev/null +++ b/src/u8a/polyfill/textDecoder.spec.js @@ -0,0 +1,27 @@ +// ISC, Copyright 2017-2018 Jaco Greeff + +describe('TextDecoder', () => { + let origTD; + + beforeEach(() => { + origTD = global.TextDecoder; + }); + + afterEach(() => { + global.TextDecoder = origTD; + }); + + it('polyfills with no exceptions (without TextDecoder)', () => { + global.TextDecoder = null; + + expect(require('./textDecoder')).toBeDefined(); + expect(global.TextDecoder).toBeDefined(); + }); + + it('polyfills with no exceptions (with TextDecoder)', () => { + global.TextDecoder = require('util').TextDecoder; + + expect(require('./textDecoder')).toBeDefined(); + expect(global.TextDecoder).toBeDefined(); + }); +}); diff --git a/src/u8a/polyfill/textEncoder.js b/src/u8a/polyfill/textEncoder.js new file mode 100644 index 000000000..4a3f00221 --- /dev/null +++ b/src/u8a/polyfill/textEncoder.js @@ -0,0 +1,7 @@ +// ISC, Copyright 2017-2018 Jaco Greeff +// @flow + +if (typeof TextEncoder === 'undefined') { + // $FlowFixMe trust me flow, it is there + global.TextEncoder = require('util').TextEncoder; +} diff --git a/src/u8a/polyfill/textEncoder.spec.js b/src/u8a/polyfill/textEncoder.spec.js new file mode 100644 index 000000000..684222fb7 --- /dev/null +++ b/src/u8a/polyfill/textEncoder.spec.js @@ -0,0 +1,27 @@ +// ISC, Copyright 2017-2018 Jaco Greeff + +describe('TextEncoder', () => { + let origTE; + + beforeEach(() => { + origTE = global.TextEncoder; + }); + + afterEach(() => { + global.TextEncoder = origTE; + }); + + it('polyfills with no exceptions (without TextEncoder)', () => { + global.TextEncoder = null; + + expect(require('./textEncoder')).toBeDefined(); + expect(global.TextEncoder).toBeDefined(); + }); + + it('polyfills with no exceptions (with TextEncoder)', () => { + global.TextEncoder = require('util').TextEncoder; + + expect(require('./textEncoder')).toBeDefined(); + expect(global.TextEncoder).toBeDefined(); + }); +}); diff --git a/src/u8a/toUtf8.js b/src/u8a/toUtf8.js new file mode 100644 index 000000000..be195eed6 --- /dev/null +++ b/src/u8a/toUtf8.js @@ -0,0 +1,25 @@ +// ISC, Copyright 2017-2018 Jaco Greeff +// @flow + +require('./polyfill/textDecoder'); + +const decoder = new TextDecoder('utf-8'); + +/** + @name u8aToUtf8 + @signature u8aToUtf8 (value?: UInt8Array): string + @summary Creates a utf-8 string from a Uint8Array object. + @description + `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string. + @example + import { u8aToString } from '@polkadot/util'; + + u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello +*/ +module.exports = function u8aToUtf8 (value?: Uint8Array): string { + if (!value || !value.length) { + return ''; + } + + return decoder.decode(value); +}; diff --git a/src/u8a/toUtf8.spec.js b/src/u8a/toUtf8.spec.js new file mode 100644 index 000000000..ff4439d8f --- /dev/null +++ b/src/u8a/toUtf8.spec.js @@ -0,0 +1,25 @@ +// ISC, Copyright 2017-2018 Jaco Greeff + +const { u8aToUtf8 } = require('./index'); + +describe('u8aToUtf8', () => { + it('decodes to an empty string for undefined', () => { + expect( + u8aToUtf8() + ).toEqual(''); + }); + + it('decodes to an empty string for empty buffer', () => { + expect( + u8aToUtf8(new Uint8Array([])) + ).toEqual(''); + }); + + it('decodes the buffer correctly', () => { + expect( + u8aToUtf8( + new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]) + ) + ).toEqual('Привет, мир!'); + }); +}); diff --git a/yarn.lock b/yarn.lock index f011db9d1..66488697f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -69,9 +69,9 @@ dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.14.6": - version "0.14.6" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.14.6.tgz#4677e48d6a0ce692acdaea75a36ee93968a60e48" +"@polkadot/dev@^0.14.8": + version "0.14.8" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.14.8.tgz#8437d616c701a556cef977c9455a2257c89a66b8" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -95,7 +95,7 @@ eslint-plugin-promise "^3.6.0" eslint-plugin-react "^7.4.0" eslint-plugin-standard "^3.0.1" - flow-bin "^0.62.0" + flow-bin "^0.63.1" flow-copy-source "^1.2.1" jest "^22.0.0" makeshift "^1.1.0" @@ -925,6 +925,10 @@ bindings@^1.2.1: version "1.3.0" resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.3.0.tgz#b346f6ecf6a95f5a815c5839fc7cdb22502f1ed7" +bindings@~1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11" + bl@^1.0.0: version "1.2.1" resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.1.tgz#cac328f7bee45730d404b692203fcb590e172d5e" @@ -1294,6 +1298,13 @@ dashdash@^1.12.0: dependencies: assert-plus "^1.0.0" +deasync@^0.1.12: + version "0.1.12" + resolved "https://registry.yarnpkg.com/deasync/-/deasync-0.1.12.tgz#0159492a4133ab301d6c778cf01e74e63b10e549" + dependencies: + bindings "~1.2.1" + nan "^2.0.7" + debug@^2.2.0, debug@^2.6.8: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" @@ -1831,9 +1842,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.62.0: - version "0.62.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.62.0.tgz#14bca669a6e3f95c0bc0c2d1eb55ec4e98cb1d83" +flow-bin@^0.63.1: + version "0.63.1" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.63.1.tgz#ab00067c197169a5fb5b4996c8f6927b06694828" flow-copy-source@^1.2.1: version "1.2.1" @@ -3185,7 +3196,7 @@ mute-stream@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" -nan@^2.0.5, nan@^2.2.1, nan@^2.3.0: +nan@^2.0.5, nan@^2.0.7, nan@^2.2.1, nan@^2.3.0: version "2.8.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a"