@erebos/hex
Advanced tools
Comparing version 0.5.4 to 0.7.0
@@ -30,2 +30,6 @@ "use strict"; | ||
var toHexValue = function toHexValue(value) { | ||
return hexValueType('0x' + Buffer.from(value).toString('hex')); | ||
}; | ||
var Hex = | ||
@@ -55,3 +59,9 @@ /*#__PURE__*/ | ||
}; | ||
this._value = hexValueType('0x' + Buffer.from(inputValue).toString('hex')); | ||
this._value = toHexValue(inputValue); | ||
} else if (Array.isArray(inputValue)) { | ||
this._input = { | ||
type: 'bytesArray', | ||
value: inputValue | ||
}; | ||
this._value = toHexValue(inputValue); | ||
} else if (Buffer.isBuffer(inputValue)) { | ||
@@ -62,3 +72,3 @@ this._input = { | ||
}; | ||
this._value = hexValueType('0x' + inputValue.toString('hex')); | ||
this._value = toHexValue(inputValue); | ||
} else if (typeof inputValue === 'object') { | ||
@@ -69,3 +79,3 @@ this._input = { | ||
}; | ||
this._value = hexValueType('0x' + Buffer.from(JSON.stringify(inputValue)).toString('hex')); | ||
this._value = toHexValue(JSON.stringify(inputValue)); | ||
} else { | ||
@@ -88,2 +98,6 @@ throw new Error('Invalid input'); | ||
case 'bytesArray': | ||
case 'string': | ||
return Buffer.from(this._input.value); | ||
case 'hex': | ||
@@ -95,5 +109,2 @@ return fromHexValue(this._input.value); | ||
case 'string': | ||
return Buffer.from(this._input.value); | ||
default: | ||
@@ -104,19 +115,8 @@ return Buffer.alloc(0); | ||
_proto.toBytesArray = function toBytesArray() { | ||
return this._input.type === 'bytesArray' ? this._input.value : Array.from(this.toBuffer()); | ||
}; | ||
_proto.toObject = function toObject() { | ||
switch (this._input.type) { | ||
case 'buffer': | ||
return JSON.parse(this._input.value.toString()); | ||
case 'hex': | ||
return JSON.parse(fromHexValue(this._input.value).toString()); | ||
case 'object': | ||
return this._input.value; | ||
case 'string': | ||
return JSON.parse(this._input.value); | ||
default: | ||
return {}; | ||
} | ||
return this._input.type === 'object' ? this._input.value : JSON.parse(this.toString()); | ||
}; | ||
@@ -129,2 +129,5 @@ | ||
case 'bytesArray': | ||
return Buffer.from(this._input.value).toString(); | ||
case 'hex': | ||
@@ -131,0 +134,0 @@ return fromHexValue(this._input.value).toString(); |
@@ -12,2 +12,7 @@ import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
}; | ||
var toHexValue = function toHexValue(value) { | ||
return hexValueType('0x' + Buffer.from(value).toString('hex')); | ||
}; | ||
export var Hex = | ||
@@ -38,3 +43,9 @@ /*#__PURE__*/ | ||
}; | ||
this._value = hexValueType('0x' + Buffer.from(inputValue).toString('hex')); | ||
this._value = toHexValue(inputValue); | ||
} else if (Array.isArray(inputValue)) { | ||
this._input = { | ||
type: 'bytesArray', | ||
value: inputValue | ||
}; | ||
this._value = toHexValue(inputValue); | ||
} else if (Buffer.isBuffer(inputValue)) { | ||
@@ -45,3 +56,3 @@ this._input = { | ||
}; | ||
this._value = hexValueType('0x' + inputValue.toString('hex')); | ||
this._value = toHexValue(inputValue); | ||
} else if (typeof inputValue === 'object') { | ||
@@ -52,3 +63,3 @@ this._input = { | ||
}; | ||
this._value = hexValueType('0x' + Buffer.from(JSON.stringify(inputValue)).toString('hex')); | ||
this._value = toHexValue(JSON.stringify(inputValue)); | ||
} else { | ||
@@ -71,2 +82,6 @@ throw new Error('Invalid input'); | ||
case 'bytesArray': | ||
case 'string': | ||
return Buffer.from(this._input.value); | ||
case 'hex': | ||
@@ -78,5 +93,2 @@ return fromHexValue(this._input.value); | ||
case 'string': | ||
return Buffer.from(this._input.value); | ||
default: | ||
@@ -87,19 +99,8 @@ return Buffer.alloc(0); | ||
_proto.toBytesArray = function toBytesArray() { | ||
return this._input.type === 'bytesArray' ? this._input.value : Array.from(this.toBuffer()); | ||
}; | ||
_proto.toObject = function toObject() { | ||
switch (this._input.type) { | ||
case 'buffer': | ||
return JSON.parse(this._input.value.toString()); | ||
case 'hex': | ||
return JSON.parse(fromHexValue(this._input.value).toString()); | ||
case 'object': | ||
return this._input.value; | ||
case 'string': | ||
return JSON.parse(this._input.value); | ||
default: | ||
return {}; | ||
} | ||
return this._input.type === 'object' ? this._input.value : JSON.parse(this.toString()); | ||
}; | ||
@@ -112,2 +113,5 @@ | ||
case 'bytesArray': | ||
return Buffer.from(this._input.value).toString(); | ||
case 'hex': | ||
@@ -114,0 +118,0 @@ return fromHexValue(this._input.value).toString(); |
{ | ||
"name": "@erebos/hex", | ||
"version": "0.5.4", | ||
"version": "0.7.0", | ||
"description": "Hex conversion utilities", | ||
@@ -26,7 +26,7 @@ "repository": "git@github.com:MainframeHQ/erebos.git", | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0" | ||
"@babel/runtime": "^7.3.4" | ||
}, | ||
"devDependencies": { | ||
"flow-bin": "^0.88.0" | ||
"flow-bin": "^0.95.1" | ||
} | ||
} |
@@ -7,3 +7,3 @@ /// <reference types="node" /> | ||
export type hexInput = hexValue | string | Object | Buffer | ||
export type hexInput = hexValue | string | Object | Buffer | Array<number> | ||
@@ -16,2 +16,3 @@ export function isHexValue(input: any): boolean | ||
| { type: 'buffer'; value: Buffer } | ||
| { type: 'bytesArray'; value: Array<number> } | ||
| { type: 'hex'; value: hexValue } | ||
@@ -26,2 +27,3 @@ | { type: 'object'; value: Object } | ||
toBuffer(): Buffer | ||
toBytesArray(): Array<number> | ||
toObject(): Object | ||
@@ -28,0 +30,0 @@ toString(): string |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15290
253
Updated@babel/runtime@^7.3.4