-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prepareSearchIndex.js
More file actions
37 lines (32 loc) · 1.23 KB
/
test_prepareSearchIndex.js
File metadata and controls
37 lines (32 loc) · 1.23 KB
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
const assert = require('assert');
function prepareSearchIndex(data) {
if (!Array.isArray(data)) return;
const now = new Date();
data.forEach(pdf => {
// 1. Pre-calculate search string
const t = pdf.title || '';
const d = pdf.description || '';
const c = pdf.category || '';
const a = pdf.author || '';
pdf._searchStr = `${t} ${d} ${c} ${a}`.toLowerCase();
// 2. Pre-calculate Date and New status
let uploadDateObj;
if (pdf.uploadDate && typeof pdf.uploadDate.toDate === 'function') {
uploadDateObj = pdf.uploadDate.toDate();
} else {
uploadDateObj = new Date(pdf.uploadDate);
}
const timeDiff = now - uploadDateObj;
pdf._isNew = timeDiff < (7 * 24 * 60 * 60 * 1000);
pdf._formattedDate = uploadDateObj.toLocaleDateString('en-US', {
year: 'numeric', month: 'short', day: 'numeric'
});
});
}
const data = [
{ title: 'Title1', description: 'Desc1', category: 'Cat1', author: 'Author1', uploadDate: new Date() }
];
prepareSearchIndex(data);
assert.strictEqual(data[0]._searchStr, 'title1 desc1 cat1 author1');
assert.strictEqual(data[0]._isNew, true);
console.log('test pass');