Skip to content

Commit

Permalink
[field] Add helpers for narrowing item diff types
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 025d0e6 commit 072d391
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/@sanity/field/src/diff/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ChangeNode, Diff, FieldChangeNode, GroupChangeNode} from '../types'
import {ChangeNode, Diff, FieldChangeNode, GroupChangeNode, ItemDiff} from '../types'

export function getChangeVerb(diff: Diff): 'Added' | 'Removed' | 'Changed' {
const hadPrevValue = hasValue(diff.fromValue)
Expand All @@ -25,6 +25,22 @@ export function isGroupChange(change: ChangeNode): change is GroupChangeNode {
return change.type === 'group'
}

export function isAddedItemDiff(
item: ItemDiff
): item is ItemDiff & {diff: Diff & {action: 'added'}} {
return item.diff.action === 'added'
}

export function isRemovedItemDiff(
item: ItemDiff
): item is ItemDiff & {diff: Diff & {action: 'removed'}} {
return item.diff.action === 'removed'
}

export function isUnchangedDiff(diff: Diff): diff is Diff & {action: 'unchanged'} {
return diff.action === 'unchanged'
}

function hasValue(value: unknown) {
return value !== null && typeof value !== 'undefined' && value !== ''
}

0 comments on commit 072d391

Please sign in to comment.