@freckle/maybe

中文文档
3.0.0发布于 2026/09/02MITTypeScriptES Module零依赖近 30 天下载 2666

可能类型操作

中文使用文档
文档目录5 节
  1. 动机
  2. 安装
  3. 使用量
  4. 发展
  5. 释放

@ frecle/ 也许

提供可能类型操作的帮助函数集合 .

动机

这个软件包试图重现在强烈打入的功能编程语言中常见的idioms,并受到来自 数据 也许哈斯凯尔包。 。 。 .

安装

pnpm add @freckle/maybe

此套装仅限无害环境管理("type": "module") (中文(简体) ). 偏爱 import。 。 。 . require('@freckle/maybe') 在22.12+节点上工作 require(esm),但不能在更古老的节点 或仅由CJS捆绑.

使用量

业务是用来完善一项可能的投入 null 或者说 undefined数字 :

import {fromJust, fromMaybe, maybe} from '@freckle/maybe'

// Return input from a user, if any:
declare function getUserInput(): ?string

const mUserInput = getUserInput()

// Go from ?string -> string:
const input = fromMaybe(() => 'No input', mUserInput)

// Run a function on ?string with a default:
const capitalized = maybe(() => 'No input to capitalize', capitalize, mUserInput)

// Or produce an error on null | undefined to make future execution more predictable:
const userInput = fromJust(mUserInput, 'No input was given!')

其他行动可能携带 null 应用函数后的值 :

import {mmap, mthen} from '@freckle/maybe'

// Return input from a user, if any:
declare function getUserInput(): ?string

// Function that does not handle a null | undefined value:
declare function transform(input: string): string

const mUserInput = getUserInput()

const mTransformed = mmap(transform, mUserInput)
// => null | undefined | transform(mUserInput)

// Alternate form that is more helpful for control flow:
mthen(mTransformed, (transformedUserInput: string) => {
  // Process value
})

用于处理可能包含的阵列 null 或者说 undefined 要素 :

import {catMaybes, mapMaybes} from '@freckle/maybe'

const arr = [
  null,
  'foo',
  undefined,
  'bar'
]

const out = catMaybes(arr)

console.log(out) // => ['foo', 'bar']

const padString = (input: string) => `  ${input}`

const mapped = mapMaybes(arr, padString)

console.log(mapped) // => ['  foo', '  bar']

有两个操作被定制用于React:

import {mEffect, asHTMLAttributeValue} from '@freckle/maybe'

type Props = {
  myInput: ?string
}

const MyComponent = (props: Props): React.Node => {
  // This prop may be string | undefined | null
  const possibleInput = props.myInput

  React.useEffect(() => {
    mEffect(possibleInput, input =>
      // Call a side effect that does not handle a null value:
      sideEffect(input)
    )
  }, [possibleInput])

  // Rendering an element with an attribute from a maybe value:
  const attrObj = {'my-attribute': asHTMLAttributeValue(possibleInput)}

  // If input is not a string, <div> is rendered;
  // otherwise: <div my-attribute="..." />
  return <div {...attrObj} />
}

发展

  • 软件包管理器: pnpm(节点版本被钉入) .nvmrc(中文(简体) )
  • pnpm build —— 说 tsc,发送到 dist/
  • pnpm test - 维特
  • pnpm coverage - 覆盖率为70%的Vitest(线路/行/功能/说明)
  • pnpm typecheck —— 说 tsc --noEmit,包括测试文件
  • pnpm lint ——ESLint公司
  • pnpm format 编号 : pnpm format-check - 更高级
  • pnpm knip ——未用文件/依赖/导出
  • CI在每份公关上 都查了一下 dist/ 最新数据

释放

见 发 言 更多细节.

版本与发布记录

@freckle/maybe 共发布 5 个版本,首次发布于 2023/03/11;过去 12 个月发布了 3 个版本。

版本发布日期距今
3.0.0latest2026/09/022 个月前
2.3.12026/08/272 个月前
2.3.02026/04/236 个月前
2.2.02023/03/143 年前
2.1.02023/03/113 年前

关于 @freckle/maybe 的常见问题

@freckle/maybe 是什么?

可能类型操作

如何安装 @freckle/maybe?

在项目目录运行 npm install @freckle/maybe,或使用 pnpm add / yarn add / bun add。

@freckle/maybe 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freckle/maybe 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@freckle/maybe 可以商用吗?

@freckle/maybe 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@freckle/maybe 的下载量有多少?

根据 npm 官方下载统计,@freckle/maybe 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,666 次,日均约 89 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freckle/maybe 还在维护吗?

最新版本 3.0.0 发布于 2026/09/02(2 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 3 个;首次发布于 2023/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。