bare-script 3.0.2 → 3.0.4
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/lib/data.js +1 -1
- package/lib/library.js +8 -7
- package/lib/value.js +10 -16
- package/package.json +1 -1
package/lib/data.js
CHANGED
|
@@ -74,7 +74,7 @@ const rCSVQuoteEscape = /""/g;
|
|
|
74
74
|
* Determine data field types and parse/validate field values
|
|
75
75
|
*
|
|
76
76
|
* @param {Object[]} data - The data array. Row objects are updated with parsed/validated values.
|
|
77
|
-
* @param {boolean} [csv=false] - If true, parse
|
|
77
|
+
* @param {boolean} [csv=false] - If true, parse value strings
|
|
78
78
|
* @returns {Object} The map of field name to field type ("datetime", "number", "string")
|
|
79
79
|
* @throws Throws an error if data is invalid
|
|
80
80
|
*/
|
package/lib/library.js
CHANGED
|
@@ -415,13 +415,14 @@ function dataTop([data = null, count = null, categoryFields = null]) {
|
|
|
415
415
|
// $group: Data
|
|
416
416
|
// $doc: Validate a data array
|
|
417
417
|
// $arg data: The data array
|
|
418
|
+
// $arg csv: Optional (default is false). If true, parse value strings.
|
|
418
419
|
// $return: The validated data array
|
|
419
|
-
function dataValidate([data = null]) {
|
|
420
|
+
function dataValidate([data = null, csv = false]) {
|
|
420
421
|
if (valueType(data) !== 'array') {
|
|
421
422
|
return null;
|
|
422
423
|
}
|
|
423
424
|
|
|
424
|
-
validateData(data);
|
|
425
|
+
validateData(data, valueBoolean(csv));
|
|
425
426
|
return data;
|
|
426
427
|
}
|
|
427
428
|
|
|
@@ -471,10 +472,10 @@ function datetimeISOFormat([datetime = null, isDate = false]) {
|
|
|
471
472
|
}
|
|
472
473
|
|
|
473
474
|
if (valueBoolean(isDate)) {
|
|
474
|
-
const year = datetime.getFullYear();
|
|
475
|
-
const month = datetime.getMonth() + 1;
|
|
476
|
-
const day = datetime.getDate();
|
|
477
|
-
return `${year}-${month
|
|
475
|
+
const year = String(datetime.getFullYear()).padStart(4, '0');
|
|
476
|
+
const month = String(datetime.getMonth() + 1).padStart(2, '0');
|
|
477
|
+
const day = String(datetime.getDate()).padStart(2, '0');
|
|
478
|
+
return `${year}-${month}-${day}`;
|
|
478
479
|
}
|
|
479
480
|
|
|
480
481
|
return valueString(datetime);
|
|
@@ -549,7 +550,7 @@ function datetimeMonth([datetime = null]) {
|
|
|
549
550
|
// $arg millisecond: Optional (default is 0). The millisecond.
|
|
550
551
|
// $return: The new datetime
|
|
551
552
|
function datetimeNew([year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0]) {
|
|
552
|
-
if (valueType(year) !== 'number' || Math.floor(year) !== year ||
|
|
553
|
+
if (valueType(year) !== 'number' || Math.floor(year) !== year || year < 100 ||
|
|
553
554
|
valueType(month) !== 'number' || Math.floor(month) !== month ||
|
|
554
555
|
valueType(day) !== 'number' || Math.floor(day) !== day || day < -10000 || day > 10000 ||
|
|
555
556
|
valueType(hour) !== 'number' || Math.floor(hour) !== hour ||
|
package/lib/value.js
CHANGED
|
@@ -54,29 +54,23 @@ export function valueString(value) {
|
|
|
54
54
|
} else if (type === 'number') {
|
|
55
55
|
return `${value}`;
|
|
56
56
|
} else if (value instanceof Date) {
|
|
57
|
-
const year = value.getFullYear();
|
|
58
|
-
const month = value.getMonth() + 1;
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const minute = value.getMinutes();
|
|
65
|
-
const minuteStr = `${minute < 10 ? '0' : ''}${minute}`;
|
|
66
|
-
const second = value.getSeconds();
|
|
67
|
-
const secondStr = `${second < 10 ? '0' : ''}${second}`;
|
|
68
|
-
const millisecond = value.getMilliseconds();
|
|
69
|
-
const millisecondStr = millisecond === 0 ? '' : `.${millisecond < 100 ? '0' : ''}${millisecond < 10 ? '0' : ''}${millisecond}`;
|
|
57
|
+
const year = String(value.getFullYear()).padStart(4, '0');
|
|
58
|
+
const month = String(value.getMonth() + 1).padStart(2, '0');
|
|
59
|
+
const day = String(value.getDate()).padStart(2, '0');
|
|
60
|
+
const hour = String(value.getHours()).padStart(2, '0');
|
|
61
|
+
const minute = String(value.getMinutes()).padStart(2, '0');
|
|
62
|
+
const second = String(value.getSeconds()).padStart(2, '0');
|
|
63
|
+
const millisecond = value.getMilliseconds() === 0 ? '' : `.${String(value.getMilliseconds()).padStart(3, '0')}`;
|
|
70
64
|
const tzOffset = value.getTimezoneOffset();
|
|
71
65
|
/* c8 ignore next */
|
|
72
66
|
const tzSign = tzOffset < 0 ? '+' : '-';
|
|
73
67
|
const tzHour = Math.floor(Math.abs(tzOffset) / 60);
|
|
74
68
|
/* c8 ignore next */
|
|
75
|
-
const tzHourStr =
|
|
69
|
+
const tzHourStr = String(tzHour).padStart(2, '0');
|
|
76
70
|
const tzMinute = Math.abs(tzOffset) - tzHour * 60;
|
|
77
71
|
/* c8 ignore next */
|
|
78
|
-
const tzMinuteStr =
|
|
79
|
-
return `${year}-${
|
|
72
|
+
const tzMinuteStr = String(tzMinute).padStart(2, '0');
|
|
73
|
+
return `${year}-${month}-${day}T${hour}:${minute}:${second}${millisecond}${tzSign}${tzHourStr}:${tzMinuteStr}`;
|
|
80
74
|
} else if (Array.isArray(value)) {
|
|
81
75
|
return valueJSON(value);
|
|
82
76
|
} else if (value instanceof RegExp) {
|