Skip to main content
AI tutorials

Use jev-leftpad to Left-Pad JavaScript Values with Jev

This tutorial explains how to install jev-leftpad, configure its TypeSafe API key, call its asynchronous left-padding API, understand its 0-to-10-space limitation, and run its mocked test suite.

Use jev-leftpad to Left-Pad JavaScript Values with Jev

What jev-leftpad Does

jev-leftpad left-pads a value by asking Jev to select how many spaces should be added. It is intentionally unconventional: although JavaScript's built-in padStart() can solve this problem without a network request, this package uses jev-latest through TypeSafe's native @typesafe-ai/sdk.

Each call makes one TypeSafe API request. Jev chooses among options named space_0 through space_10, and JavaScript converts the selected option into spaces before placing the original value after them.

Prerequisites

You need Node.js 20 or newer and a TypeSafe API key. Set the TYPESAFE_API_KEY environment variable before using the package.

export TYPESAFE_API_KEY="your-api-key"

On Windows PowerShell, you can set the variable for the current session with:

$env:TYPESAFE_API_KEY="your-api-key"

Installation

Create or open a Node.js project, then install the package with npm:

npm install jev-leftpad

The package uses ECMAScript module syntax in its documented example, so ensure your project is configured to support import statements.

Basic Usage

Import the default export and call leftPad with the value and target length. Because the package makes an API request, the function is asynchronous and must be awaited.

import leftPad from 'jev-leftpad';

const result = await leftPad('jev', 8);

console.log(JSON.stringify(result));
// "     jev" (probably)

The target length is the desired total length of the returned value. In this example, jev has three characters and the target length is eight, so Jev should select space_5. JavaScript then creates five spaces and places jev after them.

Understanding the API

leftPad(value, targetLength)

  • value: The value to place after the padding.
  • targetLength: A non-negative safe integer representing the requested target length.

The package supports adding between zero and ten spaces. If more than ten spaces are needed, Jev has no correct option. The README describes this limitation as an intentional part of the project's design.

Operational Considerations

Every invocation makes one TypeSafe API request, and retries are disabled. A request may fail, or Jev may choose the wrong option. The operation also costs more than JavaScript's native padStart().

Do not use jev-leftpad in production or for anything important.

For ordinary application code, use String.prototype.padStart() instead. jev-leftpad is best treated as an experiment or demonstration of an AI-backed implementation of a simple operation.

Testing and Development

To work on the project or run its tests, install the development dependencies and execute the test command:

npm install
npm test

The tests mock Jev, so they do not require a TypeSafe API key and do not spend TypeSafe credits. This makes the test suite suitable for local development without making live API requests.

Conclusion

jev-leftpad provides an asynchronous left-padding function powered by Jev and TypeSafe. Install it with npm, configure TYPESAFE_API_KEY, call leftPad(value, targetLength), and remember its strict 0-to-10-space range and lack of retries. For reliable production padding, prefer the built-in padStart().