-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
104 lines (100 loc) · 2.68 KB
/
utils.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
const { floor, random } = Math;
var moment = require("moment");
// 输入数组并随机返回
function randomSelect(arr) {
// 非空判断
if (typeof arr === "undefined") {
// console.log(arr);
return "no data";
}
let len = arr.length;
let choice = floor(random() * len);
// console.log(`len=${len} choice=${choice}`);
return arr[choice];
}
// obj: Notion Database Query results Object
// colName: the wanted column name
function getNotionProperty(obj, colName) {
// 非空判断
if (typeof obj === "undefined") {
console.log(obj);
return "no data";
}
let prop;
if (typeof obj.properties === "undefined") {
return "No properties";
} else {
prop = obj.properties[String(colName)];
// console.log(`get prop!`, prop);
}
switch (prop.type) {
case "title":
return prop.title
.map((item) => item.plain_text.replace(/\n/g, ""))
.join(" ");
break;
case "rich_text":
const rich_text = prop.rich_text
.map((item) => item.plain_text.replace(/\n/g, ""))
.join(" ");
return rich_text ? rich_text : `'${String(colName)}' is empty`;
break;
case "text":
const text = prop.rich_text
.map((item) => item.plain_text.replace(/\n/g, ""))
.join(" ");
return text ? text : `'${String(colName)}' is empty`;
break;
case "checkbox":
return prop.checkbox === true;
break;
case "select":
return prop.select === undefined
? `'${String(colName)}'\(select\) is empty`
: prop.select.name;
break;
case "created_time":
return moment(prop.created_time).fromNow();
break;
case "last_edited_time":
return moment(prop.last_edited_time).fromNow();
break;
case "url":
return prop.url === undefined || prop.url === null
? `'${String(colName)}'\(url\) is empty`
: prop.url;
break;
case "date":
return prop.date === null
? `'${String(colName)}'\(date\) is empty`
: moment(prop.date).format("YYYY MMM d");
break;
case "number":
return prop.number;
break;
case "phone":
return prop.phone_number;
break;
case "email":
return prop.email;
break;
case "multi_select":
let names = [];
if (prop.multi_select === undefined || prop.multi_select.length === 0) {
return `'${String(colName)}'\(date\) is empty`;
} else {
for (let i = 0; i < prop.multi_select.length; i++) {
const select = prop.multi_select[i];
names.push(select.name);
}
}
return names;
break;
default:
break;
}
}
module.exports = {
randomSelect,
getNotionProperty,
};