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

@layerzerolabs/lz-utilities

Package Overview
Dependencies
Maintainers
1
Versions
222
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@layerzerolabs/lz-utilities

  • 3.0.39
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-20.94%
Maintainers
1
Weekly downloads
 
Created
Source

@layerzerolabs/lz-utilities

The LayerZero Utilities package provides a set of essential utilities and modules to facilitate the development and integration of applications with various blockchain networks. It includes functions for logging, assertions, formatting, and more.

Features

  • Logging: Provides a customizable logger.
  • Assertions: Includes functions for runtime type assertions.
  • Formatting: Utilities for formatting and converting data.
  • Path Utilities: Functions for handling file paths.
  • Array Utilities: Safe array operations.
  • Enum Utilities: Functions for working with enums.

Installation

To install the LayerZero Utilities package, you can use npm or yarn:

npm install @layerzerolabs/lz-utilities

or

yarn add @layerzerolabs/lz-utilities

Usage

Logging

import { initLogger, getLogger } from "@layerzerolabs/lz-utilities";

initLogger("info");
const logger = getLogger();
logger.info("Logger initialized");

Assertions

Asserts that a condition is true. If the condition is false, throws an error with the provided message.

  • condition: The condition to assert.
  • message: The error message to throw if the condition is false.
import { assert } from "@layerzerolabs/lz-utilities";

const condition = true;
assert(condition, "Condition must be true");

Formatting

Convert Bytes to Hex

Converts a Uint8Array to a hex string.

  • bytes: The bytes to convert.
  • Returns: The hex string.
import { bytesToHex } from "@layerzerolabs/lz-utilities";

// Convert Bytes to Hex
const bytes = new Uint8Array([1, 2, 3, 4]);
const hex = bytesToHex(bytes);
console.log(`Hex: ${hex}`);

Path Utilities

dirname

Returns the directory name of a path.

  • path: The path to get the directory name from.
  • Returns: The directory name of the path.
import { dirname } from "@layerzerolabs/lz-utilities";

// Get Directory Name
const path = "/path/to/file.txt";
const dir = dirname(path);
console.log(`Directory: ${dir}`);

Array Utilities

Safe Map

Calls a defined callback function on each element of an array, and returns an array that contains the results. If an error occurs during the execution of the callback function, the function returns the results up to that point and the error.

  • elements: The array to map over.
  • callbackfn: A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
  • Returns: A tuple containing the array of results and an optional error.
import { safeMap } from "@layerzerolabs/lz-utilities";

const array = [1, 2, 3];
const [result, error] = safeMap(array, (item) => item * 2);
if (error) {
  console.error(`Error: ${error.message}`);
} else {
  console.log(`Result: ${result}`);
}

Enum Utilities

asEnum

Converts a string or number value to a corresponding enum value.

  • enumType: The enum object.
  • value: The value to convert.
  • Returns: The converted enum value.
  • Throws: If the value is not a valid enum value.
import { asEnum } from "@layerzerolabs/lz-utilities";

enum Color {
  Red = "red",
  Green = "green",
  Blue = "blue",
}

// Convert string to Enum
const color: Color = asEnum(Color, "red");
console.log(`Color: ${color}`);

FAQs

Package last updated on 07 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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