@effected/source

中文文档
0.5.0发布于 2026/11/10MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6383

Source-text positions as plain values: a line index over a text, LSP-style line and character, and the structural range and diagnostic shapes the kit's format packages share.

原始使用文档
文档目录5 节
  1. Why @effected/source
  2. Install
  3. Usage
  4. Documentation
  5. License

@effected/source

npm License: MIT Node.js %3E%3D24.11.0 TypeScript 7.0

Source-text positions as plain values: a line index that turns an offset into an LSP-style zero-based line and character and back, and the range, position and diagnostic shapes the @effected format packages share. No service, no platform, no effect, and no dependency at all.

Pre-1.0.0. This package is part of the @effected/* kit, still in 0.x development. It has no dependencies, not even effect.

Stability: unstable. This package's API surface is not yet considered complete and may change across 0.x releases. Pin an exact version. Full policy: release strategy.

Why @effected/source

A parser reports where something went wrong as an offset into the text. An editor, a language server and a person all want a line and a column. The conversion looks like a five-line loop, and that loop is easy to get subtly wrong: which characters end a line, what an offset between the \r and \n of a Windows line ending means, and whether a column counts code units or characters. This package is the conversion written once, by the rules the Language Server Protocol uses.

Install

npm install @effected/source
pnpm add @effected/source

Requires Node.js >=24.11.0. No dependencies and no peers. ESM-only: import it from an ES module.

Usage

import { LineIndex } from "@effected/source";

const index = LineIndex.make("name: kit\r\nversion: 1\n");

index.positionAt(11);
// { line: 1, character: 0 }

index.offsetAt({ line: 1, character: 9 });
// 20

index.spanOf({ offset: 11, length: 7 });
// { start: { line: 1, character: 0 }, end: { line: 1, character: 7 } }
  • LineIndex.make(text, options?) builds the line table once; every query after that is a binary search. positionAt and offsetAt convert both ways, lineCount, lineAt, lineStart, lineEnd and lineText describe the lines, and spanOf, rangeOf and locationOf convert ranges.
  • Positions are zero-based and character counts UTF-16 code units, LSP's default. Add 1 to both for a person.
  • Lines end at \n, \r\n or a bare \r by default. lineBreaks: "lf" ends them at \n only, and lineBreaks: "unicode" also at U+2028 and U+2029.
  • Out-of-range input clamps to the nearest valid position, as vscode-languageserver-textdocument does. A value that is not an integer throws a RangeError.
  • SourcePosition, SourceRange, SourceSpan, SourceLocation and SourceDiagnostic are structural interfaces, so the diagnostics and ranges of @effected/jsonc, @effected/yaml, @effected/toml and @effected/markdown can be read through them as they are.

Documentation

The guide and the API reference are at effected.spencerbeg.gs/source.

License

MIT

版本与发布记录

@effected/source 共发布 7 个版本,首次发布于 2026/10/29;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.0latest2026/11/102 天前
0.4.02026/11/067 天前
0.3.02026/11/0310 天前
0.2.02026/11/0112 天前
0.1.12026/10/3014 天前
0.1.02026/10/2915 天前

关于 @effected/source 的常见问题

@effected/source 是什么?

Source-text positions as plain values: a line index over a text, LSP-style line and character, and the structural range and diagnostic shapes the kit's format packages share.

如何安装 @effected/source?

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

@effected/source 支持 TypeScript 吗?

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

@effected/source 支持 ESM 还是 CommonJS?

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

@effected/source 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.11.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@effected/source 可以商用吗?

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

@effected/source 的下载量有多少?

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

@effected/source 还在维护吗?

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