@helios-lang/codec-utils
Advanced tools
Comparing version 0.1.33 to 0.1.34
{ | ||
"name": "@helios-lang/codec-utils", | ||
"version": "0.1.33", | ||
"version": "0.1.34", | ||
"description": "Primitive manipulation functions commonly used in encoding and decoding algorithms", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,3 +6,9 @@ export { toBytes } from "./ByteArrayLike.js" | ||
export { Base64, decodeBase64, encodeBase64, isValidBase64 } from "./base64.js" | ||
export { compareBytes, equalsBytes, padBytes, prepadBytes } from "./ops.js" | ||
export { | ||
compareBytes, | ||
dummyBytes, | ||
equalsBytes, | ||
padBytes, | ||
prepadBytes | ||
} from "./ops.js" | ||
@@ -9,0 +15,0 @@ /** |
@@ -0,2 +1,52 @@ | ||
import { encodeIntBE } from "../int/be.js" | ||
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @param {boolean} shortestFirst defaults to false (strictly lexicographic comparison) | ||
* @returns {-1 | 0 | 1} -1 if a < b, 0 if a == b, 1 if a > b | ||
*/ | ||
export function compareBytes(a, b, shortestFirst = false) { | ||
const na = a.length | ||
const nb = b.length | ||
if (shortestFirst && na != nb) { | ||
return na < nb ? -1 : 1 | ||
} | ||
for (let i = 0; i < Math.min(na, nb); i++) { | ||
if (a[i] < b[i]) { | ||
return -1 | ||
} else if (a[i] > b[i]) { | ||
return 1 | ||
} | ||
} | ||
if (na != nb) { | ||
return na < nb ? -1 : 1 | ||
} else { | ||
return 0 | ||
} | ||
} | ||
/** | ||
* Used to create dummy hashes for testing | ||
* @param {number} n | ||
* @param {number} seed | ||
* @returns {number[]} | ||
*/ | ||
export function dummyBytes(n, seed = 0) { | ||
return padBytes(encodeIntBE(seed), n).slice(0, n) | ||
} | ||
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @returns {boolean} | ||
*/ | ||
export function equalsBytes(a, b) { | ||
return compareBytes(a, b) == 0 | ||
} | ||
/** | ||
* Pad by appending zeroes. | ||
@@ -47,40 +97,1 @@ * If `n < nCurrent`, pad to next multiple of `n`. | ||
} | ||
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @param {boolean} shortestFirst defaults to false (strictly lexicographic comparison) | ||
* @returns {-1 | 0 | 1} -1 if a < b, 0 if a == b, 1 if a > b | ||
*/ | ||
export function compareBytes(a, b, shortestFirst = false) { | ||
const na = a.length | ||
const nb = b.length | ||
if (shortestFirst && na != nb) { | ||
return na < nb ? -1 : 1 | ||
} | ||
for (let i = 0; i < Math.min(na, nb); i++) { | ||
if (a[i] < b[i]) { | ||
return -1 | ||
} else if (a[i] > b[i]) { | ||
return 1 | ||
} | ||
} | ||
if (na != nb) { | ||
return na < nb ? -1 : 1 | ||
} else { | ||
return 0 | ||
} | ||
} | ||
/** | ||
* | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @returns {boolean} | ||
*/ | ||
export function equalsBytes(a, b) { | ||
return compareBytes(a, b) == 0 | ||
} |
@@ -7,3 +7,3 @@ export { toBytes } from "./ByteArrayLike.js"; | ||
export { Base64, decodeBase64, encodeBase64, isValidBase64 } from "./base64.js"; | ||
export { compareBytes, equalsBytes, padBytes, prepadBytes } from "./ops.js"; | ||
export { compareBytes, dummyBytes, equalsBytes, padBytes, prepadBytes } from "./ops.js"; | ||
//# sourceMappingURL=index.d.ts.map |
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @param {boolean} shortestFirst defaults to false (strictly lexicographic comparison) | ||
* @returns {-1 | 0 | 1} -1 if a < b, 0 if a == b, 1 if a > b | ||
*/ | ||
export function compareBytes(a: number[] | Uint8Array, b: number[] | Uint8Array, shortestFirst?: boolean): -1 | 0 | 1; | ||
/** | ||
* Used to create dummy hashes for testing | ||
* @param {number} n | ||
* @param {number} seed | ||
* @returns {number[]} | ||
*/ | ||
export function dummyBytes(n: number, seed?: number): number[]; | ||
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @returns {boolean} | ||
*/ | ||
export function equalsBytes(a: number[] | Uint8Array, b: number[] | Uint8Array): boolean; | ||
/** | ||
* Pad by appending zeroes. | ||
@@ -15,16 +35,2 @@ * If `n < nCurrent`, pad to next multiple of `n`. | ||
export function prepadBytes(bytes: number[], n: number): any[]; | ||
/** | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @param {boolean} shortestFirst defaults to false (strictly lexicographic comparison) | ||
* @returns {-1 | 0 | 1} -1 if a < b, 0 if a == b, 1 if a > b | ||
*/ | ||
export function compareBytes(a: number[] | Uint8Array, b: number[] | Uint8Array, shortestFirst?: boolean): -1 | 0 | 1; | ||
/** | ||
* | ||
* @param {number[] | Uint8Array} a | ||
* @param {number[] | Uint8Array} b | ||
* @returns {boolean} | ||
*/ | ||
export function equalsBytes(a: number[] | Uint8Array, b: number[] | Uint8Array): boolean; | ||
//# sourceMappingURL=ops.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
81122
2477