Comparing version 1.0.10 to 1.0.11
@@ -7,2 +7,11 @@ /** | ||
const HEX_FORMAT = /^(0x)?([0-9a-fA-F]+)$/; | ||
const HEX_MAP = "0123456789abcdef"; | ||
const HEX_MAP_R = { | ||
"0":0, "1":1, "2":2, "3":3, | ||
"4":4, "5":5, "6":6, "7":7, | ||
"8":8, "9":9, "a":10, "b":11, | ||
"c":12, "d":13, "e":14, "f":15 | ||
}; | ||
Object.defineProperty(ArrayBuffer.prototype, 'bytes', { | ||
@@ -12,2 +21,16 @@ get:function(){ return new Uint8Array(this); }, | ||
}); | ||
Object.defineProperty(ArrayBuffer.prototype, 'toString', { | ||
value:function(padding=false){ | ||
const bytes = new Uint8Array(this); | ||
let hex = ''; | ||
for(let i=0; i<bytes.length; i++) { | ||
const value = bytes[i]; | ||
hex += HEX_MAP[(value&0xF0)>>>4] + HEX_MAP[value&0x0F]; | ||
} | ||
return padding ? hex : hex.replace(/^0+/, ''); | ||
}, | ||
configurable:true, enumerable:false, writable:true | ||
}); | ||
@@ -118,6 +141,32 @@ ArrayBuffer.extract = function(input) { | ||
if ( typeof input === "string" ) { | ||
return UTF8Encode(input).buffer; | ||
if ( conversion_info === "hex" ) { | ||
const matches = input.match(HEX_FORMAT); | ||
if ( !matches ) { | ||
throw new RangeError( "Input argument is not a valid hex string!" ); | ||
} | ||
let [,,hex_string] = matches; | ||
if ( hex_string.length % 2 === 0 ) { | ||
hex_string = hex_string.toLowerCase(); | ||
} | ||
else { | ||
hex_string = '0' + hex_string.toLowerCase(); | ||
} | ||
const buff = new Uint8Array((hex_string.length/2)|0); | ||
for ( let i=0; i<buff.length; i++ ) { | ||
const offset = i * 2; | ||
buff[i] = HEX_MAP_R[hex_string[offset]]<<4 | (HEX_MAP_R[hex_string[offset+1]] & 0x0F); | ||
} | ||
return buff.buffer; | ||
} | ||
else { | ||
return UTF8Encode(input).buffer; | ||
} | ||
} | ||
throw new TypeError( "Cannot convert given input data into array buffer" ); | ||
throw new TypeError( "Cannot convert given input data into array buffer!" ); | ||
}; |
{ | ||
"name": "extes", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "A tiny library that extends native js with some handy tools", | ||
@@ -5,0 +5,0 @@ "main": "index.mjs", |
16792
541