/
helpers.js
151 lines (131 loc) · 3.59 KB
/
helpers.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function getSrcSet(media) {
const srcset =
Object.values(media.media_details.sizes)
// Get the url and width of each size.
.map(item => [item.source_url, item.width])
// Recude them to a string with the format required by `srcset`.
.reduce(
(final, current, index, array) =>
final.concat(
`${current.join(" ")}w${index !== array.length - 1 ? ", " : ""}`
),
""
) || null;
return srcset;
}
export function getMediaAttributes(state, id) {
const media = state.source.attachment[id];
if (!media) return {};
const srcSet = getSrcSet(media);
return {
id,
alt: media.alt_text,
src: media.source_url,
srcSet
};
}
export function getPostCategories(state, post) {
const allCategories = state.source.category;
const categories =
post.categories && post.categories.map(catId => allCategories[catId]);
return categories ? categories.filter(Boolean) : [];
}
export function getPostAuthor(state, post) {
return state.source.author[post.author];
}
export function getPostTags(state, post) {
const allTags = state.source.tag;
const tags = post.tags && post.tags.map(tagId => allTags[tagId]);
return tags ? tags.filter(Boolean) : [];
}
export function getPostData(state) {
const data = state.source.get(state.router.link);
const post = state.source[data.type][data.id];
return { ...post, isReady: data.isReady, isPage: data.isPage };
}
export function formatPostData(state, post) {
return {
id: post.id,
author: getPostAuthor(state, post),
publishDate: post.date,
title: post.title.rendered,
categories: getPostCategories(state, post),
tags: getPostTags(state, post),
link: post.link,
featured_media: getMediaAttributes(state, post.featured_media),
content: post.content.rendered,
excerpt: post.excerpt.rendered
};
}
export function splitPosts(state, routeData) {
const firstThreePosts = [];
const otherPosts = [];
routeData.forEach((item, idx) => {
const itemData = state.source[item.type][item.id];
if (idx < 3) firstThreePosts.push(itemData);
else otherPosts.push(itemData);
});
return [firstThreePosts, otherPosts];
}
export function omitConnectProps(props) {
const out = {};
const propsToOmit = [
"state",
"actions",
"roots",
"fills",
"libraries",
"getSnapshot"
];
const isGetSnapshot = prop =>
typeof prop === "function" && prop.name === "getSnapshot";
for (const prop in props) {
if (propsToOmit.includes(prop) || isGetSnapshot(prop)) continue;
out[prop] = props[prop];
}
return out;
}
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"Novemeber",
"December"
];
const formatDay = day => {
const lastLetter = day[day.length - 1];
if (lastLetter) return `${day}nd`;
if (lastLetter) return `${day}st`;
if (lastLetter) return `${day}rd`;
return `${day}th`;
};
export function formatDate(date) {
const jsDate = new Date(date);
const day = jsDate.getDate();
const month = jsDate.getMonth() + 1;
const year = jsDate.getFullYear();
return `${formatDay(day)} ${monthNames[month]}, ${year}`;
}
export function isUrl(str) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-/]))?/;
return regexp.test(str);
}
export function debounce(fn) {
let queued = null;
return [
(...args) => {
if (queued) cancelAnimationFrame(queued);
queued = requestAnimationFrame(fn.bind(fn, ...args));
},
() => {
cancelAnimationFrame(queued);
}
];
}