lint: lib/workers/dmpWorker.ts

- add diff-match-patch typing
- add typing annotate

Signed-off-by: Raccoon <raccoon@hackmd.io>
This commit is contained in:
Raccoon 2021-06-14 08:04:51 +08:00
parent 9ef827f7d7
commit 3eca23810f
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38
3 changed files with 102 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import DiffMatchPatch from "@hackmd/diff-match-patch";
// core
import config from "../config";
import {logger} from "../logger";
import {Revision} from "../models";
const dmp = new DiffMatchPatch()
process.on('message', function (data) {
@ -57,25 +58,31 @@ process.on('message', function (data) {
return null
})
function createPatch(lastDoc, currDoc) {
function createPatch(lastDoc: string, currDoc: string): string {
const msStart = (new Date()).getTime()
const diff = dmp.diff_main(lastDoc, currDoc)
let patch = dmp.patch_make(lastDoc, diff)
patch = dmp.patch_toText(patch)
const patch = dmp.patch_make(lastDoc, diff)
const patchText = dmp.patch_toText(patch)
const msEnd = (new Date()).getTime()
if (config.debug) {
logger.info(patch)
logger.info(patchText)
logger.info((msEnd - msStart) + 'ms')
}
return patch
return patchText
}
function getRevision(revisions, count) {
interface DiffRevision {
content: string,
patch: Patch[],
authorship: string
}
function getRevision(revisions: Revision[], count: number): DiffRevision {
const msStart = (new Date()).getTime()
let startContent = null
let lastPatch = []
let lastPatch = ""
let applyPatches = []
let authorship = []
let authorship = ""
if (count <= Math.round(revisions.length / 2)) {
// start from top to target
for (let i = 0; i < count; i++) {

View File

@ -1,12 +1,16 @@
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": ".",
"allowJs": false,
"target": "ES2019",
"outDir": "./dist/",
"module": "CommonJS",
"esModuleInterop": true,
"typeRoots": ["./typings", "./node_modules/@types"]
"typeRoots": ["./typings", "./node_modules/@types"],
"paths": {
"@hackmd/diff-match-patch": ["./typings/diff-match-patch/"]
}
},
"include": [
"./lib/**/*"

82
typings/diff-match-patch/index.d.ts vendored Normal file
View File

@ -0,0 +1,82 @@
declare global {
module diffMatchPatch {
export const DIFF_DELETE = -1;
export const DIFF_INSERT = 1;
export const DIFF_EQUAL = 0;
}
enum DiffType {
DIFF_DELETE = -1,
DIFF_INSERT = 1,
DIFF_EQUAL = 0
}
type Diff = {
[0]: DiffType
[1]: string
}
interface Patch {
diffs: Diff[];
start1?: number
start2?: number
length1: number
length2: number
toString(): string
}
class diffMatchPatch {
diff_main(text1: string, text2: string, opt_checklines?: boolean,
opt_deadline?: number): Diff[]
diff_cleanupSemantic(diffs: Diff[])
diff_cleanupEfficiency(diffs: Diff[])
diff_cleanupSemanticLossless(diffs: Diff[])
diff_levenshtein(diffs: Diff[]): number
diff_prettyHtml(diffs: Diff[]): string
diff_xIndex(diffs: Diff[], loc: number): number
diff_cleanupMerge(diffs: Diff[])
match_main(text: string, pattern: string, loc: number): number
diff_fromDelta(text1: string, delta: string): Diff[]
diff_toDelta(diffs: Diff[]): string
diff_levenshtein(diffs: Diff[]): number
diff_text1(diffs: Diff[]): string
diff_text2(diffs: Diff[]): string
patch_make(a: string, opt_b: string): Patch[]
patch_make(a: Diff[]): Patch[]
patch_make(a: string, opt_b: Diff[]): Patch[]
patch_make(a: string, opt_b: string, opt_c: Diff[]): Patch[]
patch_splitMax(patches: Patch[])
patch_addPadding(patches: Patch[]): string
patch_deepCopy(patches: Patch[]): Patch[]
patch_toText(patches: Patch[]): string
patch_fromText(textline: string): Patch[]
patch_apply(patches: Patch[], text: string): {
[0]: string
[1]: boolean[]
}
}
}
export = diffMatchPatch