Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add numeric and json diff #16

Merged
merged 4 commits into from
Sep 13, 2023
Merged

Add numeric and json diff #16

merged 4 commits into from
Sep 13, 2023

Conversation

ankrgyl
Copy link
Contributor

@ankrgyl ankrgyl commented Sep 13, 2023

This PR adds support for a few planned diff functions:

  • NumericDiff compares numbers and returns a score that's 1 - the % difference between the two numbers
  • JSONDiff recursively compares json objects, using a provided string and numeric scorer, falling back to string comparison if the object types don't match.

js/json.ts Outdated
}

function isArray(obj: any): obj is Array<unknown> {
return "[object Array]" === Object.prototype.toString.call(obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use obj instance of Array as is done in replacer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly for isObject: obj instanceof Object and !isArray(obj)

js/json.ts Outdated

// https://gist.github.com/davidfurlong/463a83a33b70a3b6618e97ec9679e490
const replacer = (key: string, value: any) =>
value instanceof Object && !(value instanceof Array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equivalent to isObject?

js/number.ts Outdated
output === 0 && expected === 0
? 1
: 1 -
Math.abs(expected - output) /
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This quotient will exceed 1 if the signs of expected and output are different. A simple solution would be to multiply the denominator by 2.

@BryanJadot-at
Copy link

Looks good thanks for doing this sir

@ankrgyl ankrgyl merged commit 9b2db2e into main Sep 13, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants