@qrvey/utils 1.3.1 → 1.3.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/README.md +1 -1
- package/dist/cjs/dates/helpers/isValidDateObject.d.ts +6 -0
- package/dist/cjs/dates/helpers/isValidDateObject.js +12 -0
- package/dist/cjs/dates/range/getDateRange.js +10 -3
- package/dist/cjs/filters/helpers/common/mergeFilters.js +2 -2
- package/dist/cjs/format/definition.d.ts +62 -0
- package/dist/cjs/format/definition.js +18 -1
- package/dist/cjs/format/duration/addDurationFormat.d.ts +13 -0
- package/dist/cjs/format/duration/addDurationFormat.js +20 -0
- package/dist/cjs/format/duration/durationFormatter.d.ts +79 -0
- package/dist/cjs/format/duration/durationFormatter.js +156 -0
- package/dist/cjs/format/duration/index.d.ts +2 -0
- package/dist/cjs/format/duration/index.js +18 -0
- package/dist/cjs/format/index.d.ts +1 -0
- package/dist/cjs/format/index.js +1 -0
- package/dist/dates/helpers/isValidDateObject.d.ts +6 -0
- package/dist/dates/helpers/isValidDateObject.js +8 -0
- package/dist/dates/range/getDateRange.js +10 -3
- package/dist/filters/helpers/common/mergeFilters.js +2 -2
- package/dist/format/definition.d.ts +62 -0
- package/dist/format/definition.js +17 -0
- package/dist/format/duration/addDurationFormat.d.ts +13 -0
- package/dist/format/duration/addDurationFormat.js +16 -0
- package/dist/format/duration/durationFormatter.d.ts +79 -0
- package/dist/format/duration/durationFormatter.js +152 -0
- package/dist/format/duration/index.d.ts +2 -0
- package/dist/format/duration/index.js +2 -0
- package/dist/format/index.d.ts +1 -0
- package/dist/format/index.js +1 -0
- package/package.json +1 -1
- package/src/dates/helpers/isValidDateObject.ts +9 -0
- package/src/dates/range/getDateRange.ts +13 -3
- package/src/filters/helpers/common/mergeFilters.ts +2 -2
- package/src/format/definition.ts +19 -0
- package/src/format/duration/addDurationFormat.ts +17 -0
- package/src/format/duration/durationFormatter.ts +169 -0
- package/src/format/duration/index.ts +2 -0
- package/src/format/index.ts +1 -0
- package/test/format/addDurationFormat.test.js +39 -0
- package/test/format/addDurationFormatWithLocale.test.js +11 -0
- package/test/format/durationFormatterClass.test.js +45 -0
package/README.md
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidDateObject = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* If the date is a valid Date object, return true, otherwise return false.
|
|
6
|
+
* @param {Date} date - The date object to check.
|
|
7
|
+
* @returns A boolean value.
|
|
8
|
+
*/
|
|
9
|
+
function isValidDateObject(date) {
|
|
10
|
+
return date instanceof Date && !isNaN(date.getTime());
|
|
11
|
+
}
|
|
12
|
+
exports.isValidDateObject = isValidDateObject;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getDateRange = void 0;
|
|
4
4
|
const capitalize_1 = require("../../general/string/capitalize");
|
|
5
5
|
const isTokenLabel_1 = require("../../tokens/isTokenLabel");
|
|
6
|
+
const isValidDateObject_1 = require("../helpers/isValidDateObject");
|
|
6
7
|
/**
|
|
7
8
|
* Get date range object from a string date value
|
|
8
9
|
*
|
|
@@ -105,10 +106,16 @@ function getStringTimeRange(value, dateGroupLabel) {
|
|
|
105
106
|
return [' 00:00:00', ' 23:59:59'];
|
|
106
107
|
}
|
|
107
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* It takes a Date object as an argument and returns a string in the format of MM/DD/YYYY
|
|
111
|
+
* @param {Date} dt - Date - The date object to convert to a string
|
|
112
|
+
* @returns A string in the format of MM/DD/YYYY
|
|
113
|
+
*/
|
|
108
114
|
function getStringDate(dt) {
|
|
109
|
-
if (
|
|
110
|
-
return '
|
|
111
|
-
|
|
115
|
+
if ((0, isValidDateObject_1.isValidDateObject)(dt)) {
|
|
116
|
+
return `${('0' + (dt.getMonth() + 1)).slice(-2)}/${('0' + dt.getDate()).slice(-2)}/${dt.getFullYear()}`;
|
|
117
|
+
}
|
|
118
|
+
return '01/01/0000';
|
|
112
119
|
}
|
|
113
120
|
function getYearRange(value) {
|
|
114
121
|
const dateFrom = new Date(Number(value), 0, 1);
|
|
@@ -47,7 +47,7 @@ function mergeScopes(scopes1 = [], scopes2 = [], settings) {
|
|
|
47
47
|
let newScopes = scopes1.reduce((scopes, scope1) => {
|
|
48
48
|
const scope2Index = scopes2.findIndex(scope2 => (0, resolveScopeConditions_1.resolveScopeConditions)(scope2, { scope: scope1.scope, scopeid: scope1.scopeid }));
|
|
49
49
|
if (scope2Index > -1) {
|
|
50
|
-
scope1 = Object.assign(Object.assign({},
|
|
50
|
+
scope1 = Object.assign(Object.assign({}, scopes2[scope2Index]), { datasets: mergeDatasets(scope1.datasets, scopes2[scope2Index].datasets, settings) });
|
|
51
51
|
scopes2.splice(scope2Index, 1);
|
|
52
52
|
}
|
|
53
53
|
scopes.push(scope1);
|
|
@@ -68,7 +68,7 @@ function mergeDatasets(datasets1 = [], datasets2 = [], settings) {
|
|
|
68
68
|
let newDatasets = datasets1.reduce((datasets, dataset1) => {
|
|
69
69
|
const dataset2Index = datasets2.findIndex(dataset2 => (0, resolveDatasetConditions_1.resolveDatasetConditions)(dataset2, { qrveyid: dataset1.qrveyid, linkid: dataset1.linkid }));
|
|
70
70
|
if (dataset2Index > -1) {
|
|
71
|
-
dataset1 = Object.assign(Object.assign({},
|
|
71
|
+
dataset1 = Object.assign(Object.assign({}, datasets2[dataset2Index]), { filters: mergeFilterss(dataset1.filters, datasets2[dataset2Index].filters, settings) });
|
|
72
72
|
datasets2.splice(dataset2Index, 1);
|
|
73
73
|
}
|
|
74
74
|
datasets.push(dataset1);
|
|
@@ -42,3 +42,65 @@ export declare const DATETIME_OPTIONS: {
|
|
|
42
42
|
minute: string;
|
|
43
43
|
second: string;
|
|
44
44
|
};
|
|
45
|
+
export declare const PARTS_REGEX: RegExp;
|
|
46
|
+
export declare const DURATION_PARTS: {
|
|
47
|
+
S: {
|
|
48
|
+
order: number;
|
|
49
|
+
symbol: string;
|
|
50
|
+
name: string;
|
|
51
|
+
ms: number;
|
|
52
|
+
};
|
|
53
|
+
s: {
|
|
54
|
+
order: number;
|
|
55
|
+
symbol: string;
|
|
56
|
+
name: string;
|
|
57
|
+
ms: number;
|
|
58
|
+
};
|
|
59
|
+
m: {
|
|
60
|
+
order: number;
|
|
61
|
+
symbol: string;
|
|
62
|
+
name: string;
|
|
63
|
+
ms: number;
|
|
64
|
+
};
|
|
65
|
+
h: {
|
|
66
|
+
order: number;
|
|
67
|
+
symbol: string;
|
|
68
|
+
name: string;
|
|
69
|
+
ms: number;
|
|
70
|
+
};
|
|
71
|
+
H: {
|
|
72
|
+
order: number;
|
|
73
|
+
symbol: string;
|
|
74
|
+
name: string;
|
|
75
|
+
ms: number;
|
|
76
|
+
};
|
|
77
|
+
D: {
|
|
78
|
+
order: number;
|
|
79
|
+
symbol: string;
|
|
80
|
+
name: string;
|
|
81
|
+
ms: number;
|
|
82
|
+
};
|
|
83
|
+
W: {
|
|
84
|
+
order: number;
|
|
85
|
+
symbol: string;
|
|
86
|
+
name: string;
|
|
87
|
+
ms: number;
|
|
88
|
+
};
|
|
89
|
+
M: {
|
|
90
|
+
order: number;
|
|
91
|
+
symbol: string;
|
|
92
|
+
name: string;
|
|
93
|
+
ms: number;
|
|
94
|
+
};
|
|
95
|
+
Y: {
|
|
96
|
+
order: number;
|
|
97
|
+
symbol: string;
|
|
98
|
+
name: string;
|
|
99
|
+
ms: number;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export declare const DEFAULT_OPTIONS: {
|
|
103
|
+
unit: string;
|
|
104
|
+
locale: string;
|
|
105
|
+
fractionDigits: number;
|
|
106
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DATETIME_OPTIONS = exports.CURRENCY_DEFAULT = exports.LANG_DEFAULT = exports.currencyISO = exports.isTextColumn = exports.supportNumericFormatting = exports.isNumericalColumn = exports.isComplexColumn = exports.hasfileSizeProperty = exports.appliesFormatting = exports.DATEGROUP_FORMATTING_UNSUPPORT = exports.QUESTION_FORMAT_SUPPORT = exports.numericalFields = exports.complexFields = exports.textFields = void 0;
|
|
3
|
+
exports.DEFAULT_OPTIONS = exports.DURATION_PARTS = exports.PARTS_REGEX = exports.DATETIME_OPTIONS = exports.CURRENCY_DEFAULT = exports.LANG_DEFAULT = exports.currencyISO = exports.isTextColumn = exports.supportNumericFormatting = exports.isNumericalColumn = exports.isComplexColumn = exports.hasfileSizeProperty = exports.appliesFormatting = exports.DATEGROUP_FORMATTING_UNSUPPORT = exports.QUESTION_FORMAT_SUPPORT = exports.numericalFields = exports.complexFields = exports.textFields = void 0;
|
|
4
4
|
const COLUMN_1 = require("../columns/constants/COLUMN");
|
|
5
5
|
const Charts_Const_1 = require("../constants/Charts.Const");
|
|
6
6
|
exports.textFields = [COLUMN_1.COLUMN.TEXTFIELD, COLUMN_1.COLUMN.TEXT_CATEGORY, COLUMN_1.COLUMN.TEXT_LABEL, COLUMN_1.COLUMN.TEXT_CATEGORY];
|
|
@@ -84,3 +84,20 @@ exports.currencyISO = [
|
|
|
84
84
|
exports.LANG_DEFAULT = 'en-US';
|
|
85
85
|
exports.CURRENCY_DEFAULT = { text: '$ (USD)', label: 'USD' };
|
|
86
86
|
exports.DATETIME_OPTIONS = { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
|
|
87
|
+
exports.PARTS_REGEX = /\[([^\]]+)]|Y{1,2}|M{1,2}|W{1,2}|D{1,2}|h{1,2}|H{1,2}|m{1,2}|s{1,2}|S{1,3}/g;
|
|
88
|
+
exports.DURATION_PARTS = {
|
|
89
|
+
S: { order: 1, symbol: "S", name: "milliseconds", ms: 1 },
|
|
90
|
+
s: { order: 2, symbol: "s", name: "seconds", ms: 1000 },
|
|
91
|
+
m: { order: 3, symbol: "m", name: "minutes", ms: 1000 * 60 },
|
|
92
|
+
h: { order: 4, symbol: "h", name: "hours", ms: 1000 * 60 * 60 },
|
|
93
|
+
H: { order: 5, symbol: "H", name: "hours", ms: 1000 * 60 * 60 },
|
|
94
|
+
D: { order: 6, symbol: "D", name: "days", ms: 1000 * 60 * 60 * 24 },
|
|
95
|
+
W: { order: 7, symbol: "W", name: "weeks", ms: 1000 * 60 * 60 * 24 * 7 },
|
|
96
|
+
M: { order: 8, symbol: "M", name: "months", ms: 1000 * 60 * 60 * 24 * 30 },
|
|
97
|
+
Y: { order: 9, symbol: "Y", name: "years", ms: 1000 * 60 * 60 * 24 * 365 },
|
|
98
|
+
};
|
|
99
|
+
exports.DEFAULT_OPTIONS = {
|
|
100
|
+
unit: "seconds",
|
|
101
|
+
locale: "en-EN",
|
|
102
|
+
fractionDigits: 0
|
|
103
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**[TODO: The decimals dots are not working correctly, we need to reviewing (fractionDigits)]
|
|
2
|
+
* "Convert a number of seconds to a human readable string."
|
|
3
|
+
*
|
|
4
|
+
* `addDurationFormat` is a function that takes a number, a format, an optional locale, and an optional
|
|
5
|
+
* number of fraction digits, and returns a string
|
|
6
|
+
* @param {number} number - The number of milliseconds to format.
|
|
7
|
+
* @param {string} format - The format string.
|
|
8
|
+
* @param {string} [locale] - The locale to use for formatting. If not specified, the default locale is
|
|
9
|
+
* used.
|
|
10
|
+
* @param {number} [fractionDigits] - The number of digits to show after the decimal point.
|
|
11
|
+
* @returns A string
|
|
12
|
+
*/
|
|
13
|
+
export declare function addDurationFormat(number: number, format: string, locale?: string, fractionDigits?: number): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addDurationFormat = void 0;
|
|
4
|
+
const durationFormatter_1 = require("./durationFormatter");
|
|
5
|
+
/**[TODO: The decimals dots are not working correctly, we need to reviewing (fractionDigits)]
|
|
6
|
+
* "Convert a number of seconds to a human readable string."
|
|
7
|
+
*
|
|
8
|
+
* `addDurationFormat` is a function that takes a number, a format, an optional locale, and an optional
|
|
9
|
+
* number of fraction digits, and returns a string
|
|
10
|
+
* @param {number} number - The number of milliseconds to format.
|
|
11
|
+
* @param {string} format - The format string.
|
|
12
|
+
* @param {string} [locale] - The locale to use for formatting. If not specified, the default locale is
|
|
13
|
+
* used.
|
|
14
|
+
* @param {number} [fractionDigits] - The number of digits to show after the decimal point.
|
|
15
|
+
* @returns A string
|
|
16
|
+
*/
|
|
17
|
+
function addDurationFormat(number, format, locale, fractionDigits) {
|
|
18
|
+
return new durationFormatter_1.DurationFormatter({ template: format, options: { locale: locale, fractionDigits: fractionDigits } }).format(number);
|
|
19
|
+
}
|
|
20
|
+
exports.addDurationFormat = addDurationFormat;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export declare class DurationFormatter {
|
|
2
|
+
private options;
|
|
3
|
+
private parts;
|
|
4
|
+
private valueFormatter;
|
|
5
|
+
private numberFactor;
|
|
6
|
+
private template;
|
|
7
|
+
private numberFormatOptions;
|
|
8
|
+
/**
|
|
9
|
+
* The constructor function takes in a template string and an options object. It then sets the
|
|
10
|
+
* options object to the default options object if no options object is passed in. It then parses
|
|
11
|
+
* the template string and sets the parts to the parsed template string. It then tries to set the
|
|
12
|
+
* valueFormatter to a new Intl.NumberFormat object with the locale and fractionDigits options. If
|
|
13
|
+
* it fails, it sets the valueFormatter to a new Intl.NumberFormat object with the default locale
|
|
14
|
+
* and fractionDigits options. It then sets the numberFactor to the duration parts object with the
|
|
15
|
+
* name of the unit option. If it can't find the unit option, it sets the numberFactor to 1
|
|
16
|
+
* @param [template] - The template string that will be used to format the duration.
|
|
17
|
+
* @param options - This is an object that contains the following properties: unit, locale and fractionDigits
|
|
18
|
+
*/
|
|
19
|
+
constructor(options: {
|
|
20
|
+
template: string;
|
|
21
|
+
options?: {
|
|
22
|
+
unit?: string;
|
|
23
|
+
locale?: string;
|
|
24
|
+
fractionDigits?: number;
|
|
25
|
+
};
|
|
26
|
+
intlNumberFormat?: Intl.NumberFormat;
|
|
27
|
+
});
|
|
28
|
+
private setOptions;
|
|
29
|
+
/**
|
|
30
|
+
* It sets the locale formatter for the number.
|
|
31
|
+
*/
|
|
32
|
+
private setLocaleFormatter;
|
|
33
|
+
/**
|
|
34
|
+
* It returns a new instance of the Intl.NumberFormat class, which is a built-in JavaScript class
|
|
35
|
+
* that formats numbers
|
|
36
|
+
* @param {string | string[]} locale - string | string[]
|
|
37
|
+
* @returns A new instance of the Intl.NumberFormat class.
|
|
38
|
+
*/
|
|
39
|
+
private getNewNumberFormat;
|
|
40
|
+
/**
|
|
41
|
+
* It takes a template string and sets the parts property to the result of calling the
|
|
42
|
+
* parseFormatTemplate function with the template string as an argument
|
|
43
|
+
* @param {string} template - The format template string.
|
|
44
|
+
*/
|
|
45
|
+
private setParts;
|
|
46
|
+
/**
|
|
47
|
+
* It replaces the H and D characters in the template with h and d respectively
|
|
48
|
+
* @param {string} template - The template string to be used for the date format.
|
|
49
|
+
* @returns The template string with all instances of H replaced with h and all instances of D
|
|
50
|
+
* replaced with d.
|
|
51
|
+
*/
|
|
52
|
+
private matchFormatsReplace;
|
|
53
|
+
/**
|
|
54
|
+
* It sets the numberFactor to the value of the duration part that matches the unit.
|
|
55
|
+
*/
|
|
56
|
+
private setNumberFactor;
|
|
57
|
+
/**
|
|
58
|
+
* It takes a string, splits it into an array of strings, then maps each string to a config object,
|
|
59
|
+
* then filters out any falsy values, then reverses the array
|
|
60
|
+
* @param {string} template - string - the template string that we want to parse
|
|
61
|
+
* @returns An array of objects that have the order and the name of the part of the duration.
|
|
62
|
+
*/
|
|
63
|
+
private parseFormatTemplate;
|
|
64
|
+
/**
|
|
65
|
+
* It takes a number, multiplies it by a factor, then uses the parts array to calculate the number
|
|
66
|
+
* of each part in the number, then uses the template to format the number
|
|
67
|
+
* @param {number} number - The number of milliseconds to format.
|
|
68
|
+
* @returns The template string with the values replaced.
|
|
69
|
+
*/
|
|
70
|
+
format(number: number): string;
|
|
71
|
+
/**
|
|
72
|
+
* It takes a dictionary of values and a list of keys, and returns a formatted string
|
|
73
|
+
* @param parts - { [x: string]: any; }
|
|
74
|
+
* @param {string | (string | number)[]} part - This is the part of the date that we're
|
|
75
|
+
* formatting.
|
|
76
|
+
* @returns The value of the part of the date that is being formatted.
|
|
77
|
+
*/
|
|
78
|
+
private formatValue;
|
|
79
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DurationFormatter = void 0;
|
|
4
|
+
const isEmpty_1 = require("../../general/mix/isEmpty");
|
|
5
|
+
const definition_1 = require("../definition");
|
|
6
|
+
/* It takes a template string and a number of milliseconds and returns a string that is formatted to
|
|
7
|
+
the user's preference
|
|
8
|
+
this class is based on moment format:
|
|
9
|
+
years: Y or y
|
|
10
|
+
months: M
|
|
11
|
+
weeks: W or w
|
|
12
|
+
days: D or d
|
|
13
|
+
hours: H or h
|
|
14
|
+
minutes: m
|
|
15
|
+
seconds: s
|
|
16
|
+
ms: S
|
|
17
|
+
*/
|
|
18
|
+
class DurationFormatter {
|
|
19
|
+
/**
|
|
20
|
+
* The constructor function takes in a template string and an options object. It then sets the
|
|
21
|
+
* options object to the default options object if no options object is passed in. It then parses
|
|
22
|
+
* the template string and sets the parts to the parsed template string. It then tries to set the
|
|
23
|
+
* valueFormatter to a new Intl.NumberFormat object with the locale and fractionDigits options. If
|
|
24
|
+
* it fails, it sets the valueFormatter to a new Intl.NumberFormat object with the default locale
|
|
25
|
+
* and fractionDigits options. It then sets the numberFactor to the duration parts object with the
|
|
26
|
+
* name of the unit option. If it can't find the unit option, it sets the numberFactor to 1
|
|
27
|
+
* @param [template] - The template string that will be used to format the duration.
|
|
28
|
+
* @param options - This is an object that contains the following properties: unit, locale and fractionDigits
|
|
29
|
+
*/
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.numberFormatOptions = {
|
|
32
|
+
minimumFractionDigits: definition_1.DEFAULT_OPTIONS.fractionDigits,
|
|
33
|
+
maximumFractionDigits: definition_1.DEFAULT_OPTIONS.fractionDigits
|
|
34
|
+
};
|
|
35
|
+
this.setOptions(options.options);
|
|
36
|
+
this.setParts(options.template);
|
|
37
|
+
this.setLocaleFormatter(options.intlNumberFormat);
|
|
38
|
+
this.setNumberFactor();
|
|
39
|
+
}
|
|
40
|
+
setOptions(options) {
|
|
41
|
+
this.options = Object.assign(Object.assign({}, definition_1.DEFAULT_OPTIONS), options);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* It sets the locale formatter for the number.
|
|
45
|
+
*/
|
|
46
|
+
setLocaleFormatter(intlNumberFormat) {
|
|
47
|
+
try {
|
|
48
|
+
if (intlNumberFormat) {
|
|
49
|
+
console.log(intlNumberFormat.resolvedOptions());
|
|
50
|
+
this.valueFormatter = intlNumberFormat;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.valueFormatter = this.getNewNumberFormat(this.options.locale);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
if (intlNumberFormat) {
|
|
58
|
+
this.valueFormatter = intlNumberFormat;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.valueFormatter = this.getNewNumberFormat(definition_1.DEFAULT_OPTIONS.locale);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* It returns a new instance of the Intl.NumberFormat class, which is a built-in JavaScript class
|
|
67
|
+
* that formats numbers
|
|
68
|
+
* @param {string | string[]} locale - string | string[]
|
|
69
|
+
* @returns A new instance of the Intl.NumberFormat class.
|
|
70
|
+
*/
|
|
71
|
+
getNewNumberFormat(locale) {
|
|
72
|
+
return new Intl.NumberFormat(locale, this.numberFormatOptions);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* It takes a template string and sets the parts property to the result of calling the
|
|
76
|
+
* parseFormatTemplate function with the template string as an argument
|
|
77
|
+
* @param {string} template - The format template string.
|
|
78
|
+
*/
|
|
79
|
+
setParts(template) {
|
|
80
|
+
this.template = this.matchFormatsReplace(template);
|
|
81
|
+
this.parts = this.parseFormatTemplate(template);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* It replaces the H and D characters in the template with h and d respectively
|
|
85
|
+
* @param {string} template - The template string to be used for the date format.
|
|
86
|
+
* @returns The template string with all instances of H replaced with h and all instances of D
|
|
87
|
+
* replaced with d.
|
|
88
|
+
*/
|
|
89
|
+
matchFormatsReplace(template) {
|
|
90
|
+
return (0, isEmpty_1.isEmpty)(template) ? template : template.replace(/H/g, 'h');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* It sets the numberFactor to the value of the duration part that matches the unit.
|
|
94
|
+
*/
|
|
95
|
+
setNumberFactor() {
|
|
96
|
+
var _a;
|
|
97
|
+
this.numberFactor = ((_a = Object.values(definition_1.DURATION_PARTS).find((d) => d.name === this.options.unit)) === null || _a === void 0 ? void 0 : _a.ms) || 1;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* It takes a string, splits it into an array of strings, then maps each string to a config object,
|
|
101
|
+
* then filters out any falsy values, then reverses the array
|
|
102
|
+
* @param {string} template - string - the template string that we want to parse
|
|
103
|
+
* @returns An array of objects that have the order and the name of the part of the duration.
|
|
104
|
+
*/
|
|
105
|
+
parseFormatTemplate(template) {
|
|
106
|
+
this.template = (0, isEmpty_1.isEmpty)(template) ? '' : template;
|
|
107
|
+
return (this.template.match(definition_1.PARTS_REGEX) || [])
|
|
108
|
+
.reduce((store, part) => {
|
|
109
|
+
const config = definition_1.DURATION_PARTS[part[0]];
|
|
110
|
+
if (config)
|
|
111
|
+
store[config.order] = config;
|
|
112
|
+
return store;
|
|
113
|
+
}, [])
|
|
114
|
+
.filter(Boolean)
|
|
115
|
+
.reverse();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* It takes a number, multiplies it by a factor, then uses the parts array to calculate the number
|
|
119
|
+
* of each part in the number, then uses the template to format the number
|
|
120
|
+
* @param {number} number - The number of milliseconds to format.
|
|
121
|
+
* @returns The template string with the values replaced.
|
|
122
|
+
*/
|
|
123
|
+
format(number) {
|
|
124
|
+
if (number === null)
|
|
125
|
+
return null;
|
|
126
|
+
if (number === undefined || this.template === undefined || isNaN(number))
|
|
127
|
+
return undefined;
|
|
128
|
+
if ((0, isEmpty_1.isEmpty)(number))
|
|
129
|
+
return "";
|
|
130
|
+
number *= this.numberFactor;
|
|
131
|
+
const durationParts = this.parts.reduce((store, part) => {
|
|
132
|
+
store[part.symbol] = number / part.ms;
|
|
133
|
+
number %= part.ms;
|
|
134
|
+
return store;
|
|
135
|
+
}, {});
|
|
136
|
+
if (!Number.isInteger(number))
|
|
137
|
+
return number === null ? null : this.valueFormatter.format(number).padStart(this.parts.length, "0");
|
|
138
|
+
if ((0, isEmpty_1.isEmpty)(this.template))
|
|
139
|
+
return this.valueFormatter.format(number).padStart(this.parts.length, "0");
|
|
140
|
+
return this.template.replace(definition_1.PARTS_REGEX, (match, $1) => {
|
|
141
|
+
return $1 || this.formatValue(durationParts, match);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* It takes a dictionary of values and a list of keys, and returns a formatted string
|
|
146
|
+
* @param parts - { [x: string]: any; }
|
|
147
|
+
* @param {string | (string | number)[]} part - This is the part of the date that we're
|
|
148
|
+
* formatting.
|
|
149
|
+
* @returns The value of the part of the date that is being formatted.
|
|
150
|
+
*/
|
|
151
|
+
formatValue(parts, part) {
|
|
152
|
+
const value = parts[part[0]];
|
|
153
|
+
return this.valueFormatter.format(value).padStart(part.length, "0");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.DurationFormatter = DurationFormatter;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./durationFormatter"), exports);
|
|
18
|
+
__exportStar(require("./addDurationFormat"), exports);
|
package/dist/cjs/format/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./format"), exports);
|
|
18
18
|
__exportStar(require("./localization"), exports);
|
|
19
|
+
__exportStar(require("./duration/index"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* If the date is a valid Date object, return true, otherwise return false.
|
|
3
|
+
* @param {Date} date - The date object to check.
|
|
4
|
+
* @returns A boolean value.
|
|
5
|
+
*/
|
|
6
|
+
export function isValidDateObject(date) {
|
|
7
|
+
return date instanceof Date && !isNaN(date.getTime());
|
|
8
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { capitalize } from "../../general/string/capitalize";
|
|
2
2
|
import { isTokenLabel } from "../../tokens/isTokenLabel";
|
|
3
|
+
import { isValidDateObject } from "../helpers/isValidDateObject";
|
|
3
4
|
/**
|
|
4
5
|
* Get date range object from a string date value
|
|
5
6
|
*
|
|
@@ -101,10 +102,16 @@ function getStringTimeRange(value, dateGroupLabel) {
|
|
|
101
102
|
return [' 00:00:00', ' 23:59:59'];
|
|
102
103
|
}
|
|
103
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* It takes a Date object as an argument and returns a string in the format of MM/DD/YYYY
|
|
107
|
+
* @param {Date} dt - Date - The date object to convert to a string
|
|
108
|
+
* @returns A string in the format of MM/DD/YYYY
|
|
109
|
+
*/
|
|
104
110
|
function getStringDate(dt) {
|
|
105
|
-
if (
|
|
106
|
-
return '
|
|
107
|
-
|
|
111
|
+
if (isValidDateObject(dt)) {
|
|
112
|
+
return `${('0' + (dt.getMonth() + 1)).slice(-2)}/${('0' + dt.getDate()).slice(-2)}/${dt.getFullYear()}`;
|
|
113
|
+
}
|
|
114
|
+
return '01/01/0000';
|
|
108
115
|
}
|
|
109
116
|
function getYearRange(value) {
|
|
110
117
|
const dateFrom = new Date(Number(value), 0, 1);
|
|
@@ -43,7 +43,7 @@ function mergeScopes(scopes1 = [], scopes2 = [], settings) {
|
|
|
43
43
|
let newScopes = scopes1.reduce((scopes, scope1) => {
|
|
44
44
|
const scope2Index = scopes2.findIndex(scope2 => resolveScopeConditions(scope2, { scope: scope1.scope, scopeid: scope1.scopeid }));
|
|
45
45
|
if (scope2Index > -1) {
|
|
46
|
-
scope1 = Object.assign(Object.assign({},
|
|
46
|
+
scope1 = Object.assign(Object.assign({}, scopes2[scope2Index]), { datasets: mergeDatasets(scope1.datasets, scopes2[scope2Index].datasets, settings) });
|
|
47
47
|
scopes2.splice(scope2Index, 1);
|
|
48
48
|
}
|
|
49
49
|
scopes.push(scope1);
|
|
@@ -64,7 +64,7 @@ function mergeDatasets(datasets1 = [], datasets2 = [], settings) {
|
|
|
64
64
|
let newDatasets = datasets1.reduce((datasets, dataset1) => {
|
|
65
65
|
const dataset2Index = datasets2.findIndex(dataset2 => resolveDatasetConditions(dataset2, { qrveyid: dataset1.qrveyid, linkid: dataset1.linkid }));
|
|
66
66
|
if (dataset2Index > -1) {
|
|
67
|
-
dataset1 = Object.assign(Object.assign({},
|
|
67
|
+
dataset1 = Object.assign(Object.assign({}, datasets2[dataset2Index]), { filters: mergeFilterss(dataset1.filters, datasets2[dataset2Index].filters, settings) });
|
|
68
68
|
datasets2.splice(dataset2Index, 1);
|
|
69
69
|
}
|
|
70
70
|
datasets.push(dataset1);
|
|
@@ -42,3 +42,65 @@ export declare const DATETIME_OPTIONS: {
|
|
|
42
42
|
minute: string;
|
|
43
43
|
second: string;
|
|
44
44
|
};
|
|
45
|
+
export declare const PARTS_REGEX: RegExp;
|
|
46
|
+
export declare const DURATION_PARTS: {
|
|
47
|
+
S: {
|
|
48
|
+
order: number;
|
|
49
|
+
symbol: string;
|
|
50
|
+
name: string;
|
|
51
|
+
ms: number;
|
|
52
|
+
};
|
|
53
|
+
s: {
|
|
54
|
+
order: number;
|
|
55
|
+
symbol: string;
|
|
56
|
+
name: string;
|
|
57
|
+
ms: number;
|
|
58
|
+
};
|
|
59
|
+
m: {
|
|
60
|
+
order: number;
|
|
61
|
+
symbol: string;
|
|
62
|
+
name: string;
|
|
63
|
+
ms: number;
|
|
64
|
+
};
|
|
65
|
+
h: {
|
|
66
|
+
order: number;
|
|
67
|
+
symbol: string;
|
|
68
|
+
name: string;
|
|
69
|
+
ms: number;
|
|
70
|
+
};
|
|
71
|
+
H: {
|
|
72
|
+
order: number;
|
|
73
|
+
symbol: string;
|
|
74
|
+
name: string;
|
|
75
|
+
ms: number;
|
|
76
|
+
};
|
|
77
|
+
D: {
|
|
78
|
+
order: number;
|
|
79
|
+
symbol: string;
|
|
80
|
+
name: string;
|
|
81
|
+
ms: number;
|
|
82
|
+
};
|
|
83
|
+
W: {
|
|
84
|
+
order: number;
|
|
85
|
+
symbol: string;
|
|
86
|
+
name: string;
|
|
87
|
+
ms: number;
|
|
88
|
+
};
|
|
89
|
+
M: {
|
|
90
|
+
order: number;
|
|
91
|
+
symbol: string;
|
|
92
|
+
name: string;
|
|
93
|
+
ms: number;
|
|
94
|
+
};
|
|
95
|
+
Y: {
|
|
96
|
+
order: number;
|
|
97
|
+
symbol: string;
|
|
98
|
+
name: string;
|
|
99
|
+
ms: number;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export declare const DEFAULT_OPTIONS: {
|
|
103
|
+
unit: string;
|
|
104
|
+
locale: string;
|
|
105
|
+
fractionDigits: number;
|
|
106
|
+
};
|
|
@@ -75,3 +75,20 @@ export const currencyISO = [
|
|
|
75
75
|
export const LANG_DEFAULT = 'en-US';
|
|
76
76
|
export const CURRENCY_DEFAULT = { text: '$ (USD)', label: 'USD' };
|
|
77
77
|
export const DATETIME_OPTIONS = { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
|
|
78
|
+
export const PARTS_REGEX = /\[([^\]]+)]|Y{1,2}|M{1,2}|W{1,2}|D{1,2}|h{1,2}|H{1,2}|m{1,2}|s{1,2}|S{1,3}/g;
|
|
79
|
+
export const DURATION_PARTS = {
|
|
80
|
+
S: { order: 1, symbol: "S", name: "milliseconds", ms: 1 },
|
|
81
|
+
s: { order: 2, symbol: "s", name: "seconds", ms: 1000 },
|
|
82
|
+
m: { order: 3, symbol: "m", name: "minutes", ms: 1000 * 60 },
|
|
83
|
+
h: { order: 4, symbol: "h", name: "hours", ms: 1000 * 60 * 60 },
|
|
84
|
+
H: { order: 5, symbol: "H", name: "hours", ms: 1000 * 60 * 60 },
|
|
85
|
+
D: { order: 6, symbol: "D", name: "days", ms: 1000 * 60 * 60 * 24 },
|
|
86
|
+
W: { order: 7, symbol: "W", name: "weeks", ms: 1000 * 60 * 60 * 24 * 7 },
|
|
87
|
+
M: { order: 8, symbol: "M", name: "months", ms: 1000 * 60 * 60 * 24 * 30 },
|
|
88
|
+
Y: { order: 9, symbol: "Y", name: "years", ms: 1000 * 60 * 60 * 24 * 365 },
|
|
89
|
+
};
|
|
90
|
+
export const DEFAULT_OPTIONS = {
|
|
91
|
+
unit: "seconds",
|
|
92
|
+
locale: "en-EN",
|
|
93
|
+
fractionDigits: 0
|
|
94
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**[TODO: The decimals dots are not working correctly, we need to reviewing (fractionDigits)]
|
|
2
|
+
* "Convert a number of seconds to a human readable string."
|
|
3
|
+
*
|
|
4
|
+
* `addDurationFormat` is a function that takes a number, a format, an optional locale, and an optional
|
|
5
|
+
* number of fraction digits, and returns a string
|
|
6
|
+
* @param {number} number - The number of milliseconds to format.
|
|
7
|
+
* @param {string} format - The format string.
|
|
8
|
+
* @param {string} [locale] - The locale to use for formatting. If not specified, the default locale is
|
|
9
|
+
* used.
|
|
10
|
+
* @param {number} [fractionDigits] - The number of digits to show after the decimal point.
|
|
11
|
+
* @returns A string
|
|
12
|
+
*/
|
|
13
|
+
export declare function addDurationFormat(number: number, format: string, locale?: string, fractionDigits?: number): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DurationFormatter } from "./durationFormatter";
|
|
2
|
+
/**[TODO: The decimals dots are not working correctly, we need to reviewing (fractionDigits)]
|
|
3
|
+
* "Convert a number of seconds to a human readable string."
|
|
4
|
+
*
|
|
5
|
+
* `addDurationFormat` is a function that takes a number, a format, an optional locale, and an optional
|
|
6
|
+
* number of fraction digits, and returns a string
|
|
7
|
+
* @param {number} number - The number of milliseconds to format.
|
|
8
|
+
* @param {string} format - The format string.
|
|
9
|
+
* @param {string} [locale] - The locale to use for formatting. If not specified, the default locale is
|
|
10
|
+
* used.
|
|
11
|
+
* @param {number} [fractionDigits] - The number of digits to show after the decimal point.
|
|
12
|
+
* @returns A string
|
|
13
|
+
*/
|
|
14
|
+
export function addDurationFormat(number, format, locale, fractionDigits) {
|
|
15
|
+
return new DurationFormatter({ template: format, options: { locale: locale, fractionDigits: fractionDigits } }).format(number);
|
|
16
|
+
}
|