Skip to content

Commit

Permalink
refactor(diff): allow args to be undefined/null
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 10, 2019
1 parent 4cba4a5 commit f6ae89d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/diff/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ const simpleDiff = <T>(
* @param equiv equality predicate function
*/
export const diffArray = <T>(
a: ArrayLike<T>,
b: ArrayLike<T>,
a: ArrayLike<T> | undefined | null,
b: ArrayLike<T> | undefined | null,
mode = DiffMode.FULL,
equiv = _equiv
) => {
Expand All @@ -80,7 +80,7 @@ export const diffArray = <T>(
if (a === b || (a == null && b == null)) {
return state;
} else if (a == null || a.length === 0) {
return simpleDiff(state, b, "adds", 1, mode);
return simpleDiff(state, b!, "adds", 1, mode);
} else if (b == null || b.length === 0) {
return simpleDiff(state, a, "dels", -1, mode);
}
Expand Down
20 changes: 12 additions & 8 deletions packages/diff/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ import { equiv } from "@thi.ng/equiv";
import { DiffMode, ObjectDiff } from "./api";

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

const diffObjectDist = (
a: IObjectOf<any>,
b: IObjectOf<any>,
a: IObjectOf<any> | undefined | null,
b: IObjectOf<any> | undefined | null,
_equiv: Predicate2<any>
) => {
if (!a) a = {};
if (!b) b = {};
let d = 0;
for (let k in a) {
const vb = b[k];
Expand All @@ -31,10 +33,12 @@ const diffObjectDist = (
};

const diffObjectFull = (
a: IObjectOf<any>,
b: IObjectOf<any>,
a: IObjectOf<any> | undefined | null,
b: IObjectOf<any> | undefined | null,
_equiv: Predicate2<any>
) => {
if (!a) a = {};
if (!b) b = {};
let d = 0;
const adds = [];
const dels = [];
Expand Down

0 comments on commit f6ae89d

Please sign in to comment.