@hestia-earth/utils 0.17.14 → 0.17.16

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.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Return list of unique values.
3
+ *
4
+ * @param values List of values
5
+ */
6
+ export declare const unique: <T>(values: T[]) => T[];
7
+ /**
8
+ * Return an array containing all values that appear in both arrays.
9
+ *
10
+ * @param array1 List of values
11
+ * @param array2 List of values
12
+ */
13
+ export declare const intersection: <T>(array1: T[], array2: T[]) => T[];
14
+ /**
15
+ * Checks if both arrays are equal.
16
+ *
17
+ * @param array1 List of values
18
+ * @param array2 List of values
19
+ */
20
+ export declare const isEqual: <T>(array1: T[], array2: T[]) => boolean;
@@ -0,0 +1,49 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
26
+ /**
27
+ * Return list of unique values.
28
+ *
29
+ * @param values List of values
30
+ */
31
+ export var unique = function (values) {
32
+ return values.some(function (v) { return typeof v === 'object'; })
33
+ ? __spreadArray([], __read(new Set(values.map(function (v) { return JSON.stringify(v); }))), false).map(function (v) { return JSON.parse(v); })
34
+ : __spreadArray([], __read(new Set(values)), false);
35
+ };
36
+ /**
37
+ * Return an array containing all values that appear in both arrays.
38
+ *
39
+ * @param array1 List of values
40
+ * @param array2 List of values
41
+ */
42
+ export var intersection = function (array1, array2) { return array1.filter(function (x) { return array2.includes(x); }); };
43
+ /**
44
+ * Checks if both arrays are equal.
45
+ *
46
+ * @param array1 List of values
47
+ * @param array2 List of values
48
+ */
49
+ export var isEqual = function (array1, array2) { return JSON.stringify(array1) === JSON.stringify(array2); };
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+ import { run } from '../validate-terms';
4
+ run()
5
+ .then(function (paths) {
6
+ console.log('Done validating', paths.length, 'files.');
7
+ console.log(paths.join('\n'));
8
+ process.exit(0);
9
+ })
10
+ .catch(function (err) {
11
+ console.log('Error validating files.');
12
+ console.error(err);
13
+ process.exit(1);
14
+ });
@@ -0,0 +1 @@
1
+ export declare const isBoolean: (value?: any) => boolean;
@@ -0,0 +1,3 @@
1
+ export var isBoolean = function (value) {
2
+ return typeof value === 'boolean' || "".concat(value).toLowerCase() === 'true' || "".concat(value).toLowerCase() === 'false';
3
+ };
@@ -0,0 +1,59 @@
1
+ export declare const secondMs = 1000;
2
+ export declare const minuteMs: number;
3
+ export declare const hourMs: number;
4
+ export declare const dayMs: number;
5
+ /**
6
+ * Get the difference in days between two dates.
7
+ *
8
+ * @param date1 The left date
9
+ * @param date2 The right date
10
+ *
11
+ * @returns The difference between date1 and date2 in days (rounded).
12
+ */
13
+ export declare const diffInDays: (date1: Date | string, date2: Date | string) => number;
14
+ /**
15
+ * Get the difference in years between two dates.
16
+ *
17
+ * @param date1 The left date
18
+ * @param date2 The right date
19
+ *
20
+ * @returns The difference between date1 and date2 in years (precision: 1).
21
+ */
22
+ export declare const diffInYears: (date1: Date | string, date2: Date | string) => number;
23
+ /**
24
+ * Remove months on the date.
25
+ *
26
+ * @param date The date.
27
+ * @param days How many months to remove. Note: you can use a negative number to add months.
28
+ *
29
+ * @returns New date.
30
+ */
31
+ export declare const monthsBefore: (date?: Date, months?: number) => Date;
32
+ /**
33
+ * Remove days on the date.
34
+ *
35
+ * @param date The date.
36
+ * @param days How many days to remove. Note: you can use a negative number to add days.
37
+ *
38
+ * @returns New date.
39
+ */
40
+ export declare const daysBefore: (date?: Date, days?: number) => Date;
41
+ /**
42
+ * Remove hours on the date.
43
+ *
44
+ * @param date The date.
45
+ * @param minutes How many hours to remove. Note: you can use a negative number to add hours.
46
+ *
47
+ * @returns New date.
48
+ */
49
+ export declare const hoursBefore: (date?: Date, hours?: number) => Date;
50
+ /**
51
+ * Remove minutes on the date.
52
+ *
53
+ * @param date The date.
54
+ * @param minutes How many minutes to remove. Note: you can use a negative number to add minutes.
55
+ *
56
+ * @returns New date.
57
+ */
58
+ export declare const minutesBefore: (date?: Date, minutes?: number) => Date;
59
+ export declare const duration: (timeInMs: number, includeMs?: boolean) => string;
@@ -0,0 +1,108 @@
1
+ export var secondMs = 1000;
2
+ export var minuteMs = 60 * secondMs;
3
+ export var hourMs = 60 * minuteMs;
4
+ export var dayMs = 24 * hourMs;
5
+ var year = 365.2425;
6
+ /**
7
+ * Get the difference in days between two dates.
8
+ *
9
+ * @param date1 The left date
10
+ * @param date2 The right date
11
+ *
12
+ * @returns The difference between date1 and date2 in days (rounded).
13
+ */
14
+ export var diffInDays = function (date1, date2) {
15
+ return Math.abs(Math.round((new Date(date2).getTime() - new Date(date1).getTime()) / dayMs));
16
+ };
17
+ /**
18
+ * Get the difference in years between two dates.
19
+ *
20
+ * @param date1 The left date
21
+ * @param date2 The right date
22
+ *
23
+ * @returns The difference between date1 and date2 in years (precision: 1).
24
+ */
25
+ export var diffInYears = function (date1, date2) {
26
+ return Math.round((diffInDays(date1, date2) / year) * 10) / 10;
27
+ };
28
+ /**
29
+ * Remove months on the date.
30
+ *
31
+ * @param date The date.
32
+ * @param days How many months to remove. Note: you can use a negative number to add months.
33
+ *
34
+ * @returns New date.
35
+ */
36
+ export var monthsBefore = function (date, months) {
37
+ if (date === void 0) { date = new Date(); }
38
+ if (months === void 0) { months = 1; }
39
+ var newDate = new Date(date);
40
+ newDate.setMonth(date.getMonth() - months);
41
+ return newDate;
42
+ };
43
+ /**
44
+ * Remove days on the date.
45
+ *
46
+ * @param date The date.
47
+ * @param days How many days to remove. Note: you can use a negative number to add days.
48
+ *
49
+ * @returns New date.
50
+ */
51
+ export var daysBefore = function (date, days) {
52
+ if (date === void 0) { date = new Date(); }
53
+ if (days === void 0) { days = 1; }
54
+ var newDate = new Date(date);
55
+ newDate.setDate(date.getDate() - days);
56
+ return newDate;
57
+ };
58
+ /**
59
+ * Remove hours on the date.
60
+ *
61
+ * @param date The date.
62
+ * @param minutes How many hours to remove. Note: you can use a negative number to add hours.
63
+ *
64
+ * @returns New date.
65
+ */
66
+ export var hoursBefore = function (date, hours) {
67
+ if (date === void 0) { date = new Date(); }
68
+ if (hours === void 0) { hours = 1; }
69
+ var newDate = new Date(date);
70
+ newDate.setHours(date.getHours() - hours);
71
+ return newDate;
72
+ };
73
+ /**
74
+ * Remove minutes on the date.
75
+ *
76
+ * @param date The date.
77
+ * @param minutes How many minutes to remove. Note: you can use a negative number to add minutes.
78
+ *
79
+ * @returns New date.
80
+ */
81
+ export var minutesBefore = function (date, minutes) {
82
+ if (date === void 0) { date = new Date(); }
83
+ if (minutes === void 0) { minutes = 1; }
84
+ var newDate = new Date(date);
85
+ newDate.setMinutes(date.getMinutes() - minutes);
86
+ return newDate;
87
+ };
88
+ var parseTime = function (timeInMs) { return ({
89
+ milliseconds: Math.floor(timeInMs % 1000),
90
+ seconds: Math.floor((timeInMs / 1000) % 60),
91
+ minutes: Math.floor((timeInMs / (1000 * 60)) % 60),
92
+ hours: Math.floor((timeInMs / (1000 * 60 * 60)) % 24),
93
+ days: Math.floor(timeInMs / (1000 * 60 * 60 * 24))
94
+ }); };
95
+ var formatValue = function (value, suffix) { return (value ? "".concat(value).concat(suffix) : ''); };
96
+ export var duration = function (timeInMs, includeMs) {
97
+ if (includeMs === void 0) { includeMs = false; }
98
+ var _a = parseTime(timeInMs), milliseconds = _a.milliseconds, seconds = _a.seconds, minutes = _a.minutes, hours = _a.hours, days = _a.days;
99
+ return [
100
+ formatValue(days, 'd'),
101
+ formatValue(hours, 'h'),
102
+ formatValue(minutes, 'm'),
103
+ formatValue(seconds, 's'),
104
+ includeMs ? formatValue(milliseconds, 'ms') : ''
105
+ ]
106
+ .filter(function (v) { return v !== ''; })
107
+ .join(' ');
108
+ };
@@ -0,0 +1,32 @@
1
+ import { propertyValueType } from './term';
2
+ export declare enum DeltaDisplayType {
3
+ absolute = "absolute",
4
+ percent = "percent"
5
+ }
6
+ type deltaTypeMapping = {
7
+ [type in DeltaDisplayType]?: (value: number, original: number) => number;
8
+ };
9
+ export declare const customDeltaFuncs: {
10
+ percent: (recalculated: number, original: number) => number;
11
+ };
12
+ /**
13
+ * Calculate the delta between 2 values.
14
+ *
15
+ * @param value
16
+ * @param originalValue
17
+ * @param displayType
18
+ * @param mapping
19
+ * @param termId If calculating delta for a specific Term.
20
+ * @returns
21
+ */
22
+ export declare const delta: (value: any, originalValue: any, displayType?: DeltaDisplayType, mapping?: deltaTypeMapping, termId?: string) => number;
23
+ /**
24
+ * Calculate the delta between original and recalculated
25
+ *
26
+ * @returns
27
+ */
28
+ export declare const recalculatedDelta: ({ original, recalculated }: {
29
+ original: propertyValueType;
30
+ recalculated: propertyValueType;
31
+ }) => number;
32
+ export {};
@@ -0,0 +1,78 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var _a, _b, _c, _d;
13
+ import { emptyValue, propertyValue } from './term';
14
+ import { toPrecision } from './number';
15
+ export var DeltaDisplayType;
16
+ (function (DeltaDisplayType) {
17
+ DeltaDisplayType["absolute"] = "absolute";
18
+ DeltaDisplayType["percent"] = "percent";
19
+ })(DeltaDisplayType || (DeltaDisplayType = {}));
20
+ var deltaPerType = (_a = {},
21
+ _a[DeltaDisplayType.absolute] = function (value, original) { return value - original; },
22
+ _a[DeltaDisplayType.percent] = function (value, original) { return ((value - original) / original) * 100; },
23
+ _a);
24
+ var PercentDeltaConditions;
25
+ (function (PercentDeltaConditions) {
26
+ PercentDeltaConditions["recalculated0"] = "recalculated should be 0";
27
+ PercentDeltaConditions["original0"] = "original is 0";
28
+ })(PercentDeltaConditions || (PercentDeltaConditions = {}));
29
+ var calculatePercentDeltaConditions = (_b = {},
30
+ _b[PercentDeltaConditions.recalculated0] = function (original, recalculated) { return original > 0 && recalculated === 0; },
31
+ _b[PercentDeltaConditions.original0] = function (original, recalculated) { return original === 0 && recalculated > 0; },
32
+ _b);
33
+ var calculatePercentDeltaResult = (_c = {},
34
+ _c[PercentDeltaConditions.recalculated0] = function (original, recalculated) { return (recalculated - original) / (original + 1); },
35
+ // Always considered an error so deliberately exceed SUCCESS_CRITERION_MAX_DELTA_PERCENT
36
+ _c[PercentDeltaConditions.original0] = function (original, recalculated) { return Math.sign(recalculated - original); },
37
+ _c.default = function (original, recalculated) { return (recalculated - original) / original; },
38
+ _c);
39
+ var calculatePercentDelta = function (recalculated, original) {
40
+ var matchingCondition = Object.values(PercentDeltaConditions).find(function (value) {
41
+ return calculatePercentDeltaConditions[value](original, recalculated);
42
+ }) || 'default';
43
+ return calculatePercentDeltaResult[matchingCondition](original, recalculated) * 100;
44
+ };
45
+ export var customDeltaFuncs = (_d = {},
46
+ _d[DeltaDisplayType.percent] = calculatePercentDelta,
47
+ _d);
48
+ /**
49
+ * Calculate the delta between 2 values.
50
+ *
51
+ * @param value
52
+ * @param originalValue
53
+ * @param displayType
54
+ * @param mapping
55
+ * @param termId If calculating delta for a specific Term.
56
+ * @returns
57
+ */
58
+ export var delta = function (value, originalValue, displayType, mapping, termId) {
59
+ if (displayType === void 0) { displayType = DeltaDisplayType.percent; }
60
+ var vvalue = toPrecision(propertyValue(value, termId));
61
+ var voriginalValue = toPrecision(propertyValue(originalValue, termId));
62
+ var deltaFuncs = __assign(__assign({}, deltaPerType), mapping);
63
+ var diff = vvalue === voriginalValue ? 0 : deltaFuncs[displayType](vvalue, voriginalValue);
64
+ return Number.isFinite(diff) ? (diff === -0 ? 0 : diff) : 0;
65
+ };
66
+ /**
67
+ * Calculate the delta between original and recalculated
68
+ *
69
+ * @returns
70
+ */
71
+ export var recalculatedDelta = function (_a) {
72
+ var original = _a.original, recalculated = _a.recalculated;
73
+ return emptyValue(original)
74
+ ? 0
75
+ : emptyValue(recalculated)
76
+ ? 100 // If original value but no recalculated value trigger test failure
77
+ : delta(recalculated, original, undefined, customDeltaFuncs);
78
+ };
@@ -0,0 +1 @@
1
+ export declare const filterParams: (obj: any) => {};
@@ -0,0 +1,18 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { isUndefined } from './utils';
13
+ export var filterParams = function (obj) {
14
+ return Object.keys(obj).reduce(function (prev, key) {
15
+ var _a;
16
+ return (__assign(__assign({}, prev), (isUndefined(obj[key]) || obj[key] === '' ? {} : (_a = {}, _a[key] = "".concat(obj[key]), _a))));
17
+ }, {});
18
+ };
@@ -0,0 +1,7 @@
1
+ export * from './array';
2
+ export * from './boolean';
3
+ export * from './date';
4
+ export * from './form';
5
+ export * from './number';
6
+ export * from './string';
7
+ export * from './utils';
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Check if the value is a number.
3
+ *
4
+ * @param n Number as string or number
5
+ * @returns true if the value is a number, false otherwise
6
+ */
7
+ export declare const isNumber: (n: string | number) => boolean;
8
+ /**
9
+ * Returns a number with significant figures.
10
+ * Example: 3645.875 with 3 significant figures will return 3650.
11
+ *
12
+ * @param n The value
13
+ * @param precision The number of significant figures
14
+ */
15
+ export declare const toPrecision: (n: number, precision?: number) => number;
16
+ /**
17
+ * Get the value of an array for a given percentile.
18
+ *
19
+ * @param values
20
+ * @param percentile
21
+ * @returns
22
+ */
23
+ export declare const getPercentileValue: (values: number[], percentile: number) => number;
24
+ /**
25
+ * Returns the number formatted with commas every thousand.
26
+ *
27
+ * @param n The value
28
+ */
29
+ export declare const toComma: (n: number) => string;
30
+ export declare enum ConvertUnits {
31
+ m3 = "m3",
32
+ kg = "kg",
33
+ L = "L",
34
+ MJ = "MJ",
35
+ kWh = "kWh",
36
+ kgCa = "kg Ca",
37
+ kgCaCO3 = "kg CaCO3",
38
+ kgCaO = "kg CaO",
39
+ kgCaMg_CO3_2 = "kg CaMg(CO3)2",
40
+ kgCa_OH_2 = "kg Ca(OH)2",
41
+ kgCH4 = "kg CH4",
42
+ kgCH4C = "kg CH4-C",
43
+ kgCO2 = "kg CO2",
44
+ kgCO2C = "kg CO2-C",
45
+ kgK = "kg K",
46
+ kgK2O = "kg K2O",
47
+ kgMgCO3 = "kg MgCO3",
48
+ kgN2 = "kg N2",
49
+ kgN2N = "kg N2-N",
50
+ kgN2O = "kg N2O",
51
+ kgN2ON = "kg N2O-N",
52
+ kgNH3 = "kg NH3",
53
+ kgNH3N = "kg NH3-N",
54
+ kgNH4 = "kg NH4",
55
+ kgNH4N = "kg NH4-N",
56
+ kgNO2 = "kg NO2",
57
+ kgNO2N = "kg NO2-N",
58
+ kgNO3 = "kg NO3",
59
+ kgNO3N = "kg NO3-N",
60
+ kgNOx = "kg NOx",
61
+ kgNOxN = "kg NOx-N",
62
+ kgP = "kg P",
63
+ kgP2O5 = "kg P2O5",
64
+ kgPO43 = "kg PO43"
65
+ }
66
+ export interface IConvertArgs {
67
+ density?: number;
68
+ }
69
+ export declare const converters: {
70
+ [fromUnit in ConvertUnits]?: {
71
+ [toUnit in ConvertUnits]?: (value: number, args: IConvertArgs) => number;
72
+ };
73
+ };
74
+ /**
75
+ * Converts a value of unit into a different unit.
76
+ * Depending on the destination unit, additional arguments might be provided.
77
+ *
78
+ * @param n The value to convert, usually a float or an integer.
79
+ * @param fromUnit The unit the value is specified in.
80
+ * @param toUnit The unit the converted value should be.
81
+ * @param args Optional arguments to provide depending on the conversion.
82
+ * @returns The converted value.
83
+ */
84
+ export declare const convertValue: (n: number, fromUnit: ConvertUnits, toUnit: ConvertUnits, args?: IConvertArgs) => number;
85
+ export declare const sum: (values?: number[]) => number;
86
+ export declare const mean: (values?: number[]) => number;
87
+ export declare const min: (values: number[]) => number;
88
+ export declare const max: (values: number[]) => number;
89
+ export declare const median: (values: number[]) => number;