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

@tikkhun/result

v0.0.8

Published

我们大量需要使用result

Readme

result

我们大量需要使用result

  • 比如日志 我们希望系统性定义他们的code message
  • 比如express 返回 我们希望可以返回国际化友好的日志
  • 前端的错误提醒等等 我们目的是实现结果的工程化,也就是可维护性能力要强

一般来说没有工程化的操作就是打印日志等爱写啥写啥,没有规定的话就是爱咋写咋写,十分混乱 本小工具目的就是工程化地、统一地管理我们的结果输出

设计

result 输入

  • success 成功与否
  • token 业务链条 比如 ['user', 'login'] 借鉴国际化常见操作,用'user.login' 这样表示也可
  • error 错误体 即代码中捕获的错误
  • payload 其他的数据可以暂存在这里 因为经常我们知道结果还想知道他相关的一些其他东西,比如上下文 我们输出主要就是以我们自己语言写的日志,以及有时候更方便的是使用code快速区分和分类结果的类型 那么我们便约定用json这种轻松的配置,来配置结果与 locale /code 的映射关系 message 约定了
  • success部分是成功的message ,error 是错误的
  • 约定了{}简单的插值来填充相关的信息 这样我们可以顺利获取多语言的日志信息,以及结果code

使用

使用上的心智负担要求主要就是要工程化的管理咱们的业务模块,以业务区分我们的结果,从而形成独一无二的token(code)

比如后台认证,我们的业务token 可以这样编写

  • 用户登录验证码校验 user.sign_in.verify_captcha
  • 用户登录获取token user.sign_in.get_token
  • 用户登录刷新token user.sign_in.refresh_token
  • 用户退出 user.sign_out 之类的 然后分别写我们的message message 的心智负担主要是要想好插值,当错误的时候要联系上下文和错误信息

使用示例

如文档未及时更新,详见单侧与 src 示例

import { BestResultFactory } from '../lib';
import zh from './zh';
const resultFactory = new BestResultFactory();

const result = resultFactory.createResult({
  success: false, // 成功与否
  token: 'user.login', // 业务链条 比如 ['user', 'login']
  error: new Error('没事抛个错'), // 错误体 即代码中捕获的错误
  payload: {}, // 其他的数据可以暂存在这里
  // 直接的输出者
});
resultFactory.addLocale('zh', zh); // 第一个添加的是默认语言
resultFactory.addLocale('en', {
  error: {
    user: {
      login: 'login failed, reason is {error}',
    },
  },
});4
console.log(`result`, result);
console.log(`result.getString() `, result.getString());
console.log(`result.getCode()`, result.getCode());
console.log(`result.getString('en')`, result.getString('en'));