191 lines
6.3 KiB
JavaScript
191 lines
6.3 KiB
JavaScript
"use strict";
|
|
/*
|
|
The MIT License
|
|
|
|
Copyright (c) 2016 Nick Dodson. nickdodson.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.isHexString = exports.getKeys = exports.fromAscii = exports.fromUtf8 = exports.toAscii = exports.arrayContainsArray = exports.getBinarySize = exports.padToEven = exports.stripHexPrefix = exports.isHexPrefixed = void 0;
|
|
/**
|
|
* Returns a `Boolean` on whether or not the a `String` starts with '0x'
|
|
* @param str the string input value
|
|
* @return a boolean if it is or is not hex prefixed
|
|
* @throws if the str input is not a string
|
|
*/
|
|
function isHexPrefixed(str) {
|
|
if (typeof str !== 'string') {
|
|
throw new Error("[isHexPrefixed] input must be type 'string', received type " + typeof str);
|
|
}
|
|
return str[0] === '0' && str[1] === 'x';
|
|
}
|
|
exports.isHexPrefixed = isHexPrefixed;
|
|
/**
|
|
* Removes '0x' from a given `String` if present
|
|
* @param str the string value
|
|
* @returns the string without 0x prefix
|
|
*/
|
|
var stripHexPrefix = function (str) {
|
|
if (typeof str !== 'string')
|
|
throw new Error("[stripHexPrefix] input must be type 'string', received " + typeof str);
|
|
return isHexPrefixed(str) ? str.slice(2) : str;
|
|
};
|
|
exports.stripHexPrefix = stripHexPrefix;
|
|
/**
|
|
* Pads a `String` to have an even length
|
|
* @param value
|
|
* @return output
|
|
*/
|
|
function padToEven(value) {
|
|
var a = value;
|
|
if (typeof a !== 'string') {
|
|
throw new Error("[padToEven] value must be type 'string', received " + typeof a);
|
|
}
|
|
if (a.length % 2)
|
|
a = "0" + a;
|
|
return a;
|
|
}
|
|
exports.padToEven = padToEven;
|
|
/**
|
|
* Get the binary size of a string
|
|
* @param str
|
|
* @returns the number of bytes contained within the string
|
|
*/
|
|
function getBinarySize(str) {
|
|
if (typeof str !== 'string') {
|
|
throw new Error("[getBinarySize] method requires input type 'string', recieved " + typeof str);
|
|
}
|
|
return Buffer.byteLength(str, 'utf8');
|
|
}
|
|
exports.getBinarySize = getBinarySize;
|
|
/**
|
|
* Returns TRUE if the first specified array contains all elements
|
|
* from the second one. FALSE otherwise.
|
|
*
|
|
* @param superset
|
|
* @param subset
|
|
*
|
|
*/
|
|
function arrayContainsArray(superset, subset, some) {
|
|
if (Array.isArray(superset) !== true) {
|
|
throw new Error("[arrayContainsArray] method requires input 'superset' to be an array, got type '" + typeof superset + "'");
|
|
}
|
|
if (Array.isArray(subset) !== true) {
|
|
throw new Error("[arrayContainsArray] method requires input 'subset' to be an array, got type '" + typeof subset + "'");
|
|
}
|
|
return subset[some ? 'some' : 'every'](function (value) { return superset.indexOf(value) >= 0; });
|
|
}
|
|
exports.arrayContainsArray = arrayContainsArray;
|
|
/**
|
|
* Should be called to get ascii from its hex representation
|
|
*
|
|
* @param string in hex
|
|
* @returns ascii string representation of hex value
|
|
*/
|
|
function toAscii(hex) {
|
|
var str = '';
|
|
var i = 0;
|
|
var l = hex.length;
|
|
if (hex.substring(0, 2) === '0x')
|
|
i = 2;
|
|
for (; i < l; i += 2) {
|
|
var code = parseInt(hex.substr(i, 2), 16);
|
|
str += String.fromCharCode(code);
|
|
}
|
|
return str;
|
|
}
|
|
exports.toAscii = toAscii;
|
|
/**
|
|
* Should be called to get hex representation (prefixed by 0x) of utf8 string
|
|
*
|
|
* @param string
|
|
* @param optional padding
|
|
* @returns hex representation of input string
|
|
*/
|
|
function fromUtf8(stringValue) {
|
|
var str = Buffer.from(stringValue, 'utf8');
|
|
return "0x" + padToEven(str.toString('hex')).replace(/^0+|0+$/g, '');
|
|
}
|
|
exports.fromUtf8 = fromUtf8;
|
|
/**
|
|
* Should be called to get hex representation (prefixed by 0x) of ascii string
|
|
*
|
|
* @param string
|
|
* @param optional padding
|
|
* @returns hex representation of input string
|
|
*/
|
|
function fromAscii(stringValue) {
|
|
var hex = '';
|
|
for (var i = 0; i < stringValue.length; i++) {
|
|
var code = stringValue.charCodeAt(i);
|
|
var n = code.toString(16);
|
|
hex += n.length < 2 ? "0" + n : n;
|
|
}
|
|
return "0x" + hex;
|
|
}
|
|
exports.fromAscii = fromAscii;
|
|
/**
|
|
* Returns the keys from an array of objects.
|
|
* @example
|
|
* ```js
|
|
* getKeys([{a: '1', b: '2'}, {a: '3', b: '4'}], 'a') => ['1', '3']
|
|
*````
|
|
* @param params
|
|
* @param key
|
|
* @param allowEmpty
|
|
* @returns output just a simple array of output keys
|
|
*/
|
|
function getKeys(params, key, allowEmpty) {
|
|
if (!Array.isArray(params)) {
|
|
throw new Error("[getKeys] method expects input 'params' to be an array, got " + typeof params);
|
|
}
|
|
if (typeof key !== 'string') {
|
|
throw new Error("[getKeys] method expects input 'key' to be type 'string', got " + typeof params);
|
|
}
|
|
var result = [];
|
|
for (var i = 0; i < params.length; i++) {
|
|
var value = params[i][key];
|
|
if (allowEmpty && !value) {
|
|
value = '';
|
|
}
|
|
else if (typeof value !== 'string') {
|
|
throw new Error("invalid abi - expected type 'string', received " + typeof value);
|
|
}
|
|
result.push(value);
|
|
}
|
|
return result;
|
|
}
|
|
exports.getKeys = getKeys;
|
|
/**
|
|
* Is the string a hex string.
|
|
*
|
|
* @param value
|
|
* @param length
|
|
* @returns output the string is a hex string
|
|
*/
|
|
function isHexString(value, length) {
|
|
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/))
|
|
return false;
|
|
if (length && value.length !== 2 + 2 * length)
|
|
return false;
|
|
return true;
|
|
}
|
|
exports.isHexString = isHexString;
|
|
//# sourceMappingURL=internal.js.map
|