This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
MatrixStateEvent.ts
68 lines (59 loc) · 2.1 KB
/
MatrixStateEvent.ts
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
// Copyright (c) 2021. Sendanor <info@sendanor.fi>. All rights reserved.
import {
explainJsonObject,
isReadonlyJsonObject,
ReadonlyJsonObject
} from "../../../core/Json";
import { MatrixStateEventOf } from "./MatrixStateEventOf";
import { MatrixType } from "./MatrixType";
import { explain, explainProperty } from "../../../core/types/explain";
import { explainString, explainStringOrUndefined, isString, isStringOrUndefined } from "../../../core/types/String";
import { explainRegularObject, isRegularObject } from "../../../core/types/RegularObject";
import { explainNoOtherKeys, hasNoOtherKeysInDevelopment } from "../../../core/types/OtherKeys";
export type MatrixStateEvent = MatrixStateEventOf<ReadonlyJsonObject>;
export function createMatrixStateEvent (
type : MatrixType | string,
state_key : string,
content : ReadonlyJsonObject
) : MatrixStateEvent {
return {
type,
state_key,
content
};
}
export function isMatrixStateEvent (value: any): value is MatrixStateEvent {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'type',
'state_key',
'content'
])
&& isString(value?.type)
&& isStringOrUndefined(value?.state_key)
&& isReadonlyJsonObject(value?.content)
);
}
export function explainMatrixStateEvent (value : any) : string {
return explain(
[
explainRegularObject(value),
explainNoOtherKeys(value, [
'type',
'state_key',
'content'
]),
explainProperty("type", explainString(value?.type)),
explainProperty("state_key", explainStringOrUndefined(value?.state_key)),
explainProperty("content", explainJsonObject(value?.content))
]
)
}
export function stringifyMatrixStateEvent (value: MatrixStateEvent): string {
return `MatrixStateEvent(${value})`;
}
export function parseMatrixStateEvent (value: any): MatrixStateEvent | undefined {
if ( isMatrixStateEvent(value) ) return value;
return undefined;
}