-
Notifications
You must be signed in to change notification settings - Fork 0
/
fic.js
266 lines (235 loc) · 5.96 KB
/
fic.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'use strict'
const url = require('url')
const validate = require('aproba')
let Site
function num (val) {
if (val == null) return val
return Number(val)
}
class Fic {
constructor (site) {
this._data = {
site: undefined,
siteName: undefined,
siteId: undefined,
link: undefined,
published: undefined,
updated: undefined,
title: undefined,
rating: undefined,
language: undefined,
status: undefined,
words: undefined,
chapterCount: undefined,
maxChapterCount: undefined,
cover: undefined,
summary: undefined,
}
this.site = site
this.authors = []
this.tags = []
this.stats = {}
this.db = undefined
}
get site () {
return this._data.site
}
set site (site) {
validate('O|S|Z', arguments)
if (typeof site === 'object') {
this._data.site = site
this._data.siteName = this.site.name
} else if (typeof site === 'string') {
try {
this._data.site = Site.create(site)
this._data.siteName = this.site.name
} catch (_) {
this._data.siteName = site
}
}
return this._data.site
}
get siteName () {
return this._data.siteName
}
set siteName (siteName) {
validate('S', arguments)
try {
this._data.site = Site.create(siteName)
} catch (_) {
// ignore errors
} finally {
return this._data.siteName = siteName
}
}
get siteId () {
return this._data.siteId
}
set siteId (id) {
validate('Z|N|S', arguments)
return this._data.siteId = id == null ? id : Number(id)
}
get link () {
return this._data.link
}
set link (href) {
validate('Z|S', arguments)
return this._data.link = href
}
get published () {
return this._data.published
}
set published (stamp) {
validate('Z|N|S', arguments)
return this._data.published = stamp == null ? stamp : Number(stamp)
}
get updated () {
return this._data.updated
}
set updated (stamp) {
validate('Z|N|S', arguments)
return this._data.updated = stamp == null ? stamp : Number(stamp)
}
get title () {
return this._data.title
}
set title (val) {
validate('S', arguments)
return this._data.title = val
}
get rating () {
return this._data.rating
}
set rating (val) {
validate('Z|S', arguments)
return this._data.rating = val
}
get language () {
return this._data.language
}
set language (val) {
validate('Z|S', arguments)
return this._data.language = val
}
get status () {
return this._data.status
}
set status (val) {
validate('Z|S', arguments)
return this._data.status = val
}
get words () {
return this._data.words
}
set words (val) {
validate('Z|N|S', arguments)
return this._data.words = val == null ? val : Number(val)
}
get chapterCount () {
return this._data.chapterCount
}
set chapterCount (val) {
validate('Z|N|S', arguments)
return this._data.chapterCount = val == null ? val : Number(val)
}
get maxChapterCount () {
return this._data.maxChapterCount
}
set maxChapterCount (val) {
validate('Z|N|S', arguments)
return this._data.maxChapterCount = val == null ? val : Number(val)
}
get cover () {
return this._data.cover
}
set cover (val) {
validate('Z|S', arguments)
return this._data.cover = val
}
get summary () {
return this._data.summary
}
set summary (val) {
validate('Z|S', arguments)
return this._data.summary = val
}
addAuthor (name_or_au, link, base) {
let au
if (arguments.length > 1) {
au = {name: name_or_au}
if (this.site) {
au.link = this.site.normalizeAuthorLink(link, base)
} else if (link) {
au.link = base ? url.resolve(base, link) : link
}
} else {
au = name_or_au
}
if (this.authors.some(_ => (_.link||_.name) === (au.link||au.name))) return
this.authors.push(au)
this.authors.sort((aa, bb) => aa.name.localeCompare(bb.name) || (aa.link||'').localeCompare(bb.link))
return au
}
tagMatch (filterTags) {
if (!filterTags) return true
return this.tags.some(_ => filterTags.test(_))
}
entryMatch (filterEntry) {
if (!filterEntry) return true
// filterEntry acts on ALL of the info we have, collectively.
// originally it matched against "rawContent" which was a site specific
// textual rendering of the entire fic page.
// Using a more normalized form let's us only carry around data
// that we'll store. It also makes the matching more predicatable.
const content =
'Tags: ' + this.tags.join(', ') + '\n' +
'Title: ' + this.title + '\n' +
'Summary: ' + this.summary
return filterEntry.test(content)
}
fromJSON (obj) {
this.siteName = obj.site
this.siteId = num(obj.siteId)
this.link = obj.link
this.published = num(obj.published)
this.updated = num(obj.updated)
this.title = obj.title
this.rating = obj.rating
this.language = obj.language
obj.authors.forEach(_ => this.addAuthor(_.name, _.link))
this.status = obj.status
this.words = num(obj.words)
this.chapterCount = num(obj.chapterCount)
this.maxChapterCount = num(obj.maxChapterCount)
this.cover = obj.cover
this.stats = {...obj.stats}
this.tags = [...obj.tags]
this.summary = obj.summary
this.db = obj.db ? {...obj.db} : undefined
return this
}
toJSON () {
return {
site: this.siteName,
siteId: this.siteId,
link: this.link,
published: this.published,
updated: this.updated,
title: this.title,
rating: this.rating,
language: this.language,
authors: this.authors,
status: this.status,
words: this.words,
chapterCount: this.chapterCount,
maxChapterCount: this.maxChapterCount,
cover: this.cover,
stats: this.stats,
tags: this.tags,
summary: this.summary,
db: this.db
}
}
}
module.exports = Fic
Site = require('./site.js')