Skip to content

Commit bcc8921

Browse files
committed
lint: lib/workers/dmpWorker.ts
- add diff-match-patch typing - add typing annotate
1 parent 2e67d0c commit bcc8921

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

lib/workers/dmpWorker.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import DiffMatchPatch from "@hackmd/diff-match-patch";
44
// core
55
import config from "../config";
66
import {logger} from "../logger";
7+
import {Revision} from "../models";
78

89
const dmp = new DiffMatchPatch()
910
process.on('message', function (data) {
@@ -57,25 +58,31 @@ process.on('message', function (data) {
5758
return null
5859
})
5960

60-
function createPatch(lastDoc, currDoc) {
61+
function createPatch(lastDoc: string, currDoc: string): string {
6162
const msStart = (new Date()).getTime()
6263
const diff = dmp.diff_main(lastDoc, currDoc)
63-
let patch = dmp.patch_make(lastDoc, diff)
64-
patch = dmp.patch_toText(patch)
64+
const patch = dmp.patch_make(lastDoc, diff)
65+
const patchText = dmp.patch_toText(patch)
6566
const msEnd = (new Date()).getTime()
6667
if (config.debug) {
67-
logger.info(patch)
68+
logger.info(patchText)
6869
logger.info((msEnd - msStart) + 'ms')
6970
}
70-
return patch
71+
return patchText
7172
}
7273

73-
function getRevision(revisions, count) {
74+
interface DiffRevision {
75+
content: string,
76+
patch: Patch[],
77+
authorship: string
78+
}
79+
80+
function getRevision(revisions: Revision[], count: number): DiffRevision {
7481
const msStart = (new Date()).getTime()
7582
let startContent = null
76-
let lastPatch = []
83+
let lastPatch = ""
7784
let applyPatches = []
78-
let authorship = []
85+
let authorship = ""
7986
if (count <= Math.round(revisions.length / 2)) {
8087
// start from top to target
8188
for (let i = 0; i < count; i++) {

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
22
"compileOnSave": true,
33
"compilerOptions": {
4+
"baseUrl": ".",
45
"allowJs": false,
56
"target": "ES2019",
67
"outDir": "./dist/",
78
"module": "CommonJS",
89
"esModuleInterop": true,
9-
"typeRoots": ["./typings", "./node_modules/@types"]
10+
"typeRoots": ["./typings", "./node_modules/@types"],
11+
"paths": {
12+
"@hackmd/diff-match-patch": ["./typings/diff-match-patch/"]
13+
}
1014
},
1115
"include": [
1216
"./lib/**/*"

typings/diff-match-patch/index.d.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
declare global {
2+
module diffMatchPatch {
3+
export const DIFF_DELETE = -1;
4+
export const DIFF_INSERT = 1;
5+
export const DIFF_EQUAL = 0;
6+
}
7+
8+
enum DiffType {
9+
DIFF_DELETE = -1,
10+
DIFF_INSERT = 1,
11+
DIFF_EQUAL = 0
12+
}
13+
14+
type Diff = {
15+
[0]: DiffType
16+
[1]: string
17+
}
18+
19+
interface Patch {
20+
diffs: Diff[];
21+
22+
start1?: number
23+
start2?: number
24+
25+
length1: number
26+
length2: number
27+
28+
toString(): string
29+
}
30+
31+
class diffMatchPatch {
32+
diff_main(text1: string, text2: string, opt_checklines?: boolean,
33+
opt_deadline?: number): Diff[]
34+
35+
diff_cleanupSemantic(diffs: Diff[])
36+
37+
diff_cleanupEfficiency(diffs: Diff[])
38+
39+
diff_cleanupSemanticLossless(diffs: Diff[])
40+
41+
diff_levenshtein(diffs: Diff[]): number
42+
43+
diff_prettyHtml(diffs: Diff[]): string
44+
45+
diff_xIndex(diffs: Diff[], loc: number): number
46+
47+
diff_cleanupMerge(diffs: Diff[])
48+
49+
match_main(text: string, pattern: string, loc: number): number
50+
51+
diff_fromDelta(text1: string, delta: string): Diff[]
52+
53+
diff_toDelta(diffs: Diff[]): string
54+
55+
diff_levenshtein(diffs: Diff[]): number
56+
57+
diff_text1(diffs: Diff[]): string
58+
59+
diff_text2(diffs: Diff[]): string
60+
61+
patch_make(a: string, opt_b: string): Patch[]
62+
patch_make(a: Diff[]): Patch[]
63+
patch_make(a: string, opt_b: Diff[]): Patch[]
64+
patch_make(a: string, opt_b: string, opt_c: Diff[]): Patch[]
65+
66+
patch_splitMax(patches: Patch[])
67+
68+
patch_addPadding(patches: Patch[]): string
69+
70+
patch_deepCopy(patches: Patch[]): Patch[]
71+
72+
patch_toText(patches: Patch[]): string
73+
74+
patch_fromText(textline: string): Patch[]
75+
76+
patch_apply(patches: Patch[], text: string): {
77+
[0]: string
78+
[1]: boolean[]
79+
}
80+
}
81+
}
82+
export = diffMatchPatch

0 commit comments

Comments
 (0)