Comparing version 1.3.2 to 1.3.3
@@ -26,3 +26,3 @@ /*! | ||
declare type BufferSource = ArrayBuffer | ArrayBufferView; | ||
type BufferSource = ArrayBuffer | ArrayBufferView; | ||
interface ArrayBufferViewConstructor<T extends ArrayBufferView> { | ||
@@ -45,2 +45,5 @@ readonly prototype: T; | ||
* @returns ArrayBuffer representation of data | ||
* @remarks If incoming data is ArrayBuffer then it returns it without copying, | ||
* otherwise it copies data into new ArrayBuffer because incoming data can be | ||
* ArrayBufferView with offset and length which is not equal to buffer length | ||
*/ | ||
@@ -101,4 +104,4 @@ static toArrayBuffer(data: BufferSource): ArrayBuffer; | ||
declare type BufferEncoding = "utf8" | "binary" | "base64" | "base64url" | "hex" | string; | ||
declare type TextEncoding = "ascii" | "utf8" | "utf16" | "utf16be" | "utf16le" | "usc2"; | ||
type BufferEncoding = "utf8" | "binary" | "base64" | "base64url" | "hex" | string; | ||
type TextEncoding = "ascii" | "utf8" | "utf16" | "utf16be" | "utf16le" | "usc2"; | ||
declare class Convert { | ||
@@ -110,10 +113,52 @@ static isHex(data: any): data is string; | ||
static FromString(str: string, enc?: BufferEncoding): ArrayBuffer; | ||
/** | ||
* Converts byte array to Base64 encoded string. | ||
* @param buffer - Byte array to encode. | ||
* @returns Base64 string. | ||
*/ | ||
static ToBase64(buffer: BufferSource): string; | ||
/** | ||
* Converts byte array to Base64 encoded string. | ||
* @param base64 - Base64 encoded string. | ||
* @returns Byte array. | ||
*/ | ||
static FromBase64(base64: string): ArrayBuffer; | ||
/** | ||
* Converts Base64Url encoded string to byte array. | ||
* @param base64url - Base64Url encoded string. | ||
* @returns Byte array. | ||
*/ | ||
static FromBase64Url(base64url: string): ArrayBuffer; | ||
/** | ||
* Converts byte array to Base64Url encoded string. | ||
* @param data - Byte array to encode. | ||
* @returns Base64Url encoded string. | ||
*/ | ||
static ToBase64Url(data: BufferSource): string; | ||
protected static DEFAULT_UTF8_ENCODING: TextEncoding; | ||
/** | ||
* Converts JavaScript string to ArrayBuffer using specified encoding. | ||
* @param text - JavaScript string to convert. | ||
* @param encoding - Encoding to use. Default is 'utf8'. | ||
* @returns ArrayBuffer representing input string. | ||
*/ | ||
static FromUtf8String(text: string, encoding?: TextEncoding): ArrayBuffer; | ||
/** | ||
* Converts ArrayBuffer to JavaScript string using specified encoding. | ||
* @param buffer - Buffer to convert. | ||
* @param encoding - Encoding to use. Default is 'utf8'. | ||
* @returns JavaScript string derived from input buffer. | ||
*/ | ||
static ToUtf8String(buffer: BufferSource, encoding?: TextEncoding): string; | ||
/** | ||
* Converts binary string to ArrayBuffer. | ||
* @param text - Binary string. | ||
* @returns Byte array. | ||
*/ | ||
static FromBinary(text: string): ArrayBuffer; | ||
/** | ||
* Converts buffer to binary string. | ||
* @param buffer - Input buffer. | ||
* @returns Binary string. | ||
*/ | ||
static ToBinary(buffer: BufferSource): string; | ||
@@ -120,0 +165,0 @@ /** |
@@ -38,2 +38,5 @@ /*! | ||
} | ||
if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) { | ||
return data.buffer; | ||
} | ||
return this.toUint8Array(data).slice().buffer; | ||
@@ -45,3 +48,3 @@ } | ||
static toView(data, type) { | ||
if (data.constructor === type) { | ||
if (data instanceof type) { | ||
return data; | ||
@@ -79,26 +82,39 @@ } | ||
static concat(...args) { | ||
if (Array.isArray(args[0])) { | ||
const buffers = args[0]; | ||
let size = 0; | ||
for (const buffer of buffers) { | ||
size += buffer.byteLength; | ||
let buffers; | ||
if (Array.isArray(args[0]) && !(args[1] instanceof Function)) { | ||
buffers = args[0]; | ||
} | ||
else if (Array.isArray(args[0]) && args[1] instanceof Function) { | ||
buffers = args[0]; | ||
} | ||
else { | ||
if (args[args.length - 1] instanceof Function) { | ||
buffers = args.slice(0, args.length - 1); | ||
} | ||
const res = new Uint8Array(size); | ||
let offset = 0; | ||
for (const buffer of buffers) { | ||
const view = this.toUint8Array(buffer); | ||
res.set(view, offset); | ||
offset += view.length; | ||
else { | ||
buffers = args; | ||
} | ||
if (args[1]) { | ||
return this.toView(res, args[1]); | ||
} | ||
return res.buffer; | ||
} | ||
else { | ||
return this.concat(args); | ||
let size = 0; | ||
for (const buffer of buffers) { | ||
size += buffer.byteLength; | ||
} | ||
const res = new Uint8Array(size); | ||
let offset = 0; | ||
for (const buffer of buffers) { | ||
const view = this.toUint8Array(buffer); | ||
res.set(view, offset); | ||
offset += view.length; | ||
} | ||
if (args[args.length - 1] instanceof Function) { | ||
return this.toView(res, args[args.length - 1]); | ||
} | ||
return res.buffer; | ||
} | ||
} | ||
const STRING_TYPE = "string"; | ||
const HEX_REGEX = /^[0-9a-f]+$/i; | ||
const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; | ||
const BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/; | ||
class Utf8Converter { | ||
@@ -145,12 +161,12 @@ static fromString(text) { | ||
static isHex(data) { | ||
return typeof data === "string" | ||
&& /^[a-z0-9]+$/i.test(data); | ||
return typeof data === STRING_TYPE | ||
&& HEX_REGEX.test(data); | ||
} | ||
static isBase64(data) { | ||
return typeof data === "string" | ||
&& /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(data); | ||
return typeof data === STRING_TYPE | ||
&& BASE64_REGEX.test(data); | ||
} | ||
static isBase64Url(data) { | ||
return typeof data === "string" | ||
&& /^[a-zA-Z0-9-_]+$/i.test(data); | ||
return typeof data === STRING_TYPE | ||
&& BASE64URL_REGEX.test(data); | ||
} | ||
@@ -291,10 +307,12 @@ static ToString(buffer, enc = "utf8") { | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const splitter = ""; | ||
const res = []; | ||
let result = ""; | ||
const len = buf.length; | ||
for (let i = 0; i < len; i++) { | ||
const char = buf[i].toString(16).padStart(2, "0"); | ||
res.push(char); | ||
const byte = buf[i]; | ||
if (byte < 16) { | ||
result += "0"; | ||
} | ||
result += byte.toString(16); | ||
} | ||
return res.join(splitter); | ||
return result; | ||
} | ||
@@ -301,0 +319,0 @@ static FromHex(hexString) { |
@@ -28,4 +28,2 @@ /*! | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const ARRAY_BUFFER_NAME = "[object ArrayBuffer]"; | ||
@@ -43,2 +41,5 @@ class BufferSourceConverter { | ||
} | ||
if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) { | ||
return data.buffer; | ||
} | ||
return this.toUint8Array(data).slice().buffer; | ||
@@ -50,3 +51,3 @@ } | ||
static toView(data, type) { | ||
if (data.constructor === type) { | ||
if (data instanceof type) { | ||
return data; | ||
@@ -84,26 +85,39 @@ } | ||
static concat(...args) { | ||
if (Array.isArray(args[0])) { | ||
const buffers = args[0]; | ||
let size = 0; | ||
for (const buffer of buffers) { | ||
size += buffer.byteLength; | ||
let buffers; | ||
if (Array.isArray(args[0]) && !(args[1] instanceof Function)) { | ||
buffers = args[0]; | ||
} | ||
else if (Array.isArray(args[0]) && args[1] instanceof Function) { | ||
buffers = args[0]; | ||
} | ||
else { | ||
if (args[args.length - 1] instanceof Function) { | ||
buffers = args.slice(0, args.length - 1); | ||
} | ||
const res = new Uint8Array(size); | ||
let offset = 0; | ||
for (const buffer of buffers) { | ||
const view = this.toUint8Array(buffer); | ||
res.set(view, offset); | ||
offset += view.length; | ||
else { | ||
buffers = args; | ||
} | ||
if (args[1]) { | ||
return this.toView(res, args[1]); | ||
} | ||
return res.buffer; | ||
} | ||
else { | ||
return this.concat(args); | ||
let size = 0; | ||
for (const buffer of buffers) { | ||
size += buffer.byteLength; | ||
} | ||
const res = new Uint8Array(size); | ||
let offset = 0; | ||
for (const buffer of buffers) { | ||
const view = this.toUint8Array(buffer); | ||
res.set(view, offset); | ||
offset += view.length; | ||
} | ||
if (args[args.length - 1] instanceof Function) { | ||
return this.toView(res, args[args.length - 1]); | ||
} | ||
return res.buffer; | ||
} | ||
} | ||
const STRING_TYPE = "string"; | ||
const HEX_REGEX = /^[0-9a-f]+$/i; | ||
const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; | ||
const BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/; | ||
class Utf8Converter { | ||
@@ -150,12 +164,12 @@ static fromString(text) { | ||
static isHex(data) { | ||
return typeof data === "string" | ||
&& /^[a-z0-9]+$/i.test(data); | ||
return typeof data === STRING_TYPE | ||
&& HEX_REGEX.test(data); | ||
} | ||
static isBase64(data) { | ||
return typeof data === "string" | ||
&& /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(data); | ||
return typeof data === STRING_TYPE | ||
&& BASE64_REGEX.test(data); | ||
} | ||
static isBase64Url(data) { | ||
return typeof data === "string" | ||
&& /^[a-zA-Z0-9-_]+$/i.test(data); | ||
return typeof data === STRING_TYPE | ||
&& BASE64URL_REGEX.test(data); | ||
} | ||
@@ -296,10 +310,12 @@ static ToString(buffer, enc = "utf8") { | ||
const buf = BufferSourceConverter.toUint8Array(buffer); | ||
const splitter = ""; | ||
const res = []; | ||
let result = ""; | ||
const len = buf.length; | ||
for (let i = 0; i < len; i++) { | ||
const char = buf[i].toString(16).padStart(2, "0"); | ||
res.push(char); | ||
const byte = buf[i]; | ||
if (byte < 16) { | ||
result += "0"; | ||
} | ||
result += byte.toString(16); | ||
} | ||
return res.join(splitter); | ||
return result; | ||
} | ||
@@ -306,0 +322,0 @@ static FromHex(hexString) { |
{ | ||
"name": "pvtsutils", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "pvtsutils is a set of common utility functions used in various Peculiar Ventures TypeScript based projects.", | ||
@@ -57,18 +57,23 @@ "main": "build/index.js", | ||
"dependencies": { | ||
"tslib": "^2.4.0" | ||
"tslib": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^17.0.31", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.4.8", | ||
"coveralls": "^3.1.1", | ||
"mocha": "^10.0.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.71.1", | ||
"rollup-plugin-dts": "^4.2.1", | ||
"rollup-plugin-typescript2": "^0.31.2", | ||
"ts-node": "^10.7.0", | ||
"rimraf": "^5.0.1", | ||
"rollup": "^3.27.2", | ||
"rollup-plugin-dts": "^5.3.1", | ||
"rollup-plugin-typescript2": "^0.35.0", | ||
"ts-node": "^10.9.1", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.6.4" | ||
"typescript": "^5.1.6" | ||
}, | ||
"resolutions": { | ||
"json5": "^2.2.2", | ||
"semver": "^6.3.1", | ||
"tough-cookie": "^4.1.3" | ||
} | ||
} |
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
40131
978
Updatedtslib@^2.6.1