Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xylabs/hex

Package Overview
Dependencies
Maintainers
7
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xylabs/hex - npm Package Compare versions

Comparing version 2.13.20 to 2.13.21

dist/browser/hex/from/from.d.cts

1

dist/browser/address.d.ts
import { AssertConfig } from './assert';
import { HexConfig } from './hex';
export declare const addressRegex: RegExp;
export type Address = string;

@@ -5,0 +4,0 @@ export declare const toAddress: (value: unknown, config?: HexConfig) => string;

export * from './as';
export * from './from';
export * from './is';
export * from './isHexZero';
export * from './legacy';

@@ -5,0 +6,0 @@ export * from './model';

@@ -1,3 +0,3 @@

export declare const isHexZero: (value: string) => boolean;
export declare const isHex: (value: unknown, bitLength?: number) => value is string;
import { HexConfig } from './model';
export declare const isHex: (value: unknown, config?: HexConfig) => value is string;
//# sourceMappingURL=is.d.ts.map

@@ -6,3 +6,3 @@ // src/assert.ts

if (assertString) {
throw Error(assertString === true ? defaultMessage : assertString);
throw new Error(assertString === true ? defaultMessage : assertString);
}

@@ -13,3 +13,3 @@ }

// src/hex/from.ts
// src/hex/from/from.ts
import { isArrayBuffer } from "@xylabs/arraybuffer";

@@ -21,3 +21,3 @@

if (value !== nibbles << 2)
throw Error("Bits for nibbles must multiple of 4");
throw new Error("Bits for nibbles must multiple of 4");
return nibbles;

@@ -30,18 +30,29 @@ };

// src/hex/regex.ts
var hexRegex = /^[0-9a-f]+$/i;
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
var hexRegex = /^[\da-f]+$/i;
var hexRegexWithPrefix = /0x[\da-f]+$/i;
// src/hex/is.ts
var isHexZero = (value) => {
return BigInt(hexFromHexString(value, { prefix: true })) === 0n;
};
var isHex = (value, bitLength) => {
var isHex = (value, config) => {
if (typeof value !== "string")
return false;
if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
const valueCharLength = config?.prefix ? value.length - 2 : value.length;
if (config?.bitLength !== void 0 && valueCharLength !== bitsToNibbles(config?.bitLength))
return false;
return hexRegex.test(value);
return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value);
};
// src/hex/from.ts
// src/hex/from/fromHexString.ts
var hexFromHexString = (value, config = {}) => {
const { prefix = false, byteSize = 8 } = config;
const nibbleBoundary = bitsToNibbles(byteSize);
const unPadded = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase();
if (isHex(unPadded)) {
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
return prefix ? `0x${padded}` : padded;
} else {
throw new Error("Received string is not a value hex");
}
};
// src/hex/from/from.ts
var hexFromArrayBuffer = (buffer, config) => {

@@ -60,29 +71,23 @@ const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");

};
var hexFromHexString = (value, config = {}) => {
const { prefix = false, byteSize = 8 } = config;
const nibbleBoundary = bitsToNibbles(byteSize);
const unPadded = (value.startsWith("0x") ? value.substring(2) : value).toLowerCase();
if (isHex(unPadded)) {
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
return prefix ? `0x${padded}` : padded;
} else {
throw Error("Received string is not a value hex");
}
};
var hexFrom = (value, config) => {
switch (typeof value) {
case "string":
case "string": {
return hexFromHexString(value, config);
case "bigint":
}
case "bigint": {
return hexFromBigInt(value, config);
case "number":
}
case "number": {
return hexFromNumber(value, config);
case "object":
}
case "object": {
if (isArrayBuffer(value)) {
return hexFromArrayBuffer(value, config);
} else {
throw Error("Invalid type: object !== ArrayBuffer");
throw new Error("Invalid type: object !== ArrayBuffer");
}
default:
throw Error(`Invalid type: ${typeof value}`);
}
default: {
throw new Error(`Invalid type: ${typeof value}`);
}
}

@@ -95,7 +100,9 @@ };

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value);
break;
default:
}
default: {
return assertError(value, assert, `Unsupported type [${typeof value}]`);
}
}

@@ -105,2 +112,7 @@ return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`);

// src/hex/isHexZero.ts
var isHexZero = (value) => {
return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0;
};
// src/hex/legacy.ts

@@ -115,3 +127,2 @@ var toHexLegacy = (buffer) => {

// src/address.ts
var addressRegex = /0x[0-9a-f]+/i;
var toAddress = (value, config = {}) => {

@@ -122,10 +133,3 @@ const { bitLength = 160, prefix = true } = config;

var isAddress = (value, bitLength = 160) => {
if (typeof value !== "string")
return false;
if (!addressRegex.test(value))
return false;
const valueHex = value.substring(2);
if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
return false;
return isHex(valueHex, bitLength);
return isHex(value, { bitLength, prefix: true });
};

@@ -135,7 +139,9 @@ function asAddress(value, assert) {

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value, { prefix: true });
break;
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
}
}

@@ -151,3 +157,3 @@ return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`);

