-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-intl.js
87 lines (85 loc) · 2.71 KB
/
be-intl.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
// @ts-check
import { propInfo, rejected, resolved } from 'be-enhanced/cc.js';
import { BeValueAdded } from 'be-value-added/be-value-added.js';
/** @import {Actions, AP} from './ts-refs/be-intl/types' */
/** @import {BEConfig, IEnhancement, BEAllProps} from './ts-refs/be-enhanced/types.d.ts' */
/** @import {Positraction, PropLookup, PropInfo} from './ts-refs/trans-render/froop/types.d.ts' */
/** @import {BVAActions, BVAAllProps, BVAP} from './ts-refs/be-value-added/types' */;
/**
* @implements {Actions}
*/
class BeIntl extends BeValueAdded {
/**
* @type {BEConfig<AP & BEAllProps, Actions & IEnhancement>}
*/
static config = {
propInfo: {
...propInfo,
locale: {},
format: {},
intlDateFormat: {},
intlNumberFormat: {},
currency: {},
},
actions: {
hydrate: {
ifAllOf: ['attached'],
},
formatNumber: {
ifKeyIn: ['value'],
ifAllOf: ['intlNumberFormat']
},
formatDate: {
ifKeyIn: ['value'],
ifAllOf: ['intlDateFormat']
},
onFormattingChange: {
ifAllOf: ['locale'],
ifKeyIn: ['format', 'currency']
}
},
positractions: [
/** @type {Positraction<BVAP & BEAllProps>} */
(resolved),
/** @type {Positraction<BVAP & BEAllProps>} */
(rejected),
]
};
/**
*
* @param {AP & BEAllProps} self
* @returns {void}
*/
formatNumber(self) {
const { enhancedElement, value } = self;
if (value === undefined) {
enhancedElement.textContent = '';
return;
}
const { intlNumberFormat } = self;
enhancedElement.textContent = intlNumberFormat.format(value);
}
formatDate(self) {
const { enhancedElement, value, intlNumberFormat } = self;
enhancedElement.textContent = self.intlDateFormat.format(value);
}
onFormattingChange(self) {
const { enhancedElement, locale, format, currency } = self;
switch (enhancedElement.localName) {
case 'time':
return {
intlDateFormat: new Intl.DateTimeFormat(locale, format),
resolved: true
};
default:
if (currency !== undefined)
format.currency = currency;
return {
intlNumberFormat: new Intl.NumberFormat(locale, format),
resolved: true
};
}
}
}
await BeIntl.bootUp();
export { BeIntl };