be-intl 0.0.0 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -1
- package/be-intl.js +128 -0
- package/package.json +33 -4
- package/types.d.ts +27 -0
package/README.md
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
-
# be-intl
|
|
1
|
+
# be-intl
|
|
2
|
+
|
|
3
|
+
Format numbers, dates automatically and semantically.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/bahrus/be-intl/actions/workflows/CI.yml)
|
|
6
|
+
[](https://bundlephobia.com/result?p=be-intl)
|
|
7
|
+
<img src="http://img.badgesize.io/https://cdn.jsdelivr.net/npm/be-intl?compression=gzip">
|
|
8
|
+
[](http://badge.fury.io/js/be-intl)
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
<data value=123456.789 lang="de-DE" be-intl='{ "style": "currency", "currency": "EUR" }'></data>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
emits
|
|
15
|
+
|
|
16
|
+
```html
|
|
17
|
+
<data value=123456.789 lang="de-DE" be-intl='{ "style": "currency", "currency": "EUR" }'>123.456,79 €</data>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Viewing Locally
|
|
21
|
+
|
|
22
|
+
1. Install git.
|
|
23
|
+
2. Fork/clone this repo.
|
|
24
|
+
3. Install node.
|
|
25
|
+
4. Open command window to folder where you cloned this repo.
|
|
26
|
+
5. > npm install
|
|
27
|
+
6. > npm run serve
|
|
28
|
+
7. Open http://localhost:3030/demo in a modern browser.
|
|
29
|
+
|
|
30
|
+
## Importing in ES Modules:
|
|
31
|
+
|
|
32
|
+
```JavaScript
|
|
33
|
+
import 'be-intl/be-intl.js';
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Using from CDN:
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<script type=module crossorigin=anonymous>
|
|
41
|
+
import 'https://esm.run/be-intl';
|
|
42
|
+
</script>
|
|
43
|
+
```
|
package/be-intl.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { BE, propDefaults, propInfo } from 'be-enhanced/BE.js';
|
|
2
|
+
import { XE } from 'xtal-element/XE.js';
|
|
3
|
+
import { register } from 'be-hive/register.js';
|
|
4
|
+
export class BeIntl extends BE {
|
|
5
|
+
static get beConfig() {
|
|
6
|
+
return {
|
|
7
|
+
parse: true,
|
|
8
|
+
primaryProp: 'format',
|
|
9
|
+
primaryPropReq: true,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
async attach(enhancedElement, enhancementInfo) {
|
|
13
|
+
await super.attach(enhancedElement, enhancementInfo);
|
|
14
|
+
import('be-propagating/be-propagating.js');
|
|
15
|
+
const base = await enhancedElement.beEnhanced.whenResolved('be-propagating');
|
|
16
|
+
const propagator = base.propagators.get('self');
|
|
17
|
+
propagator.addEventListener('lang', e => {
|
|
18
|
+
this.locale = e.detail.newVal;
|
|
19
|
+
});
|
|
20
|
+
switch (enhancedElement.localName) {
|
|
21
|
+
case 'data':
|
|
22
|
+
case 'output':
|
|
23
|
+
{
|
|
24
|
+
propagator.addEventListener('value', e => {
|
|
25
|
+
const newVal = e.detail.newVal;
|
|
26
|
+
switch (typeof newVal) {
|
|
27
|
+
case 'string':
|
|
28
|
+
if (!newVal) {
|
|
29
|
+
this.value = 0;
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
default:
|
|
33
|
+
this.value = newVal;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const val = enhancedElement.value;
|
|
37
|
+
if (val !== '') {
|
|
38
|
+
this.value = JSON.parse(val);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
case 'time':
|
|
43
|
+
{
|
|
44
|
+
propagator.addEventListener('dateTime', e => {
|
|
45
|
+
const newVal = e.detail.newVal;
|
|
46
|
+
switch (typeof newVal) {
|
|
47
|
+
case 'string':
|
|
48
|
+
try {
|
|
49
|
+
this.value = new Date(newVal);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
console.error(e);
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
if (newVal instanceof Date) {
|
|
56
|
+
this.value = newVal;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const val = enhancedElement.dateTime;
|
|
61
|
+
if (val !== '') {
|
|
62
|
+
try {
|
|
63
|
+
this.value = new Date(val);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.error(e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
this.locale = enhancedElement.lang || defaultLocale;
|
|
73
|
+
}
|
|
74
|
+
formatNumber(self) {
|
|
75
|
+
const { enhancedElement, value, intlNumberFormat } = self;
|
|
76
|
+
enhancedElement.textContent = intlNumberFormat.format(value);
|
|
77
|
+
}
|
|
78
|
+
formatDate(self) {
|
|
79
|
+
const { enhancedElement, value, intlNumberFormat } = self;
|
|
80
|
+
enhancedElement.textContent = this.intlDateFormat.format(value);
|
|
81
|
+
}
|
|
82
|
+
onFormattingChange(self) {
|
|
83
|
+
const { enhancedElement, locale, format } = self;
|
|
84
|
+
switch (enhancedElement.localName) {
|
|
85
|
+
case 'time':
|
|
86
|
+
return {
|
|
87
|
+
intlDateFormat: new Intl.DateTimeFormat(locale, format),
|
|
88
|
+
resolved: true
|
|
89
|
+
};
|
|
90
|
+
default:
|
|
91
|
+
return {
|
|
92
|
+
intlNumberFormat: new Intl.NumberFormat(locale, format),
|
|
93
|
+
resolved: true
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const defaultLocale = new Intl.NumberFormat().resolvedOptions().locale;
|
|
99
|
+
const tagName = 'be-intl';
|
|
100
|
+
const ifWantsToBe = 'intl';
|
|
101
|
+
const upgrade = 'data,time,output';
|
|
102
|
+
const xe = new XE({
|
|
103
|
+
config: {
|
|
104
|
+
tagName,
|
|
105
|
+
propDefaults: {
|
|
106
|
+
...propDefaults,
|
|
107
|
+
},
|
|
108
|
+
propInfo: {
|
|
109
|
+
...propInfo
|
|
110
|
+
},
|
|
111
|
+
actions: {
|
|
112
|
+
formatNumber: {
|
|
113
|
+
ifKeyIn: ['value'],
|
|
114
|
+
ifAllOf: ['intlNumberFormat']
|
|
115
|
+
},
|
|
116
|
+
formatDate: {
|
|
117
|
+
ifKeyIn: ['value'],
|
|
118
|
+
ifAllOf: ['intlDateFormat']
|
|
119
|
+
},
|
|
120
|
+
onFormattingChange: {
|
|
121
|
+
ifAllOf: ['locale'],
|
|
122
|
+
ifKeyIn: ['format']
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
superclass: BeIntl
|
|
127
|
+
});
|
|
128
|
+
register(ifWantsToBe, upgrade, tagName);
|
package/package.json
CHANGED
|
@@ -1,10 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "be-intl",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"web-components",
|
|
6
|
+
"web-component",
|
|
7
|
+
"custom-element",
|
|
8
|
+
"custom-elements"
|
|
9
|
+
],
|
|
10
|
+
"description": "Format numbers, dates automatically and semantically.",
|
|
11
|
+
"main": "be-intl.js",
|
|
12
|
+
"module": "be-intl.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./be-intl.js",
|
|
15
|
+
"./be-intl.js": "./be-intl.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"*.js",
|
|
19
|
+
"types.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"types": "types.d.ts",
|
|
6
22
|
"scripts": {
|
|
7
|
-
"test": "
|
|
23
|
+
"test": "playwright test",
|
|
24
|
+
"serve": "node node_modules/may-it-serve/serve.js",
|
|
25
|
+
"safari": "npx playwright wk http://localhost:3030"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"be-enhanced": "0.0.21",
|
|
29
|
+
"be-hive": "0.0.95",
|
|
30
|
+
"be-propagating": "0.0.10",
|
|
31
|
+
"trans-render": "0.0.683",
|
|
32
|
+
"xtal-element": "0.0.562"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"may-it-serve": "0.0.5",
|
|
36
|
+
"@playwright/test": "1.34.3"
|
|
8
37
|
},
|
|
9
38
|
"author": "",
|
|
10
39
|
"license": "MIT"
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ActionOnEventConfigs } from "trans-render/froop/types";
|
|
2
|
+
import {IBE, Declarations} from 'be-enhanced/types';
|
|
3
|
+
|
|
4
|
+
export interface EndUserProps extends IBE<HTMLDataElement | HTMLTimeElement | HTMLOutputElement>{
|
|
5
|
+
format?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions,
|
|
6
|
+
value?: number | Date;
|
|
7
|
+
locale?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface AllProps extends EndUserProps{
|
|
11
|
+
intlDateFormat?: Intl.DateTimeFormat,
|
|
12
|
+
intlNumberFormat?: Intl.NumberFormat,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type AP = AllProps;
|
|
16
|
+
|
|
17
|
+
export type PAP = Partial<AP>;
|
|
18
|
+
|
|
19
|
+
export type ProPAP = Promise<PAP>;
|
|
20
|
+
|
|
21
|
+
export type POA = [PAP | undefined, ActionOnEventConfigs<PAP, Actions>];
|
|
22
|
+
|
|
23
|
+
export interface Actions{
|
|
24
|
+
formatNumber(self: this): void;
|
|
25
|
+
formatDate(self: this): void;
|
|
26
|
+
onFormattingChange(self: this): PAP;
|
|
27
|
+
}
|