@hestia-earth/utils 0.10.17 → 0.10.20
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/delta.d.ts +12 -0
- package/dist/delta.js +58 -0
- package/dist/number.js +2 -1
- package/dist/string.d.ts +1 -0
- package/dist/string.js +18 -2
- package/dist/term.d.ts +3 -0
- package/dist/term.js +20 -0
- package/package.json +5 -1
package/dist/delta.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare enum DeltaDisplayType {
|
|
2
|
+
absolute = "absolute",
|
|
3
|
+
percent = "percent"
|
|
4
|
+
}
|
|
5
|
+
declare type deltaTypeMapping = {
|
|
6
|
+
[type in DeltaDisplayType]?: (value: number, original: number) => number;
|
|
7
|
+
};
|
|
8
|
+
export declare const customDeltaFuncs: {
|
|
9
|
+
percent: (recalculated: number, original: number) => number;
|
|
10
|
+
};
|
|
11
|
+
export declare const delta: (value: any, originalValue: any, displayType?: DeltaDisplayType, mapping?: deltaTypeMapping, termId?: string) => number;
|
|
12
|
+
export {};
|
package/dist/delta.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var _a, _b, _c, _d;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.delta = exports.customDeltaFuncs = exports.DeltaDisplayType = void 0;
|
|
16
|
+
var term_1 = require("./term");
|
|
17
|
+
var DeltaDisplayType;
|
|
18
|
+
(function (DeltaDisplayType) {
|
|
19
|
+
DeltaDisplayType["absolute"] = "absolute";
|
|
20
|
+
DeltaDisplayType["percent"] = "percent";
|
|
21
|
+
})(DeltaDisplayType = exports.DeltaDisplayType || (exports.DeltaDisplayType = {}));
|
|
22
|
+
var deltaPerType = (_a = {},
|
|
23
|
+
_a[DeltaDisplayType.absolute] = function (value, original) { return value - original; },
|
|
24
|
+
_a[DeltaDisplayType.percent] = function (value, original) { return ((value - original) / original) * 100; },
|
|
25
|
+
_a);
|
|
26
|
+
var roundValue = function (value) { return +("" + value).substring(0, 10); };
|
|
27
|
+
var PercentDeltaConditions;
|
|
28
|
+
(function (PercentDeltaConditions) {
|
|
29
|
+
PercentDeltaConditions["recalculated0"] = "recalculated should be 0";
|
|
30
|
+
PercentDeltaConditions["original0"] = "original is 0";
|
|
31
|
+
})(PercentDeltaConditions || (PercentDeltaConditions = {}));
|
|
32
|
+
var calculatePercentDeltaConditions = (_b = {},
|
|
33
|
+
_b[PercentDeltaConditions.recalculated0] = function (original, recalculated) { return original > 0 && recalculated === 0; },
|
|
34
|
+
_b[PercentDeltaConditions.original0] = function (original, recalculated) { return original === 0 && recalculated > 0; },
|
|
35
|
+
_b);
|
|
36
|
+
var calculatePercentDeltaResult = (_c = {},
|
|
37
|
+
_c[PercentDeltaConditions.recalculated0] = function (original, recalculated) { return (recalculated - original) / (original + 1); },
|
|
38
|
+
_c[PercentDeltaConditions.original0] = function (original, recalculated) { return Math.sign(recalculated - original); },
|
|
39
|
+
_c.default = function (original, recalculated) { return (recalculated - original) / original; },
|
|
40
|
+
_c);
|
|
41
|
+
var calculatePercentDelta = function (recalculated, original) {
|
|
42
|
+
var matchingCondition = Object.values(PercentDeltaConditions).find(function (value) {
|
|
43
|
+
return calculatePercentDeltaConditions[value](original, recalculated);
|
|
44
|
+
}) || 'default';
|
|
45
|
+
return calculatePercentDeltaResult[matchingCondition](original, recalculated) * 100;
|
|
46
|
+
};
|
|
47
|
+
exports.customDeltaFuncs = (_d = {},
|
|
48
|
+
_d[DeltaDisplayType.percent] = calculatePercentDelta,
|
|
49
|
+
_d);
|
|
50
|
+
var delta = function (value, originalValue, displayType, mapping, termId) {
|
|
51
|
+
if (displayType === void 0) { displayType = DeltaDisplayType.percent; }
|
|
52
|
+
var vvalue = roundValue(term_1.propertyValue(value, termId));
|
|
53
|
+
var voriginalValue = roundValue(term_1.propertyValue(originalValue, termId));
|
|
54
|
+
var deltaFuncs = __assign(__assign({}, deltaPerType), mapping);
|
|
55
|
+
var diff = vvalue === voriginalValue ? 0 : deltaFuncs[displayType](vvalue, voriginalValue);
|
|
56
|
+
return Number.isFinite(diff) ? (diff === -0 ? 0 : diff) : 0;
|
|
57
|
+
};
|
|
58
|
+
exports.delta = delta;
|
package/dist/number.js
CHANGED
|
@@ -7,7 +7,8 @@ exports.isNumber = isNumber;
|
|
|
7
7
|
var toPrecision = function (n, precision) {
|
|
8
8
|
if (precision === void 0) { precision = 3; }
|
|
9
9
|
var mult = Math.pow(10, precision - Math.floor(Math.log(n < 0 ? -n : n) / Math.LN10) - 1);
|
|
10
|
-
|
|
10
|
+
var multiplier = +("" + mult).replace(/[0]([1-9])\1+/g, function (v) { return "" + (+(v.substring(0, 1)) + 1); });
|
|
11
|
+
return n === 0 ? 0 : Math.round(n * multiplier) / multiplier;
|
|
11
12
|
};
|
|
12
13
|
exports.toPrecision = toPrecision;
|
|
13
14
|
var ConvertUnits;
|
package/dist/string.d.ts
CHANGED
package/dist/string.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.now = exports.keyToLabel = exports.ellipsis = void 0;
|
|
3
|
+
exports.now = exports.toDashCase = exports.keyToLabel = exports.ellipsis = void 0;
|
|
4
4
|
var ellipsis = function (text, maxlength) {
|
|
5
5
|
if (text === void 0) { text = ''; }
|
|
6
6
|
if (maxlength === void 0) { maxlength = 20; }
|
|
@@ -8,8 +8,24 @@ var ellipsis = function (text, maxlength) {
|
|
|
8
8
|
};
|
|
9
9
|
exports.ellipsis = ellipsis;
|
|
10
10
|
var keyToLabel = function (key) {
|
|
11
|
-
return
|
|
11
|
+
return "" + key[0].toUpperCase() + key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/([_])([a-zA-Z])/g, function (g) { return " " + g[1].toUpperCase(); }).substring(1);
|
|
12
12
|
};
|
|
13
13
|
exports.keyToLabel = keyToLabel;
|
|
14
|
+
var toDashCase = function (value) {
|
|
15
|
+
return value
|
|
16
|
+
? value
|
|
17
|
+
.replace(/([\d]{4})([A-Z]{1})/g, function (g) { return g.substring(0, 4) + "-" + g[4].toLowerCase(); })
|
|
18
|
+
.replace(/([\d]{1}[A-Z]{1}?)([A-Z]{1})/g, function (_g) {
|
|
19
|
+
var args = [];
|
|
20
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
21
|
+
args[_i - 1] = arguments[_i];
|
|
22
|
+
}
|
|
23
|
+
return (args[0] + "-" + args[1]).toLowerCase();
|
|
24
|
+
})
|
|
25
|
+
.replace(/([A-Z])/g, function (g) { return "-" + g[0].toLowerCase(); })
|
|
26
|
+
.replace(/([0-9]{4})/g, function (g) { return "-" + g; })
|
|
27
|
+
: null;
|
|
28
|
+
};
|
|
29
|
+
exports.toDashCase = toDashCase;
|
|
14
30
|
var now = function () { return new Date().toJSON().substring(0, 10); };
|
|
15
31
|
exports.now = now;
|
package/dist/term.d.ts
ADDED
package/dist/term.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emptyValue = exports.propertyValue = exports.arrayValue = void 0;
|
|
4
|
+
var glossary_1 = require("@hestia-earth/glossary");
|
|
5
|
+
var utils_1 = require("./utils");
|
|
6
|
+
var arrayValue = function (values, isAverage) {
|
|
7
|
+
if (isAverage === void 0) { isAverage = false; }
|
|
8
|
+
return (values || []).reduce(function (prev, curr) { return prev + parseFloat("" + curr); }, 0) / (isAverage ? values.length : 1);
|
|
9
|
+
};
|
|
10
|
+
exports.arrayValue = arrayValue;
|
|
11
|
+
var propertyValue = function (value, termId) {
|
|
12
|
+
return typeof value === 'undefined' || value === null ?
|
|
13
|
+
null :
|
|
14
|
+
(Array.isArray(value) ?
|
|
15
|
+
exports.arrayValue(value, (termId ? glossary_1.getArrayTreatment(termId) : null) === 'mean') :
|
|
16
|
+
parseFloat("" + value));
|
|
17
|
+
};
|
|
18
|
+
exports.propertyValue = propertyValue;
|
|
19
|
+
var emptyValue = function (value, termId) { return utils_1.isEmpty(value) || isNaN(exports.propertyValue(value, termId)); };
|
|
20
|
+
exports.emptyValue = emptyValue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hestia-earth/utils",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.20",
|
|
4
4
|
"description": "Hestia Utils library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://gitlab.com/hestia-earth/hestia-utils#readme",
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@hestia-earth/glossary": "^0.3.0",
|
|
35
36
|
"@types/chai": "^4.2.21",
|
|
36
37
|
"@types/mocha": "^7.0.2",
|
|
37
38
|
"@types/node": "^14.17.6",
|
|
@@ -47,5 +48,8 @@
|
|
|
47
48
|
"source-map-support": "^0.5.19",
|
|
48
49
|
"ts-node": "^9.1.1",
|
|
49
50
|
"typescript": "^4.3.5"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@hestia-earth/glossary": "^0.3.0"
|
|
50
54
|
}
|
|
51
55
|
}
|