-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5d333c
commit 61a95cf
Showing
14 changed files
with
551 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
coverage/ | ||
npm/ | ||
coverage/* | ||
npm/* | ||
!npm/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { diff } from "./diff.ts" | ||
export { generateDiffText } from "./parser/index.ts" | ||
export * from "./type.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { DiffResultBase } from "../type.ts" | ||
|
||
export default abstract class AbstractDiffParser { | ||
abstract getDiff<O extends object, N extends object>( | ||
result: DiffResultBase<O, N>, | ||
): string | ||
|
||
protected toJsonValue = (value: unknown): string | number | undefined => { | ||
if (typeof value === "string") return `"${value}"` | ||
if (typeof value === "number") return value | ||
|
||
return undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AddedDiffResult } from "../type.ts" | ||
import AbstractDiffParser from "./AbstractDiffParser.ts" | ||
|
||
export default class AddedObjectParser extends AbstractDiffParser { | ||
constructor(private indent: string) { | ||
super() | ||
} | ||
|
||
public getDiff<O extends object, N extends object>( | ||
result: AddedDiffResult<O, N>, | ||
): string { | ||
let text = "" | ||
text += `+ {\n` | ||
Object.entries(result.new).forEach(([key, value]) => { | ||
text += `+ ${this.indent}${key}: ${this.toJsonValue(value)},\n` | ||
}) | ||
text += `+ },\n` | ||
|
||
return text | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { DiffKey, ExistedDiffResult } from "../type.ts" | ||
import AbstractDiffParser from "./AbstractDiffParser.ts" | ||
|
||
export default class KeyParser extends AbstractDiffParser { | ||
constructor(private indent: string) { | ||
super() | ||
} | ||
|
||
public getDiff<O extends object, N extends object>( | ||
result: ExistedDiffResult<O, N>, | ||
) { | ||
let text = "" | ||
text += `${this.indent}{\n` | ||
result.keys.forEach((key) => { | ||
text += this.parseDetail(key) | ||
}) | ||
text += `${this.indent}},\n` | ||
|
||
return text | ||
} | ||
|
||
private parseDetail<O extends object, N extends object>( | ||
key: DiffKey<O, N>, | ||
): string { | ||
const keyName = String(key.name) | ||
const oldValue = this.toJsonValue(key.old) | ||
const newValue = this.toJsonValue(key.new) | ||
|
||
if (key.type === "added") { | ||
return `+ ${this.indent}${keyName}: ${newValue},\n` | ||
} | ||
if (key.type === "removed") { | ||
return `- ${this.indent}${keyName}: ${oldValue},\n` | ||
} | ||
if (key.type === "unchanged") { | ||
return ` ${this.indent}${keyName}: ${oldValue},\n` | ||
} | ||
const removed = `- ${this.indent}${keyName}: ${oldValue},\n` | ||
const added = `+ ${this.indent}${keyName}: ${newValue},\n` | ||
|
||
return `${removed}${added}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RemovedDiffResult } from "../type.ts" | ||
import AbstractDiffParser from "./AbstractDiffParser.ts" | ||
|
||
export default class RemovedObjectParser extends AbstractDiffParser { | ||
constructor(private indent: string) { | ||
super() | ||
} | ||
|
||
public getDiff<O extends object, N extends object>( | ||
result: RemovedDiffResult<O, N>, | ||
): string { | ||
let text = "" | ||
text += `- {\n` | ||
Object.entries(result.old).forEach(([key, value]) => { | ||
text += `- ${this.indent}${key}: ${this.toJsonValue(value)},\n` | ||
}) | ||
text += `- },\n` | ||
|
||
return text | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { DiffResult } from "../type.ts" | ||
import AddedObjectParser from "./AddedObjectParser.ts" | ||
import KeyParser from "./KeyParser.ts" | ||
import RemovedObjectParser from "./RemovedObjectParser.ts" | ||
|
||
export const generateDiffText = <O extends object, N extends object>( | ||
results: DiffResult<O, N>[], | ||
): string => { | ||
const INDENT = " " | ||
const parser = { | ||
added: new AddedObjectParser(INDENT), | ||
removed: new RemovedObjectParser(INDENT), | ||
key: new KeyParser(INDENT), | ||
} | ||
|
||
let text = "[\n" | ||
results.forEach((result) => { | ||
if (result.type === "added") text += parser.added.getDiff(result) | ||
if (result.type === "removed") text += parser.removed.getDiff(result) | ||
if (result.type === "unchanged") text += parser.key.getDiff(result) | ||
if (result.type === "updated") text += parser.key.getDiff(result) | ||
}) | ||
text += "]" | ||
|
||
return text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.