Skip to content

Commit

Permalink
refactor(diff): fix #256 replace DiffMode enum
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace DiffMode enum w/ type alias

- rename DiffMode.ONLY_DISTANCE_LINEAR_ONLY_CHANGES => "minimal"
- update diffObject() mode arg to only allow: "full" or "only-distance"
  • Loading branch information
postspectacular committed Dec 22, 2020
1 parent 5086a33 commit cc77c71
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
6 changes: 6 additions & 0 deletions packages/diff/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { IObjectOf } from "@thi.ng/api";

export type DiffMode =
| "only-distance"
| "only-distance-linear"
| "minimal"
| "full";

export type DiffKeyMap<T> = IObjectOf<T>;

export interface ArrayDiff<T> {
Expand Down
15 changes: 7 additions & 8 deletions packages/diff/src/array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FnU3, Nullable } from "@thi.ng/api";
import { equiv as _equiv } from "@thi.ng/equiv";
import type { ArrayDiff, DiffKeyMap, EditLog } from "./api";
import { DiffMode } from "./constants";
import type { ArrayDiff, DiffKeyMap, DiffMode, EditLog } from "./api";

let _cachedFP: Nullable<Int32Array>;
let _cachedPath: Nullable<Int32Array>;
Expand Down Expand Up @@ -34,13 +33,13 @@ const simpleDiff = <T>(
const n = src.length;
const linear = <EditLog<Number, T>>state.linear;
state.distance = n;
if (mode !== DiffMode.ONLY_DISTANCE) {
if (mode !== "only-distance") {
for (let i = 0, j = 0; i < n; i++, j += 3) {
linear[j] = logDir;
linear[j + 1] = i;
linear[j + 2] = src[i];
}
if (mode === DiffMode.FULL) {
if (mode === "full") {
const _state = <DiffKeyMap<T>>state[key];
for (let i = 0; i < n; i++) {
_state[i] = src[i];
Expand Down Expand Up @@ -70,7 +69,7 @@ const simpleDiff = <T>(
export const diffArray = <T>(
a: ArrayLike<T> | undefined | null,
b: ArrayLike<T> | undefined | null,
mode = DiffMode.FULL,
mode: DiffMode = "full",
equiv = _equiv
) => {
const state = <ArrayDiff<T>>{
Expand Down Expand Up @@ -145,14 +144,14 @@ export const diffArray = <T>(

state.distance = delta + 2 * p;

if (mode !== DiffMode.ONLY_DISTANCE) {
if (mode !== "only-distance") {
p = path[doff] * 3;
while (p >= 0) {
epc.push(p);
p = pathPos[p + 2] * 3;
}

if (mode === DiffMode.FULL) {
if (mode === "full") {
buildFullLog<T>(epc, pathPos, state, _a, _b, reverse);
} else {
buildLinearLog<T>(
Expand All @@ -162,7 +161,7 @@ export const diffArray = <T>(
_a,
_b,
reverse,
mode === DiffMode.ONLY_DISTANCE_LINEAR
mode === "only-distance-linear"
);
}
}
Expand Down
6 changes: 0 additions & 6 deletions packages/diff/src/constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/diff/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./api";
export * from "./array";
export * from "./constants";
export * from "./object";
5 changes: 2 additions & 3 deletions packages/diff/src/object.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { IObjectOf, Predicate2 } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import type { ObjectDiff } from "./api";
import { DiffMode } from "./constants";

export const diffObject = <T>(
a: IObjectOf<T> | undefined | null,
b: IObjectOf<T> | undefined | null,
mode = DiffMode.FULL,
mode: "full" | "only-distance" = "full",
_equiv: Predicate2<any> = equiv
): ObjectDiff<T> =>
a === b
? { distance: 0 }
: mode === DiffMode.ONLY_DISTANCE
: mode === "only-distance"
? diffObjectDist(a, b, _equiv)
: diffObjectFull(a, b, _equiv);

Expand Down
8 changes: 2 additions & 6 deletions packages/diff/test/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from "assert";
import { ArrayDiff, diffArray, DiffMode } from "../src";
import { ArrayDiff, diffArray } from "../src";

describe("array", function () {
const state = <ArrayDiff<number>>{
Expand Down Expand Up @@ -70,11 +70,7 @@ describe("array", function () {

it("diff insert 2nd last (changes only)", () => {
assert.deepStrictEqual(
diffArray(
[1, 2, 3, 4],
[1, 2, 3, 5, 4],
DiffMode.ONLY_DISTANCE_LINEAR_ONLY_CHANGES
),
diffArray([1, 2, 3, 4], [1, 2, 3, 5, 4], "minimal"),
<ArrayDiff<number>>{
distance: 1,
adds: {},
Expand Down

0 comments on commit cc77c71

Please sign in to comment.