This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
CompositeProductOption.ts
92 lines (81 loc) · 3.25 KB
/
CompositeProductOption.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
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
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { explainProductIdListWithAmount, isProductIdListWithAmount, ProductIdListWithAmount } from "./ProductIdList";
import { explain, explainProperty } from "../../../types/explain";
import { explainStringOrUndefined, isStringOrUndefined } from "../../../types/String";
import { explainNumber, explainNumberOrUndefined, isNumber, isNumberOrUndefined } from "../../../types/Number";
import { explainNumberOrStringOrBooleanOrUndefined, isNumberOrStringOrBooleanOrUndefined } from "../../../types/NumberOrStringOrBooleanOrUndefined";
import { explainRegularObject, isRegularObject } from "../../../types/RegularObject";
import { explainNoOtherKeys, hasNoOtherKeys } from "../../../types/OtherKeys";
export interface CompositeProductOption {
readonly value : number | string | boolean;
readonly minAmount : number;
readonly maxAmount ?: number;
/**
* If defined, make this option part of a group.
*
* E.g. only one in a group can be selected at one time.
*/
readonly groupBy ?: string;
/**
* These products will be selected if user selects this option
*/
readonly products : ProductIdListWithAmount;
}
export function createCompositeProductOption (
value : number | string | boolean,
products : ProductIdListWithAmount,
groupBy ?: string,
minAmount ?: number,
maxAmount ?: number
) : CompositeProductOption {
return {
value,
groupBy,
products: products ?? [],
minAmount: minAmount ?? 0,
maxAmount
};
}
export function isCompositeProductOption (value: any) : value is CompositeProductOption {
return (
isRegularObject(value)
&& hasNoOtherKeys(value, [
'value',
'products',
'groupBy',
'minAmount',
'maxAmount'
])
&& isNumberOrStringOrBooleanOrUndefined(value?.value)
&& isStringOrUndefined(value?.groupBy)
&& isProductIdListWithAmount(value?.products)
&& isNumber(value?.minAmount)
&& isNumberOrUndefined(value?.maxAmount)
);
}
export function explainCompositeProductOption (value: any) : string {
return explain(
[
explainRegularObject(value),
explainNoOtherKeys(value, [
'value',
'products',
'groupBy',
'minAmount',
'maxAmount'
])
, explainProperty("value", explainNumberOrStringOrBooleanOrUndefined(value?.value))
, explainProperty("products", explainProductIdListWithAmount(value?.products))
, explainProperty("groupBy", explainStringOrUndefined(value?.groupBy))
, explainProperty("minAmount", explainNumber(value?.minAmount))
, explainProperty("maxAmount", explainNumberOrUndefined(value?.maxAmount))
]
);
}
export function stringifyCompositeProductOption (value : CompositeProductOption) : string {
return `CompositeProductOption(${value})`;
}
export function parseCompositeProductOption (value: any) : CompositeProductOption | undefined {
if (isCompositeProductOption(value)) return value;
return undefined;
}