JS實(shí)現(xiàn)文本比較差異的示例代碼
內(nèi)部系統(tǒng)上線了一個(gè)發(fā)版記錄發(fā)版內(nèi)容的功能。維護(hù)發(fā)版記錄的同事提出一個(gè)可以展示前后文本差異的優(yōu)化需求。 使的每次變更前可以確認(rèn)新增了哪些,或者刪除了哪些內(nèi)容。項(xiàng)目使用react。
另外,互聯(lián)網(wǎng)面試前刷八股+leetCode已經(jīng)是約定俗成的事情的,但一直覺得刷算法題只是為了應(yīng)付面試。但是這個(gè)任務(wù)意識(shí)到之所以用不到,是因?yàn)榱?xí)慣了三方庫,沒有三方庫,這些算法很有用。
預(yù)期結(jié)果

引入第三方插件jsdiff
基本用法:
npm install diff --save
根據(jù)官方demo,常見的用法有三種:

分別對(duì)應(yīng)提供的方法如下:
Diff.diffChars(oldStr, newStr[, options])Diff.diffWords(oldStr, newStr[, options])Diff.diffWordsWithSpace(oldStr, newStr[, options])Diff.diffLines(oldStr, newStr[, options])
以diffChars為例,項(xiàng)目中按需引入如下:
import { diffChars } from "diff";
在項(xiàng)目中將其提取成一個(gè)組件:
// .....
const { oldStr, newStr } = props;
useEffect(() => {
const diff = diffChars(oldStr, newStr);
console.log(diff, newStr);
let span = null;
const fragment = document.createDocumentFragment();
const display = document.getElementById('content');
diff.forEach((part) => {
const color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
span = document.createElement('span');
span.style.color = color;
if (color == "red") {
span.style.textDecoration = "line-through";
}
if (color == "green") {
span.style.background = "#48ff00";
}
span.appendChild(document
.createTextNode(part.value));
fragment.appendChild(span);
});
display.appendChild(fragment);
}, [oldStr, newStr]);
//.....
對(duì)于接受展示內(nèi)容的外層容器來說,需要注意: 對(duì)于換行符號(hào) \n 需要使用<pre>標(biāo)簽包裹才能保持文本的展示格式。如下
return <>
<pre><div id="content"></div></pre>
</>;
關(guān)于jsdiff算法
An O(ND) Difference Algorithm and Its Variations
Eugene W. Myers • Algorithmica • Published 1986
The problems of finding a longest common subsequence of two sequences A and B and a shortest edit script for transforming A into B have long been known to be dual problems. In this paper, they are shown to be equivalent to finding a shortest/longest path in an edit graph. Using this perspective, a simple O(ND) time and space algorithm is developed where N is the sum of the lengths of A and B and D is the size of the minimum edit script for A and B. The algorithm performs well when differences are small (sequences are similar) and is consequently fast in typical applications. The algorithm is shown to have O(N +D expected-time performance under a basic stochastic model. A refinement of the algorithm requires only O(N) space, and the use of suffix trees leads to an O(NlgN +D ) time variation.
從上面的描述可知,這個(gè)算法的空間復(fù)雜度是O(N),時(shí)間復(fù)雜度是O(nLgN)
刷題常見的[求兩個(gè)字符串中最大子串及最大子序列]以及[最短編輯距離問題]
從這個(gè)庫可知,這些算法很有用。
以上就是JS實(shí)現(xiàn)文本比較差異的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JS文本比較的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript實(shí)現(xiàn)日期格式化的操作詳解
在我們做業(yè)務(wù)開發(fā)的漫長歲月里,會(huì)多次跟時(shí)間打交道,相信大多數(shù)小伙伴對(duì)日期格式化也并不陌生,本文簡(jiǎn)單記錄了JavaScript實(shí)現(xiàn)日期格式化的過程,以及一些拓展,希望對(duì)大家有所幫助2023-05-05
JavaScript自動(dòng)點(diǎn)擊鏈接 防止繞過瀏覽器訪問的方法
下面小編就為大家?guī)硪黄狫avaScript自動(dòng)點(diǎn)擊鏈接 防止繞過瀏覽器訪問的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
詳解微信小程序(Taro)手動(dòng)埋點(diǎn)和自動(dòng)埋點(diǎn)的實(shí)現(xiàn)
這篇文章主要介紹了詳解微信小程序(Taro)手動(dòng)埋點(diǎn)和自動(dòng)埋點(diǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
用javascript取得傳遞參數(shù)的個(gè)數(shù)的代碼
用javascript取得傳遞參數(shù)的個(gè)數(shù)的代碼...2007-10-10
ES6中Proxy與Reflect實(shí)現(xiàn)重載(overload)的方法
這篇文章主要介紹了ES6中Proxy與Reflect實(shí)現(xiàn)重載(overload)的方法,分析了重載的原理及使用Proxy和Reflect來實(shí)現(xiàn)重載的操作步驟與相關(guān)技巧,需要的朋友可以參考下2017-03-03
JavaScript實(shí)現(xiàn)翻頁功能(附效果圖)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)翻頁功能(附效果圖),在項(xiàng)目需求中經(jīng)常遇到,今天小編抽時(shí)間給大家分享JavaScript實(shí)現(xiàn)翻頁功能實(shí)例代碼,需要的朋友參考下吧2017-02-02

