cx 24.3.8 → 24.3.9

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,242 +1,234 @@
1
- import {debug} from "./Debug";
2
- import {GlobalCacheIdentifier} from './GlobalCacheIdentifier';
3
- import {isNumber} from '../util/isNumber';
4
- import {isUndefined} from '../util/isUndefined';
5
- import {isArray} from '../util/isArray';
6
-
7
- //Culture dependent formatters are defined in the ui package.
8
-
9
- const defaultFormatter = v => v.toString();
10
-
11
- let formatFactory = {
12
-
13
- string: function() {
14
- return defaultFormatter
15
- },
16
-
17
- wrap: function(part0, prefix, suffix) {
18
- if (!prefix)
19
- prefix = '';
20
-
21
- if (!suffix)
22
- suffix = '';
23
-
24
- return value => prefix + value.toString() + suffix;
25
- },
26
-
27
- fixed: function(part0, digits) {
28
- return value => value.toFixed(digits)
29
- },
30
-
31
- prefix: function(part0, prefix) {
32
- if (!prefix)
33
- prefix = '';
34
-
35
- return value => prefix + value.toString();
36
- },
37
-
38
- suffix: function(part0, suffix) {
39
- if (!suffix)
40
- suffix = '';
41
-
42
- return value => value.toString() + suffix;
43
- },
44
-
45
- uppercase: function() {
46
- return value => value.toString().toUpperCase();
47
- },
48
-
49
- lowercase: function() {
50
- return value => value.toString().toLowerCase();
51
- },
52
-
53
- urlencode: function() {
54
- return value => encodeURIComponent(value);
55
- },
56
-
57
- number: function (part0, minFractionDigits, maxFractionDigits) {
58
- let {minimumFractionDigits, maximumFractionDigits} = resolveMinMaxFractionDigits(minFractionDigits, maxFractionDigits);
59
- let trimmable = maximumFractionDigits - minimumFractionDigits;
60
- if (trimmable > 0) {
61
- if (minimumFractionDigits == 0)
62
- ++trimmable;
63
- return value => trimFractionZeros(value.toFixed(maximumFractionDigits), trimmable);
64
- }
65
- return value => value.toFixed(maximumFractionDigits);
66
- },
67
-
68
- percentage: function (part0, minFractionDigits, maxFractionDigits) {
69
- let numberFormatter = formatFactory.number(part0, minFractionDigits, maxFractionDigits);
70
- return value => numberFormatter(value * 100) + '%';
71
- },
72
-
73
- percentageSign: function (part0, minFractionDigits, maxFractionDigits) {
74
- let numberFormatter = formatFactory.number(part0, minFractionDigits, maxFractionDigits);
75
- return value => numberFormatter(value) + '%';
76
- },
77
-
78
- date: function () {
79
- return value => {
80
- let date = new Date(value);
81
- return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
82
- }
83
- },
84
-
85
- time: function () {
86
- return value => {
87
- let date = new Date(value);
88
- let h = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
89
- let m = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
90
- return `${h}:${m}`;
91
- }
92
- },
93
-
94
- datetime: function () {
95
- let date = formatFactory.date();
96
- let time = formatFactory.time();
97
- return value => date(value) + ' ' + time(value);
98
- },
99
-
100
- ellipsis: function (part0, length, where) {
101
- length = Number(length);
102
- if (!(length > 3))
103
- length = 10;
104
- switch (where) {
105
- default:
106
- case "end":
107
- return (value) => {
108
- let s = String(value);
109
- if (s.length > length)
110
- return s.substring(0, length - 3) + "...";
111
- return s;
112
- };
113
-
114
- case "start":
115
- return (value) => {
116
- let s = String(value);
117
- if (s.length > length)
118
- return "..." + s.substring(s.length - length + 3);
119
- return s;
120
- };
121
-
122
- case "middle":
123
- return (value) => {
124
- let s = String(value);
125
- if (s.length > length) {
126
- let x = Math.floor(length - 2) / 2;
127
- return s.substring(0, x) + "..." + s.substring(s.length - (length - 3 - x));
128
- }
129
- return s;
130
- };
131
- }
132
- }
133
- };
134
-
135
- formatFactory.s = formatFactory.str = formatFactory.string;
136
- formatFactory.f = formatFactory.fixed;
137
- formatFactory.n = formatFactory.number;
138
- formatFactory.p = formatFactory.percentage;
139
- formatFactory.ps = formatFactory.percentageSign;
140
- formatFactory.d = formatFactory.date;
141
- formatFactory.t = formatFactory.time;
142
- formatFactory.dt = formatFactory.datetime;
143
-
144
- function buildFormatter(format) {
145
- let formatter = defaultFormatter, nullText = '';
146
- if (format) {
147
- let pipeParts = format.split('|');
148
- nullText = pipeParts[1] || '';
149
- let colonSepParts = pipeParts[0].split(':');
150
- for (let i = 0; i < colonSepParts.length; i++) {
151
- let parts = colonSepParts[i].split(';');
152
- let factory = formatFactory[parts[0]];
153
- if (!factory)
154
- debug('Unknown string format: ' + format);
155
- else if (i == 0)
156
- formatter = factory(...parts);
157
- else {
158
- let outerFmt = factory(...parts);
159
- let innerFmt = formatter;
160
- formatter = v => outerFmt(innerFmt(v));
161
- }
162
- }
163
- }
164
- return v => (v == null || v === '') ? nullText : formatter(v);
165
- }
166
-
167
- let format = {
168
- cache: {},
169
- };
170
-
171
- function getFormatCache() {
172
- if (format.cacheIdentifier != GlobalCacheIdentifier.get()) {
173
- format = {
174
- cache: {},
175
- cacheIdentifier: GlobalCacheIdentifier.get()
176
- };
177
- }
178
- return format.cache;
179
- }
180
-
181
- function getFormatter(format) {
182
- if (!format)
183
- format = '';
184
- let formatCache = getFormatCache();
185
- let formatter = formatCache[format];
186
- if (!formatter)
187
- formatter = formatCache[format] = buildFormatter(format);
188
-
189
- return formatter;
190
- }
191
-
192
- export class Format {
193
-
194
- static value(v, format) {
195
- let formatter = getFormatter(format);
196
- return formatter(v);
197
- }
198
-
199
- static parse(format) {
200
- return getFormatter(format);
201
- }
202
-
203
- static register(format, formatter) {
204
- this.registerFactory(format, () => formatter);
205
- }
206
-
207
- static registerFactory(format, factory) {
208
- if (isArray(format))
209
- format.forEach(f => this.registerFactory(f, factory));
210
- else
211
- formatFactory[format] = factory;
212
- }
213
- }
214
-
215
- export function resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits) {
216
- minimumFractionDigits = minimumFractionDigits != null ? Number(minimumFractionDigits) : minimumFractionDigits;
217
- maximumFractionDigits = maximumFractionDigits != null ? Number(maximumFractionDigits) : maximumFractionDigits;
218
-
219
- if (isNumber(minimumFractionDigits)) {
220
- if (isUndefined(maximumFractionDigits))
221
- maximumFractionDigits = minimumFractionDigits;
222
- else if (isNumber(maximumFractionDigits) && maximumFractionDigits < minimumFractionDigits)
223
- maximumFractionDigits = minimumFractionDigits;
224
- }
225
- else if (minimumFractionDigits == null && maximumFractionDigits == null) {
226
- minimumFractionDigits = 0;
227
- maximumFractionDigits = 18;
228
- }
229
-
230
- return {
231
- minimumFractionDigits,
232
- maximumFractionDigits
233
- }
234
- }
235
-
236
- export function trimFractionZeros(str, max) {
237
- let cnt = 0, l = str.length;
238
- while (cnt < max && (str[l - 1 - cnt] === '0' || str[l - 1 - cnt] === '.'))
239
- cnt++;
240
-
241
- return cnt > 0 ? str.substring(0, l - cnt) : str;
242
- }
1
+ import { debug } from "./Debug";
2
+ import { GlobalCacheIdentifier } from "./GlobalCacheIdentifier";
3
+ import { isNumber } from "../util/isNumber";
4
+ import { isUndefined } from "../util/isUndefined";
5
+ import { isArray } from "../util/isArray";
6
+
7
+ //Culture dependent formatters are defined in the ui package.
8
+
9
+ const defaultFormatter = (v) => v.toString();
10
+
11
+ let formatFactory = {
12
+ string: function () {
13
+ return defaultFormatter;
14
+ },
15
+
16
+ wrap: function (part0, prefix, suffix) {
17
+ if (!prefix) prefix = "";
18
+
19
+ if (!suffix) suffix = "";
20
+
21
+ return (value) => prefix + value.toString() + suffix;
22
+ },
23
+
24
+ fixed: function (part0, digits) {
25
+ return (value) => value.toFixed(digits);
26
+ },
27
+
28
+ prefix: function (part0, prefix) {
29
+ if (!prefix) prefix = "";
30
+
31
+ return (value) => prefix + value.toString();
32
+ },
33
+
34
+ suffix: function (part0, suffix) {
35
+ if (!suffix) suffix = "";
36
+
37
+ return (value) => value.toString() + suffix;
38
+ },
39
+
40
+ uppercase: function () {
41
+ return (value) => value.toString().toUpperCase();
42
+ },
43
+
44
+ lowercase: function () {
45
+ return (value) => value.toString().toLowerCase();
46
+ },
47
+
48
+ urlencode: function () {
49
+ return (value) => encodeURIComponent(value);
50
+ },
51
+
52
+ number: function (part0, minFractionDigits, maxFractionDigits) {
53
+ let { minimumFractionDigits, maximumFractionDigits } = resolveMinMaxFractionDigits(
54
+ minFractionDigits,
55
+ maxFractionDigits,
56
+ );
57
+ let trimmable = maximumFractionDigits - minimumFractionDigits;
58
+ if (trimmable > 0) {
59
+ if (minimumFractionDigits == 0) ++trimmable;
60
+ return (value) => trimFractionZeros(value.toFixed(maximumFractionDigits), trimmable);
61
+ }
62
+ return (value) => value.toFixed(maximumFractionDigits);
63
+ },
64
+
65
+ percentage: function (part0, minFractionDigits, maxFractionDigits) {
66
+ let numberFormatter = formatFactory.number(part0, minFractionDigits, maxFractionDigits);
67
+ return (value) => numberFormatter(value * 100) + "%";
68
+ },
69
+
70
+ percentageSign: function (part0, minFractionDigits, maxFractionDigits) {
71
+ let numberFormatter = formatFactory.number(part0, minFractionDigits, maxFractionDigits);
72
+ return (value) => numberFormatter(value) + "%";
73
+ },
74
+
75
+ date: function () {
76
+ return (value) => {
77
+ let date = new Date(value);
78
+ return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
79
+ };
80
+ },
81
+
82
+ time: function () {
83
+ return (value) => {
84
+ let date = new Date(value);
85
+ let h = date.getHours() >= 10 ? date.getHours() : "0" + date.getHours();
86
+ let m = date.getMinutes() >= 10 ? date.getMinutes() : "0" + date.getMinutes();
87
+ return `${h}:${m}`;
88
+ };
89
+ },
90
+
91
+ datetime: function () {
92
+ let date = formatFactory.date();
93
+ let time = formatFactory.time();
94
+ return (value) => date(value) + " " + time(value);
95
+ },
96
+
97
+ ellipsis: function (part0, length, where) {
98
+ length = Number(length);
99
+ if (!(length > 3)) length = 10;
100
+ switch (where) {
101
+ default:
102
+ case "end":
103
+ return (value) => {
104
+ let s = String(value);
105
+ if (s.length > length) return s.substring(0, length - 3) + "...";
106
+ return s;
107
+ };
108
+
109
+ case "start":
110
+ return (value) => {
111
+ let s = String(value);
112
+ if (s.length > length) return "..." + s.substring(s.length - length + 3);
113
+ return s;
114
+ };
115
+
116
+ case "middle":
117
+ return (value) => {
118
+ let s = String(value);
119
+ if (s.length > length) {
120
+ let x = Math.floor(length - 2) / 2;
121
+ return s.substring(0, x) + "..." + s.substring(s.length - (length - 3 - x));
122
+ }
123
+ return s;
124
+ };
125
+ }
126
+ },
127
+ };
128
+
129
+ formatFactory.s = formatFactory.str = formatFactory.string;
130
+ formatFactory.f = formatFactory.fixed;
131
+ formatFactory.n = formatFactory.number;
132
+ formatFactory.p = formatFactory.percentage;
133
+ formatFactory.ps = formatFactory.percentageSign;
134
+ formatFactory.d = formatFactory.date;
135
+ formatFactory.t = formatFactory.time;
136
+ formatFactory.dt = formatFactory.datetime;
137
+
138
+ function buildFormatter(format) {
139
+ let formatter = defaultFormatter,
140
+ nullText = "";
141
+ if (format) {
142
+ let pipeParts = format.split("|");
143
+ nullText = pipeParts[1] || "";
144
+ let colonSepParts = pipeParts[0].split(":");
145
+ for (let i = 0; i < colonSepParts.length; i++) {
146
+ let parts = colonSepParts[i].split(";");
147
+ let factory = formatFactory[parts[0]];
148
+ if (!factory) debug("Unknown string format: " + format);
149
+ else if (i == 0) formatter = factory(...parts);
150
+ else {
151
+ let outerFmt = factory(...parts);
152
+ let innerFmt = formatter;
153
+ formatter = (v) => outerFmt(innerFmt(v));
154
+ }
155
+ }
156
+ }
157
+ return (v) => (v == null || v === "" ? nullText : formatter(v));
158
+ }
159
+
160
+ let format = {
161
+ cache: {},
162
+ };
163
+
164
+ function getDefaultFormatCache() {
165
+ if (format.cacheIdentifier != GlobalCacheIdentifier.get()) {
166
+ format = {
167
+ cache: {},
168
+ cacheIdentifier: GlobalCacheIdentifier.get(),
169
+ };
170
+ }
171
+ return format.cache;
172
+ }
173
+
174
+ let getFormatCache = getDefaultFormatCache;
175
+
176
+ export function setGetFormatCacheCallback(callback) {
177
+ getFormatCache = callback;
178
+ }
179
+
180
+ function getFormatter(format) {
181
+ if (!format) format = "";
182
+ let formatCache = getFormatCache();
183
+ let formatter = formatCache[format];
184
+ if (!formatter) formatter = formatCache[format] = buildFormatter(format);
185
+
186
+ return formatter;
187
+ }
188
+
189
+ export class Format {
190
+ static value(v, format) {
191
+ let formatter = getFormatter(format);
192
+ return formatter(v);
193
+ }
194
+
195
+ static parse(format) {
196
+ return getFormatter(format);
197
+ }
198
+
199
+ static register(format, formatter) {
200
+ this.registerFactory(format, () => formatter);
201
+ }
202
+
203
+ static registerFactory(format, factory) {
204
+ if (isArray(format)) format.forEach((f) => this.registerFactory(f, factory));
205
+ else formatFactory[format] = factory;
206
+ }
207
+ }
208
+
209
+ export function resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits) {
210
+ minimumFractionDigits = minimumFractionDigits != null ? Number(minimumFractionDigits) : minimumFractionDigits;
211
+ maximumFractionDigits = maximumFractionDigits != null ? Number(maximumFractionDigits) : maximumFractionDigits;
212
+
213
+ if (isNumber(minimumFractionDigits)) {
214
+ if (isUndefined(maximumFractionDigits)) maximumFractionDigits = minimumFractionDigits;
215
+ else if (isNumber(maximumFractionDigits) && maximumFractionDigits < minimumFractionDigits)
216
+ maximumFractionDigits = minimumFractionDigits;
217
+ } else if (minimumFractionDigits == null && maximumFractionDigits == null) {
218
+ minimumFractionDigits = 0;
219
+ maximumFractionDigits = 18;
220
+ }
221
+
222
+ return {
223
+ minimumFractionDigits,
224
+ maximumFractionDigits,
225
+ };
226
+ }
227
+
228
+ export function trimFractionZeros(str, max) {
229
+ let cnt = 0,
230
+ l = str.length;
231
+ while (cnt < max && (str[l - 1 - cnt] === "0" || str[l - 1 - cnt] === ".")) cnt++;
232
+
233
+ return cnt > 0 ? str.substring(0, l - cnt) : str;
234
+ }