Comparing version 1.9.5 to 1.9.6
@@ -17,7 +17,9 @@ /** | ||
const configurable=true, writable=true, enumerable=false; | ||
Object.defineProperty(ArrayBuffer.prototype, 'bytes', { | ||
get:function(){ return new Uint8Array(this); }, | ||
configurable:true, enumerable:false | ||
configurable, enumerable, | ||
get:function(){ return new Uint8Array(this); } | ||
}); | ||
Object.defineProperty(ArrayBuffer.prototype, 'toString', { | ||
configurable, writable, enumerable, | ||
value:function(format=16, padding=false){ | ||
@@ -49,6 +51,6 @@ const bytes = new Uint8Array(this); | ||
return padding ? result : result.replace(/^0+/, ''); | ||
}, | ||
configurable:true, enumerable:false, writable:true | ||
} | ||
}); | ||
Object.defineProperty(ArrayBuffer.prototype, 'compare', { | ||
configurable, writable, enumerable, | ||
value:function(array_buffer) { | ||
@@ -68,173 +70,181 @@ if ( !(array_buffer instanceof ArrayBuffer) ) { | ||
return 0; | ||
}, | ||
configurable:true, enumerable:false, writable:true | ||
} | ||
}); | ||
ArrayBuffer.extract = function(input) { | ||
if ( typeof Buffer !== "undefined" ) { | ||
if ( input instanceof Buffer ) { | ||
let buff = Buffer.alloc(input.length); | ||
input.copy(buff, 0); | ||
return buff.buffer; | ||
Object.defineProperty(ArrayBuffer, 'extract', { | ||
configurable, writable, enumerable, | ||
value: function(input) { | ||
if ( typeof Buffer !== "undefined" ) { | ||
if ( input instanceof Buffer ) { | ||
let buff = Buffer.alloc(input.length); | ||
input.copy(buff, 0); | ||
return buff.buffer; | ||
} | ||
} | ||
if ( ArrayBuffer.isView(input) ) { | ||
return input.buffer; | ||
} | ||
if ( input instanceof ArrayBuffer ) { | ||
return input; | ||
} | ||
throw new TypeError( "Cannot convert given input data into array buffer" ); | ||
} | ||
if ( ArrayBuffer.isView(input) ) { | ||
return input.buffer; | ||
} | ||
if ( input instanceof ArrayBuffer ) { | ||
return input; | ||
} | ||
throw new TypeError( "Cannot convert given input data into array buffer" ); | ||
}; | ||
ArrayBuffer.from = function(input, conversion_info=null) { | ||
if ( typeof Buffer !== "undefined" ) { | ||
if ( input instanceof Buffer ) { | ||
let buff = Buffer.alloc(input.length); | ||
input.copy(buff, 0); | ||
return buff.buffer; | ||
}); | ||
Object.defineProperty(ArrayBuffer, 'from', { | ||
configurable, writable, enumerable, | ||
value: function(input, conversion_info=null) { | ||
if ( typeof Buffer !== "undefined" ) { | ||
if ( input instanceof Buffer ) { | ||
let buff = Buffer.alloc(input.length); | ||
input.copy(buff, 0); | ||
return buff.buffer; | ||
} | ||
} | ||
} | ||
if ( ArrayBuffer.isView(input) ) { | ||
return input.buffer.slice(0); | ||
} | ||
if ( input instanceof ArrayBuffer ) { | ||
return input.slice(0); | ||
} | ||
if ( Array.isArray(input) ) { | ||
const buffer = new Uint8Array(input); | ||
return buffer.buffer; | ||
} | ||
if ( typeof input === "number" ) { | ||
let data_buffer = null; | ||
switch(conversion_info) { | ||
case 'int8': | ||
data_buffer = new Int8Array([input]); | ||
break; | ||
if ( ArrayBuffer.isView(input) ) { | ||
return input.buffer.slice(0); | ||
} | ||
if ( input instanceof ArrayBuffer ) { | ||
return input.slice(0); | ||
} | ||
if ( Array.isArray(input) ) { | ||
const buffer = new Uint8Array(input); | ||
return buffer.buffer; | ||
} | ||
if ( typeof input === "number" ) { | ||
let data_buffer = null; | ||
switch(conversion_info) { | ||
case 'int8': | ||
data_buffer = new Int8Array([input]); | ||
break; | ||
case 'uint8': | ||
data_buffer = new Uint8Array([input]); | ||
break; | ||
case 'int16': | ||
data_buffer = new Int16Array([input]); | ||
break; | ||
case 'uint16': | ||
data_buffer = new Uint16Array([input]); | ||
break; | ||
case 'int32': | ||
data_buffer = new Int32Array([input]); | ||
break; | ||
case 'int64':{ | ||
const negative = input < 0; | ||
if ( negative ) { input = -input; } | ||
let upper = Math.floor(input/0xFFFFFFFF); | ||
let lower = input & 0xFFFFFFFF; | ||
if ( negative ) { | ||
lower = ((~lower)>>>0) + 1; | ||
upper = (~upper) + Math.floor(lower/0xFFFFFFFF); | ||
} | ||
data_buffer = new Uint32Array([lower, upper]); | ||
break; | ||
} | ||
case 'uint64': { | ||
const upper = Math.floor(input/0xFFFFFFFF); | ||
const lower = input & 0xFFFFFFFF; | ||
data_buffer = new Uint32Array([lower, upper]); | ||
break; | ||
} | ||
case 'float32': | ||
data_buffer = new Float32Array([input]); | ||
break; | ||
case 'float64': | ||
data_buffer = new Float64Array([input]); | ||
break; | ||
case 'uint32': | ||
default: | ||
data_buffer = new Uint32Array([input]); | ||
break; | ||
} | ||
case 'uint8': | ||
data_buffer = new Uint8Array([input]); | ||
break; | ||
return data_buffer.buffer; | ||
} | ||
if ( typeof input === "string" ) { | ||
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(); | ||
} | ||
case 'int16': | ||
data_buffer = new Int16Array([input]); | ||
break; | ||
case 'uint16': | ||
data_buffer = new Uint16Array([input]); | ||
break; | ||
case 'int32': | ||
data_buffer = new Int32Array([input]); | ||
break; | ||
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); | ||
} | ||
case 'int64':{ | ||
const negative = input < 0; | ||
if ( negative ) { input = -input; } | ||
return buff.buffer; | ||
} | ||
else | ||
if ( conversion_info === "bits" ) { | ||
const matches = input.match(BIT_FORMAT); | ||
if ( !matches ) { | ||
throw new RangeError( "Input argument is not a valid bit string!" ); | ||
} | ||
let upper = Math.floor(input/0xFFFFFFFF); | ||
let lower = input & 0xFFFFFFFF; | ||
if ( negative ) { | ||
lower = ((~lower)>>>0) + 1; | ||
upper = (~upper) + Math.floor(lower/0xFFFFFFFF); | ||
let [,,bit_string] = matches; | ||
if ( bit_string.length % 8 !== 0 ) { | ||
bit_string = '0'.repeat(bit_string.length%8) + bit_string; | ||
} | ||
data_buffer = new Uint32Array([lower, upper]); | ||
break; | ||
const buff = new Uint8Array((bit_string.length/8)|0); | ||
for ( let i=0; i<buff.length; i++ ) { | ||
const offset = i * 8; | ||
let value = (bit_string[offset]==='1'?1:0); | ||
for (let k=1; k<8; k++) { | ||
value = (value << 1) | (bit_string[offset + k]==='1'?1:0); | ||
} | ||
buff[i] = value; | ||
} | ||
return buff.buffer; | ||
} | ||
case 'uint64': { | ||
const upper = Math.floor(input/0xFFFFFFFF); | ||
const lower = input & 0xFFFFFFFF; | ||
data_buffer = new Uint32Array([lower, upper]); | ||
break; | ||
else { | ||
return UTF8Encode(input).buffer; | ||
} | ||
case 'float32': | ||
data_buffer = new Float32Array([input]); | ||
break; | ||
case 'float64': | ||
data_buffer = new Float64Array([input]); | ||
break; | ||
case 'uint32': | ||
default: | ||
data_buffer = new Uint32Array([input]); | ||
break; | ||
} | ||
return data_buffer.buffer; | ||
throw new TypeError( "Cannot convert given input data into array buffer!" ); | ||
} | ||
if ( typeof input === "string" ) { | ||
if ( conversion_info === "hex" ) { | ||
const matches = input.match(HEX_FORMAT); | ||
if ( !matches ) { | ||
throw new RangeError( "Input argument is not a valid hex string!" ); | ||
} | ||
}); | ||
Object.defineProperty(ArrayBuffer, 'compare', { | ||
configurable, writable, enumerable, | ||
value: function(a, b) { | ||
if ( !(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer) ) { | ||
throw new TypeError("ArrayBuffer.compare only accepts two array buffers!"); | ||
} | ||
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 | ||
if ( conversion_info === "bits" ) { | ||
const matches = input.match(BIT_FORMAT); | ||
if ( !matches ) { | ||
throw new RangeError( "Input argument is not a valid bit string!" ); | ||
} | ||
let [,,bit_string] = matches; | ||
if ( bit_string.length % 8 !== 0 ) { | ||
bit_string = '0'.repeat(bit_string.length%8) + bit_string; | ||
} | ||
const buff = new Uint8Array((bit_string.length/8)|0); | ||
for ( let i=0; i<buff.length; i++ ) { | ||
const offset = i * 8; | ||
let value = (bit_string[offset]==='1'?1:0); | ||
for (let k=1; k<8; k++) { | ||
value = (value << 1) | (bit_string[offset + k]==='1'?1:0); | ||
} | ||
buff[i] = value; | ||
} | ||
return buff.buffer; | ||
} | ||
else { | ||
return UTF8Encode(input).buffer; | ||
} | ||
return a.compare(b); | ||
} | ||
throw new TypeError( "Cannot convert given input data into array buffer!" ); | ||
}; | ||
ArrayBuffer.compare = function(a, b) { | ||
if ( !(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer) ) { | ||
throw new TypeError("ArrayBuffer.compare only accepts two array buffers!"); | ||
} | ||
return a.compare(b); | ||
}; | ||
}); |
@@ -5,12 +5,14 @@ /** | ||
**/ | ||
Object.defineProperty(Blob.prototype, 'arrayBuffer', { | ||
configurable:true, writable:true, enumerable:false, | ||
value:function() { | ||
return new Promise((resolve, reject)=>{ | ||
const reader = new FileReader(); | ||
reader.onerror = reject; | ||
reader.onload = ()=>resolve(reader.result); | ||
reader.readAsArrayBuffer(this); | ||
}); | ||
} | ||
}); | ||
if ( typeof Blob !== "undefined" ) { | ||
Object.defineProperty(Blob.prototype, 'arrayBuffer', { | ||
configurable:true, writable:true, enumerable:false, | ||
value:function() { | ||
return new Promise((resolve, reject)=>{ | ||
const reader = new FileReader(); | ||
reader.onerror = reject; | ||
reader.onload = ()=>resolve(reader.result); | ||
reader.readAsArrayBuffer(this); | ||
}); | ||
} | ||
}); | ||
} |
@@ -6,2 +6,4 @@ /** | ||
if ( typeof Element !== "undefined" ) { | ||
const configurable = true, writable = true, enumerable = false; | ||
const _ELEMENT_SET_ATTRIBUTE = Element.prototype.setAttribute; | ||
@@ -11,28 +13,50 @@ const _ELEMENT_REMOVE_ATTRIBUTE = Element.prototype.removeAttribute; | ||
const _ELEMENT_REMOVE_ATTRIBUTE_NS = Element.prototype.removeAttributeNS; | ||
Element.prototype.addClass = function(...classes) { | ||
this.classList.add(...classes); | ||
return this; | ||
}; | ||
Element.prototype.removeClass = function(...classes) { | ||
this.classList.remove(...classes); | ||
return this; | ||
}; | ||
Element.prototype.setAttribute = function(name, value) { | ||
if ( arguments.length < 2 ) { value = ''; } | ||
_ELEMENT_SET_ATTRIBUTE.call(this, name, value); | ||
return this; | ||
}; | ||
Element.prototype.removeAttribute = function(...args) { | ||
_ELEMENT_REMOVE_ATTRIBUTE.apply(this, args); | ||
return this; | ||
}; | ||
Element.prototype.setAttributeNS = function(...args) { | ||
_ELEMENT_SET_ATTRIBUTE_NS.apply(this, args); | ||
return this; | ||
}; | ||
Element.prototype.removeAttributeNS = function(...args) { | ||
_ELEMENT_REMOVE_ATTRIBUTE_NS.apply(this, args); | ||
return this; | ||
}; | ||
Object.defineProperties(Element.prototype, { | ||
addClass: { | ||
configurable, enumerable, writable, | ||
value: function(...classes) { | ||
this.classList.add(...classes); | ||
return this; | ||
} | ||
}, | ||
removeClass: { | ||
configurable, enumerable, writable, | ||
value: function(...classes) { | ||
this.classList.remove(...classes); | ||
return this; | ||
} | ||
}, | ||
setAttribute: { | ||
configurable, enumerable, writable, | ||
value: function(name, value) { | ||
if ( arguments.length < 2 ) { value = ''; } | ||
_ELEMENT_SET_ATTRIBUTE.call(this, name, value); | ||
return this; | ||
} | ||
}, | ||
removeAttribute: { | ||
configurable, enumerable, writable, | ||
value: function(...args) { | ||
_ELEMENT_REMOVE_ATTRIBUTE.apply(this, args); | ||
return this; | ||
} | ||
}, | ||
setAttributeNS: { | ||
configurable, enumerable, writable, | ||
value: function(...args) { | ||
_ELEMENT_SET_ATTRIBUTE_NS.apply(this, args); | ||
return this; | ||
} | ||
}, | ||
removeAttributeNS: { | ||
configurable, enumerable, writable, | ||
value: function(...args) { | ||
_ELEMENT_REMOVE_ATTRIBUTE_NS.apply(this, args); | ||
return this; | ||
} | ||
}, | ||
}); | ||
} |
@@ -6,21 +6,42 @@ /** | ||
if ( typeof HTMLElement !== "undefined" ) { | ||
HTMLElement.prototype.setData = function(key_val={}) { | ||
for(const key in key_val) { | ||
this.dataset[key] = key_val[key]; | ||
const configurable = true, writable = true, enumerable = false; | ||
Object.defineProperties(HTMLElement.prototype, { | ||
setData: { | ||
configurable, writable, enumerable, | ||
value: function(key, value) { | ||
if ( Object(key) === key ) { | ||
for(const _key in key) { | ||
this.dataset[_key] = key[_key]; | ||
} | ||
} | ||
else { | ||
this.dataset[key] = value; | ||
} | ||
return this; | ||
} | ||
}, | ||
getData: { | ||
configurable, writable, enumerable, | ||
value: function(key) { | ||
return this.dataset[key]; | ||
} | ||
}, | ||
removeData: { | ||
configurable, writable, enumerable, | ||
value: function(...data_names) { | ||
for( const name of data_names ) { | ||
delete this.dataset[name]; | ||
} | ||
return this; | ||
} | ||
}, | ||
setContentHtml: { | ||
configurable, writable, enumerable, | ||
value: function(html) { | ||
this.innerHTML = html; | ||
return this; | ||
} | ||
} | ||
return this; | ||
}; | ||
HTMLElement.prototype.getData = function(key) { | ||
return this.dataset[key]; | ||
}; | ||
HTMLElement.prototype.removeData = function(...data_names) { | ||
for( const name of data_names ) { | ||
delete this.dataset[name]; | ||
} | ||
return this; | ||
}; | ||
HTMLElement.prototype.setContentHtml = function(html) { | ||
this.innerHTML = html; | ||
return this; | ||
}; | ||
}); | ||
} |
@@ -6,6 +6,11 @@ /** | ||
if ( typeof HTMLInputElement !== "undefined" ) { | ||
HTMLInputElement.prototype.setValue = function(value) { | ||
this.value = value; | ||
return this; | ||
}; | ||
const configurable = true, writable = true, enumerable = false; | ||
Object.defineProperty( HTMLInputElement.prototype, 'setValue', { | ||
configurable, writable, enumerable, | ||
value: function(value) { | ||
this.value = value; | ||
return this; | ||
} | ||
}); | ||
} |
@@ -6,36 +6,49 @@ /** | ||
if ( typeof Node !== "undefined" ) { | ||
Node.prototype.prependChild = function(child) { | ||
this.insertBefore(child, this.children[0]||null); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
}; | ||
Node.prototype.insertNeighborBefore = function(child) { | ||
if ( !this.parentNode ) { | ||
throw new RangeError( "Reference element is currently in detached mode! No way to add neighbors!" ); | ||
const configurable = true, writable = true, enumerable = false; | ||
Object.defineProperty( Node.prototype, 'prependChild', { | ||
configurable, writable, enumerable, | ||
value: function(child) { | ||
this.insertBefore(child, this.children[0]||null); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
} | ||
this.parentNode.insertBefore(child, this); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
}; | ||
Node.prototype.insertNeighborAfter = function(child) { | ||
if ( !this.parentNode ) { | ||
throw new RangeError( "Reference element is currently in detached mode! No way to add neighbors!" ); | ||
} | ||
}); | ||
Object.defineProperty( Node.prototype, 'insertNeighborBefore', { | ||
configurable, writable, enumerable, | ||
value: function(child) { | ||
if ( !this.parentNode ) { | ||
throw new RangeError( "Reference element is currently in detached mode! No way to add neighbors!" ); | ||
} | ||
this.parentNode.insertBefore(child, this.nextSibling); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
}; | ||
Node.prototype.setContentText = function(text) { | ||
this.textContent = text; | ||
return this; | ||
}; | ||
Node.prototype.process = function(processor, ...args) { | ||
if ( typeof processor === "function" ) { | ||
processor.call(this, ...args); | ||
this.parentNode.insertBefore(child, this); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
} | ||
return this; | ||
} | ||
}); | ||
Object.defineProperty( Node.prototype, 'insertNeighborAfter', { | ||
configurable, writable, enumerable, | ||
value: function(child) { | ||
if ( !this.parentNode ) { | ||
throw new RangeError( "Reference element is currently in detached mode! No way to add neighbors!" ); | ||
} | ||
this.parentNode.insertBefore(child, this.nextSibling); | ||
return ( this instanceof DocumentFragment ) ? new DocumentFragment() : child; | ||
} | ||
}); | ||
Object.defineProperty( Node.prototype, 'setContentText', { | ||
configurable, writable, enumerable, | ||
value: function(text) { | ||
this.textContent = text; | ||
return this; | ||
} | ||
}); | ||
Object.defineProperty( Node.prototype, 'process', { | ||
configurable, writable, enumerable, | ||
value: function(processor, ...args) { | ||
if ( typeof processor === "function" ) { | ||
processor.call(this, ...args); | ||
} | ||
return this; | ||
} | ||
}); | ||
} |
@@ -44,3 +44,3 @@ /** | ||
}); | ||
Object.defineProperty(Object, 'typeof', { | ||
Object.defineProperty(Object, 'typeOf', { | ||
writable, configurable, enumerable, | ||
@@ -50,2 +50,11 @@ value: TypeOf | ||
Object.defineProperty(Object.prototype, '_decorate', { | ||
writable, configurable, enumerable, | ||
value: function(processor, ...args) { | ||
if ( typeof processor === "function" ) { | ||
processor.call(this, ...args); | ||
} | ||
return this; | ||
} | ||
}); | ||
@@ -52,0 +61,0 @@ |
{ | ||
"name": "extes", | ||
"version": "1.9.5", | ||
"version": "1.9.6", | ||
"description": "A tiny library that extends native js with some handy tools", | ||
@@ -5,0 +5,0 @@ "main": "index.mjs", |
@@ -13,3 +13,2 @@ /** | ||
}); | ||
Object.defineProperty(setInterval, 'create', { | ||
@@ -16,0 +15,0 @@ writable:true, configurable:true, enumerable:false, |
33324
22
1135