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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kp-kit/result

v0.1.1

Published

提供类型安全、灵活的类似 [Rust Result](https://doc.rust-lang.org/std/result) 类型实现

Readme

@kp-kit/result

提供类型安全、灵活的类似 Rust Result 类型实现

特性

  • 类型安全
  • 优雅且强制的处理错误

安装

npm install @kp-kit/result

或使用 bun:

bun add @kp-kit/result

快速开始

import { ok, err, Result } from '@kp-kit/result';

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return err('DIVISION_BY_ZERO', '不能除以零');
  }
  return ok(a / b);
}

const result = divide(10, 2);
if (result.isOk()) {
  console.log(result.value); // 5
} else {
  console.log(result.code, result.message); // 错误信息
}

API

类型

  • Result<Value, Code>: 结果抽象类,表示可能成功或失败的结果
  • Ok<Value>: 成功结果
  • Err<Code>: 错误结果

工厂函数

  • ok(value?): 创建成功结果
  • err(code?, message?, cause?): 创建错误结果

实例方法

isOk()

判断结果是否为成功(Ok)。返回布尔值,并自动类型收窄。

const result = ok(123);
if (result.isOk()) {
  // result 类型被收窄为 Ok
  console.log(result.value); // 123
}

isErr()

判断结果是否为失败(Err)。返回布尔值,并自动类型收窄。

const result = err('E', 'fail');
if (result.isErr()) {
  // result 类型被收窄为 Err
  console.log(result.message); // fail
}

unwrap(default?)

解包成功值。若为 Ok,返回其值;若为 Err,无参数时抛出异常,有参数时返回默认值或调用默认函数。

ok(1).unwrap(); // 1
err('E').unwrap(42); // 42
err('E').unwrap(() => 'default'); // 'default'
err('E').unwrap(); // 抛出 Err 异常

expect(message?)

解包成功值。若为 Ok,返回其值;若为 Err,抛出异常并可自定义错误信息。

ok(1).expect(); // 1
err('E').expect('出错了'); // 抛出 Err,message 字段为 '出错了'

map(fn)

对成功值进行转换,返回新的 Result。若为 Err,原样返回。

ok(2).map((x) => x * 10); // Ok(20)
err('E').map((x) => x * 10); // Err('E')

mapErr(fn)

对错误进行转换,返回新的 Err。若为 Ok,原样返回。

err('E', 'fail').mapErr((e) => err('NEW', 'new error')); // Err('NEW', 'new error')
ok(1).mapErr((e) => err('NEW')); // Ok(1)

toPromise()

将 Result 转为 Promise。Ok 时 resolve,Err 时 reject。

await ok(1).toPromise(); // 1
await err('E').toPromise(); // 抛出 Err

静态方法

  • Result.is(obj): 判断对象是否为 Result