Skip to content

Commit

Permalink
refactor(diff): address TS strictNullChecks flag, add/update types
Browse files Browse the repository at this point in the history
- add EditLog type alias
- update ArrayDiff, ObjectDiff interfaces
  • Loading branch information
postspectacular committed Jun 7, 2019
1 parent ba7d31e commit 0252a4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
6 changes: 4 additions & 2 deletions packages/diff/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export interface ArrayDiff<T> {
adds?: DiffKeyMap<T>;
dels?: DiffKeyMap<T>;
const?: DiffKeyMap<T>;
linear?: (number | T)[];
linear?: EditLog<number, T>;
}

export interface ObjectDiff<T> {
distance: number;
adds?: string[];
dels?: string[];
edits?: (PropertyKey | T)[];
edits?: EditLog<string, T>;
}

export type EditLog<K, T> = (K | T)[];
36 changes: 22 additions & 14 deletions packages/diff/src/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { equiv as _equiv } from "@thi.ng/equiv";
import { ArrayDiff, DiffMode } from "./api";
import {
ArrayDiff,
DiffKeyMap,
DiffMode,
EditLog
} from "./api";

let _cachedFP: Int32Array;
let _cachedPath: Int32Array;
Expand All @@ -25,7 +30,7 @@ const simpleDiff = <T>(
mode: DiffMode
) => {
const n = src.length;
const linear = state.linear;
const linear = <EditLog<Number, T>>state.linear;
state.distance = n;
if (mode !== DiffMode.ONLY_DISTANCE) {
for (let i = 0, j = 0; i < n; i++, j += 3) {
Expand All @@ -34,7 +39,7 @@ const simpleDiff = <T>(
linear[j + 2] = src[i];
}
if (mode === DiffMode.FULL) {
const _state = state[key];
const _state = <DiffKeyMap<T>>state[key];
for (let i = 0; i < n; i++) {
_state[i] = src[i];
}
Expand Down Expand Up @@ -172,20 +177,23 @@ const buildFullLog = <T>(
b: ArrayLike<T>,
reverse: boolean
) => {
const linear = state.linear;
const _const = state.const;
let i = epc.length,
px = 0,
py = 0;
let adds, dels, aID, dID;
const linear = <EditLog<Number, T>>state.linear;
const _const = <DiffKeyMap<T>>state.const;
let i = epc.length;
let px = 0;
let py = 0;
let adds: DiffKeyMap<T>;
let dels: DiffKeyMap<T>;
let aID: number;
let dID: number;
if (reverse) {
adds = state.dels;
dels = state.adds;
adds = <DiffKeyMap<T>>state.dels;
dels = <DiffKeyMap<T>>state.adds;
aID = -1;
dID = 1;
} else {
adds = state.adds;
dels = state.dels;
adds = <DiffKeyMap<T>>state.adds;
dels = <DiffKeyMap<T>>state.dels;
aID = 1;
dID = -1;
}
Expand Down Expand Up @@ -220,7 +228,7 @@ const buildLinearLog = <T>(
reverse: boolean,
inclConst: boolean
) => {
const linear = state.linear;
const linear = <EditLog<number, T>>state.linear;
const aID = reverse ? -1 : 1;
const dID = reverse ? 1 : -1;
let i = epc.length,
Expand Down

0 comments on commit 0252a4b

Please sign in to comment.