File size: 1,416 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export function keyForPost( post ) {
	if ( ! post ) {
		return;
	}

	if ( post.feed_ID && post.feed_item_ID ) {
		return {
			feedId: post.feed_ID,
			postId: post.feed_item_ID,
		};
	}
	if ( post.is_external ) {
		return {
			feedId: post.feed_ID || post.site_ID,
			postId: post.feed_item_ID || post.ID,
		};
	}
	return {
		blogId: post.site_ID,
		postId: post.ID,
	};
}

export function keysAreEqual( a, b ) {
	if ( a === b ) {
		return true;
	}
	if ( ( ! a && b ) || ( a && ! b ) || ( ! a && ! b && a !== b ) ) {
		return false;
	}
	if ( ( a.isGap && ! b.isGap ) || ( ! a.isGap && b.isGap ) ) {
		return false;
	}
	if ( a.isGap && b.isGap ) {
		return a.from === b.from && a.to === b.to;
	}
	if ( a.postId !== b.postId ) {
		return false;
	}
	if ( a.feedId ) {
		return a.feedId === b.feedId;
	}
	return a.blogId === b.blogId;
}

export function keyToString( postKey ) {
	if ( ! postKey || postKey.isGap ) {
		return null;
	}

	if ( postKey.isRecommendationBlock ) {
		return `rec-${ postKey.index }`;
	} else if ( postKey.isPromptBlock ) {
		return `prompt-${ postKey.index }`;
	} else if ( postKey.feedId ) {
		return `${ postKey.postId }-${ postKey.feedId }`;
	} else if ( postKey.blogId ) {
		return `${ postKey.postId }-${ postKey.blogId }`;
	}

	return null; // should never happen!
}

export function isPostKeyLike( postKey ) {
	return postKey && postKey.postId && ( postKey.blogId || postKey.feedId );
}