Comparing version 1.1.3 to 1.1.4
@@ -17,10 +17,13 @@ /** | ||
static toUint8Array(data) { | ||
return this.toView(data, Uint8Array); | ||
} | ||
static toView(data, type) { | ||
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) { | ||
return new Uint8Array(data); | ||
return new type(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
if (ArrayBuffer.isView(data)) { | ||
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
return new type(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
if (this.isArrayBuffer(data)) { | ||
return new Uint8Array(data); | ||
return new type(data); | ||
} | ||
@@ -39,13 +42,2 @@ throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
function PrepareBuffer(buffer) { | ||
if (typeof Buffer !== "undefined" && Buffer.isBuffer(buffer)) { | ||
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
} | ||
else if (BufferSourceConverter.isArrayBufferView(buffer)) { | ||
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
} | ||
else { | ||
return new Uint8Array(buffer); | ||
} | ||
} | ||
class Convert { | ||
@@ -65,3 +57,3 @@ static isHex(data) { | ||
static ToString(buffer, enc = "utf8") { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
switch (enc.toLowerCase()) { | ||
@@ -102,3 +94,3 @@ case "utf8": | ||
static ToBase64(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
if (typeof btoa !== "undefined") { | ||
@@ -149,3 +141,3 @@ const binary = this.ToString(buf, "binary"); | ||
static ToUtf8String(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const encodedString = String.fromCharCode.apply(null, buf); | ||
@@ -164,3 +156,3 @@ const decodedString = decodeURIComponent(escape(encodedString)); | ||
static ToBinary(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
let resultString = ""; | ||
@@ -174,3 +166,3 @@ const len = buf.length; | ||
static ToHex(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const splitter = ""; | ||
@@ -177,0 +169,0 @@ const res = []; |
@@ -23,10 +23,13 @@ /** | ||
static toUint8Array(data) { | ||
return this.toView(data, Uint8Array); | ||
} | ||
static toView(data, type) { | ||
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) { | ||
return new Uint8Array(data); | ||
return new type(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
if (ArrayBuffer.isView(data)) { | ||
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
return new type(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
if (this.isArrayBuffer(data)) { | ||
return new Uint8Array(data); | ||
return new type(data); | ||
} | ||
@@ -45,13 +48,2 @@ throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
function PrepareBuffer(buffer) { | ||
if (typeof Buffer !== "undefined" && Buffer.isBuffer(buffer)) { | ||
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
} | ||
else if (BufferSourceConverter.isArrayBufferView(buffer)) { | ||
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
} | ||
else { | ||
return new Uint8Array(buffer); | ||
} | ||
} | ||
class Convert { | ||
@@ -71,3 +63,3 @@ static isHex(data) { | ||
static ToString(buffer, enc = "utf8") { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
switch (enc.toLowerCase()) { | ||
@@ -108,3 +100,3 @@ case "utf8": | ||
static ToBase64(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
if (typeof btoa !== "undefined") { | ||
@@ -155,3 +147,3 @@ const binary = this.ToString(buf, "binary"); | ||
static ToUtf8String(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const encodedString = String.fromCharCode.apply(null, buf); | ||
@@ -170,3 +162,3 @@ const decodedString = decodeURIComponent(escape(encodedString)); | ||
static ToBinary(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
let resultString = ""; | ||
@@ -180,3 +172,3 @@ const len = buf.length; | ||
static ToHex(buffer) { | ||
const buf = PrepareBuffer(buffer); | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const splitter = ""; | ||
@@ -183,0 +175,0 @@ const res = []; |
export declare type BufferSource = ArrayBuffer | ArrayBufferView; | ||
export interface ArrayBufferViewConstructor<T extends ArrayBufferView> { | ||
readonly prototype: T; | ||
new (length: number): T; | ||
new (array: ArrayLike<number> | ArrayBufferLike): T; | ||
new (buffer: ArrayBufferLike, byteOffset?: number, length?: number): T; | ||
} | ||
export declare class BufferSourceConverter { | ||
static isArrayBuffer(data: any): data is ArrayBuffer; | ||
static toArrayBuffer(data: BufferSource): ArrayBufferLike; | ||
static toArrayBuffer(data: BufferSource): ArrayBuffer; | ||
static toUint8Array(data: BufferSource): Uint8Array; | ||
/** | ||
* Converts BufferSource to ArrayBufferView specified view | ||
* @param data Buffer source | ||
* @param type Type of ArrayBufferView | ||
* @returns Specified ArrayBufferView | ||
*/ | ||
static toView<T extends ArrayBufferView>(data: BufferSource, type: ArrayBufferViewConstructor<T>): T; | ||
static isBufferSource(data: any): data is BufferSource; | ||
static isArrayBufferView(data: any): data is ArrayBufferView; | ||
} |
{ | ||
"name": "pvtsutils", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "pvtsutils is a set of common utility functions used in various Peculiar Ventures TypeScript based projects.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
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
25653
549