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

@x-oasis/diff-match-patch

v0.2.4

Published

Restore file content to original version based on offset range

Readme

@x-oasis/diff-match-patch

基于 Google 的 diff-match-patch 库,提供将最新文件中指定 offset range 的内容恢复到原始版本的功能。

功能

当文件经过一系列增删改操作后,可以基于最新文件提供 start-offsetend-offset,将指定 range 内的内容恢复到最初版本。

安装

npm install @x-oasis/diff-match-patch
# 或
pnpm add @x-oasis/diff-match-patch

在线示例

查看 交互式示例 来可视化地了解如何使用这个库。示例包含:

  • 文件差异的可视化显示
  • 交互式的 offset range 恢复功能
  • 详细的调试信息展示
  • 多个预设的测试场景

直接在浏览器中打开 examples/index.html 即可使用。

使用方法

使用类方式

import { FileRestoreManager } from '@x-oasis/diff-match-patch';

const originalContent = 'Hello World';
const currentContent = 'Hello Beautiful World';

const manager = new FileRestoreManager(originalContent);

// 恢复 offset 6-15 的内容("Beautiful ")到原始版本
const restored = manager.restoreRange(currentContent, {
  startOffset: 6,
  endOffset: 15
});

console.log(restored); // "Hello World"

使用便捷函数

import { restoreRange } from '@x-oasis/diff-match-patch';

const originalContent = 'Hello World';
const currentContent = 'Hello Beautiful World';

// 恢复 offset 6-15 的内容到原始版本
const restored = restoreRange(originalContent, currentContent, 6, 15);

console.log(restored); // "Hello World"

API

FileRestoreManager

构造函数

new FileRestoreManager(originalContent: string)

创建一个文件恢复管理器实例。

  • originalContent: 原始文件内容

方法

restoreRange
restoreRange(
  currentContent: string,
  options: { startOffset: number; endOffset: number }
): string

将最新文件中指定 offset range 的内容恢复到原始版本。

  • currentContent: 最新文件内容
  • options.startOffset: 恢复范围的起始 offset(基于最新文件)
  • options.endOffset: 恢复范围的结束 offset(基于最新文件)
  • 返回: 恢复后的文件内容
getOriginalContent
getOriginalContent(): string

获取原始文件内容。

updateOriginalContent
updateOriginalContent(newOriginalContent: string): void

更新原始文件内容。

restoreRange (便捷函数)

restoreRange(
  originalContent: string,
  currentContent: string,
  startOffset: number,
  endOffset: number
): string

便捷函数,直接恢复指定 range 的内容。

示例

示例 1: 恢复插入的内容

const original = 'Hello World';
const current = 'Hello Beautiful World';

// 恢复插入的 "Beautiful " 部分
const restored = restoreRange(original, current, 6, 15);
// 结果: "Hello World"

示例 2: 恢复删除的内容

const original = 'Hello Beautiful World';
const current = 'Hello World';

// 在删除的位置恢复内容
const restored = restoreRange(original, current, 6, 6);
// 结果: "Hello Beautiful World"

示例 3: 恢复修改的内容

const original = 'The quick brown fox';
const current = 'The fast brown fox';

// 恢复 "fast" 为 "quick"
const restored = restoreRange(original, current, 4, 8);
// 结果: "The quick brown fox"

错误处理

如果提供的 offset range 无效,函数会抛出错误:

  • Invalid offset range: startOffset 或 endOffset 无效
  • Offset range exceeds current content length: offset 超出文件长度

依赖

License

ISC