Skip to content

Commit 8edd425

Browse files
committed
feat: provide a property content extractor
1 parent 261de93 commit 8edd425

File tree

2 files changed

+479
-0
lines changed

2 files changed

+479
-0
lines changed

source/property.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* *** MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
*
8+
* @summary Helpers for property handling
9+
*
10+
* @author Alvis HT Tang <alvis@hilbert.space>
11+
* @license MIT
12+
* @copyright Copyright (c) 2021 - All Rights Reserved.
13+
* -------------------------------------------------------------------------
14+
*/
15+
16+
import type { FormulaValue, PropertyValue, RollupValue, User } from './types';
17+
18+
type Date = { start: string; end?: string };
19+
20+
type Person = { name: string; avatar?: string };
21+
22+
type NormalisedValue =
23+
| undefined
24+
| boolean
25+
| number
26+
| string
27+
| string[]
28+
| Date
29+
| Person
30+
| NormalisedValue[];
31+
32+
/* eslint-disable max-lines-per-function */
33+
/**
34+
* extract the content from a property
35+
* @param property a property returned from Notion API
36+
* @returns its content
37+
*/
38+
export function getPropertyContent(property: PropertyValue): NormalisedValue {
39+
switch (property.type) {
40+
case 'title':
41+
return property.title.map((text) => text.plain_text).join('');
42+
case 'rich_text':
43+
return property.rich_text.map((text) => text.plain_text).join('');
44+
case 'number':
45+
return property.number;
46+
case 'select':
47+
return property.select.name;
48+
case 'multi_select':
49+
return property.multi_select.map((value) => value.name);
50+
case 'date':
51+
return property.date;
52+
case 'people':
53+
return property.people.map(getUserContent);
54+
case 'files':
55+
return property.files.map((file) => file.name);
56+
case 'checkbox':
57+
return property.checkbox;
58+
case 'url':
59+
return property.url;
60+
case 'email':
61+
return property.email;
62+
case 'phone_number':
63+
return property.phone_number;
64+
case 'formula':
65+
return getFormulaPropertyContent(property.formula);
66+
case 'relation':
67+
return undefined;
68+
case 'rollup':
69+
return getRollupPropertyContent(property.rollup);
70+
case 'created_by':
71+
return getUserContent(property.created_by);
72+
case 'created_time':
73+
return property.created_time;
74+
case 'last_edited_by':
75+
return getUserContent(property.last_edited_by);
76+
case 'last_edited_time':
77+
return property.last_edited_time;
78+
/* istanbul ignore next */
79+
default:
80+
throw new TypeError(`unknown property`);
81+
}
82+
}
83+
/* eslint-enable */
84+
85+
/**
86+
* extract the content from a formula property
87+
* @param formula a formula property returned from Notion API
88+
* @returns its content
89+
*/
90+
function getFormulaPropertyContent(
91+
formula: FormulaValue['formula'],
92+
): NormalisedValue {
93+
switch (formula.type) {
94+
case 'string':
95+
return formula.string;
96+
case 'number':
97+
return formula.number;
98+
case 'boolean':
99+
return formula.boolean;
100+
case 'date':
101+
return formula.date;
102+
/* istanbul ignore next */
103+
default:
104+
throw new TypeError(`unknown formula property`);
105+
}
106+
}
107+
108+
/**
109+
* extract the content from a formula property
110+
* @param rollup a formula property returned from Notion API
111+
* @returns its content
112+
*/
113+
function getRollupPropertyContent(
114+
rollup: RollupValue['rollup'],
115+
): NormalisedValue | NormalisedValue[] {
116+
switch (rollup.type) {
117+
case 'number':
118+
return rollup.number;
119+
case 'date':
120+
return rollup.date;
121+
case 'array':
122+
return rollup.array.map(getPropertyContent);
123+
/* istanbul ignore next */
124+
default:
125+
throw new TypeError(`unknown rollup property`);
126+
}
127+
}
128+
129+
/**
130+
* extract useful user information
131+
* @param user a user property returned from Notion API
132+
* @returns its content
133+
*/
134+
function getUserContent(user: User): Person {
135+
return {
136+
name: user.name,
137+
avatar: user.avatar_url,
138+
};
139+
}

0 commit comments

Comments
 (0)