@perses-dev/core 0.53.0-beta.1 → 0.53.0-beta.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/dist/cjs/model/units/date.js +297 -0
- package/dist/cjs/model/units/temperature.js +64 -0
- package/dist/cjs/model/units/time.js +16 -2
- package/dist/cjs/model/units/units.js +27 -3
- package/dist/model/units/date.d.ts +28 -0
- package/dist/model/units/date.d.ts.map +1 -0
- package/dist/model/units/date.js +278 -0
- package/dist/model/units/date.js.map +1 -0
- package/dist/model/units/temperature.d.ts +11 -0
- package/dist/model/units/temperature.d.ts.map +1 -0
- package/dist/model/units/temperature.js +45 -0
- package/dist/model/units/temperature.js.map +1 -0
- package/dist/model/units/time.d.ts +3 -1
- package/dist/model/units/time.d.ts.map +1 -1
- package/dist/model/units/time.js +16 -2
- package/dist/model/units/time.js.map +1 -1
- package/dist/model/units/types.d.ts +1 -1
- package/dist/model/units/types.d.ts.map +1 -1
- package/dist/model/units/types.js.map +1 -1
- package/dist/model/units/units.d.ts +21 -1
- package/dist/model/units/units.d.ts.map +1 -1
- package/dist/model/units/units.js +21 -3
- package/dist/model/units/units.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// Copyright 2025 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
"use strict";
|
|
14
|
+
Object.defineProperty(exports, "__esModule", {
|
|
15
|
+
value: true
|
|
16
|
+
});
|
|
17
|
+
function _export(target, all) {
|
|
18
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: all[name]
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
_export(exports, {
|
|
24
|
+
DATE_GROUP_CONFIG: function() {
|
|
25
|
+
return DATE_GROUP_CONFIG;
|
|
26
|
+
},
|
|
27
|
+
DATE_UNIT_CONFIG: function() {
|
|
28
|
+
return DATE_UNIT_CONFIG;
|
|
29
|
+
},
|
|
30
|
+
formatDate: function() {
|
|
31
|
+
return formatDate;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const DATE_GROUP = 'Date';
|
|
35
|
+
const DATE_GROUP_CONFIG = {
|
|
36
|
+
label: 'Date & Time'
|
|
37
|
+
};
|
|
38
|
+
const DATE_UNIT_CONFIG = {
|
|
39
|
+
'datetime-iso': {
|
|
40
|
+
group: DATE_GROUP,
|
|
41
|
+
label: 'DateTime (GMT)'
|
|
42
|
+
},
|
|
43
|
+
'datetime-us': {
|
|
44
|
+
group: DATE_GROUP,
|
|
45
|
+
label: 'DateTime (US-East)'
|
|
46
|
+
},
|
|
47
|
+
'datetime-local': {
|
|
48
|
+
group: DATE_GROUP,
|
|
49
|
+
label: 'DateTime (Browser Local)'
|
|
50
|
+
},
|
|
51
|
+
'date-iso': {
|
|
52
|
+
group: DATE_GROUP,
|
|
53
|
+
label: 'Date (GMT)'
|
|
54
|
+
},
|
|
55
|
+
'date-us': {
|
|
56
|
+
group: DATE_GROUP,
|
|
57
|
+
label: 'Date (US-East)'
|
|
58
|
+
},
|
|
59
|
+
'date-local': {
|
|
60
|
+
group: DATE_GROUP,
|
|
61
|
+
label: 'Date (Browser Local)'
|
|
62
|
+
},
|
|
63
|
+
'time-local': {
|
|
64
|
+
group: DATE_GROUP,
|
|
65
|
+
label: 'Time (Browser Local)'
|
|
66
|
+
},
|
|
67
|
+
'time-iso': {
|
|
68
|
+
group: DATE_GROUP,
|
|
69
|
+
label: 'Time (GMT)'
|
|
70
|
+
},
|
|
71
|
+
'time-us': {
|
|
72
|
+
group: DATE_GROUP,
|
|
73
|
+
label: 'Time (US-East)'
|
|
74
|
+
},
|
|
75
|
+
'relative-time': {
|
|
76
|
+
group: DATE_GROUP,
|
|
77
|
+
label: 'Relative Time'
|
|
78
|
+
},
|
|
79
|
+
'unix-timestamp': {
|
|
80
|
+
group: DATE_GROUP,
|
|
81
|
+
label: 'Unix Timestamp (s)'
|
|
82
|
+
},
|
|
83
|
+
'unix-timestamp-ms': {
|
|
84
|
+
group: DATE_GROUP,
|
|
85
|
+
label: 'Unix Timestamp (ms)'
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Converts a numeric value to a Date object.
|
|
90
|
+
* Handles both Unix timestamps (seconds) and millisecond timestamps.
|
|
91
|
+
*/ function valueToDate(value) {
|
|
92
|
+
// Timestamp detection logic with special case handling
|
|
93
|
+
// Main threshold stays at 10 billion to maintain existing behavior
|
|
94
|
+
// Handle special edge cases explicitly
|
|
95
|
+
// Special case: negative timestamps
|
|
96
|
+
if (value < 0) {
|
|
97
|
+
// For negative values, check magnitude to distinguish seconds vs milliseconds
|
|
98
|
+
// Large negative values (> 1 billion in magnitude) are likely milliseconds
|
|
99
|
+
// Small negative values are likely seconds (rare edge case)
|
|
100
|
+
if (Math.abs(value) > 10000000000) {
|
|
101
|
+
return new Date(value); // milliseconds
|
|
102
|
+
} else {
|
|
103
|
+
return new Date(value * 1000); // seconds
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Special case: year 9999 in seconds (~253402300799)
|
|
107
|
+
// This is a very specific edge case for far-future timestamps
|
|
108
|
+
if (value >= 250000000000 && value <= 260000000000) {
|
|
109
|
+
// Check if this looks like year 9999 in seconds
|
|
110
|
+
const asSeconds = new Date(value * 1000);
|
|
111
|
+
const year = asSeconds.getUTCFullYear();
|
|
112
|
+
if (year >= 9999) {
|
|
113
|
+
return asSeconds; // seconds
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const SECONDS_THRESHOLD = 10000000000; // ~year 2286 - original threshold
|
|
117
|
+
if (value < SECONDS_THRESHOLD) {
|
|
118
|
+
// Assume it's in seconds
|
|
119
|
+
return new Date(value * 1000);
|
|
120
|
+
} else {
|
|
121
|
+
// Assume it's in milliseconds
|
|
122
|
+
return new Date(value);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Formats a relative time string using the Intl.RelativeTimeFormat API.
|
|
127
|
+
*/ function formatRelativeTime(date, referenceTime, locale) {
|
|
128
|
+
const referenceDate = new Date(referenceTime);
|
|
129
|
+
const diffMs = date.getTime() - referenceDate.getTime();
|
|
130
|
+
const units = [
|
|
131
|
+
{
|
|
132
|
+
unit: 'year',
|
|
133
|
+
ms: 1000 * 60 * 60 * 24 * 365
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
unit: 'month',
|
|
137
|
+
ms: 1000 * 60 * 60 * 24 * 30
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
unit: 'week',
|
|
141
|
+
ms: 1000 * 60 * 60 * 24 * 7
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
unit: 'day',
|
|
145
|
+
ms: 1000 * 60 * 60 * 24
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
unit: 'hour',
|
|
149
|
+
ms: 1000 * 60 * 60
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
unit: 'minute',
|
|
153
|
+
ms: 1000 * 60
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
unit: 'second',
|
|
157
|
+
ms: 1000
|
|
158
|
+
}
|
|
159
|
+
];
|
|
160
|
+
for (const { unit, ms } of units){
|
|
161
|
+
// Determine the value for the current unit, ensuring it's an integer for Intl.RelativeTimeFormat
|
|
162
|
+
const value = Math.round(diffMs / ms);
|
|
163
|
+
// If the absolute value is 1 or more, use this unit
|
|
164
|
+
if (Math.abs(value) >= 1) {
|
|
165
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
166
|
+
numeric: 'auto'
|
|
167
|
+
});
|
|
168
|
+
return rtf.format(value, unit);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// If less than a second, show "now" or "0 seconds"
|
|
172
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
173
|
+
numeric: 'auto'
|
|
174
|
+
});
|
|
175
|
+
return rtf.format(0, 'second');
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Gets the browser's preferred locale with comprehensive fallbacks.
|
|
179
|
+
*/ const getBrowserLocale = ()=>{
|
|
180
|
+
if (typeof navigator !== 'undefined') {
|
|
181
|
+
if (navigator.language) return navigator.language;
|
|
182
|
+
if (navigator.languages && navigator.languages.length > 0) {
|
|
183
|
+
const firstLanguage = navigator.languages[0];
|
|
184
|
+
if (firstLanguage) return firstLanguage;
|
|
185
|
+
}
|
|
186
|
+
// Legacy fallbacks for older browsers
|
|
187
|
+
const nav = navigator;
|
|
188
|
+
if (nav.userLanguage) return nav.userLanguage;
|
|
189
|
+
if (nav.browserLanguage) return nav.browserLanguage;
|
|
190
|
+
if (nav.systemLanguage) return nav.systemLanguage;
|
|
191
|
+
}
|
|
192
|
+
// Node.js or server-side fallback, or if navigator is not available/empty
|
|
193
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';
|
|
194
|
+
};
|
|
195
|
+
function formatDate(value, options = {}) {
|
|
196
|
+
const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
197
|
+
const { unit = 'datetime-local', locale = getBrowserLocale(), timeZone = systemTimeZone, referenceTime = Date.now() } = options;
|
|
198
|
+
// Handle raw timestamp display
|
|
199
|
+
if (unit === 'unix-timestamp') {
|
|
200
|
+
// Ensure it's in seconds. If it looks like milliseconds, convert.
|
|
201
|
+
const timestamp = value > 1000000000000 ? Math.floor(value / 1000) : value;
|
|
202
|
+
return timestamp.toString();
|
|
203
|
+
}
|
|
204
|
+
if (unit === 'unix-timestamp-ms') {
|
|
205
|
+
// Ensure it's in milliseconds. If it looks like seconds, convert.
|
|
206
|
+
// Use 100 billion as threshold - values < 100B are likely seconds, >= 100B are likely milliseconds
|
|
207
|
+
// This distinguishes between 999999999 (seconds) and 999999999000 (milliseconds)
|
|
208
|
+
const MILLISECONDS_THRESHOLD = 100000000000; // 100 billion
|
|
209
|
+
const timestamp = value < MILLISECONDS_THRESHOLD ? value * 1000 : value;
|
|
210
|
+
return Math.floor(timestamp).toString();
|
|
211
|
+
}
|
|
212
|
+
const date = valueToDate(value);
|
|
213
|
+
// Handle relative time
|
|
214
|
+
if (unit === 'relative-time') {
|
|
215
|
+
return formatRelativeTime(date, referenceTime, locale);
|
|
216
|
+
}
|
|
217
|
+
// Configure Intl.DateTimeFormat options based on unit
|
|
218
|
+
const formatOptions = {
|
|
219
|
+
timeZone
|
|
220
|
+
};
|
|
221
|
+
switch(unit){
|
|
222
|
+
case 'datetime-iso':
|
|
223
|
+
// datetime-iso should ALWAYS show GMT/UTC time
|
|
224
|
+
return date.toISOString();
|
|
225
|
+
case 'datetime-us':
|
|
226
|
+
{
|
|
227
|
+
// datetime-us should ALWAYS show date in US Eastern timezone
|
|
228
|
+
formatOptions.timeZone = 'America/New_York';
|
|
229
|
+
formatOptions.year = 'numeric';
|
|
230
|
+
formatOptions.month = '2-digit';
|
|
231
|
+
formatOptions.day = '2-digit';
|
|
232
|
+
formatOptions.hour = '2-digit';
|
|
233
|
+
formatOptions.minute = '2-digit';
|
|
234
|
+
formatOptions.second = '2-digit';
|
|
235
|
+
formatOptions.hour12 = true; // 12-hour format with AM/PM
|
|
236
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
237
|
+
return formatter.format(date);
|
|
238
|
+
}
|
|
239
|
+
case 'datetime-local':
|
|
240
|
+
// datetime-local should use the browser's local timezone (detected automatically)
|
|
241
|
+
// Don't override timeZone - let it use the detected system timezone
|
|
242
|
+
formatOptions.year = 'numeric';
|
|
243
|
+
formatOptions.month = '2-digit';
|
|
244
|
+
formatOptions.day = '2-digit';
|
|
245
|
+
formatOptions.hour = '2-digit';
|
|
246
|
+
formatOptions.minute = '2-digit';
|
|
247
|
+
formatOptions.second = '2-digit';
|
|
248
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
249
|
+
break;
|
|
250
|
+
case 'date-iso':
|
|
251
|
+
// date-iso should ALWAYS show GMT/UTC date
|
|
252
|
+
return date.toISOString().split('T')[0];
|
|
253
|
+
case 'date-us':
|
|
254
|
+
{
|
|
255
|
+
// date-us should ALWAYS show date in US Eastern timezone
|
|
256
|
+
formatOptions.timeZone = 'America/New_York';
|
|
257
|
+
formatOptions.year = 'numeric';
|
|
258
|
+
formatOptions.month = '2-digit';
|
|
259
|
+
formatOptions.day = '2-digit';
|
|
260
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
261
|
+
return formatter.format(date);
|
|
262
|
+
}
|
|
263
|
+
case 'date-local':
|
|
264
|
+
formatOptions.year = 'numeric';
|
|
265
|
+
formatOptions.month = '2-digit';
|
|
266
|
+
formatOptions.day = '2-digit';
|
|
267
|
+
break;
|
|
268
|
+
case 'time-local':
|
|
269
|
+
formatOptions.hour = '2-digit';
|
|
270
|
+
formatOptions.minute = '2-digit';
|
|
271
|
+
formatOptions.second = '2-digit';
|
|
272
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
273
|
+
break;
|
|
274
|
+
case 'time-iso':
|
|
275
|
+
// time-iso should ALWAYS show GMT/UTC time
|
|
276
|
+
return date.toISOString().split('T')[1].replace('Z', '');
|
|
277
|
+
case 'time-us':
|
|
278
|
+
{
|
|
279
|
+
// time-us should show time in US-East timezone (Eastern Time)
|
|
280
|
+
formatOptions.timeZone = 'America/New_York';
|
|
281
|
+
formatOptions.hour = '2-digit';
|
|
282
|
+
formatOptions.minute = '2-digit';
|
|
283
|
+
formatOptions.second = '2-digit';
|
|
284
|
+
formatOptions.hour12 = true;
|
|
285
|
+
return new Intl.DateTimeFormat('en-US', formatOptions).format(date);
|
|
286
|
+
}
|
|
287
|
+
default:
|
|
288
|
+
{
|
|
289
|
+
// This ensures that all DateUnits are handled at compile time.
|
|
290
|
+
const exhaustive = unit;
|
|
291
|
+
throw new Error(`Unknown date unit: ${exhaustive}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// For all other units, use Intl.DateTimeFormat with the specified locale and options
|
|
295
|
+
const formatter = new Intl.DateTimeFormat(locale, formatOptions);
|
|
296
|
+
return formatter.format(date);
|
|
297
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2025 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
"use strict";
|
|
14
|
+
Object.defineProperty(exports, "__esModule", {
|
|
15
|
+
value: true
|
|
16
|
+
});
|
|
17
|
+
function _export(target, all) {
|
|
18
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: all[name]
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
_export(exports, {
|
|
24
|
+
TEMPERATURE_GROUP_CONFIG: function() {
|
|
25
|
+
return TEMPERATURE_GROUP_CONFIG;
|
|
26
|
+
},
|
|
27
|
+
TEMPERATURE_UNIT_CONFIG: function() {
|
|
28
|
+
return TEMPERATURE_UNIT_CONFIG;
|
|
29
|
+
},
|
|
30
|
+
formatTemperature: function() {
|
|
31
|
+
return formatTemperature;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const _constants = require("./constants");
|
|
35
|
+
const _utils = require("./utils");
|
|
36
|
+
const TEMPERATURE_GROUP = 'Temperature';
|
|
37
|
+
const TEMPERATURE_GROUP_CONFIG = {
|
|
38
|
+
label: TEMPERATURE_GROUP,
|
|
39
|
+
decimalPlaces: true
|
|
40
|
+
};
|
|
41
|
+
const TEMPERATURE_UNIT_CONFIG = {
|
|
42
|
+
celsius: {
|
|
43
|
+
group: TEMPERATURE_GROUP,
|
|
44
|
+
label: 'Celsius (°C)'
|
|
45
|
+
},
|
|
46
|
+
fahrenheit: {
|
|
47
|
+
group: TEMPERATURE_GROUP,
|
|
48
|
+
label: 'Fahrenheit (°F)'
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const formatTemperature = (value, { unit, decimalPlaces })=>{
|
|
52
|
+
const formatterOptions = {
|
|
53
|
+
unit,
|
|
54
|
+
style: 'unit'
|
|
55
|
+
};
|
|
56
|
+
if ((0, _utils.hasDecimalPlaces)(decimalPlaces)) {
|
|
57
|
+
formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimalPlaces);
|
|
58
|
+
formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimalPlaces);
|
|
59
|
+
} else {
|
|
60
|
+
formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
|
|
61
|
+
}
|
|
62
|
+
const locals = unit === 'celsius' ? 'en-GB' : 'en-US';
|
|
63
|
+
return Intl.NumberFormat(locals, formatterOptions).format(value);
|
|
64
|
+
};
|
|
@@ -42,6 +42,14 @@ const TIME_GROUP_CONFIG = {
|
|
|
42
42
|
decimalPlaces: true
|
|
43
43
|
};
|
|
44
44
|
const TIME_UNIT_CONFIG = {
|
|
45
|
+
nanoseconds: {
|
|
46
|
+
group: TIME_GROUP,
|
|
47
|
+
label: 'Nanoseconds'
|
|
48
|
+
},
|
|
49
|
+
microseconds: {
|
|
50
|
+
group: TIME_GROUP,
|
|
51
|
+
label: 'Microseconds'
|
|
52
|
+
},
|
|
45
53
|
milliseconds: {
|
|
46
54
|
group: TIME_GROUP,
|
|
47
55
|
label: 'Milliseconds'
|
|
@@ -76,6 +84,8 @@ const TIME_UNIT_CONFIG = {
|
|
|
76
84
|
}
|
|
77
85
|
};
|
|
78
86
|
var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
87
|
+
PersesTimeToIntlTime["nanoseconds"] = "nanosecond";
|
|
88
|
+
PersesTimeToIntlTime["microseconds"] = "microsecond";
|
|
79
89
|
PersesTimeToIntlTime["milliseconds"] = "millisecond";
|
|
80
90
|
PersesTimeToIntlTime["seconds"] = "second";
|
|
81
91
|
PersesTimeToIntlTime["minutes"] = "minute";
|
|
@@ -99,7 +109,9 @@ var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
|
99
109
|
hours: 3600,
|
|
100
110
|
minutes: 60,
|
|
101
111
|
seconds: 1,
|
|
102
|
-
milliseconds: 0.001
|
|
112
|
+
milliseconds: 0.001,
|
|
113
|
+
microseconds: 0.000001,
|
|
114
|
+
nanoseconds: 0.000000001
|
|
103
115
|
};
|
|
104
116
|
const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
105
117
|
'years',
|
|
@@ -109,7 +121,9 @@ const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
|
109
121
|
'hours',
|
|
110
122
|
'minutes',
|
|
111
123
|
'seconds',
|
|
112
|
-
'milliseconds'
|
|
124
|
+
'milliseconds',
|
|
125
|
+
'microseconds',
|
|
126
|
+
'nanoseconds'
|
|
113
127
|
];
|
|
114
128
|
/**
|
|
115
129
|
* Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.
|
|
@@ -45,12 +45,18 @@ _export(exports, {
|
|
|
45
45
|
isCurrencyUnit: function() {
|
|
46
46
|
return isCurrencyUnit;
|
|
47
47
|
},
|
|
48
|
+
isDateUnit: function() {
|
|
49
|
+
return isDateUnit;
|
|
50
|
+
},
|
|
48
51
|
isDecimalUnit: function() {
|
|
49
52
|
return isDecimalUnit;
|
|
50
53
|
},
|
|
51
54
|
isPercentUnit: function() {
|
|
52
55
|
return isPercentUnit;
|
|
53
56
|
},
|
|
57
|
+
isTemperatureUnit: function() {
|
|
58
|
+
return isTemperatureUnit;
|
|
59
|
+
},
|
|
54
60
|
isThroughputUnit: function() {
|
|
55
61
|
return isThroughputUnit;
|
|
56
62
|
},
|
|
@@ -67,16 +73,20 @@ _export(exports, {
|
|
|
67
73
|
const _bytes = require("./bytes");
|
|
68
74
|
const _decimal = require("./decimal");
|
|
69
75
|
const _percent = require("./percent");
|
|
76
|
+
const _temperature = require("./temperature");
|
|
70
77
|
const _time = require("./time");
|
|
71
78
|
const _throughput = require("./throughput");
|
|
72
79
|
const _currency = require("./currency");
|
|
80
|
+
const _date = require("./date");
|
|
73
81
|
const UNIT_GROUP_CONFIG = {
|
|
74
82
|
Time: _time.TIME_GROUP_CONFIG,
|
|
75
83
|
Percent: _percent.PERCENT_GROUP_CONFIG,
|
|
76
84
|
Decimal: _decimal.DECIMAL_GROUP_CONFIG,
|
|
77
85
|
Bytes: _bytes.BYTES_GROUP_CONFIG,
|
|
78
86
|
Throughput: _throughput.THROUGHPUT_GROUP_CONFIG,
|
|
79
|
-
Currency: _currency.CURRENCY_GROUP_CONFIG
|
|
87
|
+
Currency: _currency.CURRENCY_GROUP_CONFIG,
|
|
88
|
+
Temperature: _temperature.TEMPERATURE_GROUP_CONFIG,
|
|
89
|
+
Date: _date.DATE_GROUP_CONFIG
|
|
80
90
|
};
|
|
81
91
|
const UNIT_CONFIG = {
|
|
82
92
|
..._time.TIME_UNIT_CONFIG,
|
|
@@ -84,10 +94,12 @@ const UNIT_CONFIG = {
|
|
|
84
94
|
..._decimal.DECIMAL_UNIT_CONFIG,
|
|
85
95
|
..._bytes.BYTES_UNIT_CONFIG,
|
|
86
96
|
..._throughput.THROUGHPUT_UNIT_CONFIG,
|
|
87
|
-
..._currency.CURRENCY_UNIT_CONFIG
|
|
97
|
+
..._currency.CURRENCY_UNIT_CONFIG,
|
|
98
|
+
..._temperature.TEMPERATURE_UNIT_CONFIG,
|
|
99
|
+
..._date.DATE_UNIT_CONFIG
|
|
88
100
|
};
|
|
89
101
|
function formatValue(value, formatOptions) {
|
|
90
|
-
if (formatOptions
|
|
102
|
+
if (!formatOptions) {
|
|
91
103
|
return value.toString();
|
|
92
104
|
}
|
|
93
105
|
if (isBytesUnit(formatOptions)) {
|
|
@@ -108,6 +120,12 @@ function formatValue(value, formatOptions) {
|
|
|
108
120
|
if (isCurrencyUnit(formatOptions)) {
|
|
109
121
|
return (0, _currency.formatCurrency)(value, formatOptions);
|
|
110
122
|
}
|
|
123
|
+
if (isDateUnit(formatOptions)) {
|
|
124
|
+
return (0, _date.formatDate)(value, formatOptions);
|
|
125
|
+
}
|
|
126
|
+
if (isTemperatureUnit(formatOptions)) {
|
|
127
|
+
return (0, _temperature.formatTemperature)(value, formatOptions);
|
|
128
|
+
}
|
|
111
129
|
const exhaustive = formatOptions;
|
|
112
130
|
throw new Error(`Unknown unit options ${exhaustive}`);
|
|
113
131
|
}
|
|
@@ -148,3 +166,9 @@ function isThroughputUnit(formatOptions) {
|
|
|
148
166
|
function isCurrencyUnit(formatOptions) {
|
|
149
167
|
return getUnitGroup(formatOptions) === 'Currency';
|
|
150
168
|
}
|
|
169
|
+
function isDateUnit(formatOptions) {
|
|
170
|
+
return getUnitGroup(formatOptions) === 'Date';
|
|
171
|
+
}
|
|
172
|
+
function isTemperatureUnit(formatOptions) {
|
|
173
|
+
return getUnitGroup(formatOptions) === 'Temperature';
|
|
174
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { UnitGroupConfig, UnitConfig } from './types';
|
|
2
|
+
type DateUnits = 'datetime-iso' | 'datetime-us' | 'datetime-local' | 'date-iso' | 'date-us' | 'date-local' | 'time-local' | 'time-iso' | 'time-us' | 'relative-time' | 'unix-timestamp' | 'unix-timestamp-ms';
|
|
3
|
+
export type DateFormatOptions = {
|
|
4
|
+
unit?: DateUnits;
|
|
5
|
+
/**
|
|
6
|
+
* The locale to use for formatting. Defaults to the system's locale.
|
|
7
|
+
*/
|
|
8
|
+
locale?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The timezone to use for formatting. Defaults to the user's local timezone.
|
|
11
|
+
*/
|
|
12
|
+
timeZone?: string;
|
|
13
|
+
/**
|
|
14
|
+
* For relative time formatting, the reference time to compare against.
|
|
15
|
+
* Defaults to current time.
|
|
16
|
+
*/
|
|
17
|
+
referenceTime?: number;
|
|
18
|
+
/**
|
|
19
|
+
* This property is not used for date formatting, but is included for
|
|
20
|
+
* compatibility with the FormatControls UI component.
|
|
21
|
+
*/
|
|
22
|
+
decimalPlaces?: number;
|
|
23
|
+
};
|
|
24
|
+
export declare const DATE_GROUP_CONFIG: UnitGroupConfig;
|
|
25
|
+
export declare const DATE_UNIT_CONFIG: Readonly<Record<DateUnits, UnitConfig>>;
|
|
26
|
+
export declare function formatDate(value: number, options?: DateFormatOptions): string;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=date.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../../src/model/units/date.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,KAAK,SAAS,GACV,cAAc,GACd,aAAa,GACb,gBAAgB,GAChB,UAAU,GACV,SAAS,GACT,YAAY,GACZ,YAAY,GACZ,UAAU,GACV,SAAS,GACT,eAAe,GACf,gBAAgB,GAChB,mBAAmB,CAAC;AAExB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAIF,eAAO,MAAM,iBAAiB,EAAE,eAE/B,CAAC;AAEF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAiDpE,CAAC;AAqGF,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAwHjF"}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Copyright 2025 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
const DATE_GROUP = 'Date';
|
|
14
|
+
export const DATE_GROUP_CONFIG = {
|
|
15
|
+
label: 'Date & Time'
|
|
16
|
+
};
|
|
17
|
+
export const DATE_UNIT_CONFIG = {
|
|
18
|
+
'datetime-iso': {
|
|
19
|
+
group: DATE_GROUP,
|
|
20
|
+
label: 'DateTime (GMT)'
|
|
21
|
+
},
|
|
22
|
+
'datetime-us': {
|
|
23
|
+
group: DATE_GROUP,
|
|
24
|
+
label: 'DateTime (US-East)'
|
|
25
|
+
},
|
|
26
|
+
'datetime-local': {
|
|
27
|
+
group: DATE_GROUP,
|
|
28
|
+
label: 'DateTime (Browser Local)'
|
|
29
|
+
},
|
|
30
|
+
'date-iso': {
|
|
31
|
+
group: DATE_GROUP,
|
|
32
|
+
label: 'Date (GMT)'
|
|
33
|
+
},
|
|
34
|
+
'date-us': {
|
|
35
|
+
group: DATE_GROUP,
|
|
36
|
+
label: 'Date (US-East)'
|
|
37
|
+
},
|
|
38
|
+
'date-local': {
|
|
39
|
+
group: DATE_GROUP,
|
|
40
|
+
label: 'Date (Browser Local)'
|
|
41
|
+
},
|
|
42
|
+
'time-local': {
|
|
43
|
+
group: DATE_GROUP,
|
|
44
|
+
label: 'Time (Browser Local)'
|
|
45
|
+
},
|
|
46
|
+
'time-iso': {
|
|
47
|
+
group: DATE_GROUP,
|
|
48
|
+
label: 'Time (GMT)'
|
|
49
|
+
},
|
|
50
|
+
'time-us': {
|
|
51
|
+
group: DATE_GROUP,
|
|
52
|
+
label: 'Time (US-East)'
|
|
53
|
+
},
|
|
54
|
+
'relative-time': {
|
|
55
|
+
group: DATE_GROUP,
|
|
56
|
+
label: 'Relative Time'
|
|
57
|
+
},
|
|
58
|
+
'unix-timestamp': {
|
|
59
|
+
group: DATE_GROUP,
|
|
60
|
+
label: 'Unix Timestamp (s)'
|
|
61
|
+
},
|
|
62
|
+
'unix-timestamp-ms': {
|
|
63
|
+
group: DATE_GROUP,
|
|
64
|
+
label: 'Unix Timestamp (ms)'
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Converts a numeric value to a Date object.
|
|
69
|
+
* Handles both Unix timestamps (seconds) and millisecond timestamps.
|
|
70
|
+
*/ function valueToDate(value) {
|
|
71
|
+
// Timestamp detection logic with special case handling
|
|
72
|
+
// Main threshold stays at 10 billion to maintain existing behavior
|
|
73
|
+
// Handle special edge cases explicitly
|
|
74
|
+
// Special case: negative timestamps
|
|
75
|
+
if (value < 0) {
|
|
76
|
+
// For negative values, check magnitude to distinguish seconds vs milliseconds
|
|
77
|
+
// Large negative values (> 1 billion in magnitude) are likely milliseconds
|
|
78
|
+
// Small negative values are likely seconds (rare edge case)
|
|
79
|
+
if (Math.abs(value) > 10000000000) {
|
|
80
|
+
return new Date(value); // milliseconds
|
|
81
|
+
} else {
|
|
82
|
+
return new Date(value * 1000); // seconds
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Special case: year 9999 in seconds (~253402300799)
|
|
86
|
+
// This is a very specific edge case for far-future timestamps
|
|
87
|
+
if (value >= 250000000000 && value <= 260000000000) {
|
|
88
|
+
// Check if this looks like year 9999 in seconds
|
|
89
|
+
const asSeconds = new Date(value * 1000);
|
|
90
|
+
const year = asSeconds.getUTCFullYear();
|
|
91
|
+
if (year >= 9999) {
|
|
92
|
+
return asSeconds; // seconds
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const SECONDS_THRESHOLD = 10000000000; // ~year 2286 - original threshold
|
|
96
|
+
if (value < SECONDS_THRESHOLD) {
|
|
97
|
+
// Assume it's in seconds
|
|
98
|
+
return new Date(value * 1000);
|
|
99
|
+
} else {
|
|
100
|
+
// Assume it's in milliseconds
|
|
101
|
+
return new Date(value);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Formats a relative time string using the Intl.RelativeTimeFormat API.
|
|
106
|
+
*/ function formatRelativeTime(date, referenceTime, locale) {
|
|
107
|
+
const referenceDate = new Date(referenceTime);
|
|
108
|
+
const diffMs = date.getTime() - referenceDate.getTime();
|
|
109
|
+
const units = [
|
|
110
|
+
{
|
|
111
|
+
unit: 'year',
|
|
112
|
+
ms: 1000 * 60 * 60 * 24 * 365
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
unit: 'month',
|
|
116
|
+
ms: 1000 * 60 * 60 * 24 * 30
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
unit: 'week',
|
|
120
|
+
ms: 1000 * 60 * 60 * 24 * 7
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
unit: 'day',
|
|
124
|
+
ms: 1000 * 60 * 60 * 24
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
unit: 'hour',
|
|
128
|
+
ms: 1000 * 60 * 60
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
unit: 'minute',
|
|
132
|
+
ms: 1000 * 60
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
unit: 'second',
|
|
136
|
+
ms: 1000
|
|
137
|
+
}
|
|
138
|
+
];
|
|
139
|
+
for (const { unit, ms } of units){
|
|
140
|
+
// Determine the value for the current unit, ensuring it's an integer for Intl.RelativeTimeFormat
|
|
141
|
+
const value = Math.round(diffMs / ms);
|
|
142
|
+
// If the absolute value is 1 or more, use this unit
|
|
143
|
+
if (Math.abs(value) >= 1) {
|
|
144
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
145
|
+
numeric: 'auto'
|
|
146
|
+
});
|
|
147
|
+
return rtf.format(value, unit);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// If less than a second, show "now" or "0 seconds"
|
|
151
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
152
|
+
numeric: 'auto'
|
|
153
|
+
});
|
|
154
|
+
return rtf.format(0, 'second');
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Gets the browser's preferred locale with comprehensive fallbacks.
|
|
158
|
+
*/ const getBrowserLocale = ()=>{
|
|
159
|
+
if (typeof navigator !== 'undefined') {
|
|
160
|
+
if (navigator.language) return navigator.language;
|
|
161
|
+
if (navigator.languages && navigator.languages.length > 0) {
|
|
162
|
+
const firstLanguage = navigator.languages[0];
|
|
163
|
+
if (firstLanguage) return firstLanguage;
|
|
164
|
+
}
|
|
165
|
+
// Legacy fallbacks for older browsers
|
|
166
|
+
const nav = navigator;
|
|
167
|
+
if (nav.userLanguage) return nav.userLanguage;
|
|
168
|
+
if (nav.browserLanguage) return nav.browserLanguage;
|
|
169
|
+
if (nav.systemLanguage) return nav.systemLanguage;
|
|
170
|
+
}
|
|
171
|
+
// Node.js or server-side fallback, or if navigator is not available/empty
|
|
172
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';
|
|
173
|
+
};
|
|
174
|
+
export function formatDate(value, options = {}) {
|
|
175
|
+
const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
176
|
+
const { unit = 'datetime-local', locale = getBrowserLocale(), timeZone = systemTimeZone, referenceTime = Date.now() } = options;
|
|
177
|
+
// Handle raw timestamp display
|
|
178
|
+
if (unit === 'unix-timestamp') {
|
|
179
|
+
// Ensure it's in seconds. If it looks like milliseconds, convert.
|
|
180
|
+
const timestamp = value > 1000000000000 ? Math.floor(value / 1000) : value;
|
|
181
|
+
return timestamp.toString();
|
|
182
|
+
}
|
|
183
|
+
if (unit === 'unix-timestamp-ms') {
|
|
184
|
+
// Ensure it's in milliseconds. If it looks like seconds, convert.
|
|
185
|
+
// Use 100 billion as threshold - values < 100B are likely seconds, >= 100B are likely milliseconds
|
|
186
|
+
// This distinguishes between 999999999 (seconds) and 999999999000 (milliseconds)
|
|
187
|
+
const MILLISECONDS_THRESHOLD = 100000000000; // 100 billion
|
|
188
|
+
const timestamp = value < MILLISECONDS_THRESHOLD ? value * 1000 : value;
|
|
189
|
+
return Math.floor(timestamp).toString();
|
|
190
|
+
}
|
|
191
|
+
const date = valueToDate(value);
|
|
192
|
+
// Handle relative time
|
|
193
|
+
if (unit === 'relative-time') {
|
|
194
|
+
return formatRelativeTime(date, referenceTime, locale);
|
|
195
|
+
}
|
|
196
|
+
// Configure Intl.DateTimeFormat options based on unit
|
|
197
|
+
const formatOptions = {
|
|
198
|
+
timeZone
|
|
199
|
+
};
|
|
200
|
+
switch(unit){
|
|
201
|
+
case 'datetime-iso':
|
|
202
|
+
// datetime-iso should ALWAYS show GMT/UTC time
|
|
203
|
+
return date.toISOString();
|
|
204
|
+
case 'datetime-us':
|
|
205
|
+
{
|
|
206
|
+
// datetime-us should ALWAYS show date in US Eastern timezone
|
|
207
|
+
formatOptions.timeZone = 'America/New_York';
|
|
208
|
+
formatOptions.year = 'numeric';
|
|
209
|
+
formatOptions.month = '2-digit';
|
|
210
|
+
formatOptions.day = '2-digit';
|
|
211
|
+
formatOptions.hour = '2-digit';
|
|
212
|
+
formatOptions.minute = '2-digit';
|
|
213
|
+
formatOptions.second = '2-digit';
|
|
214
|
+
formatOptions.hour12 = true; // 12-hour format with AM/PM
|
|
215
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
216
|
+
return formatter.format(date);
|
|
217
|
+
}
|
|
218
|
+
case 'datetime-local':
|
|
219
|
+
// datetime-local should use the browser's local timezone (detected automatically)
|
|
220
|
+
// Don't override timeZone - let it use the detected system timezone
|
|
221
|
+
formatOptions.year = 'numeric';
|
|
222
|
+
formatOptions.month = '2-digit';
|
|
223
|
+
formatOptions.day = '2-digit';
|
|
224
|
+
formatOptions.hour = '2-digit';
|
|
225
|
+
formatOptions.minute = '2-digit';
|
|
226
|
+
formatOptions.second = '2-digit';
|
|
227
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
228
|
+
break;
|
|
229
|
+
case 'date-iso':
|
|
230
|
+
// date-iso should ALWAYS show GMT/UTC date
|
|
231
|
+
return date.toISOString().split('T')[0];
|
|
232
|
+
case 'date-us':
|
|
233
|
+
{
|
|
234
|
+
// date-us should ALWAYS show date in US Eastern timezone
|
|
235
|
+
formatOptions.timeZone = 'America/New_York';
|
|
236
|
+
formatOptions.year = 'numeric';
|
|
237
|
+
formatOptions.month = '2-digit';
|
|
238
|
+
formatOptions.day = '2-digit';
|
|
239
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
240
|
+
return formatter.format(date);
|
|
241
|
+
}
|
|
242
|
+
case 'date-local':
|
|
243
|
+
formatOptions.year = 'numeric';
|
|
244
|
+
formatOptions.month = '2-digit';
|
|
245
|
+
formatOptions.day = '2-digit';
|
|
246
|
+
break;
|
|
247
|
+
case 'time-local':
|
|
248
|
+
formatOptions.hour = '2-digit';
|
|
249
|
+
formatOptions.minute = '2-digit';
|
|
250
|
+
formatOptions.second = '2-digit';
|
|
251
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
252
|
+
break;
|
|
253
|
+
case 'time-iso':
|
|
254
|
+
// time-iso should ALWAYS show GMT/UTC time
|
|
255
|
+
return date.toISOString().split('T')[1].replace('Z', '');
|
|
256
|
+
case 'time-us':
|
|
257
|
+
{
|
|
258
|
+
// time-us should show time in US-East timezone (Eastern Time)
|
|
259
|
+
formatOptions.timeZone = 'America/New_York';
|
|
260
|
+
formatOptions.hour = '2-digit';
|
|
261
|
+
formatOptions.minute = '2-digit';
|
|
262
|
+
formatOptions.second = '2-digit';
|
|
263
|
+
formatOptions.hour12 = true;
|
|
264
|
+
return new Intl.DateTimeFormat('en-US', formatOptions).format(date);
|
|
265
|
+
}
|
|
266
|
+
default:
|
|
267
|
+
{
|
|
268
|
+
// This ensures that all DateUnits are handled at compile time.
|
|
269
|
+
const exhaustive = unit;
|
|
270
|
+
throw new Error(`Unknown date unit: ${exhaustive}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// For all other units, use Intl.DateTimeFormat with the specified locale and options
|
|
274
|
+
const formatter = new Intl.DateTimeFormat(locale, formatOptions);
|
|
275
|
+
return formatter.format(date);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/date.ts"],"sourcesContent":["// Copyright 2025 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { UnitGroupConfig, UnitConfig } from './types';\n\ntype DateUnits =\n | 'datetime-iso'\n | 'datetime-us'\n | 'datetime-local'\n | 'date-iso'\n | 'date-us'\n | 'date-local'\n | 'time-local'\n | 'time-iso'\n | 'time-us'\n | 'relative-time'\n | 'unix-timestamp'\n | 'unix-timestamp-ms';\n\nexport type DateFormatOptions = {\n unit?: DateUnits;\n /**\n * The locale to use for formatting. Defaults to the system's locale.\n */\n locale?: string;\n /**\n * The timezone to use for formatting. Defaults to the user's local timezone.\n */\n timeZone?: string;\n /**\n * For relative time formatting, the reference time to compare against.\n * Defaults to current time.\n */\n referenceTime?: number;\n /**\n * This property is not used for date formatting, but is included for\n * compatibility with the FormatControls UI component.\n */\n decimalPlaces?: number;\n};\n\nconst DATE_GROUP = 'Date';\n\nexport const DATE_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Date & Time',\n};\n\nexport const DATE_UNIT_CONFIG: Readonly<Record<DateUnits, UnitConfig>> = {\n 'datetime-iso': {\n group: DATE_GROUP,\n label: 'DateTime (GMT)',\n },\n 'datetime-us': {\n group: DATE_GROUP,\n label: 'DateTime (US-East)',\n },\n 'datetime-local': {\n group: DATE_GROUP,\n label: 'DateTime (Browser Local)',\n },\n 'date-iso': {\n group: DATE_GROUP,\n label: 'Date (GMT)',\n },\n 'date-us': {\n group: DATE_GROUP,\n label: 'Date (US-East)',\n },\n 'date-local': {\n group: DATE_GROUP,\n label: 'Date (Browser Local)',\n },\n 'time-local': {\n group: DATE_GROUP,\n label: 'Time (Browser Local)',\n },\n 'time-iso': {\n group: DATE_GROUP,\n label: 'Time (GMT)',\n },\n 'time-us': {\n group: DATE_GROUP,\n label: 'Time (US-East)',\n },\n 'relative-time': {\n group: DATE_GROUP,\n label: 'Relative Time',\n },\n 'unix-timestamp': {\n group: DATE_GROUP,\n label: 'Unix Timestamp (s)',\n },\n 'unix-timestamp-ms': {\n group: DATE_GROUP,\n label: 'Unix Timestamp (ms)',\n },\n};\n\n/**\n * Converts a numeric value to a Date object.\n * Handles both Unix timestamps (seconds) and millisecond timestamps.\n */\nfunction valueToDate(value: number): Date {\n // Timestamp detection logic with special case handling\n // Main threshold stays at 10 billion to maintain existing behavior\n // Handle special edge cases explicitly\n\n // Special case: negative timestamps\n if (value < 0) {\n // For negative values, check magnitude to distinguish seconds vs milliseconds\n // Large negative values (> 1 billion in magnitude) are likely milliseconds\n // Small negative values are likely seconds (rare edge case)\n if (Math.abs(value) > 10000000000) {\n return new Date(value); // milliseconds\n } else {\n return new Date(value * 1000); // seconds\n }\n }\n\n // Special case: year 9999 in seconds (~253402300799)\n // This is a very specific edge case for far-future timestamps\n if (value >= 250000000000 && value <= 260000000000) {\n // Check if this looks like year 9999 in seconds\n const asSeconds = new Date(value * 1000);\n const year = asSeconds.getUTCFullYear();\n if (year >= 9999) {\n return asSeconds; // seconds\n }\n }\n\n const SECONDS_THRESHOLD = 10000000000; // ~year 2286 - original threshold\n\n if (value < SECONDS_THRESHOLD) {\n // Assume it's in seconds\n return new Date(value * 1000);\n } else {\n // Assume it's in milliseconds\n return new Date(value);\n }\n}\n\n/**\n * Formats a relative time string using the Intl.RelativeTimeFormat API.\n */\nfunction formatRelativeTime(date: Date, referenceTime: number, locale: string): string {\n const referenceDate = new Date(referenceTime);\n const diffMs = date.getTime() - referenceDate.getTime();\n\n const units = [\n { unit: 'year', ms: 1000 * 60 * 60 * 24 * 365 },\n { unit: 'month', ms: 1000 * 60 * 60 * 24 * 30 },\n { unit: 'week', ms: 1000 * 60 * 60 * 24 * 7 },\n { unit: 'day', ms: 1000 * 60 * 60 * 24 },\n { unit: 'hour', ms: 1000 * 60 * 60 },\n { unit: 'minute', ms: 1000 * 60 },\n { unit: 'second', ms: 1000 },\n ] as const;\n\n for (const { unit, ms } of units) {\n // Determine the value for the current unit, ensuring it's an integer for Intl.RelativeTimeFormat\n const value = Math.round(diffMs / ms);\n // If the absolute value is 1 or more, use this unit\n if (Math.abs(value) >= 1) {\n const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });\n return rtf.format(value, unit);\n }\n }\n\n // If less than a second, show \"now\" or \"0 seconds\"\n const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });\n return rtf.format(0, 'second');\n}\n\n/**\n * Gets the browser's preferred locale with comprehensive fallbacks.\n */\nconst getBrowserLocale = (): string => {\n if (typeof navigator !== 'undefined') {\n if (navigator.language) return navigator.language;\n if (navigator.languages && navigator.languages.length > 0) {\n const firstLanguage = navigator.languages[0];\n if (firstLanguage) return firstLanguage;\n }\n // Legacy fallbacks for older browsers\n const nav = navigator as Navigator & {\n userLanguage?: string;\n browserLanguage?: string;\n systemLanguage?: string;\n };\n if (nav.userLanguage) return nav.userLanguage;\n if (nav.browserLanguage) return nav.browserLanguage;\n if (nav.systemLanguage) return nav.systemLanguage;\n }\n // Node.js or server-side fallback, or if navigator is not available/empty\n return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';\n};\n\nexport function formatDate(value: number, options: DateFormatOptions = {}): string {\n const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n\n const {\n unit = 'datetime-local',\n locale = getBrowserLocale(),\n timeZone = systemTimeZone,\n referenceTime = Date.now(),\n } = options;\n\n // Handle raw timestamp display\n if (unit === 'unix-timestamp') {\n // Ensure it's in seconds. If it looks like milliseconds, convert.\n const timestamp = value > 1000000000000 ? Math.floor(value / 1000) : value;\n return timestamp.toString();\n }\n\n if (unit === 'unix-timestamp-ms') {\n // Ensure it's in milliseconds. If it looks like seconds, convert.\n // Use 100 billion as threshold - values < 100B are likely seconds, >= 100B are likely milliseconds\n // This distinguishes between 999999999 (seconds) and 999999999000 (milliseconds)\n const MILLISECONDS_THRESHOLD = 100000000000; // 100 billion\n const timestamp = value < MILLISECONDS_THRESHOLD ? value * 1000 : value;\n return Math.floor(timestamp).toString();\n }\n\n const date = valueToDate(value);\n\n // Handle relative time\n if (unit === 'relative-time') {\n return formatRelativeTime(date, referenceTime, locale);\n }\n\n // Configure Intl.DateTimeFormat options based on unit\n const formatOptions: Intl.DateTimeFormatOptions = {\n timeZone, // This will be overridden for specific units that need different timezones\n };\n\n switch (unit) {\n case 'datetime-iso':\n // datetime-iso should ALWAYS show GMT/UTC time\n return date.toISOString();\n\n case 'datetime-us': {\n // datetime-us should ALWAYS show date in US Eastern timezone\n formatOptions.timeZone = 'America/New_York';\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = true; // 12-hour format with AM/PM\n const formatter = new Intl.DateTimeFormat('en-US', formatOptions);\n return formatter.format(date);\n }\n\n case 'datetime-local':\n // datetime-local should use the browser's local timezone (detected automatically)\n // Don't override timeZone - let it use the detected system timezone\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = false; // 24-hour format\n break;\n\n case 'date-iso':\n // date-iso should ALWAYS show GMT/UTC date\n return date.toISOString().split('T')[0]!;\n\n case 'date-us': {\n // date-us should ALWAYS show date in US Eastern timezone\n formatOptions.timeZone = 'America/New_York';\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n const formatter = new Intl.DateTimeFormat('en-US', formatOptions);\n return formatter.format(date);\n }\n\n case 'date-local':\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n break;\n\n case 'time-local':\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = false; // 24-hour format\n break;\n\n case 'time-iso':\n // time-iso should ALWAYS show GMT/UTC time\n return date.toISOString().split('T')[1]!.replace('Z', '');\n\n case 'time-us': {\n // time-us should show time in US-East timezone (Eastern Time)\n formatOptions.timeZone = 'America/New_York';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = true;\n return new Intl.DateTimeFormat('en-US', formatOptions).format(date);\n }\n\n default: {\n // This ensures that all DateUnits are handled at compile time.\n const exhaustive: never = unit;\n throw new Error(`Unknown date unit: ${exhaustive}`);\n }\n }\n\n // For all other units, use Intl.DateTimeFormat with the specified locale and options\n const formatter = new Intl.DateTimeFormat(locale, formatOptions);\n return formatter.format(date);\n}\n"],"names":["DATE_GROUP","DATE_GROUP_CONFIG","label","DATE_UNIT_CONFIG","group","valueToDate","value","Math","abs","Date","asSeconds","year","getUTCFullYear","SECONDS_THRESHOLD","formatRelativeTime","date","referenceTime","locale","referenceDate","diffMs","getTime","units","unit","ms","round","rtf","Intl","RelativeTimeFormat","numeric","format","getBrowserLocale","navigator","language","languages","length","firstLanguage","nav","userLanguage","browserLanguage","systemLanguage","DateTimeFormat","resolvedOptions","formatDate","options","systemTimeZone","timeZone","now","timestamp","floor","toString","MILLISECONDS_THRESHOLD","formatOptions","toISOString","month","day","hour","minute","second","hour12","formatter","split","replace","exhaustive","Error"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAwCjC,MAAMA,aAAa;AAEnB,OAAO,MAAMC,oBAAqC;IAChDC,OAAO;AACT,EAAE;AAEF,OAAO,MAAMC,mBAA4D;IACvE,gBAAgB;QACdC,OAAOJ;QACPE,OAAO;IACT;IACA,eAAe;QACbE,OAAOJ;QACPE,OAAO;IACT;IACA,kBAAkB;QAChBE,OAAOJ;QACPE,OAAO;IACT;IACA,YAAY;QACVE,OAAOJ;QACPE,OAAO;IACT;IACA,WAAW;QACTE,OAAOJ;QACPE,OAAO;IACT;IACA,cAAc;QACZE,OAAOJ;QACPE,OAAO;IACT;IACA,cAAc;QACZ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;gBACvB,OAAO,IAAIhC,KAAKc,cAAc,CAAC,SAASW,eAAetB,MAAM,CAACd;YAChE;QAEA;YAAS;gBACP,+DAA+D;gBAC/D,MAAM+C,aAAoBxC;gBAC1B,MAAM,IAAIyC,MAAM,CAAC,mBAAmB,EAAED,YAAY;YACpD;IACF;IAEA,qFAAqF;IACrF,MAAMH,YAAY,IAAIjC,KAAKc,cAAc,CAACvB,QAAQkC;IAClD,OAAOQ,UAAU9B,MAAM,CAACd;AAC1B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UnitConfig, UnitGroupConfig } from './types';
|
|
2
|
+
type TemperatureUnits = 'celsius' | 'fahrenheit';
|
|
3
|
+
export type TemperatureFormatOptions = {
|
|
4
|
+
unit: TemperatureUnits;
|
|
5
|
+
decimalPlaces?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const TEMPERATURE_GROUP_CONFIG: UnitGroupConfig;
|
|
8
|
+
export declare const TEMPERATURE_UNIT_CONFIG: Readonly<Record<TemperatureUnits, UnitConfig>>;
|
|
9
|
+
export declare const formatTemperature: (value: number, { unit, decimalPlaces }: TemperatureFormatOptions) => string;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=temperature.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temperature.d.ts","sourceRoot":"","sources":["../../../src/model/units/temperature.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAKtD,KAAK,gBAAgB,GAAG,SAAS,GAAG,YAAY,CAAC;AAEjD,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,gBAAgB,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,eAGtC,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,gBAAgB,EAAE,UAAU,CAAC,CASlF,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,MAAM,2BAA2B,wBAAwB,KAAG,MAepG,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2025 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
14
|
+
import { hasDecimalPlaces, limitDecimalPlaces } from './utils';
|
|
15
|
+
const TEMPERATURE_GROUP = 'Temperature';
|
|
16
|
+
export const TEMPERATURE_GROUP_CONFIG = {
|
|
17
|
+
label: TEMPERATURE_GROUP,
|
|
18
|
+
decimalPlaces: true
|
|
19
|
+
};
|
|
20
|
+
export const TEMPERATURE_UNIT_CONFIG = {
|
|
21
|
+
celsius: {
|
|
22
|
+
group: TEMPERATURE_GROUP,
|
|
23
|
+
label: 'Celsius (°C)'
|
|
24
|
+
},
|
|
25
|
+
fahrenheit: {
|
|
26
|
+
group: TEMPERATURE_GROUP,
|
|
27
|
+
label: 'Fahrenheit (°F)'
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const formatTemperature = (value, { unit, decimalPlaces })=>{
|
|
31
|
+
const formatterOptions = {
|
|
32
|
+
unit,
|
|
33
|
+
style: 'unit'
|
|
34
|
+
};
|
|
35
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
36
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
37
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
38
|
+
} else {
|
|
39
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
40
|
+
}
|
|
41
|
+
const locals = unit === 'celsius' ? 'en-GB' : 'en-US';
|
|
42
|
+
return Intl.NumberFormat(locals, formatterOptions).format(value);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=temperature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/temperature.ts"],"sourcesContent":["// Copyright 2025 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitConfig, UnitGroupConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\nconst TEMPERATURE_GROUP = 'Temperature';\n\ntype TemperatureUnits = 'celsius' | 'fahrenheit';\n\nexport type TemperatureFormatOptions = {\n unit: TemperatureUnits;\n decimalPlaces?: number;\n};\n\nexport const TEMPERATURE_GROUP_CONFIG: UnitGroupConfig = {\n label: TEMPERATURE_GROUP,\n decimalPlaces: true,\n};\n\nexport const TEMPERATURE_UNIT_CONFIG: Readonly<Record<TemperatureUnits, UnitConfig>> = {\n celsius: {\n group: TEMPERATURE_GROUP,\n label: 'Celsius (°C)',\n },\n fahrenheit: {\n group: TEMPERATURE_GROUP,\n label: 'Fahrenheit (°F)',\n },\n};\n\nexport const formatTemperature = (value: number, { unit, decimalPlaces }: TemperatureFormatOptions): string => {\n const formatterOptions: Intl.NumberFormatOptions = {\n unit,\n style: 'unit',\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n const locals = unit === 'celsius' ? 'en-GB' : 'en-US';\n return Intl.NumberFormat(locals, formatterOptions).format(value);\n};\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","TEMPERATURE_GROUP","TEMPERATURE_GROUP_CONFIG","label","decimalPlaces","TEMPERATURE_UNIT_CONFIG","celsius","group","fahrenheit","formatTemperature","value","unit","formatterOptions","style","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","locals","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAE/D,MAAMC,oBAAoB;AAS1B,OAAO,MAAMC,2BAA4C;IACvDC,OAAOF;IACPG,eAAe;AACjB,EAAE;AAEF,OAAO,MAAMC,0BAA0E;IACrFC,SAAS;QACPC,OAAON;QACPE,OAAO;IACT;IACAK,YAAY;QACVD,OAAON;QACPE,OAAO;IACT;AACF,EAAE;AAEF,OAAO,MAAMM,oBAAoB,CAACC,OAAe,EAAEC,IAAI,EAAEP,aAAa,EAA4B;IAChG,MAAMQ,mBAA6C;QACjDD;QACAE,OAAO;IACT;IAEA,IAAId,iBAAiBK,gBAAgB;QACnCQ,iBAAiBE,qBAAqB,GAAGd,mBAAmBI;QAC5DQ,iBAAiBG,qBAAqB,GAAGf,mBAAmBI;IAC9D,OAAO;QACLQ,iBAAiBI,wBAAwB,GAAGlB;IAC9C;IAEA,MAAMmB,SAASN,SAAS,YAAY,UAAU;IAC9C,OAAOO,KAAKC,YAAY,CAACF,QAAQL,kBAAkBQ,MAAM,CAACV;AAC5D,EAAE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { UnitGroupConfig, UnitConfig } from './types';
|
|
2
|
-
type TimeUnits = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
|
|
2
|
+
type TimeUnits = 'nanoseconds' | 'microseconds' | 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
|
|
3
3
|
export type TimeFormatOptions = {
|
|
4
4
|
unit?: TimeUnits;
|
|
5
5
|
decimalPlaces?: number;
|
|
@@ -7,6 +7,8 @@ export type TimeFormatOptions = {
|
|
|
7
7
|
export declare const TIME_GROUP_CONFIG: UnitGroupConfig;
|
|
8
8
|
export declare const TIME_UNIT_CONFIG: Readonly<Record<TimeUnits, UnitConfig>>;
|
|
9
9
|
export declare enum PersesTimeToIntlTime {
|
|
10
|
+
nanoseconds = "nanosecond",
|
|
11
|
+
microseconds = "microsecond",
|
|
10
12
|
milliseconds = "millisecond",
|
|
11
13
|
seconds = "second",
|
|
12
14
|
minutes = "minute",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,KAAK,SAAS,
|
|
1
|
+
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,KAAK,SAAS,GACV,aAAa,GACb,cAAc,GACd,cAAc,GACd,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,OAAO,GACP,QAAQ,GACR,OAAO,CAAC;AACZ,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,eAG/B,CAAC;AACF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAyCpE,CAAC;AAIF,oBAAY,oBAAoB;IAC9B,WAAW,eAAe;IAC1B,YAAY,gBAAgB;IAC5B,YAAY,gBAAgB;IAC5B,OAAO,WAAW;IAClB,OAAO,WAAW;IAClB,KAAK,SAAS;IACd,IAAI,QAAQ;IACZ,KAAK,SAAS;IACd,MAAM,UAAU;IAChB,KAAK,SAAS;CACf;AA4DD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,iBAAiB,GAAG,MAAM,CAoB5F"}
|
package/dist/model/units/time.js
CHANGED
|
@@ -18,6 +18,14 @@ export const TIME_GROUP_CONFIG = {
|
|
|
18
18
|
decimalPlaces: true
|
|
19
19
|
};
|
|
20
20
|
export const TIME_UNIT_CONFIG = {
|
|
21
|
+
nanoseconds: {
|
|
22
|
+
group: TIME_GROUP,
|
|
23
|
+
label: 'Nanoseconds'
|
|
24
|
+
},
|
|
25
|
+
microseconds: {
|
|
26
|
+
group: TIME_GROUP,
|
|
27
|
+
label: 'Microseconds'
|
|
28
|
+
},
|
|
21
29
|
milliseconds: {
|
|
22
30
|
group: TIME_GROUP,
|
|
23
31
|
label: 'Milliseconds'
|
|
@@ -54,6 +62,8 @@ export const TIME_UNIT_CONFIG = {
|
|
|
54
62
|
// Mapping of time units to what Intl.NumberFormat formatter expects
|
|
55
63
|
// https://v8.dev/features/intl-numberformat#units
|
|
56
64
|
export var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
65
|
+
PersesTimeToIntlTime["nanoseconds"] = "nanosecond";
|
|
66
|
+
PersesTimeToIntlTime["microseconds"] = "microsecond";
|
|
57
67
|
PersesTimeToIntlTime["milliseconds"] = "millisecond";
|
|
58
68
|
PersesTimeToIntlTime["seconds"] = "second";
|
|
59
69
|
PersesTimeToIntlTime["minutes"] = "minute";
|
|
@@ -77,7 +87,9 @@ export var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
|
77
87
|
hours: 3600,
|
|
78
88
|
minutes: 60,
|
|
79
89
|
seconds: 1,
|
|
80
|
-
milliseconds: 0.001
|
|
90
|
+
milliseconds: 0.001,
|
|
91
|
+
microseconds: 0.000001,
|
|
92
|
+
nanoseconds: 0.000000001
|
|
81
93
|
};
|
|
82
94
|
const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
83
95
|
'years',
|
|
@@ -87,7 +99,9 @@ const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
|
87
99
|
'hours',
|
|
88
100
|
'minutes',
|
|
89
101
|
'seconds',
|
|
90
|
-
'milliseconds'
|
|
102
|
+
'milliseconds',
|
|
103
|
+
'microseconds',
|
|
104
|
+
'nanoseconds'
|
|
91
105
|
];
|
|
92
106
|
/**
|
|
93
107
|
* Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/model/units/time.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\ntype TimeUnits
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/time.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\ntype TimeUnits =\n | 'nanoseconds'\n | 'microseconds'\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'weeks'\n | 'months'\n | 'years';\nexport type TimeFormatOptions = {\n unit?: TimeUnits;\n decimalPlaces?: number;\n};\nconst TIME_GROUP = 'Time';\nexport const TIME_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Time',\n decimalPlaces: true,\n};\nexport const TIME_UNIT_CONFIG: Readonly<Record<TimeUnits, UnitConfig>> = {\n nanoseconds: {\n group: TIME_GROUP,\n label: 'Nanoseconds',\n },\n microseconds: {\n group: TIME_GROUP,\n label: 'Microseconds',\n },\n milliseconds: {\n group: TIME_GROUP,\n label: 'Milliseconds',\n },\n seconds: {\n group: TIME_GROUP,\n label: 'Seconds',\n },\n minutes: {\n group: TIME_GROUP,\n label: 'Minutes',\n },\n hours: {\n group: TIME_GROUP,\n label: 'Hours',\n },\n days: {\n group: TIME_GROUP,\n label: 'Days',\n },\n weeks: {\n group: TIME_GROUP,\n label: 'Weeks',\n },\n months: {\n group: TIME_GROUP,\n label: 'Months',\n },\n years: {\n group: TIME_GROUP,\n label: 'Years',\n },\n};\n\n// Mapping of time units to what Intl.NumberFormat formatter expects\n// https://v8.dev/features/intl-numberformat#units\nexport enum PersesTimeToIntlTime {\n nanoseconds = 'nanosecond',\n microseconds = 'microsecond',\n milliseconds = 'millisecond',\n seconds = 'second',\n minutes = 'minute',\n hours = 'hour',\n days = 'day',\n weeks = 'week',\n months = 'month',\n years = 'year',\n}\n\n/**\n * Note: This conversion will not be exactly accurate for months and years,\n * due variations in the lengths of months (i.e. 28 - 31 days) and years (i.e. leap years).\n * For precision with months and years, we would need more complex algorithms and/or external libraries.\n * However, we expect that measurements in months and years will be rare.\n */\nconst TIME_UNITS_IN_SECONDS: Record<TimeUnits, number> = {\n years: 31536000, // 365 days\n months: 2592000, // 30 days\n weeks: 604800, // 7 days\n days: 86400,\n hours: 3600,\n minutes: 60,\n seconds: 1,\n milliseconds: 0.001,\n microseconds: 0.000001,\n nanoseconds: 0.000000001,\n};\n\nconst LARGEST_TO_SMALLEST_TIME_UNITS: TimeUnits[] = [\n 'years',\n 'months',\n 'weeks',\n 'days',\n 'hours',\n 'minutes',\n 'seconds',\n 'milliseconds',\n 'microseconds',\n 'nanoseconds',\n];\n\n/**\n * Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.\n */\nfunction getValueAndKindForNaturalNumbers(value: number, unit: TimeUnits): { value: number; unit: TimeUnits } {\n const valueInSeconds = value * TIME_UNITS_IN_SECONDS[unit];\n\n // Initialize for TS\n const largestTimeUnit = LARGEST_TO_SMALLEST_TIME_UNITS[0] || 'years';\n let timeUnit: TimeUnits = largestTimeUnit;\n let valueInTimeUnit: number = valueInSeconds / TIME_UNITS_IN_SECONDS[largestTimeUnit];\n\n for (timeUnit of LARGEST_TO_SMALLEST_TIME_UNITS) {\n valueInTimeUnit = valueInSeconds / TIME_UNITS_IN_SECONDS[timeUnit];\n if (valueInTimeUnit >= 1) {\n return { value: valueInTimeUnit, unit: timeUnit };\n }\n }\n\n // If we didn't find a time unit, we have to settle for the smallest time unit (which is the last time unit).\n return { value: valueInTimeUnit, unit: timeUnit };\n}\n\nfunction isMonthOrYear(unit: TimeUnits): boolean {\n return unit === 'months' || unit === 'years';\n}\n\nexport function formatTime(value: number, { unit, decimalPlaces }: TimeFormatOptions): string {\n if (value === 0) return '0s';\n\n const results = getValueAndKindForNaturalNumbers(value, unit ?? 'seconds');\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: PersesTimeToIntlTime[results.unit],\n unitDisplay: isMonthOrYear(results.unit) ? 'long' : 'narrow',\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(results.value);\n}\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","TIME_GROUP","TIME_GROUP_CONFIG","label","decimalPlaces","TIME_UNIT_CONFIG","nanoseconds","group","microseconds","milliseconds","seconds","minutes","hours","days","weeks","months","years","PersesTimeToIntlTime","TIME_UNITS_IN_SECONDS","LARGEST_TO_SMALLEST_TIME_UNITS","getValueAndKindForNaturalNumbers","value","unit","valueInSeconds","largestTimeUnit","timeUnit","valueInTimeUnit","isMonthOrYear","formatTime","results","formatterOptions","style","unitDisplay","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAiB/D,MA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BAAiBJ,MAAMG;YAAS;QAClD;IACF;IAEA,6GAA6G;IAC7G,OAAO;QAAEJ,OAAOK;QAAiBJ,MAAMG;IAAS;AAClD;AAEA,SAASE,cAAcL,IAAe;IACpC,OAAOA,SAAS,YAAYA,SAAS;AACvC;AAEA,OAAO,SAASM,WAAWP,KAAa,EAAE,EAAEC,IAAI,EAAElB,aAAa,EAAqB;IAClF,IAAIiB,UAAU,GAAG,OAAO;IAExB,MAAMQ,UAAUT,iCAAiCC,OAAOC,QAAQ;IAEhE,MAAMQ,mBAA6C;QACjDC,OAAO;QACPT,MAAML,oBAAoB,CAACY,QAAQP,IAAI,CAAC;QACxCU,aAAaL,cAAcE,QAAQP,IAAI,IAAI,SAAS;IACtD;IAEA,IAAIvB,iBAAiBK,gBAAgB;QACnC0B,iBAAiBG,qBAAqB,GAAGjC,mBAAmBI;QAC5D0B,iBAAiBI,qBAAqB,GAAGlC,mBAAmBI;IAC9D,OAAO;QACL0B,iBAAiBK,wBAAwB,GAAGrC;IAC9C;IAEA,MAAMsC,YAAYC,KAAKC,YAAY,CAAC,SAASR;IAC7C,OAAOM,UAAUG,MAAM,CAACV,QAAQR,KAAK;AACvC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Duration } from 'date-fns';
|
|
2
2
|
import { AbsoluteTimeRange, DurationString } from '../time';
|
|
3
3
|
import { FormatOptions } from './units';
|
|
4
|
-
export type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Throughput' | 'Currency';
|
|
4
|
+
export type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Throughput' | 'Currency' | 'Temperature' | 'Date';
|
|
5
5
|
/**
|
|
6
6
|
* Configuration for rendering units that are part of a group.
|
|
7
7
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/model/units/types.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,QAAQ,EAAE,MAA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AAW,cAAc;IAC7B,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,cAAc,CAAC;CAC1B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/model/units/types.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// Common types needed across individual unit groups and the overall combined\n// units.\n\nimport { Duration } from 'date-fns';\nimport { AbsoluteTimeRange, DurationString } from '../time';\nimport { FormatOptions } from './units';\n\nexport type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Throughput' | 'Currency';\n\n/**\n * Configuration for rendering units that are part of a group.\n */\nexport type UnitGroupConfig = {\n /**\n * The label that is shown in the UI.\n */\n label: string;\n /**\n * When true, the unit group supports setting decimal places.\n */\n decimalPlaces?: boolean;\n /**\n * When true, the unit group supports enabling shortValues.\n */\n shortValues?: boolean;\n};\n\n/**\n * Configuration for rendering a specific unit.\n */\nexport type UnitConfig = {\n /**\n * The group the unit is part of. This will inform common rendering behavior.\n */\n group?: UnitGroup;\n\n /**\n * When true, this unit will not be displayed in the unit selector. This is\n * useful for units that are shorthand variants of other units.\n */\n disableSelectorOption?: boolean;\n\n /**\n * The label that is shown in the UI.\n */\n label: string;\n};\n\n/**\n * Used in the tests for each type of unit.\n */\nexport interface UnitTestCase {\n value: number;\n format: FormatOptions;\n expected: string;\n}\n\nexport interface IntervalTestCase {\n timeRange: AbsoluteTimeRange;\n expected: Duration;\n}\n\nexport interface FormatTestCase {\n duration: Duration;\n expected: DurationString;\n}\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,6EAA6E;AAC7E,SAAS;AA6DT,WAGC"}
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/types.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// Common types needed across individual unit groups and the overall combined\n// units.\n\nimport { Duration } from 'date-fns';\nimport { AbsoluteTimeRange, DurationString } from '../time';\nimport { FormatOptions } from './units';\n\nexport type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Throughput' | 'Currency' | 'Temperature' | 'Date';\n\n/**\n * Configuration for rendering units that are part of a group.\n */\nexport type UnitGroupConfig = {\n /**\n * The label that is shown in the UI.\n */\n label: string;\n /**\n * When true, the unit group supports setting decimal places.\n */\n decimalPlaces?: boolean;\n /**\n * When true, the unit group supports enabling shortValues.\n */\n shortValues?: boolean;\n};\n\n/**\n * Configuration for rendering a specific unit.\n */\nexport type UnitConfig = {\n /**\n * The group the unit is part of. This will inform common rendering behavior.\n */\n group?: UnitGroup;\n\n /**\n * When true, this unit will not be displayed in the unit selector. This is\n * useful for units that are shorthand variants of other units.\n */\n disableSelectorOption?: boolean;\n\n /**\n * The label that is shown in the UI.\n */\n label: string;\n};\n\n/**\n * Used in the tests for each type of unit.\n */\nexport interface UnitTestCase {\n value: number;\n format: FormatOptions;\n expected: string;\n}\n\nexport interface IntervalTestCase {\n timeRange: AbsoluteTimeRange;\n expected: Duration;\n}\n\nexport interface FormatTestCase {\n duration: Duration;\n expected: DurationString;\n}\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,6EAA6E;AAC7E,SAAS;AA6DT,WAGC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { BytesFormatOptions as BytesFormatOptions } from './bytes';
|
|
2
2
|
import { DecimalFormatOptions as DecimalFormatOptions } from './decimal';
|
|
3
3
|
import { PercentFormatOptions as PercentFormatOptions } from './percent';
|
|
4
|
+
import { TemperatureFormatOptions } from './temperature';
|
|
4
5
|
import { TimeFormatOptions as TimeFormatOptions } from './time';
|
|
5
6
|
import { UnitGroup, UnitGroupConfig, UnitConfig } from './types';
|
|
6
7
|
import { ThroughputFormatOptions } from './throughput';
|
|
7
8
|
import { CurrencyFormatOptions } from './currency';
|
|
9
|
+
import { DateFormatOptions } from './date';
|
|
8
10
|
/**
|
|
9
11
|
* Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.
|
|
10
12
|
* Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.
|
|
@@ -14,6 +16,20 @@ import { CurrencyFormatOptions } from './currency';
|
|
|
14
16
|
*/
|
|
15
17
|
export declare const UNIT_GROUP_CONFIG: Readonly<Record<UnitGroup, UnitGroupConfig>>;
|
|
16
18
|
export declare const UNIT_CONFIG: {
|
|
19
|
+
readonly "datetime-iso": UnitConfig;
|
|
20
|
+
readonly "datetime-us": UnitConfig;
|
|
21
|
+
readonly "datetime-local": UnitConfig;
|
|
22
|
+
readonly "date-iso": UnitConfig;
|
|
23
|
+
readonly "date-us": UnitConfig;
|
|
24
|
+
readonly "date-local": UnitConfig;
|
|
25
|
+
readonly "time-local": UnitConfig;
|
|
26
|
+
readonly "time-iso": UnitConfig;
|
|
27
|
+
readonly "time-us": UnitConfig;
|
|
28
|
+
readonly "relative-time": UnitConfig;
|
|
29
|
+
readonly "unix-timestamp": UnitConfig;
|
|
30
|
+
readonly "unix-timestamp-ms": UnitConfig;
|
|
31
|
+
readonly celsius: UnitConfig;
|
|
32
|
+
readonly fahrenheit: UnitConfig;
|
|
17
33
|
readonly aud: UnitConfig;
|
|
18
34
|
readonly cad: UnitConfig;
|
|
19
35
|
readonly chf: UnitConfig;
|
|
@@ -48,6 +64,8 @@ export declare const UNIT_CONFIG: {
|
|
|
48
64
|
readonly percent: UnitConfig;
|
|
49
65
|
readonly "percent-decimal": UnitConfig;
|
|
50
66
|
readonly "%": UnitConfig;
|
|
67
|
+
readonly nanoseconds: UnitConfig;
|
|
68
|
+
readonly microseconds: UnitConfig;
|
|
51
69
|
readonly milliseconds: UnitConfig;
|
|
52
70
|
readonly seconds: UnitConfig;
|
|
53
71
|
readonly minutes: UnitConfig;
|
|
@@ -57,7 +75,7 @@ export declare const UNIT_CONFIG: {
|
|
|
57
75
|
readonly months: UnitConfig;
|
|
58
76
|
readonly years: UnitConfig;
|
|
59
77
|
};
|
|
60
|
-
export type FormatOptions = TimeFormatOptions | PercentFormatOptions | DecimalFormatOptions | BytesFormatOptions | ThroughputFormatOptions | CurrencyFormatOptions;
|
|
78
|
+
export type FormatOptions = TimeFormatOptions | PercentFormatOptions | DecimalFormatOptions | BytesFormatOptions | ThroughputFormatOptions | CurrencyFormatOptions | TemperatureFormatOptions | DateFormatOptions;
|
|
61
79
|
type HasDecimalPlaces<UnitOpt> = UnitOpt extends {
|
|
62
80
|
decimalPlaces?: number;
|
|
63
81
|
} ? UnitOpt : never;
|
|
@@ -76,5 +94,7 @@ export declare function isUnitWithDecimalPlaces(formatOptions: FormatOptions): f
|
|
|
76
94
|
export declare function isUnitWithShortValues(formatOptions: FormatOptions): formatOptions is HasShortValues<FormatOptions>;
|
|
77
95
|
export declare function isThroughputUnit(formatOptions: FormatOptions): formatOptions is ThroughputFormatOptions;
|
|
78
96
|
export declare function isCurrencyUnit(formatOptions: FormatOptions): formatOptions is CurrencyFormatOptions;
|
|
97
|
+
export declare function isDateUnit(formatOptions: FormatOptions): formatOptions is DateFormatOptions;
|
|
98
|
+
export declare function isTemperatureUnit(formatOptions: FormatOptions): formatOptions is TemperatureFormatOptions;
|
|
79
99
|
export {};
|
|
80
100
|
//# sourceMappingURL=units.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../../src/model/units/units.ts"],"names":[],"mappings":"AAaA,OAAO,EAAe,kBAAkB,IAAI,kBAAkB,EAAyC,MAAM,SAAS,CAAC;AACvH,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAAc,iBAAiB,IAAI,iBAAiB,EAAuC,MAAM,QAAQ,CAAC;AACjH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAIL,uBAAuB,EACxB,MAAM,cAAc,CAAC;AACtB,OAAO,EAA+D,qBAAqB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../../src/model/units/units.ts"],"names":[],"mappings":"AAaA,OAAO,EAAe,kBAAkB,IAAI,kBAAkB,EAAyC,MAAM,SAAS,CAAC;AACvH,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAIL,wBAAwB,EACzB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAc,iBAAiB,IAAI,iBAAiB,EAAuC,MAAM,QAAQ,CAAC;AACjH,OA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a,GAAG,aAAa,IAAI,oBAAoB,CAEjG;AAED,wBAAgB,aAAa,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,oBAAoB,CAEjG;AAED,wBAAgB,WAAW,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,kBAAkB,CAE7F;AAED,wBAAgB,uBAAuB,CACrC,aAAa,EAAE,aAAa,GAC3B,aAAa,IAAI,gBAAgB,CAAC,aAAa,CAAC,CAIlD;AAED,wBAAgB,qBAAqB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,cAAc,CAAC,aAAa,CAAC,CAIlH;AAED,wBAAgB,gBAAgB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,uBAAuB,CAEvG;AAED,wBAAgB,cAAc,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,qBAAqB,CAEnG;AAED,wBAAgB,UAAU,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,iBAAiB,CAE3F;AAED,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,wBAAwB,CAEzG"}
|
|
@@ -13,9 +13,11 @@
|
|
|
13
13
|
import { formatBytes, BYTES_GROUP_CONFIG, BYTES_UNIT_CONFIG } from './bytes';
|
|
14
14
|
import { formatDecimal, DECIMAL_GROUP_CONFIG, DECIMAL_UNIT_CONFIG } from './decimal';
|
|
15
15
|
import { formatPercent, PERCENT_GROUP_CONFIG, PERCENT_UNIT_CONFIG } from './percent';
|
|
16
|
+
import { TEMPERATURE_GROUP_CONFIG, formatTemperature, TEMPERATURE_UNIT_CONFIG } from './temperature';
|
|
16
17
|
import { formatTime, TIME_GROUP_CONFIG, TIME_UNIT_CONFIG } from './time';
|
|
17
18
|
import { formatThroughput, THROUGHPUT_GROUP_CONFIG, THROUGHPUT_UNIT_CONFIG } from './throughput';
|
|
18
19
|
import { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG } from './currency';
|
|
20
|
+
import { formatDate, DATE_GROUP_CONFIG, DATE_UNIT_CONFIG } from './date';
|
|
19
21
|
/**
|
|
20
22
|
* Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.
|
|
21
23
|
* Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.
|
|
@@ -28,7 +30,9 @@ import { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG } from './c
|
|
|
28
30
|
Decimal: DECIMAL_GROUP_CONFIG,
|
|
29
31
|
Bytes: BYTES_GROUP_CONFIG,
|
|
30
32
|
Throughput: THROUGHPUT_GROUP_CONFIG,
|
|
31
|
-
Currency: CURRENCY_GROUP_CONFIG
|
|
33
|
+
Currency: CURRENCY_GROUP_CONFIG,
|
|
34
|
+
Temperature: TEMPERATURE_GROUP_CONFIG,
|
|
35
|
+
Date: DATE_GROUP_CONFIG
|
|
32
36
|
};
|
|
33
37
|
export const UNIT_CONFIG = {
|
|
34
38
|
...TIME_UNIT_CONFIG,
|
|
@@ -36,10 +40,12 @@ export const UNIT_CONFIG = {
|
|
|
36
40
|
...DECIMAL_UNIT_CONFIG,
|
|
37
41
|
...BYTES_UNIT_CONFIG,
|
|
38
42
|
...THROUGHPUT_UNIT_CONFIG,
|
|
39
|
-
...CURRENCY_UNIT_CONFIG
|
|
43
|
+
...CURRENCY_UNIT_CONFIG,
|
|
44
|
+
...TEMPERATURE_UNIT_CONFIG,
|
|
45
|
+
...DATE_UNIT_CONFIG
|
|
40
46
|
};
|
|
41
47
|
export function formatValue(value, formatOptions) {
|
|
42
|
-
if (formatOptions
|
|
48
|
+
if (!formatOptions) {
|
|
43
49
|
return value.toString();
|
|
44
50
|
}
|
|
45
51
|
if (isBytesUnit(formatOptions)) {
|
|
@@ -60,6 +66,12 @@ export function formatValue(value, formatOptions) {
|
|
|
60
66
|
if (isCurrencyUnit(formatOptions)) {
|
|
61
67
|
return formatCurrency(value, formatOptions);
|
|
62
68
|
}
|
|
69
|
+
if (isDateUnit(formatOptions)) {
|
|
70
|
+
return formatDate(value, formatOptions);
|
|
71
|
+
}
|
|
72
|
+
if (isTemperatureUnit(formatOptions)) {
|
|
73
|
+
return formatTemperature(value, formatOptions);
|
|
74
|
+
}
|
|
63
75
|
const exhaustive = formatOptions;
|
|
64
76
|
throw new Error(`Unknown unit options ${exhaustive}`);
|
|
65
77
|
}
|
|
@@ -101,5 +113,11 @@ export function isThroughputUnit(formatOptions) {
|
|
|
101
113
|
export function isCurrencyUnit(formatOptions) {
|
|
102
114
|
return getUnitGroup(formatOptions) === 'Currency';
|
|
103
115
|
}
|
|
116
|
+
export function isDateUnit(formatOptions) {
|
|
117
|
+
return getUnitGroup(formatOptions) === 'Date';
|
|
118
|
+
}
|
|
119
|
+
export function isTemperatureUnit(formatOptions) {
|
|
120
|
+
return getUnitGroup(formatOptions) === 'Temperature';
|
|
121
|
+
}
|
|
104
122
|
|
|
105
123
|
//# sourceMappingURL=units.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/model/units/units.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { formatBytes, BytesFormatOptions as BytesFormatOptions, BYTES_GROUP_CONFIG, BYTES_UNIT_CONFIG } from './bytes';\nimport {\n formatDecimal,\n DecimalFormatOptions as DecimalFormatOptions,\n DECIMAL_GROUP_CONFIG,\n DECIMAL_UNIT_CONFIG,\n} from './decimal';\nimport {\n formatPercent,\n PercentFormatOptions as PercentFormatOptions,\n PERCENT_GROUP_CONFIG,\n PERCENT_UNIT_CONFIG,\n} from './percent';\nimport { formatTime, TimeFormatOptions as TimeFormatOptions, TIME_GROUP_CONFIG, TIME_UNIT_CONFIG } from './time';\nimport { UnitGroup, UnitGroupConfig, UnitConfig } from './types';\nimport {\n formatThroughput,\n THROUGHPUT_GROUP_CONFIG,\n THROUGHPUT_UNIT_CONFIG,\n ThroughputFormatOptions,\n} from './throughput';\nimport { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG, CurrencyFormatOptions } from './currency';\n\n/**\n * Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.\n * Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.\n *\n * To format bytes, we also make use of the `numbro` package,\n * because it can handle adding units like KB, MB, GB, etc. correctly.\n */\n\nexport const UNIT_GROUP_CONFIG: Readonly<Record<UnitGroup, UnitGroupConfig>> = {\n Time: TIME_GROUP_CONFIG,\n Percent: PERCENT_GROUP_CONFIG,\n Decimal: DECIMAL_GROUP_CONFIG,\n Bytes: BYTES_GROUP_CONFIG,\n Throughput: THROUGHPUT_GROUP_CONFIG,\n Currency: CURRENCY_GROUP_CONFIG,\n};\nexport const UNIT_CONFIG = {\n ...TIME_UNIT_CONFIG,\n ...PERCENT_UNIT_CONFIG,\n ...DECIMAL_UNIT_CONFIG,\n ...BYTES_UNIT_CONFIG,\n ...THROUGHPUT_UNIT_CONFIG,\n ...CURRENCY_UNIT_CONFIG,\n} as const;\n\nexport type FormatOptions =\n | TimeFormatOptions\n | PercentFormatOptions\n | DecimalFormatOptions\n | BytesFormatOptions\n | ThroughputFormatOptions\n | CurrencyFormatOptions;\n\ntype HasDecimalPlaces<UnitOpt> = UnitOpt extends { decimalPlaces?: number } ? UnitOpt : never;\ntype HasShortValues<UnitOpt> = UnitOpt extends { shortValues?: boolean } ? UnitOpt : never;\n\nexport function formatValue(value: number, formatOptions?: FormatOptions): string {\n if (formatOptions
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/units.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { formatBytes, BytesFormatOptions as BytesFormatOptions, BYTES_GROUP_CONFIG, BYTES_UNIT_CONFIG } from './bytes';\nimport {\n formatDecimal,\n DecimalFormatOptions as DecimalFormatOptions,\n DECIMAL_GROUP_CONFIG,\n DECIMAL_UNIT_CONFIG,\n} from './decimal';\nimport {\n formatPercent,\n PercentFormatOptions as PercentFormatOptions,\n PERCENT_GROUP_CONFIG,\n PERCENT_UNIT_CONFIG,\n} from './percent';\nimport {\n TEMPERATURE_GROUP_CONFIG,\n formatTemperature,\n TEMPERATURE_UNIT_CONFIG,\n TemperatureFormatOptions,\n} from './temperature';\nimport { formatTime, TimeFormatOptions as TimeFormatOptions, TIME_GROUP_CONFIG, TIME_UNIT_CONFIG } from './time';\nimport { UnitGroup, UnitGroupConfig, UnitConfig } from './types';\nimport {\n formatThroughput,\n THROUGHPUT_GROUP_CONFIG,\n THROUGHPUT_UNIT_CONFIG,\n ThroughputFormatOptions,\n} from './throughput';\nimport { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG, CurrencyFormatOptions } from './currency';\nimport { formatDate, DateFormatOptions, DATE_GROUP_CONFIG, DATE_UNIT_CONFIG } from './date';\n\n/**\n * Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.\n * Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.\n *\n * To format bytes, we also make use of the `numbro` package,\n * because it can handle adding units like KB, MB, GB, etc. correctly.\n */\n\nexport const UNIT_GROUP_CONFIG: Readonly<Record<UnitGroup, UnitGroupConfig>> = {\n Time: TIME_GROUP_CONFIG,\n Percent: PERCENT_GROUP_CONFIG,\n Decimal: DECIMAL_GROUP_CONFIG,\n Bytes: BYTES_GROUP_CONFIG,\n Throughput: THROUGHPUT_GROUP_CONFIG,\n Currency: CURRENCY_GROUP_CONFIG,\n Temperature: TEMPERATURE_GROUP_CONFIG,\n Date: DATE_GROUP_CONFIG,\n};\nexport const UNIT_CONFIG = {\n ...TIME_UNIT_CONFIG,\n ...PERCENT_UNIT_CONFIG,\n ...DECIMAL_UNIT_CONFIG,\n ...BYTES_UNIT_CONFIG,\n ...THROUGHPUT_UNIT_CONFIG,\n ...CURRENCY_UNIT_CONFIG,\n ...TEMPERATURE_UNIT_CONFIG,\n ...DATE_UNIT_CONFIG,\n} as const;\n\nexport type FormatOptions =\n | TimeFormatOptions\n | PercentFormatOptions\n | DecimalFormatOptions\n | BytesFormatOptions\n | ThroughputFormatOptions\n | CurrencyFormatOptions\n | TemperatureFormatOptions\n | DateFormatOptions;\n\ntype HasDecimalPlaces<UnitOpt> = UnitOpt extends { decimalPlaces?: number } ? UnitOpt : never;\ntype HasShortValues<UnitOpt> = UnitOpt extends { shortValues?: boolean } ? UnitOpt : never;\n\nexport function formatValue(value: number, formatOptions?: FormatOptions): string {\n if (!formatOptions) {\n return value.toString();\n }\n\n if (isBytesUnit(formatOptions)) {\n return formatBytes(value, formatOptions);\n }\n\n if (isDecimalUnit(formatOptions)) {\n return formatDecimal(value, formatOptions);\n }\n\n if (isPercentUnit(formatOptions)) {\n return formatPercent(value, formatOptions);\n }\n\n if (isTimeUnit(formatOptions)) {\n return formatTime(value, formatOptions);\n }\n\n if (isThroughputUnit(formatOptions)) {\n return formatThroughput(value, formatOptions);\n }\n\n if (isCurrencyUnit(formatOptions)) {\n return formatCurrency(value, formatOptions);\n }\n\n if (isDateUnit(formatOptions)) {\n return formatDate(value, formatOptions);\n }\n\n if (isTemperatureUnit(formatOptions)) {\n return formatTemperature(value, formatOptions);\n }\n\n const exhaustive: never = formatOptions;\n throw new Error(`Unknown unit options ${exhaustive}`);\n}\n\nexport function getUnitConfig(formatOptions: FormatOptions): UnitConfig {\n const unit = formatOptions.unit ?? 'decimal';\n return UNIT_CONFIG[unit];\n}\n\nexport function getUnitGroup(formatOptions: FormatOptions): UnitGroup {\n return getUnitConfig(formatOptions).group ?? 'Decimal';\n}\n\nexport function getUnitGroupConfig(formatOptions: FormatOptions): UnitGroupConfig {\n const unitConfig = getUnitConfig(formatOptions);\n return UNIT_GROUP_CONFIG[unitConfig.group ?? 'Decimal'];\n}\n\n// Type guards\nexport function isTimeUnit(formatOptions: FormatOptions): formatOptions is TimeFormatOptions {\n return getUnitGroup(formatOptions) === 'Time';\n}\n\nexport function isPercentUnit(formatOptions: FormatOptions): formatOptions is PercentFormatOptions {\n return getUnitGroup(formatOptions) === 'Percent';\n}\n\nexport function isDecimalUnit(formatOptions: FormatOptions): formatOptions is DecimalFormatOptions {\n return getUnitGroup(formatOptions) === 'Decimal';\n}\n\nexport function isBytesUnit(formatOptions: FormatOptions): formatOptions is BytesFormatOptions {\n return getUnitGroup(formatOptions) === 'Bytes';\n}\n\nexport function isUnitWithDecimalPlaces(\n formatOptions: FormatOptions\n): formatOptions is HasDecimalPlaces<FormatOptions> {\n const groupConfig = getUnitGroupConfig(formatOptions);\n\n return !!groupConfig.decimalPlaces;\n}\n\nexport function isUnitWithShortValues(formatOptions: FormatOptions): formatOptions is HasShortValues<FormatOptions> {\n const groupConfig = getUnitGroupConfig(formatOptions);\n\n return !!groupConfig.shortValues;\n}\n\nexport function isThroughputUnit(formatOptions: FormatOptions): formatOptions is ThroughputFormatOptions {\n return getUnitGroup(formatOptions) === 'Throughput';\n}\n\nexport function isCurrencyUnit(formatOptions: FormatOptions): formatOptions is CurrencyFormatOptions {\n return getUnitGroup(formatOptions) === 'Currency';\n}\n\nexport function isDateUnit(formatOptions: FormatOptions): formatOptions is DateFormatOptions {\n return getUnitGroup(formatOptions) === 'Date';\n}\n\nexport function isTemperatureUnit(formatOptions: FormatOptions): formatOptions is TemperatureFormatOptions {\n return getUnitGroup(formatOptions) === 'Temperature';\n}\n"],"names":["formatBytes","BYTES_GROUP_CONFIG","BYTES_UNIT_CONFIG","formatDecimal","DECIMAL_GROUP_CONFIG","DECIMAL_UNIT_CONFIG","formatPercent","PERCENT_GROUP_CONFIG","PERCENT_UNIT_CONFIG","TEMPERATURE_GROUP_CONFIG","formatTemperature","TEMPERATURE_UNIT_CONFIG","formatTime","TIME_GROUP_CONFIG","TIME_UNIT_CONFIG","formatThroughput","THROUGHPUT_GROUP_CONFIG","THROUGHPUT_UNIT_CONFIG","formatCurrency","CURRENCY_GROUP_CONFIG","CURRENCY_UNIT_CONFIG","formatDate","DATE_GROUP_CONFIG","DATE_UNIT_CONFIG","UNIT_GROUP_CONFIG","Time","Percent","Decimal","Bytes","Throughput","Currency","Temperature","Date","UNIT_CONFIG","formatValue","value","formatOptions","toString","isBytesUnit","isDecimalUnit","isPercentUnit","isTimeUnit","isThroughputUnit","isCurrencyUnit","isDateUnit","isTemperatureUnit","exhaustive","Error","getUnitConfig","unit","getUnitGroup","group","getUnitGroupConfig","unitConfig","isUnitWithDecimalPlaces","groupConfig","decimalPlaces","isUnitWithShortValues","shortValues"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,WAAW,EAA4CC,kBAAkB,EAAEC,iBAAiB,QAAQ,UAAU;AACvH,SACEC,aAAa,EAEbC,oBAAoB,EACpBC,mBAAmB,QACd,YAAY;AACnB,SACEC,aAAa,EAEbC,oBAAoB,EACpBC,mBAAmB,QACd,YAAY;AACnB,SACEC,wBAAwB,EACxBC,iBAAiB,EACjBC,uBAAuB,QAElB,gBAAgB;AACvB,SAASC,UAAU,EAA0CC,iBAAiB,EAAEC,gBAAgB,QAAQ,SAAS;AAE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name": "@perses-dev/core",
|
|
3
|
-
"version": "0.53.0-beta.
|
|
3
|
+
"version": "0.53.0-beta.2",
|
|
4
4
|
"description": "Core functionality consumed by both the Perses UI and plugins",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/perses/perses/blob/main/README.md",
|