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

node-result

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-result - npm Package Compare versions

Comparing version 10.1.0 to 11.0.0

54

lib/index.d.ts
export { ResultError, resultError } from 'node-result-error';
export declare enum AtomOK {
OK = "OK"
export declare namespace types {
type Result<DataType, ErrorType> = ResultOK<DataType> | ResultFAIL<ErrorType>;
type ResultAsync<DataType, ErrorType> = Promise<Result<DataType, ErrorType>>;
}
export declare enum AtomFAIL {
FAIL = "FAIL"
export declare type ErrorProcessing<DataType, ErrorType> = (error: ErrorType) => DataType;
export declare type ErrorProcessingAsync<DataType, ErrorType> = (error: ErrorType) => Promise<DataType>;
export declare class Result<DataType, ErrorType> {
protected readonly data: DataType;
protected readonly error: ErrorType;
constructor(data: DataType, error: ErrorType);
}
declare type Atom = typeof AtomOK.OK | typeof AtomFAIL.FAIL;
export declare type ErrorProcessing<D, E> = (error: E) => D;
export declare type ErrorProcessingAsync<D, E> = (error: E) => Promise<D>;
export declare type ReturningResult<D, E> = ResultOK<D> | ResultFAIL<E>;
export declare type ReturningResultAsync<D, E> = Promise<ResultOK<D> | ResultFAIL<E>>;
export declare class Result<D, E> {
protected readonly data: D;
protected readonly error: E;
protected readonly atom: Atom;
constructor(data: D, error: E, atom?: Atom);
onError(func: ErrorProcessing<D, E>): D;
onErrorAsync(func: ErrorProcessingAsync<D, E>): Promise<D>;
export declare class ResultOK<DataType> extends Result<DataType, undefined> {
constructor(data: DataType);
unwrap(): DataType;
unwrapAsync(): Promise<DataType>;
isOk(): boolean;
isFail(): boolean;
onError(func: ErrorProcessing<DataType, never>): DataType;
onErrorAsync(func: ErrorProcessingAsync<DataType, never>): Promise<DataType>;
}
export declare class ResultOK<D> extends Result<D, undefined> {
constructor(data: D);
unwrap(): D;
unwrapAsync(): Promise<D>;
}
export declare class ResultFAIL<E> extends Result<undefined, E> {
constructor(error: E);
export declare class ResultFAIL<ErrorType> extends Result<undefined, ErrorType> {
constructor(error: ErrorType);
unwrap(): never;
unwrapAsync(): Promise<E>;
unwrapAsync(): Promise<ErrorType>;
isOk(): boolean;
isFail(): boolean;
onError<DataType>(func: ErrorProcessing<never, ErrorType>): DataType;
onErrorAsync<DataType>(func: ErrorProcessingAsync<never, ErrorType>): Promise<DataType>;
}
export declare const ResultOk: <D>(data: D) => ResultOK<D>;
export declare const ResultFail: <E>(error: E) => ResultFAIL<E>;
export declare function tryCatchWrapper<C, D, E>(target: C, property: string, descriptor: TypedPropertyDescriptor<(...args: never[]) => D | ResultFAIL<E>>): TypedPropertyDescriptor<(...args: never[]) => D | ResultFAIL<E>>;
export declare function tryCatchWrapperAsync<C, D, E>(target: C, property: string, descriptor: TypedPropertyDescriptor<(...args: never[]) => Promise<D | ResultFAIL<E>>>): TypedPropertyDescriptor<(...args: never[]) => Promise<D | ResultFAIL<E>>>;
export declare const ok: <DataType>(data: DataType) => ResultOK<DataType>;
export declare const fail: <ErrorType>(error: ErrorType) => ResultFAIL<ErrorType>;
export declare function tryCatch<C, D, E>(target: C, property: string, descriptor: TypedPropertyDescriptor<(...args: never[]) => D | ResultFAIL<E>>): TypedPropertyDescriptor<(...args: never[]) => D | ResultFAIL<E>>;
export declare function tryCatchAsync<C, D, E>(target: C, property: string, descriptor: TypedPropertyDescriptor<(...args: never[]) => Promise<D | ResultFAIL<E>>>): TypedPropertyDescriptor<(...args: never[]) => Promise<D | ResultFAIL<E>>>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryCatchWrapperAsync = exports.tryCatchWrapper = exports.ResultFail = exports.ResultOk = exports.ResultFAIL = exports.ResultOK = exports.Result = exports.AtomFAIL = exports.AtomOK = exports.resultError = exports.ResultError = void 0;
exports.tryCatchAsync = exports.tryCatch = exports.fail = exports.ok = exports.ResultFAIL = exports.ResultOK = exports.Result = exports.resultError = exports.ResultError = void 0;
var node_result_error_1 = require("node-result-error");
Object.defineProperty(exports, "ResultError", { enumerable: true, get: function () { return node_result_error_1.ResultError; } });
Object.defineProperty(exports, "resultError", { enumerable: true, get: function () { return node_result_error_1.resultError; } });
var AtomOK;
(function (AtomOK) {
AtomOK["OK"] = "OK";
})(AtomOK = exports.AtomOK || (exports.AtomOK = {}));
var AtomFAIL;
(function (AtomFAIL) {
AtomFAIL["FAIL"] = "FAIL";
})(AtomFAIL = exports.AtomFAIL || (exports.AtomFAIL = {}));
class Result {
constructor(data, error, atom = AtomOK.OK) {
constructor(data, error) {
this.data = data;
this.error = error;
this.atom = atom;
}
onError(func) {
if (this.atom === AtomOK.OK) {
return this.data;
}
return func(this.error);
}
onErrorAsync(func) {
if (this.atom === AtomOK.OK) {
return Promise.resolve(this.data);
}
return func(this.error);
}
isOk() {
return this.atom === AtomOK.OK;
}
isFail() {
return this.atom !== AtomOK.OK;
}
}

@@ -43,3 +16,3 @@ exports.Result = Result;

constructor(data) {
super(data, void 0, AtomOK.OK);
super(data, void 0);
}

@@ -52,2 +25,16 @@ unwrap() {

}
isOk() {
return true;
}
isFail() {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onError(func) {
return this.data;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onErrorAsync(func) {
return Promise.resolve(this.data);
}
}

@@ -57,3 +44,3 @@ exports.ResultOK = ResultOK;

constructor(error) {
super(void 0, error, AtomFAIL.FAIL);
super(void 0, error);
}

@@ -66,9 +53,21 @@ unwrap() {

}
isOk() {
return false;
}
isFail() {
return true;
}
onError(func) {
return func(this.error);
}
onErrorAsync(func) {
return func(this.error);
}
}
exports.ResultFAIL = ResultFAIL;
const ResultOk = (data) => new ResultOK(data);
exports.ResultOk = ResultOk;
const ResultFail = (error) => new ResultFAIL(error);
exports.ResultFail = ResultFail;
function tryCatchWrapper(target, property, descriptor) {
const ok = (data) => new ResultOK(data);
exports.ok = ok;
const fail = (error) => new ResultFAIL(error);
exports.fail = fail;
function tryCatch(target, property, descriptor) {
const self = descriptor.value;

@@ -81,7 +80,7 @@ descriptor.value = function (...args) {

else {
return exports.ResultFail(new TypeError('Descriptor value is not a function.'));
return exports.fail(new TypeError('Descriptor value is not a function.'));
}
}
catch (error) {
return exports.ResultFail(error);
return exports.fail(error);
}

@@ -91,4 +90,4 @@ };

}
exports.tryCatchWrapper = tryCatchWrapper;
function tryCatchWrapperAsync(target, property, descriptor) {
exports.tryCatch = tryCatch;
function tryCatchAsync(target, property, descriptor) {
const self = descriptor.value;

@@ -101,7 +100,7 @@ descriptor.value = async function (...args) {

else {
return exports.ResultFail(new TypeError('Descriptor value is not a function.'));
return exports.fail(new TypeError('Descriptor value is not a function.'));
}
}
catch (error) {
return exports.ResultFail(error);
return exports.fail(error);
}

@@ -111,2 +110,2 @@ };

}
exports.tryCatchWrapperAsync = tryCatchWrapperAsync;
exports.tryCatchAsync = tryCatchAsync;
{
"name": "node-result",
"version": "10.1.0",
"version": "11.0.0",
"description": "result",

@@ -5,0 +5,0 @@ "keywords": [

@@ -14,13 +14,13 @@ # node-result

```ts
import {ResultOk, ResultFail} from "node-result";
import { ok, fail } from "node-result";
async function checkIfNotString(data: any) {
try {
if (typeof data !== 'string') {
return ResultFail(void 0);
}
return ResultOk(null);
} catch (error) {
return ResultFail(error);
try {
if (typeof data !== 'string') {
return fail(void 0);
}
return ok(null);
} catch (error) {
return fail(error);
}
}

@@ -30,17 +30,17 @@

(await checkIfNotString('foo'));
// return Result
(await checkIfNotString(5));
// return Result
(await checkIfNotString('bar'));
// return Result
(await checkIfNotString('foo'));
// return Result
(await checkIfNotString(5));
// return Result
(await checkIfNotString('bar'));
// return Result
(await checkIfNotString('foo')).unwrap();
// return null
(await checkIfNotString(5)).unwrap();
// throw undefined or Error
(await checkIfNotString('bar')).unwrap();
// not done
(await checkIfNotString('foo')).unwrap();
// return null
(await checkIfNotString(5)).unwrap();
// throw undefined or Error
(await checkIfNotString('bar')).unwrap();
// not done
})();
```
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