@freckle/maybe

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

Operations for maybe types

原始使用文档
文档目录5 节
  1. Motivation
  2. Install
  3. Usage
  4. Development
  5. Release

@freckle/maybe

Provides a collection of helper functions for operations on maybe types.

Motivation

This package seeks to recreate idioms common in strongly typed functional programming languages, with heavy influence from the Data.Maybe Haskell package.

Install

pnpm add @freckle/maybe

This package is ESM-only ("type": "module"). Prefer import. require('@freckle/maybe') works on Node 22.12+ via require(esm), but not on older Node or under CJS-only bundlers.

Usage

Operations are used to refine an input from being possibly null or 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!')

Other operations carry the possibly null value after applying a function:

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
})

For dealing with Arrays that may contain null or undefined elements:

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']

Two operations are tailored to use with 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} />
}

Development

  • Package manager: pnpm (Node version pinned in .nvmrc)
  • pnpm build — tsc, emits to dist/
  • pnpm test — Vitest
  • pnpm coverage — Vitest with coverage, gated at 70% (lines/branches/functions/statements)
  • pnpm typecheck — tsc --noEmit, includes test files
  • pnpm lint — ESLint
  • pnpm format / pnpm format-check — Prettier
  • pnpm knip — unused files/dependencies/exports
  • CI runs all of the above on every PR, plus a check that dist/ is up to date

Release

See RELEASE.md for more details.

版本与发布记录

@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 是什么?

Operations for maybe types

如何安装 @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 和提交记录判断。