be-intl 0.0.0 → 0.0.1

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.
Files changed (4) hide show
  1. package/README.md +43 -1
  2. package/be-intl.js +126 -0
  3. package/package.json +33 -4
  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
+ [![Playwright Tests](https://github.com/bahrus/be-intl/actions/workflows/CI.yml/badge.svg?branch=baseline)](https://github.com/bahrus/be-intl/actions/workflows/CI.yml)
6
+ [![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/be-intl?style=for-the-badge)](https://bundlephobia.com/result?p=be-intl)
7
+ <img src="http://img.badgesize.io/https://cdn.jsdelivr.net/npm/be-intl?compression=gzip">
8
+ [![NPM version](https://badge.fury.io/js/be-intl.png)](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,126 @@
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
+ };
89
+ default:
90
+ return {
91
+ intlNumberFormat: new Intl.NumberFormat(locale, format)
92
+ };
93
+ }
94
+ }
95
+ }
96
+ const defaultLocale = new Intl.NumberFormat().resolvedOptions().locale;
97
+ const tagName = 'be-intl';
98
+ const ifWantsToBe = 'intl';
99
+ const upgrade = 'data,time,output';
100
+ const xe = new XE({
101
+ config: {
102
+ tagName,
103
+ propDefaults: {
104
+ ...propDefaults,
105
+ },
106
+ propInfo: {
107
+ ...propInfo
108
+ },
109
+ actions: {
110
+ formatNumber: {
111
+ ifKeyIn: ['value'],
112
+ ifAllOf: ['intlNumberFormat']
113
+ },
114
+ formatDate: {
115
+ ifKeyIn: ['value'],
116
+ ifAllOf: ['intlDateFormat']
117
+ },
118
+ onFormattingChange: {
119
+ ifAllOf: ['locale'],
120
+ ifKeyIn: ['format']
121
+ }
122
+ }
123
+ },
124
+ superclass: BeIntl
125
+ });
126
+ register(ifWantsToBe, upgrade, tagName);
package/package.json CHANGED
@@ -1,10 +1,39 @@
1
1
  {
2
2
  "name": "be-intl",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.0.1",
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": "echo \"Error: no test specified\" && exit 1"
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
+ }