@aemforms/af-formatters 0.22.23 → 0.22.26

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.
@@ -1,54 +1,61 @@
1
- /*************************************************************************
2
- * ADOBE CONFIDENTIAL
3
- * ___________________
4
- *
5
- * Copyright 2022 Adobe
6
- * All Rights Reserved.
7
- *
8
- * NOTICE: All information contained herein is, and remains
9
- * the property of Adobe and its suppliers, if any. The intellectual
10
- * and technical concepts contained herein are proprietary to Adobe
11
- * and its suppliers and are protected by all applicable intellectual
12
- * property laws, including trade secret and copyright laws.
13
- * Dissemination of this information or reproduction of this material
14
- * is strictly forbidden unless prior written permission is obtained
15
- * from Adobe.
16
-
17
- * Adobe permits you to use and modify this file solely in accordance with
18
- * the terms of the Adobe license agreement accompanying it.
19
- *************************************************************************/
20
-
21
- import { getCurrency } from './currencies.js';
1
+ import {getCurrency} from "./currencies.js"
22
2
 
23
3
  const NUMBER_REGEX =
24
- /(?:[#]+|[@]+(#+)?|[0]+|[,]|[.]|[-]|[+]|[%]|[¤]{1,4}(?:\/([a-zA-Z]{3}))?|[;]|[K]{1,2}|E{1,2}[+]?|'(?:[^']|'')*')|[^a-zA-Z']+/g;
4
+ // eslint-disable-next-line max-len
5
+ /(?:[#]+|[@]+(?:#+)?|[0]+|[,]|[.]|[-]|[+]|[%]|[¤]{1,4}(?:\/([a-zA-Z]{3}))?|[;]|[K]{1,2}|E{1,2}[+]?|'(?:[^']|'')*')|[^a-zA-Z']+/g;
6
+
7
+ const options = {
8
+ compactDisplay : ['short', 'long'], //valid only when notation is compact
9
+ currency: '', // must be provided if the style is currency
10
+ currencyDisplay: ["symbol", 'narrowSymbol', 'code', 'name'],
11
+ currencySign : ['accounting', 'standard'],
12
+ localeMatcher : ['lookup', 'best fit'],
13
+ notation : ['standard', 'scientific', 'engineering', 'compact'],
14
+ numberingSystem :'' ,
15
+ signDisplay : ['auto', 'always', 'exceptZero', 'negative', 'never'],
16
+ style : ['decimal', 'currency', 'percent', 'unit'],
17
+ unit: '', //https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier
18
+ unitDisplay : ['long', 'short', 'narrow'],
19
+ useGrouping : ['always', 'auto', 'min2', false, true],
20
+ roundingMode : '',
21
+ roundingPriority : '',
22
+ roundingIncrement : '',
23
+ trailingZeroDisplay : ['auto', 'stripIfInteger'],
24
+ minimumIntegerDigits : '',
25
+ minimumFractionDigits : '',
26
+ maximumFractionDigits : '',
27
+ minimumSignificantDigits : '',
28
+ maximumSignificantDigits : ''
29
+ }
25
30
  const supportedUnits = ['acre', 'bit', 'byte', 'celsius', 'centimeter', 'day',
26
31
  'degree', 'fahrenheit', 'fluid-ounce', 'foot', 'gallon', 'gigabit',
27
32
  'gigabyte', 'gram', 'hectare', 'hour', 'inch', 'kilobit', 'kilobyte',
28
33
  'kilogram', 'kilometer', 'liter', 'megabit', 'megabyte', 'meter', 'mile',
29
34
  'mile-scandinavian', 'milliliter', 'millimeter', 'millisecond', 'minute', 'month',
30
- 'ounce', 'percent', 'petabyte', 'pound', 'second', 'stone', 'terabit', 'terabyte', 'week', 'yard', 'year'].join('|');
31
- const ShorthandStyles = [/^currency(?:\/([a-zA-Z]{3}))?$/, /^decimal$/, /^integer$/, /^percent$/, new RegExp(`^unit\/(${supportedUnits})$`)];
32
- function parseNumberSkeleton(skeleton, language) {
33
- const options = {};
35
+ 'ounce', 'percent', 'petabyte', 'pound', 'second', 'stone', 'terabit', 'terabyte', 'week', 'yard', 'year'].join('|')
36
+ export const ShorthandStyles = [/^currency(?:\/([a-zA-Z]{3}))?$/, /^decimal$/, /^integer$/, /^percent$/, new RegExp(`^unit\/(${supportedUnits})$`)]
37
+
38
+
39
+ export function parseNumberSkeleton(skeleton, language) {
40
+ const options = {}
34
41
  const order = [];
35
- let match, index;
42
+ let match, index
36
43
  for (index = 0; index < ShorthandStyles.length && match == null; index++) {
37
- match = ShorthandStyles[index].exec(skeleton);
44
+ match = ShorthandStyles[index].exec(skeleton)
38
45
  }
39
46
  if (match) {
40
47
  switch(index) {
41
48
  case 1:
42
- options.style = 'currency';
43
- options.currencyDisplay = 'narrowSymbol';
49
+ options.style = 'currency'
50
+ options.currencyDisplay = 'narrowSymbol'
44
51
  if (match[1]) {
45
- options.currency = match[1];
52
+ options.currency = match[1]
46
53
  } else {
47
- options.currency = getCurrency(language);
54
+ options.currency = getCurrency(language)
48
55
  }
49
56
  break;
50
57
  case 2:
51
- new Intl.NumberFormat(language, {}).resolvedOptions();
58
+ const defaultOptions = new Intl.NumberFormat(language, {}).resolvedOptions();
52
59
  options.minimumFractionDigits = options.minimumFractionDigits || 2;
53
60
  break;
54
61
  case 3:
@@ -56,8 +63,7 @@ function parseNumberSkeleton(skeleton, language) {
56
63
  options.maximumFractionDigits = 0;
57
64
  break;
58
65
  case 4:
59
- options.style = 'percent';
60
- options.maximumFractionDigits = 2;
66
+ options.style = 'percent'
61
67
  break;
62
68
  case 5:
63
69
  options.style = "unit";
@@ -70,104 +76,97 @@ function parseNumberSkeleton(skeleton, language) {
70
76
  order
71
77
  }
72
78
  }
73
- options.useGrouping = false;
74
- options.minimumIntegerDigits = 1;
75
- options.maximumFractionDigits = 0;
76
- options.minimumFractionDigits = 0;
77
- skeleton.replace(NUMBER_REGEX, (match, maxSignificantDigits, currencySymbol, offset) => {
79
+ options.useGrouping = false
80
+ options.minimumIntegerDigits = 1
81
+ options.maximumFractionDigits = 0
82
+ options.minimumFractionDigits = 0
83
+ skeleton.replace(NUMBER_REGEX, (match, offset) => {
78
84
  const len = match.length;
79
85
  switch(match[0]) {
80
86
  case '#':
81
- order.push(['digit', len]);
87
+ order.push(['digit', len])
82
88
  if (options?.decimal === true) {
83
- options.maximumFractionDigits = options.minimumFractionDigits + len;
89
+ options.maximumFractionDigits = options.minimumFractionDigits + len
84
90
  }
85
91
  break;
86
92
  case '@':
87
93
  if (options?.minimumSignificantDigits) {
88
94
  throw "@ symbol should occur together"
89
95
  }
90
- const hashes = maxSignificantDigits || "";
91
- order.push(['@', len - hashes.length]);
92
- options.minimumSignificantDigits = len - hashes.length;
93
- options.maximumSignificantDigits = len;
94
- order.push(['digit', hashes.length]);
96
+ order.push(['@', len])
97
+ options.minimumSignificantDigits = len
98
+ const hashes = match.match(/#+/) || ""
99
+ options.maximumSignificantDigits = len + hashes.length
100
+ order.push(['digit', hashes.length])
95
101
  break;
96
102
  case ',':
97
103
  if (options?.decimal === true) {
98
104
  throw "grouping character not supporting for fractions"
99
105
  }
100
- order.push(['group', 1]);
106
+ order.push(['group', 1])
101
107
  options.useGrouping = 'auto';
102
108
  break;
103
109
  case '.':
104
110
  if (options?.decimal) {
105
111
  console.error("only one decimal symbol is allowed");
106
112
  } else {
107
- order.push(['decimal', 1]);
113
+ order.push(['decimal', 1])
108
114
  options.decimal = true;
109
115
  }
110
116
  break;
111
117
  case '0':
112
- order.push('0', len);
118
+ order.push('0', len)
113
119
  if(options.minimumSignificantDigits || options.maximumSignificantDigits) {
114
120
  throw "0 is not supported with @"
115
121
  }
116
122
  if (options?.decimal === true) {
117
123
  options.minimumFractionDigits = len;
118
- if (!options.maximumFractionDigits) {
119
- options.maximumFractionDigits = len;
120
- }
121
124
  } else {
122
- options.minimumIntegerDigits = len;
125
+ options.minimumIntegerDigits = len
123
126
  }
124
127
  break;
125
128
  case '-':
126
129
  if (offset !== 0) {
127
- console.error("sign display is always in the beginning");
130
+ console.error("sign display is always in the beginning")
128
131
  }
129
- options.signDisplay = 'negative';
130
- order.push(['signDisplay', 1, '-']);
132
+ options.signDisplay = 'negative'
133
+ order.push(['signDisplay', 1, '-'])
131
134
  break;
132
135
  case '+':
133
136
  if (offset !== 0 && order[order.length - 1][0] === 'E') {
134
- console.error("sign display is always in the beginning");
137
+ console.error("sign display is always in the beginning")
135
138
  }
136
139
  if (offset === 0) {
137
- options.signDisplay = 'always';
140
+ options.signDisplay = 'always'
138
141
  }
139
- order.push(['signDisplay', 1, '+']);
142
+ order.push(['signDisplay', 1, '+'])
140
143
  break;
141
144
  case '¤':
142
145
  if (offset !== 0 && offset !== skeleton.length - 1) {
143
- console.error("currency display should be either in the beginning or at the end");
144
- } else {
145
- options.style = 'currency';
146
- options.currencyDisplay = ['symbol', 'code', 'name', 'narrowSymbol'][len - 1];
147
- options.currency = currencySymbol || getCurrency(language);
148
- order.push(['currency', len]);
146
+ console.error("currency display should be either in the beginning or at the end")
149
147
  }
148
+ options.style = 'currency'
149
+ options.currencyDisplay = ['symbol', 'code', 'name', 'narrowSymbol'][len -1]
150
+ options.currency = getCurrency(language)
151
+ order.push(['currency', len])
150
152
  break;
151
153
  case '%':
152
154
  if (offset !== 0 && offset !== skeleton.length - 1) {
153
- console.error("percent display should be either in the beginning or at the end");
154
- } else {
155
- order.push(['%', 1]);
156
- options.style = 'percent';
155
+ console.error("percent display should be either in the beginning or at the end")
157
156
  }
157
+ order.push(['%', 1])
158
+ options.style = 'percent'
158
159
  break;
159
160
  case 'E':
160
- order.push(['E', len]);
161
- options.style = ['scientific','engineering'](len - 1);
161
+ order.push(['E', len])
162
+ options.style = ['scientific','engineering'](len - 1)
162
163
  break;
163
164
  default:
164
165
  console.error("unknown chars" + match);
165
166
  }
166
- });
167
+ })
167
168
  return {
168
169
  options,
169
170
  order
170
171
  };
171
172
  }
172
-
173
- export { ShorthandStyles, parseNumberSkeleton };
@@ -1,23 +1,3 @@
1
- /*************************************************************************
2
- * ADOBE CONFIDENTIAL
3
- * ___________________
4
- *
5
- * Copyright 2022 Adobe
6
- * All Rights Reserved.
7
- *
8
- * NOTICE: All information contained herein is, and remains
9
- * the property of Adobe and its suppliers, if any. The intellectual
10
- * and technical concepts contained herein are proprietary to Adobe
11
- * and its suppliers and are protected by all applicable intellectual
12
- * property laws, including trade secret and copyright laws.
13
- * Dissemination of this information or reproduction of this material
14
- * is strictly forbidden unless prior written permission is obtained
15
- * from Adobe.
16
-
17
- * Adobe permits you to use and modify this file solely in accordance with
18
- * the terms of the Adobe license agreement accompanying it.
19
- *************************************************************************/
20
-
21
1
  const currencies = {
22
2
  'da-DK': 'DKK',
23
3
  'de-DE': 'EUR',
@@ -40,17 +20,19 @@ const currencies = {
40
20
  'ru-RU': 'RUB',
41
21
  'tr-TR': 'TRY'
42
22
  };
43
- const locales = Object.keys(currencies);
23
+
24
+ const locales = Object.keys(currencies)
25
+
44
26
  const getCurrency = function (locale) {
45
27
  if (locales.indexOf(locale) > -1) {
46
28
  return currencies[locale]
47
29
  } else {
48
- const matchingLocale = locales.find(x => x.startsWith(locale));
30
+ const matchingLocale = locales.find(x => x.startsWith(locale))
49
31
  if (matchingLocale) {
50
32
  return currencies[matchingLocale]
51
33
  }
52
34
  }
53
35
  return ''
54
- };
36
+ }
55
37
 
56
- export { getCurrency };
38
+ export {getCurrency}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aemforms/af-formatters",
3
- "version": "0.22.23",
3
+ "version": "0.22.26",
4
4
  "description": "Formatting Module for Forms Runtime",
5
5
  "author": "Adobe Systems",
6
6
  "license": "Adobe Proprietary",
@@ -27,16 +27,6 @@
27
27
  "lib",
28
28
  "LICENSE"
29
29
  ],
30
- "scripts": {
31
- "test": "NODE_OPTIONS=--experimental-vm-modules jest --silent",
32
- "eslint": "npx eslint src/**",
33
- "eslint:fix": "npx eslint --fix src/**",
34
- "test:ci": "NODE_OPTIONS=--experimental-vm-modules jest --silent --coverage",
35
- "build": "npm run clean && mkdir -p lib/esm && cp -R src/* lib/esm/ && rollup -c rollup.config.js",
36
- "clean": "rm -rf lib target",
37
- "prepublishOnly": "npm run build && npm run test",
38
- "docs": "npx typedoc --options .typedoc.cjs"
39
- },
40
30
  "devDependencies": {
41
31
  "@babel/cli": "^7.18.10",
42
32
  "@babel/core": "^7.19.0",