var isHash = (value, bitLength = 256) => {
return isHex(value, bitLength);
return isHex(value, { bitLength });
};

@@ -157,7 +163,9 @@ function asHash(value, assert) {

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value);
break;
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
}
}

@@ -168,3 +176,2 @@ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`);

HashBitLength,
addressRegex,
asAddress,

@@ -171,0 +178,0 @@ asHash,

import { AssertConfig } from './assert';
import { HexConfig } from './hex';
export declare const addressRegex: RegExp;
export type Address = string;

@@ -5,0 +4,0 @@ export declare const toAddress: (value: unknown, config?: HexConfig) => string;

export * from './as';
export * from './from';
export * from './is';
export * from './isHexZero';
export * from './legacy';

@@ -5,0 +6,0 @@ export * from './model';

@@ -1,3 +0,3 @@

export declare const isHexZero: (value: string) => boolean;
export declare const isHex: (value: unknown, bitLength?: number) => value is string;
import { HexConfig } from './model';
export declare const isHex: (value: unknown, config?: HexConfig) => value is string;
//# sourceMappingURL=is.d.ts.map

@@ -6,3 +6,3 @@ // src/assert.ts

if (assertString) {
throw Error(assertString === true ? defaultMessage : assertString);
throw new Error(assertString === true ? defaultMessage : assertString);
}

@@ -13,3 +13,3 @@ }

// src/hex/from.ts
// src/hex/from/from.ts
import { isArrayBuffer } from "@xylabs/arraybuffer";

@@ -21,3 +21,3 @@

if (value !== nibbles << 2)
throw Error("Bits for nibbles must multiple of 4");
throw new Error("Bits for nibbles must multiple of 4");
return nibbles;

@@ -30,18 +30,29 @@ };

// src/hex/regex.ts
var hexRegex = /^[0-9a-f]+$/i;
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
var hexRegex = /^[\da-f]+$/i;
var hexRegexWithPrefix = /0x[\da-f]+$/i;
// src/hex/is.ts
var isHexZero = (value) => {
return BigInt(hexFromHexString(value, { prefix: true })) === 0n;
};
var isHex = (value, bitLength) => {
var isHex = (value, config) => {
if (typeof value !== "string")
return false;
if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
const valueCharLength = (config == null ? void 0 : config.prefix) ? value.length - 2 : value.length;
if ((config == null ? void 0 : config.bitLength) !== void 0 && valueCharLength !== bitsToNibbles(config == null ? void 0 : config.bitLength))
return false;
return hexRegex.test(value);
return (config == null ? void 0 : config.prefix) ? hexRegexWithPrefix.test(value) : hexRegex.test(value);
};
// src/hex/from.ts
// src/hex/from/fromHexString.ts
var hexFromHexString = (value, config = {}) => {
const { prefix = false, byteSize = 8 } = config;
const nibbleBoundary = bitsToNibbles(byteSize);
const unPadded = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase();
if (isHex(unPadded)) {
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
return prefix ? `0x${padded}` : padded;
} else {
throw new Error("Received string is not a value hex");
}
};
// src/hex/from/from.ts
var hexFromArrayBuffer = (buffer, config) => {

@@ -60,29 +71,23 @@ const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");

};
var hexFromHexString = (value, config = {}) => {
const { prefix = false, byteSize = 8 } = config;
const nibbleBoundary = bitsToNibbles(byteSize);
const unPadded = (value.startsWith("0x") ? value.substring(2) : value).toLowerCase();
if (isHex(unPadded)) {
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
return prefix ? `0x${padded}` : padded;
} else {
throw Error("Received string is not a value hex");
}
};
var hexFrom = (value, config) => {
switch (typeof value) {
case "string":
case "string": {
return hexFromHexString(value, config);
case "bigint":
}
case "bigint": {
return hexFromBigInt(value, config);
case "number":
}
case "number": {
return hexFromNumber(value, config);
case "object":
}
case "object": {
if (isArrayBuffer(value)) {
return hexFromArrayBuffer(value, config);
} else {
throw Error("Invalid type: object !== ArrayBuffer");
throw new Error("Invalid type: object !== ArrayBuffer");
}
default:
throw Error(`Invalid type: ${typeof value}`);
}
default: {
throw new Error(`Invalid type: ${typeof value}`);
}
}

@@ -95,7 +100,9 @@ };

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value);
break;
default:
}
default: {
return assertError(value, assert, `Unsupported type [${typeof value}]`);
}
}

@@ -105,2 +112,7 @@ return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`);

// src/hex/isHexZero.ts
var isHexZero = (value) => {
return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0;
};
// src/hex/legacy.ts

@@ -115,3 +127,2 @@ var toHexLegacy = (buffer) => {

// src/address.ts
var addressRegex = /0x[0-9a-f]+/i;
var toAddress = (value, config = {}) => {

@@ -122,10 +133,3 @@ const { bitLength = 160, prefix = true } = config;

var isAddress = (value, bitLength = 160) => {
if (typeof value !== "string")
return false;
if (!addressRegex.test(value))
return false;
const valueHex = value.substring(2);
if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
return false;
return isHex(valueHex, bitLength);
return isHex(value, { bitLength, prefix: true });
};

@@ -135,7 +139,9 @@ function asAddress(value, assert) {

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value, { prefix: true });
break;
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
}
}

