Comparing version 1.0.13 to 1.0.14
@@ -77,2 +77,5 @@ /** | ||
static FromString(str, enc = "utf8") { | ||
if (!str) { | ||
return new ArrayBuffer(0); | ||
} | ||
switch (enc.toLowerCase()) { | ||
@@ -103,19 +106,26 @@ case "utf8": | ||
} | ||
static FromBase64(base64Text) { | ||
if (!Convert.isBase64(base64Text)) { | ||
static FromBase64(base64) { | ||
const formatted = this.formatString(base64); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isBase64(formatted)) { | ||
throw new TypeError("Argument 'base64Text' is not Base64 encoded"); | ||
} | ||
base64Text = base64Text.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "").replace(/\s/g, ""); | ||
if (typeof atob !== "undefined") { | ||
return this.FromBinary(atob(base64Text)); | ||
return this.FromBinary(atob(formatted)); | ||
} | ||
else { | ||
return new Uint8Array(Buffer.from(base64Text, "base64")).buffer; | ||
return new Uint8Array(Buffer.from(formatted, "base64")).buffer; | ||
} | ||
} | ||
static FromBase64Url(base64url) { | ||
if (!Convert.isBase64Url(base64url)) { | ||
const formatted = this.formatString(base64url); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isBase64Url(formatted)) { | ||
throw new TypeError("Argument 'base64url' is not Base64Url encoded"); | ||
} | ||
return this.FromBase64(this.Base64Padding(base64url.replace(/\-/g, "+").replace(/\_/g, "/"))); | ||
return this.FromBase64(this.Base64Padding(formatted.replace(/\-/g, "+").replace(/\_/g, "/"))); | ||
} | ||
@@ -168,11 +178,15 @@ static ToBase64Url(data) { | ||
static FromHex(hexString) { | ||
if (!Convert.isHex(hexString)) { | ||
let formatted = this.formatString(hexString); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isHex(formatted)) { | ||
throw new TypeError("Argument 'hexString' is not HEX encoded"); | ||
} | ||
if (hexString.length % 2) { | ||
hexString = `0${hexString}`; | ||
if (formatted.length % 2) { | ||
formatted = `0${formatted}`; | ||
} | ||
const res = new Uint8Array(hexString.length / 2); | ||
for (let i = 0; i < hexString.length; i = i + 2) { | ||
const c = hexString.slice(i, i + 2); | ||
const res = new Uint8Array(formatted.length / 2); | ||
for (let i = 0; i < formatted.length; i = i + 2) { | ||
const c = formatted.slice(i, i + 2); | ||
res[i / 2] = parseInt(c, 16); | ||
@@ -191,2 +205,5 @@ } | ||
} | ||
static formatString(data) { | ||
return (data === null || data === void 0 ? void 0 : data.replace(/[\n\r\t ]/g, "")) || ""; | ||
} | ||
} | ||
@@ -193,0 +210,0 @@ |
@@ -83,2 +83,5 @@ /** | ||
static FromString(str, enc = "utf8") { | ||
if (!str) { | ||
return new ArrayBuffer(0); | ||
} | ||
switch (enc.toLowerCase()) { | ||
@@ -109,19 +112,26 @@ case "utf8": | ||
} | ||
static FromBase64(base64Text) { | ||
if (!Convert.isBase64(base64Text)) { | ||
static FromBase64(base64) { | ||
const formatted = this.formatString(base64); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isBase64(formatted)) { | ||
throw new TypeError("Argument 'base64Text' is not Base64 encoded"); | ||
} | ||
base64Text = base64Text.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "").replace(/\s/g, ""); | ||
if (typeof atob !== "undefined") { | ||
return this.FromBinary(atob(base64Text)); | ||
return this.FromBinary(atob(formatted)); | ||
} | ||
else { | ||
return new Uint8Array(Buffer.from(base64Text, "base64")).buffer; | ||
return new Uint8Array(Buffer.from(formatted, "base64")).buffer; | ||
} | ||
} | ||
static FromBase64Url(base64url) { | ||
if (!Convert.isBase64Url(base64url)) { | ||
const formatted = this.formatString(base64url); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isBase64Url(formatted)) { | ||
throw new TypeError("Argument 'base64url' is not Base64Url encoded"); | ||
} | ||
return this.FromBase64(this.Base64Padding(base64url.replace(/\-/g, "+").replace(/\_/g, "/"))); | ||
return this.FromBase64(this.Base64Padding(formatted.replace(/\-/g, "+").replace(/\_/g, "/"))); | ||
} | ||
@@ -174,11 +184,15 @@ static ToBase64Url(data) { | ||
static FromHex(hexString) { | ||
if (!Convert.isHex(hexString)) { | ||
let formatted = this.formatString(hexString); | ||
if (!formatted) { | ||
return new ArrayBuffer(0); | ||
} | ||
if (!Convert.isHex(formatted)) { | ||
throw new TypeError("Argument 'hexString' is not HEX encoded"); | ||
} | ||
if (hexString.length % 2) { | ||
hexString = `0${hexString}`; | ||
if (formatted.length % 2) { | ||
formatted = `0${formatted}`; | ||
} | ||
const res = new Uint8Array(hexString.length / 2); | ||
for (let i = 0; i < hexString.length; i = i + 2) { | ||
const c = hexString.slice(i, i + 2); | ||
const res = new Uint8Array(formatted.length / 2); | ||
for (let i = 0; i < formatted.length; i = i + 2) { | ||
const c = formatted.slice(i, i + 2); | ||
res[i / 2] = parseInt(c, 16); | ||
@@ -197,2 +211,5 @@ } | ||
} | ||
static formatString(data) { | ||
return (data === null || data === void 0 ? void 0 : data.replace(/[\n\r\t ]/g, "")) || ""; | ||
} | ||
} | ||
@@ -199,0 +216,0 @@ |
@@ -9,3 +9,3 @@ export declare type BufferEncoding = "utf8" | "binary" | "base64" | "base64url" | "hex" | string; | ||
static ToBase64(buffer: BufferSource): string; | ||
static FromBase64(base64Text: string): ArrayBuffer; | ||
static FromBase64(base64: string): ArrayBuffer; | ||
static FromBase64Url(base64url: string): ArrayBuffer; | ||
@@ -27,3 +27,3 @@ static ToBase64Url(data: BufferSource): string; | ||
* @static | ||
* @param {string} hexString | ||
* @param {string} formatted | ||
* @returns {Uint8Array} | ||
@@ -35,2 +35,7 @@ * | ||
protected static Base64Padding(base64: string): string; | ||
/** | ||
* Removes odd chars from string data | ||
* @param data String data | ||
*/ | ||
static formatString(data: string): string; | ||
} |
{ | ||
"name": "pvtsutils", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"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
24997
543