Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
This library contains Option
and Result
types ported from Rust. You can use it for JavaScript
and TypeScript projects to create null
-safe applications.
npm install --save opty
var opty = require("opty");
Require .d.ts for typescript:
/// <reference path="./node_modules/opty/index.d.ts" />
import opty = require("opty");
interface Option<T> {
/**
* Returns true if the option is a Some value
*/
isSome(): boolean;
/**
* Returns true if the option is a None value
*/
isNone(): boolean;
/**
* Returns check result if the option is a Some value
*/
isSomeAnd(fn: (a: T) => boolean): boolean;
/**
* Returns check result if the option is a None value
*/
isNoneAnd(fn: () => boolean): boolean;
/**
* Returns the inner T of a Some(T). Throws an exception if the self value equals None.
*/
unwrap(): T;
/**
* Returns the contained value or a default.
* @param def
*/
unwrapOr(def: T): T;
/**
* Returns the contained value or computes it from a closure.
* @param f
*/
unwrapOrElse(f: () => T): T;
/**
* Maps an Option<T> to Option<U> by applying a function to a contained value
* @param f
*/
map<U>(f: (a: T) => U): Option<U>;
/**
* Applies a function to the contained value or returns a default.
* @param def
* @param f
*/
mapOr<U>(def: U, f: (a: T) => U): U;
/**
* Applies a function to the contained value or computes a default.
* @param def
* @param f
*/
mapOrElse<U>(def: () => U, f: (a: T) => U): U;
/**
* Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).
* @param err
*/
okOr<E>(err: E): Result<T, E>;
/**
* Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).
* @param err
*/
okOrElse<E>(err: () => E): Result<T, E>;
/**
* Returns None if the option is None, otherwise returns optb.
* @param optb
*/
and<U>(optb: Option<U>): Option<U>;
/**
* Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.
* Some languages call this operation flatmap.
* @param f
*/
andThen<U>(f: (a: T) => Option<U>): Option<U>;
/**
* Returns the option if it contains a value, otherwise returns optb.
* @param optb
*/
or(optb: Option<T>): Option<T>;
/**
* Returns the option if it contains a value, otherwise calls f and returns the result.
* @param f
*/
orElse(f: () => Option<T>): Option<T>;
}
export interface Result<T, E> {
/**
* Returns true if the result is Ok
*/
isOk(): boolean;
/**
* Returns true if the result is Err
*/
isErr(): boolean;
/**
* Convert from Result<T, E> to Option<T>
* Converts self into an Option<T>, and discarding the error, if any.
*/
ok(): Option<T>;
/**
* Convert from Result<T, E> to Option<E>
* Converts self into an Option<E>, and discarding the value, if any.
*/
err(): Option<E>;
/**
* Maps a Result<T, E> to Result<U, E> by applying a function to an contained Ok value, leaving an Err value untouched.
* This function can be used to compose the results of two functions.
* @param fn
*/
map<U>(fn: (a: T) => U): Result<U, E>;
/**
* Maps a Result<T, E> to Result<T, F> by applying a function to an contained Err value, leaving an Ok value untouched.
* This function can be used to pass through a successful result while handling an error.
* @param fn
*/
mapErr<U>(fn: (a: E) => U): Result<T, U>;
/**
* Returns res if the result is Ok, otherwise returns the Err value of self.
* @param res
*/
and<U>(res: Result<U,E>): Result<U,E>;
/**
* Calls op if the result is Ok, otherwise returns the Err value of self.
* This function can be used for control flow based on result values.
* @param op
*/
andThen<U>(op: (a: T) => Result<U,E>): Result<U,E>;
/**
* Returns res if the result is Err, otherwise returns the Ok value of self.
* @param res
*/
or(res: Result<T,E>): Result<T,E>;
/**
* Calls op if the result is Err, otherwise returns the Ok value of self.
* This function can be used for control flow based on result values.
* @param op
*/
orElse<U>(op: (a: E) => Result<T,U>): Result<T,U>;
/**
* Unwraps a result, yielding the content of an Ok.
*/
unwrap(): T;
/**
* Unwraps a result, yielding the content of an Ok. Else it returns optb.
*/
unwrapOr(optb: T): T;
/**
* Unwraps a result, yielding the content of an Err.
* @param op
*/
unwrapOrElse(op: (u: E) => T): T
}
FAQs
Option and Result type ported from Rust
The npm package opty receives a total of 0 weekly downloads. As such, opty popularity was classified as not popular.
We found that opty demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.