Skip to content

Commit

Permalink
Proper formatting for added/removed objects and arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Damnae committed Apr 12, 2024
1 parent 56441f5 commit edbdad6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/views/diff/AnyDiff.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<template v-if="prefix">
{{ prefix }}
</template>

<template v-if="diffNode.Type == DiffNodeType.Value">
<ValueDiff :diffNode="diffNode" />
</template>
Expand Down
32 changes: 32 additions & 0 deletions src/views/diff/AnyPlain.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script setup lang="ts">
defineProps<{value:any, prefix?:string}>()
</script>

<template>
<span class="diff-node">
<template v-if="prefix">
{{ prefix }}
</template>

<span class="diff-array" v-if="Array.isArray(value)">
[
<span class="array-item block" v-for="entry, _key in value">
<AnyPlain :value="entry" />
</span>
]
</span>
<span class="diff-object" v-else-if="typeof value === 'object'">
{
<span class="object-item block" v-for="entry, key in value">
<AnyPlain :value="entry" :prefix="`${key}: `" />
</span>
}
</span>
<span class="diff-value" v-else>
{{ value }}
</span>
</span>
</template>

<style scoped>
</style>
5 changes: 3 additions & 2 deletions src/views/diff/ArrayDiff.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ref } from 'vue';
import { DiffNode, DiffNodeChange, } from './diff';
import AnyDiff from '@/views/diff/AnyDiff.vue';
import AnyPlain from '@/views/diff/AnyPlain.vue';
const props = defineProps<{diffNode:DiffNode}>()
const values = ref(props.diffNode.Content as DiffNode[])
</script>
Expand All @@ -16,10 +17,10 @@
]
</template>
<span class="array-item" v-else-if="diffNode.Change == DiffNodeChange.Removed">
{{ diffNode.FromValue }}
<AnyPlain :value="diffNode.FromValue" />
</span>
<span class="array-item" v-else>
{{ diffNode.ToValue }}
<AnyPlain :value="diffNode.ToValue" />
</span>
</span>
</template>
Expand Down
5 changes: 3 additions & 2 deletions src/views/diff/ObjectDiff.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ref } from 'vue';
import { DiffNode, DiffNodeChange, KeyToDiffNode } from './diff';
import AnyDiff from '@/views/diff/AnyDiff.vue';
import AnyPlain from '@/views/diff/AnyPlain.vue';
const props = defineProps<{diffNode:DiffNode}>()
const properties = ref(props.diffNode.Content as KeyToDiffNode)
</script>
Expand All @@ -16,10 +17,10 @@
}
</template>
<span class="object-item" v-else-if="diffNode.Change == DiffNodeChange.Removed">
{{ diffNode.FromValue }}
<AnyPlain :value="diffNode.FromValue" />
</span>
<span class="object-item" v-else>
{{ diffNode.ToValue }}
<AnyPlain :value="diffNode.ToValue" />
</span>
</span>
</template>
Expand Down
5 changes: 3 additions & 2 deletions src/views/diff/ValueDiff.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { DiffNode, DiffNodeChange } from './diff';
import AnyPlain from '@/views/diff/AnyPlain.vue';
defineProps<{diffNode:DiffNode}>()
</script>

Expand All @@ -9,10 +10,10 @@
<span class="change-Removed">{{ diffNode.FromValue }}</span> <span class="change-Added">{{ diffNode.ToValue }}</span>
</template>
<template v-else-if="diffNode.Change == DiffNodeChange.Removed">
{{ diffNode.FromValue }}
<AnyPlain :value="diffNode.FromValue" />
</template>
<template v-else>
{{ diffNode.ToValue }}
<AnyPlain :value="diffNode.ToValue" />
</template>
</span>
</template>
Expand Down
12 changes: 8 additions & 4 deletions src/views/diff/diff.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import translate from "@/common/translate"

Check failure on line 1 in src/views/diff/diff.ts

View workflow job for this annotation

GitHub Actions / deploy

'translate' is declared but its value is never read.

export enum DiffNodeType
{
Expand Down Expand Up @@ -98,8 +99,11 @@ function diffObject(from:KeyToAny, to:KeyToAny) : DiffNode
ToValue:to,
Content:content
}

for (const key of Object.keys(to))

const toKeys = Object.keys(to)
const fromKeys = Object.keys(from)

for (const key of toKeys)
{
if (from[key] === undefined)
content[key] = {
Expand All @@ -111,7 +115,7 @@ function diffObject(from:KeyToAny, to:KeyToAny) : DiffNode
else content[key] = diffAny(from[key], to[key])
}

for (const key of Object.keys(from))
for (const key of fromKeys)
if (to[key] === undefined)
content[key] = {
Type:findType(from[key]),
Expand All @@ -120,7 +124,7 @@ function diffObject(from:KeyToAny, to:KeyToAny) : DiffNode
ToValue:to[key],
}

result.Change = Object.values(content).some(c => c.Change != DiffNodeChange.Same) ?
result.Change = toKeys.length != fromKeys.length || Object.values(content).some(c => c.Change != DiffNodeChange.Same) ?
DiffNodeChange.Changed : DiffNodeChange.Same

// If it's a collection of *only* objects, strip entries that didn't change
Expand Down

0 comments on commit edbdad6

Please sign in to comment.