-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathformat.js
More file actions
168 lines (151 loc) 路 6.42 KB
/
Copy pathformat.js
File metadata and controls
168 lines (151 loc) 路 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* DataFormsJS [app.format] extension for formatting values
* (numbers, dates, etc) in the user's local language.
*
* Functions added:
* app.format.number(value)
* app.format.round(value, decimalPlaces)
* app.format.currency(value, currencyCode)
* app.format.percent(value, decimalPlaces)
* app.format.date(value)
* app.format.dateTime(value)
* app.format.time(value)
* app.format.isNumber(value)
*
* Advanced Functions - Used internally but they
* can also be used by an App:
* app.format.formatDateTime(dateTime, options)
* app.format.formatNumber(value, options)
*
* If using DataFormsJS with Vue or Handlebars then this file would not be
* used by most apps and instead one of the following scripts can be used:
* js/extensions/handlebars-helpers.js
* js/extensions/vue-directives.js
*/
/* Validates with both [jshint] and [eslint] */
/* global app */
/* jshint strict: true */
/* eslint-env browser */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint strict: ["error", "function"] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
(function () {
'use strict';
var format = {
number: function(value) {
return this.formatNumber(value, {});
},
round: function(value, decimalPlaces) {
if (decimalPlaces === undefined) {
decimalPlaces = 0;
}
var intlOptions = {
style: 'decimal',
maximumFractionDigits: decimalPlaces,
minimumFractionDigits: decimalPlaces,
};
return this.formatNumber(value, intlOptions);
},
currency: function(value, currencyCode) {
var intlOptions = { style: 'currency', currency: currencyCode, maximumFractionDigits: 2 };
return this.formatNumber(value, intlOptions);
},
percent: function(value, decimalPlaces) {
if (decimalPlaces === undefined) {
decimalPlaces = 0;
}
var intlOptions = {
style: 'percent',
maximumFractionDigits: decimalPlaces,
minimumFractionDigits: decimalPlaces,
};
return this.formatNumber(value, intlOptions);
},
date: function(value) {
return this.formatDateTime(value, {});
},
dateTime: function(value) {
var intlOptions = { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
return this.formatDateTime(value, intlOptions);
},
time: function(value) {
var intlOptions = { hour: 'numeric', minute: 'numeric', second: 'numeric' };
return this.formatDateTime(value, intlOptions);
},
// Return true if a valid number, this excludes Infinity and NaN
isNumber: function(value) {
return (!isNaN(parseFloat(value)) && isFinite(value));
},
// Format a date, date/time or time value with Intl.DateTimeFormat()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
formatDateTime: function(dateTime, options) {
// Fallback to data in original format.
// As of 2020 this would most likely happen on older iOS.
if (window.Intl === undefined) {
return dateTime;
}
// Make sure a value exists
if (dateTime === null || dateTime === '') {
return null;
}
// Return formatted date/time in the user's local language
try {
if (dateTime instanceof Date) {
return new Intl.DateTimeFormat(navigator.language, options).format(dateTime);
} else if (/^\d{4}-\d{2}-\d{2}$/.test(dateTime)) {
// Basic date without timezone (YYYY-MM-DD)
var nums = dateTime.split('-').map(function(n) { return parseInt(n, 10); });
var date = new Date(nums[0], nums[1] - 1, nums[2]);
return new Intl.DateTimeFormat(navigator.language, options).format(date);
} else {
// Assume JavaScript `Date` object can parse the date.
// In the future a new Temporal may be used instead:
// https://tc39.es/proposal-temporal/docs/
var localDate = new Date(dateTime);
return new Intl.DateTimeFormat(navigator.language, options).format(localDate);
}
} catch (e) {
// If Error log to console and return 'Error' text
console.warn('Error formatting Date/Time Value:');
console.log(navigator.language);
console.log(options);
console.log(dateTime);
console.log(e);
return 'Error';
}
},
// Format a numeric value with Intl.NumberFormat()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
formatNumber: function(value, options) {
// Check for a valid number
if (value === null || value === '') {
return null;
}
if (!this.isNumber(value)) {
console.warn('Warning value specified in DateFormsJS function formatNumber() is not a number:');
console.log(value);
return value;
}
// Fallback to data in original format.
// As of 2020 this would most likely happen on older iOS.
if (window.Intl === undefined) {
return value;
}
// Return formatted number/currency/percent/etc in the user's local language
try {
return new Intl.NumberFormat(navigator.language, options).format(value);
} catch (e) {
// If Error log to console and return 'Error' text
console.warn('Error formatting Numeric Value:');
console.log(navigator.language);
console.log(options);
console.log(value);
console.log(e);
return 'Error';
}
},
};
// Assign format Object to the app Object
app.format = format;
})();