Skip to main content

latest

Wrappers for indeterminate data

Works with
This package works with Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
16 hours ago (1.0.1)

wrap

Object wrappers for data: Box, Result and Future. All wrappers are a union between their two forms. All of the constructors' return types are the vaguest of that wrapper type to enforce that wrapper data is not consistent.

Currently, the Full constructor will consider a lack of an argument as being full with undefined. All wrapper constructors are explicit with what the wrapper will be and won't infer that from their single parameter.

Importing

The default entrypoint exports all wrapper modules. If multiple entrypoints are supported, you can import a specific wrapper.

// all wrappers
import * as warp from "@deleteables/warp"
// individual wrapper files
import { type Box, Full, Empty } from "@deleteables/warp/box"
import { type Result, Success, Failure } from "@deleteables/warp/result"
import { type Future, Ready, Pending } from "@deleteables/warp/future"

Examples

A zoo that uses a Box to hold an animal

function zooExhibit(): Box<Animal> { /* ... */}

const animalBox = zooExhibit()
if (animalBox.full) {
	const animal = animalBox.deref() // type: Animal
	console.log(`This ${animal.name} is properly contained.`)
} else {
	console.log("Oh no. This box has no animal. It escaped!")
}

Web requests and their errors can be wrangled with a Result

function askCouncil(question: string): Result<string, Error> { /* ... */ }

const pertinentAnswerResult = askCouncil("When was I is?")
if (pertinentAnswerResult.good) { // good === true for a Success
	const pertinentAnswer = pertinentAnswerResult.deref() // type: string
	console.log(`Ah, finally. The answer we've all been looking for: ${pertinentAnswer}`)
} else {
	const error = pertinentAnswerResult.catch() // type: Error
	console.log(`Something went wrong. ${error.message}`)
}

Dealing with something that might not be loaded yet with Future

function getPicture(person: Person): Future<Image> { /* ... */ }

const myPictureFuture = getPicture(me)
if (!myPictureFuture.ready) {
	showLoadingIcon()
	console.log("Picture will load in soon")
}
myPictureFuture.then(image => { // executed once myPictureFuture is ready
	hideLoadingIcon()
	showImage(image)
	console.log("Picture loaded")
})

License

All files under this package are licensed as shown in the LICENSE file.

Add Package

deno add @deleteables/wrap

Import symbol

import * as mod from "@deleteables/wrap";

Add Package

npx jsr add @deleteables/wrap

Import symbol

import * as mod from "@deleteables/wrap";

Add Package

yarn dlx jsr add @deleteables/wrap

Import symbol

import * as mod from "@deleteables/wrap";

Add Package

pnpm dlx jsr add @deleteables/wrap

Import symbol

import * as mod from "@deleteables/wrap";

Add Package

bunx jsr add @deleteables/wrap

Import symbol

import * as mod from "@deleteables/wrap";