npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@deepzs/trustscript

v1.0.4

Published

The package implements some of the features present in the Rust programming language, which are:

Downloads

10

Readme

TRustScript

The package implements some of the features present in the Rust programming language, which are:

  • Option: The Option type provides a way to express the presence or absence of a value. It is used to handle situations where a value may or may not exist.

  • Result: The Result is used to handle operations that can either succeed or fail. It allows for more explicit error handling and propagation of errors throughout the code, similar to Rust's Result type.

These features aim to bring some of the functional error handling and optional value handling capabilities from Rust to the Node.js and TypeScript environment.

Usage

Option type

import { Option } from '@deepzs/trustscript'

// Initializing option 
const option = new Option('Hello!')
// OR
const option = Option.from('Hello!')

const someOption = Option.from('Hello!')
console.log(someOption.isSome()) // true
console.log(someOption.isNone()) // false
console.log(someOption.unwrap()) // "Hello!"
console.log(someOption.unwrapOr("default")) // "Hello!"
console.log(someOption.expect("What happened?")) // "Hello!"
console.log(someOption.map((str) => `${str} World!`)) // Option { _value: "Hello World!" }

const noneOption = Option.from<string>(undefined)
console.log(noneOption.isSome()) // false
console.log(noneOption.isNone()) // true
console.log(noneOption.unwrap()) // Error: Unwrapped value is None
console.log(noneOption.unwrapOr("default")) // "default"
console.log(noneOption.expect("What happened?")) // Error: What happened?
console.log(noneOption.map((str) => `${str} World!`)) // Option { _value: undefined }

Result type

import { Result } from '@deepzs/trustscript'

// Initializing option 
const result = new Result<string, Error>('Hello!', null)
// OR
const result = Result.fromFunction<string, Error>(() => 'Hello!')
// OR
const result = Result.fromAsyncFunction<string, Error>(() => new Promise((resolve) => resolve('Hello!')))
// OR
const myFunction = (arg1: boolean): Result<string, Error> => {
  if (arg1) {
    return Result.ok('Hello!')
  } else {
    return Result.error(new Error('Falsy...'))
  }
}

const successResult = new Result<string, Error>('Hello!', null)
console.log(successResult.isOk()) // true
console.log(successResult.isError()) // false
console.log(successResult.unwrap()) // "Hello!"
console.log(successResult.unwrapOr("default")) // "Hello!"
console.log(successResult.expect("What happened?")) // "Hello!"
console.log(successResult.expectErr("Should be an error")) // Error: Should be an error
console.log(successResult.map((str) => `${str} World!`)) // Result { value: "Hello World!", error: undefined }
console.log(successResult.mapErr((e) => new Error(e.message.toUpperCase())).unwrap()) // Result { value: "Hello!", error: undefined }

const errorResult = new Result<string, Error>(undefined, new Error('MyCustomError'))
console.log(options.isOk()) // false
console.log(options.isError()) // true
console.log(options.unwrap()) // Error: Unwrapped value is Error
console.log(options.unwrapOr("default")) // "default"
console.log(options.expect("What happened?")) // Error: What happened?
console.log(options.expectErr("Should be an error")) // Error { message: "MyCustomError" }
console.log(options.map((str) => `${str} World!`)) // Result { value: undefined, error: Error { message: "MyCustomError" } }
console.log(options.mapErr((e) => new Error(e.message.toUpperCase()))) // Result { value: undefined, error: Error { message: "MYCUSTOMERROR" } }