Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@polkadot/util

Package Overview
Dependencies
Maintainers
1
Versions
1411
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/util - npm Package Compare versions

Comparing version 0.4.1 to 0.5.1

jsonrpc/index.js

13

bn/fromHex.js

@@ -11,3 +11,14 @@ 'use strict';

module.exports = function fromHex(value) {
/**
@name bnFromHex
@signature bnFromHex (value?: string): BN
@summary Creates a BN.js bignumber object from a hex string.
@description
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
@example
import { bnFromHex } from '@polkadot/util';
console.log('BN object', bnFromHex('0x123480001f'));
*/
module.exports = function bnFromHex(value) {
if (!value) {

@@ -14,0 +25,0 @@ return ZERO_BN;

@@ -7,2 +7,5 @@ 'use strict';

/**
@summary Utility methods to convert to and from `BN` objects
*/
module.exports = {

@@ -9,0 +12,0 @@ bnFromHex: bnFromHex,

@@ -11,3 +11,15 @@ 'use strict';

module.exports = function toHex(value) {
/**
@name bnToHex
@signature bnToHex (value?: BN): string
@summary Creates a hex value from a BN.js bignumber object.
@description
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error.
@example
import BN from 'bn.js';
import { bnToHex } from '@polkadot/util';
console.log('Hex value', bnToHex(new BN(123456));
*/
module.exports = function bnToHex(value) {
if (!value) {

@@ -14,0 +26,0 @@ return ZERO_STR;

@@ -9,3 +9,14 @@ 'use strict';

module.exports = function fromHex(value) {
/**
@name bufferFromHex
@signature bufferFromHex (value?: string): Buffer
@summary Creates a Buffer object from a hex string.
@description
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
@example
import { bufferFromHex } from '@polkadot/util';
console.log('Buffer object', bufferFromHex('0x123480001f'));
*/
module.exports = function bufferFromHex(value) {
if (!value) {

@@ -12,0 +23,0 @@ return EMPTY_BUFFER;

@@ -7,2 +7,5 @@ 'use strict';

/**
@summary Utility methods to convert to and from `Buffer` objects
*/
module.exports = {

@@ -9,0 +12,0 @@ bufferFromHex: bufferFromHex,

@@ -9,3 +9,14 @@ 'use strict';

module.exports = function toHex(value) {
/**
@name bufferToHex
@signature bufferToHex (value?: Buffer): string
@summary Creates a hex value from a Buffer object.
@description
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
@example
import { bufferToHex } from '@polkadot/util';
console.log('Hex value', bufferToHex(Buffer.from([1, 2, 3]));
*/
module.exports = function bufferToHex(value) {
if (!value) {

@@ -12,0 +23,0 @@ return ZERO_HEX;

@@ -6,3 +6,14 @@ 'use strict';

module.exports = function addPrefix(value) {
/**
@name hexAddPrefix
@signature hexAddPrefix (value: ?string): string
@summary Adds the `0x` prefix to string values.
@description
Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
@example
import { hexAddPrefix } from '@polkadot/util';
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
*/
module.exports = function hexAddPrefix(value) {
if (value && hasPrefix(value)) {

@@ -9,0 +20,0 @@ return value;

@@ -6,4 +6,15 @@ 'use strict';

module.exports = function hasPrefix(value) {
/**
@name hexHasPrefix
@signature hexHasPrefix (value: ?string): boolean
@summary Tests for the existence of a `0x` prefix.
@description
Checks for a valid hex input value and if the start matched `0x`
@example
import { hexHasPrefix } from '@polkadot/util';
console.log('has prefix', hexHasPrefix('0x1234')); // => true
*/
module.exports = function hexHasPrefix(value) {
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
};

@@ -8,2 +8,5 @@ 'use strict';

/**
@summary Internal utilities to create and test for hex values
*/
module.exports = {

@@ -10,0 +13,0 @@ hexAddPrefix: hexAddPrefix,

@@ -6,3 +6,14 @@ 'use strict';

module.exports = function stripPrefix(value) {
/**
@name hexStripPrefix
@signature hexStripPrefix (value: ?string): string
@summary Strips any leading `0x` prefix.
@description
Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
@example
import { hexStripPrefix } from '@polkadot/util';
console.log('stripped', hexStripPrefix('0x1234')); // => 1234
*/
module.exports = function hexStripPrefix(value) {
if (value && hasPrefix(value)) {

@@ -9,0 +20,0 @@ return value.substr(2);

@@ -14,4 +14,8 @@ 'use strict';

var is = require('./is');
var jsonrpc = require('./jsonrpc');
var keccak = require('./keccak');
module.exports = (0, _assign2.default)({}, bn, buffer, hex, is, keccak);
/**
@summary Utility methods for this package are split into groups
*/
module.exports = (0, _assign2.default)({}, bn, buffer, hex, is, jsonrpc, keccak);

@@ -6,4 +6,16 @@ 'use strict';

/**
@name isBN
@signature isBN (value: any): boolean
@summary Tests for a `BN` object instance.
@description
Checks to see if the input object is an instance of `BN` (bn.js).
@example
import BN from 'bn.js';
import { isBN } from '@polkadot/util';
console.log('isBN', isBN(new BN(1))); // => true
*/
module.exports = function isBN(value) {
return BN.isBN(value);
};

@@ -6,4 +6,15 @@ 'use strict';

/**
@name isBuffer
@signature isBuffer (value: any): boolean
@summary Tests for a `Buffer` object instance.
@description
Checks to see if the input object is an instance of `Buffer`.
@example
import { isBuffer } from '@polkadot/util';
console.log('isBuffer', isBuffer(Buffer.from([]))); // => true
*/
module.exports = function isBuffer(value) {
return isInstanceOf(value, Buffer);
};
'use strict';
// ISC, Copyright 2017 Jaco Greeff
/**
@name isFunction
@signature isFunction (value: any): boolean
@summary Tests for a `function`.
@description
Checks to see if the input value is a JavaScript function.
@example
import { isFunction } from '@polkadot/util';
console.log('isFunction', isFunction(() => false)); // => true
*/
module.exports = function isFunction(value) {
return typeof value === 'function';
};

@@ -8,4 +8,15 @@ 'use strict';

/**
@name isHex
@signature isHex (value: any): boolean
@summary Tests for a hex string.
@description
Checks to see if the input value is a `0x` prefixed hex string.
@example
import { isHex } from '@polkadot/util';
console.log('isHex', isHex('0x1234')); // => true
*/
module.exports = function isHex(value) {
return isString(value) && HEX_REGEX.test(value);
};

@@ -12,2 +12,5 @@ 'use strict';

/**
@summary Type checking utilities
*/
module.exports = {

@@ -14,0 +17,0 @@ isBN: isBN,

"use strict";
// ISC, Copyright 2017 Jaco Greeff
/**
@name isInstanceOf
@signature isInstanceOf (value: any, clazz: any): boolean
@summary Tests for a instance of a class.
@description
Checks to see if the input value is an instance of the test class.
@example
import { isInstanceOf } from '@polkadot/util';
console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true
*/
module.exports = function isInstanceOf(value, clazz) {
return value instanceof clazz;
};
'use strict';
// ISC, Copyright 2017 Jaco Greeff
/**
@name isNumber
@signature isNumber (value: any): boolean
@summary Tests for a JavaScript number.
@description
Checks to see if the input value is a valid number.
@example
import { isNumber } from '@polkadot/util';
console.log('isNumber', isNumber(1234)); // => true
*/
module.exports = function isNumber(value) {
return typeof value === 'number';
};
'use strict';
// ISC, Copyright 2017 Jaco Greeff
/**
@name isString
@signature isString (value: any): boolean
@summary Tests for a string.
@description
Checks to see if the input value is a JavaScript string.
@example
import { isString } from '@polkadot/util';
console.log('isString', isString('test')); // => true
*/
module.exports = function isString(value) {
return typeof value === 'string';
};
'use strict';
// ISC, Copyright 2017 Jaco Greeff
/**
@name isUndefined
@signature isUndefined (value: any): boolean
@summary Tests for a `undefined` values.
@description
Checks to see if the input value is `undefined`.
@example
import { isUndefined } from '@polkadot/util';
console.log('isUndefined', isUndefined(void(0))); // => true
*/
module.exports = function isUndefined(value) {
return typeof value === 'undefined';
};

@@ -6,4 +6,15 @@ 'use strict';

/**
@name keccakAsBuffer
@signature keccakAsBuffer (value: Buffer | string): Buffer
@summary Creates a keccak Buffer from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a `Buffer`.
@example
import { keccakAsBuffer } from '@polkadot/util';
console.log('asBuffer', keccakAsBuffer('123')) // => Buffer
*/
module.exports = function keccakAsBuffer(value) {
return createKeccak('keccak256').update(value).digest();
};

@@ -7,4 +7,15 @@ 'use strict';

/**
@name keccakAsHex
@signature keccakAsHex (value: Buffer | string): string
@summary Creates a keccak hex string from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a `0x` prefixed hex string.
@example
import { keccakAsHex } from '@polkadot/util';
console.log('asHex', keccakAsHex('123')) // => 0x...
*/
module.exports = function keccakAsHex(value) {
return bufferToHex(keccakAsBuffer(value));
};

@@ -6,4 +6,15 @@ 'use strict';

/**
@name keccakAsString
@signature keccakAsString (value: Buffer | string): string
@summary Creates a keccak string from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a non-prefixed string.
@example
import { keccakAsString } from '@polkadot/util';
console.log('asString', keccakAsString('123')) // => string
*/
module.exports = function keccakAsString(value) {
return keccakAsBuffer(value).toString('hex');
};

@@ -8,2 +8,5 @@ 'use strict';

/**
@summary Create Keccak256 values as hex, string & buffer output
*/
module.exports = {

@@ -10,0 +13,0 @@ keccakAsBuffer: keccakAsBuffer,

36

package.json
{
"name": "@polkadot/util",
"version": "0.4.1",
"version": "0.5.1",
"description": "A collection of useful utilities for @polkadot",

@@ -27,38 +27,17 @@ "main": "index.js",

"scripts": {
"build": "npm run build:js",
"build:js": "rimraf lib && babel src --out-dir lib --ignore *.spec.js,types.js",
"check": "npm run check:flow && npm run check:lint",
"build": "npm run build:js && npm run build:md",
"build:js": "rimraf lib && babel src --out-dir build --ignore *.spec.js",
"build:md": "polkadot-dev-build-docs",
"check": "npm run check:lint && npm run check:flow",
"check:flow": "flow check",
"check:lint": "eslint src",
"ci:makeshift": "makeshift",
"ci:coveralls": "coveralls < coverage/lcov.info",
"test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"@polkadot/dev": "^0.1.5",
"@polkadot/jsonrpc": "^0.3.4",
"babel-istanbul": "^0.12.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-stage-0": "^6.24.1",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"eslint-config-semistandard": "^11.0.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"flow-bin": "^0.59.0",
"makeshift": "^1.1.0",
"mocha": "^4.0.1",
"rimraf": "^2.6.2",
"sinon": "^4.1.2",

@@ -68,3 +47,2 @@ "sinon-chai": "^2.14.0"

"dependencies": {
"babel-runtime": "^6.26.0",
"bn.js": "^4.11.8",

@@ -71,0 +49,0 @@ "keccak": "^1.3.0"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc