Move util function from @polkadot/client (#62)

This commit is contained in:
Jaco Greeff
2018-01-11 17:48:38 +01:00
committed by GitHub
parent 561992bbda
commit f20035fe97
14 changed files with 283 additions and 11 deletions
+36
View File
@@ -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
```
+3 -2
View File
@@ -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"
}
+32
View File
@@ -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>): 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;
};
+25
View File
@@ -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/);
});
});
+26
View File
@@ -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);
};
+19
View File
@@ -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])
);
});
});
+5 -1
View File
@@ -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
};
+7
View File
@@ -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;
}
+27
View File
@@ -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();
});
});
+7
View File
@@ -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;
}
+27
View File
@@ -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();
});
});
+25
View File
@@ -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);
};
+25
View File
@@ -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('Привет, мир!');
});
});
+19 -8
View File
@@ -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"