This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.js
47 lines (43 loc) · 1.52 KB
/
parsers.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
const dashify = require('dashify')
const formatDate = require('./format-date')
const dataParser = (reactClass, transformer) => (w) => {
const el = w.document.querySelector(`[data-react-class=${reactClass}]`)
const data = JSON.parse(el.getAttribute('data-react-props'))
return transformer(data)
}
const videoParser = (video) => ({
slug: video.slug,
id: video.id,
title: video.title,
description: video.description,
duration: video.duration_in_seconds,
poses: video.poses.map((i) => i.content),
exercises: video.exercises.map((i) => i.content),
target_areas: video.target_areas.map((i) => i.content)
})
const parsers = {
name: (value) => ({
path: `workout/${dashify(value)}`,
parser: dataParser('VideoView', ({ video }) => videoParser(video))
}),
date: (value) => ({
path: `wod/${formatDate.url(value)}`,
parser: dataParser('WeeklySchedule', ({ schedule }) => {
const workout = schedule.scheduled_workouts.find((w) => formatDate.day(w.date) === formatDate.day(value))
return videoParser(workout.video)
})
}),
week: (value) => ({
path: `wod/${formatDate.url(value)}`,
parser: dataParser('WeeklySchedule', ({ schedule }) => ({
id: schedule.id,
start_date: schedule.start_date,
end_date: schedule.end_date,
slug: schedule.slug,
title: schedule.title,
scheduled_workouts: schedule.scheduled_workouts.map((i) => Object.assign({ date: i.date }, videoParser(i.video)))
}))
})
}
module.exports = parsers
module.exports.keys = Object.keys(parsers)