@@ -151,3 +157,3 @@ return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`);

var isHash = (value, bitLength = 256) => {
return isHex(value, bitLength);
return isHex(value, { bitLength });
};

@@ -157,7 +163,9 @@ function asHash(value, assert) {

switch (typeof value) {
case "string":
case "string": {
stringValue = hexFromHexString(value);
break;
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
}
}

@@ -168,3 +176,2 @@ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`);

HashBitLength,
addressRegex,
asAddress,

@@ -171,0 +178,0 @@ asHash,

@@ -35,2 +35,3 @@ {

"keywords": [
"hex",
"xylabs",

@@ -42,9 +43,12 @@ "utility",

"dependencies": {
"@xylabs/arraybuffer": "~2.13.20"
"@xylabs/arraybuffer": "~2.13.21"
},
"devDependencies": {
"@xylabs/ts-scripts-yarn3": "^3.2.10",
"@xylabs/tsconfig": "^3.2.10",
"typescript": "^5.3.2"
"@xylabs/ts-scripts-yarn3": "^3.2.25",
"@xylabs/tsconfig": "^3.2.25",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18"
},
"publishConfig": {

@@ -58,5 +62,4 @@ "access": "public"

"sideEffects": false,
"version": "2.13.20",
"packageManager": "yarn@3.3.1",
"version": "2.13.21",
"type": "module"
}
import { AssertConfig, assertError } from './assert'
import { bitsToNibbles, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'
import { HexConfig, hexFrom, hexFromHexString, isHex } from './hex'
export const addressRegex = /0x[0-9a-f]+/i
export type Address = string

@@ -14,14 +12,3 @@

export const isAddress = (value: unknown, bitLength = 160): value is Address => {
//Is it a string?
if (typeof value !== 'string') return false
//Does it only has hex values and leading 0x?
if (!addressRegex.test(value)) return false
const valueHex = value.substring(2)
//If a bitLength specified, does it conform?
if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false
return isHex(valueHex, bitLength)
return isHex(value, { bitLength, prefix: true })
}

@@ -35,9 +22,11 @@

switch (typeof value) {
case 'string':
case 'string': {
stringValue = hexFromHexString(value, { prefix: true })
break
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
}
}
return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)
}

@@ -9,6 +9,7 @@ export type AssertCallback = (value: unknown, message: string) => string | boolean

if (assertString) {
throw Error(assertString === true ? defaultMessage : assertString)
throw new Error(assertString === true ? defaultMessage : assertString)
}
}
// eslint-disable-next-line unicorn/no-useless-undefined
return undefined
}

@@ -13,3 +13,3 @@ import { AssertConfig, assertError } from './assert'

export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
return isHex(value, bitLength)
return isHex(value, { bitLength })
}

@@ -23,9 +23,11 @@

switch (typeof value) {
case 'string':
case 'string': {
stringValue = hexFromHexString(value)
break
default:
}
default: {
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
}
}
return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
}

@@ -12,7 +12,9 @@ import { AssertConfig, assertError } from '../assert'

switch (typeof value) {
case 'string':
case 'string': {
stringValue = hexFromHexString(value)
break
default:
}
default: {
return assertError(value, assert, `Unsupported type [${typeof value}]`)
}
}

@@ -19,0 +21,0 @@

export * from './as'
export * from './from'
export * from './is'
export * from './isHexZero'
export * from './legacy'

@@ -5,0 +6,0 @@ export * from './model'

@@ -1,19 +0,16 @@

import { hexFromHexString } from './from'
import { Hex } from './model'
import { Hex, HexConfig } from './model'
import { bitsToNibbles } from './nibble'
import { hexRegex } from './regex'
import { hexRegex, hexRegexWithPrefix } from './regex'
export const isHexZero = (value: string) => {
return BigInt(hexFromHexString(value, { prefix: true })) === 0n
}
export const isHex = (value: unknown, bitLength?: number): value is Hex => {
export const isHex = (value: unknown, config?: HexConfig): value is Hex => {
//Is it a string?
if (typeof value !== 'string') return false
const valueCharLength = config?.prefix ? value.length - 2 : value.length
//If a bitLength specified, does it conform?
if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false
if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false
//Does it only has hex values?
return hexRegex.test(value)
return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)
}
//determine the number of nibbles for a given number of bits
export const bitsToNibbles = (value: number): number => {
const nibbles = value >> 2
if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')
if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')
return nibbles

@@ -6,0 +6,0 @@ }

@@ -1,2 +0,2 @@

export const hexRegex = /^[0-9a-f]+$/i
export const hexRegexWithPrefix = /0x[0-9a-f]+$/i
export const hexRegex = /^[\da-f]+$/i
export const hexRegexWithPrefix = /0x[\da-f]+$/i

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc