UNPKG

6.41 kBJavaScriptView Raw
1"use strict";
2/*
3The MIT License
4
5Copyright (c) 2016 Nick Dodson. nickdodson.com
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE
24 */
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.isHexString = exports.getKeys = exports.fromAscii = exports.fromUtf8 = exports.toAscii = exports.arrayContainsArray = exports.getBinarySize = exports.padToEven = exports.stripHexPrefix = exports.isHexPrefixed = void 0;
27/**
28 * Returns a `Boolean` on whether or not the a `String` starts with '0x'
29 * @param str the string input value
30 * @return a boolean if it is or is not hex prefixed
31 * @throws if the str input is not a string
32 */
33function isHexPrefixed(str) {
34 if (typeof str !== 'string') {
35 throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`);
36 }
37 return str[0] === '0' && str[1] === 'x';
38}
39exports.isHexPrefixed = isHexPrefixed;
40/**
41 * Removes '0x' from a given `String` if present
42 * @param str the string value
43 * @returns the string without 0x prefix
44 */
45const stripHexPrefix = (str) => {
46 if (typeof str !== 'string')
47 throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`);
48 return isHexPrefixed(str) ? str.slice(2) : str;
49};
50exports.stripHexPrefix = stripHexPrefix;
51/**
52 * Pads a `String` to have an even length
53 * @param value
54 * @return output
55 */
56function padToEven(value) {
57 let a = value;
58 if (typeof a !== 'string') {
59 throw new Error(`[padToEven] value must be type 'string', received ${typeof a}`);
60 }
61 if (a.length % 2)
62 a = `0${a}`;
63 return a;
64}
65exports.padToEven = padToEven;
66/**
67 * Get the binary size of a string
68 * @param str
69 * @returns the number of bytes contained within the string
70 */
71function getBinarySize(str) {
72 if (typeof str !== 'string') {
73 throw new Error(`[getBinarySize] method requires input type 'string', recieved ${typeof str}`);
74 }
75 return Buffer.byteLength(str, 'utf8');
76}
77exports.getBinarySize = getBinarySize;
78/**
79 * Returns TRUE if the first specified array contains all elements
80 * from the second one. FALSE otherwise.
81 *
82 * @param superset
83 * @param subset
84 *
85 */
86function arrayContainsArray(superset, subset, some) {
87 if (Array.isArray(superset) !== true) {
88 throw new Error(`[arrayContainsArray] method requires input 'superset' to be an array, got type '${typeof superset}'`);
89 }
90 if (Array.isArray(subset) !== true) {
91 throw new Error(`[arrayContainsArray] method requires input 'subset' to be an array, got type '${typeof subset}'`);
92 }
93 return subset[some ? 'some' : 'every']((value) => superset.indexOf(value) >= 0);
94}
95exports.arrayContainsArray = arrayContainsArray;
96/**
97 * Should be called to get ascii from its hex representation
98 *
99 * @param string in hex
100 * @returns ascii string representation of hex value
101 */
102function toAscii(hex) {
103 let str = '';
104 let i = 0;
105 const l = hex.length;
106 if (hex.substring(0, 2) === '0x')
107 i = 2;
108 for (; i < l; i += 2) {
109 const code = parseInt(hex.substr(i, 2), 16);
110 str += String.fromCharCode(code);
111 }
112 return str;
113}
114exports.toAscii = toAscii;
115/**
116 * Should be called to get hex representation (prefixed by 0x) of utf8 string
117 *
118 * @param string
119 * @param optional padding
120 * @returns hex representation of input string
121 */
122function fromUtf8(stringValue) {
123 const str = Buffer.from(stringValue, 'utf8');
124 return `0x${padToEven(str.toString('hex')).replace(/^0+|0+$/g, '')}`;
125}
126exports.fromUtf8 = fromUtf8;
127/**
128 * Should be called to get hex representation (prefixed by 0x) of ascii string
129 *
130 * @param string
131 * @param optional padding
132 * @returns hex representation of input string
133 */
134function fromAscii(stringValue) {
135 let hex = '';
136 for (let i = 0; i < stringValue.length; i++) {
137 const code = stringValue.charCodeAt(i);
138 const n = code.toString(16);
139 hex += n.length < 2 ? `0${n}` : n;
140 }
141 return `0x${hex}`;
142}
143exports.fromAscii = fromAscii;
144/**
145 * Returns the keys from an array of objects.
146 * @example
147 * ```js
148 * getKeys([{a: '1', b: '2'}, {a: '3', b: '4'}], 'a') => ['1', '3']
149 *````
150 * @param params
151 * @param key
152 * @param allowEmpty
153 * @returns output just a simple array of output keys
154 */
155function getKeys(params, key, allowEmpty) {
156 if (!Array.isArray(params)) {
157 throw new Error(`[getKeys] method expects input 'params' to be an array, got ${typeof params}`);
158 }
159 if (typeof key !== 'string') {
160 throw new Error(`[getKeys] method expects input 'key' to be type 'string', got ${typeof params}`);
161 }
162 const result = [];
163 for (let i = 0; i < params.length; i++) {
164 let value = params[i][key];
165 if (allowEmpty && !value) {
166 value = '';
167 }
168 else if (typeof value !== 'string') {
169 throw new Error(`invalid abi - expected type 'string', received ${typeof value}`);
170 }
171 result.push(value);
172 }
173 return result;
174}
175exports.getKeys = getKeys;
176/**
177 * Is the string a hex string.
178 *
179 * @param value
180 * @param length
181 * @returns output the string is a hex string
182 */
183function isHexString(value, length) {
184 if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/))
185 return false;
186 if (length && value.length !== 2 + 2 * length)
187 return false;
188 return true;
189}
190exports.isHexString = isHexString;
191//# sourceMappingURL=internal.js.map
\No newline at end of file