@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.
- package/dist/esm/array.d.ts +20 -0
- package/dist/esm/array.js +49 -0
- package/dist/esm/bin/bin-validate-terms.d.ts +2 -0
- package/dist/esm/bin/bin-validate-terms.js +14 -0
- package/dist/esm/boolean.d.ts +1 -0
- package/dist/esm/boolean.js +3 -0
- package/dist/esm/date.d.ts +59 -0
- package/dist/esm/date.js +108 -0
- package/dist/esm/delta.d.ts +32 -0
- package/dist/esm/delta.js +78 -0
- package/dist/esm/form.d.ts +1 -0
- package/dist/esm/form.js +18 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/number.d.ts +89 -0
- package/dist/esm/number.js +218 -0
- package/dist/esm/string.d.ts +55 -0
- package/dist/esm/string.js +117 -0
- package/dist/esm/term.d.ts +21 -0
- package/dist/esm/term.js +52 -0
- package/dist/esm/utils.d.ts +5 -0
- package/dist/esm/utils.js +58 -0
- package/dist/esm/validate-terms.d.ts +13 -0
- package/dist/esm/validate-terms.js +267 -0
- package/dist/validate-terms.js +1 -1
- package/package.json +11 -2
|
@@ -0,0 +1,218 @@
|
|
|
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 _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
|
18
|
+
/**
|
|
19
|
+
* Check if the value is a number.
|
|
20
|
+
*
|
|
21
|
+
* @param n Number as string or number
|
|
22
|
+
* @returns true if the value is a number, false otherwise
|
|
23
|
+
*/
|
|
24
|
+
export var isNumber = function (n) {
|
|
25
|
+
var _a;
|
|
26
|
+
return [(_a = /^(-)?[\d\.]+((e|e-)[\d]+)?$/.exec("".concat(n))) === null || _a === void 0 ? void 0 : _a.length, !isNaN(parseFloat("".concat(n))), isFinite(parseFloat("".concat(n)))].every(Boolean);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Returns a number with significant figures.
|
|
30
|
+
* Example: 3645.875 with 3 significant figures will return 3650.
|
|
31
|
+
*
|
|
32
|
+
* @param n The value
|
|
33
|
+
* @param precision The number of significant figures
|
|
34
|
+
*/
|
|
35
|
+
export var toPrecision = function (n, precision) {
|
|
36
|
+
if (precision === void 0) { precision = 3; }
|
|
37
|
+
// Use Math.log10 for better accuracy
|
|
38
|
+
var logN = Math.log10(Math.abs(n));
|
|
39
|
+
// Use a small epsilon to handle floating point errors in log10
|
|
40
|
+
var floorLogN = Math.floor(logN + 1e-13);
|
|
41
|
+
var mult = Math.pow(10, precision - floorLogN - 1);
|
|
42
|
+
var cappedMultiplied = 0.0001;
|
|
43
|
+
return n === 0
|
|
44
|
+
? 0
|
|
45
|
+
: !isNumber(n)
|
|
46
|
+
? undefined
|
|
47
|
+
: mult < cappedMultiplied
|
|
48
|
+
? Math.round(n * cappedMultiplied) / cappedMultiplied
|
|
49
|
+
: Number(n.toPrecision(precision));
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Get the value of an array for a given percentile.
|
|
53
|
+
*
|
|
54
|
+
* @param values
|
|
55
|
+
* @param percentile
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
export var getPercentileValue = function (values, percentile) {
|
|
59
|
+
var sorted = values.slice().sort(function (a, b) { return a - b; });
|
|
60
|
+
var index = (sorted.length - 1) * percentile;
|
|
61
|
+
var lower = Math.floor(index);
|
|
62
|
+
var upper = Math.ceil(index);
|
|
63
|
+
var weight = index - lower;
|
|
64
|
+
return lower === upper ? sorted[index] : sorted[lower] * (1 - weight) + sorted[upper] * weight;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Returns the number formatted with commas every thousand.
|
|
68
|
+
*
|
|
69
|
+
* @param n The value
|
|
70
|
+
*/
|
|
71
|
+
export var toComma = function (n) {
|
|
72
|
+
var _a = __read(n.toString().split('.'), 2), numberPart = _a[0], decimalPart = _a[1];
|
|
73
|
+
var thousands = /\B(?=(\d{3})+(?!\d))/g;
|
|
74
|
+
return "".concat(numberPart.replace(thousands, ',')).concat(decimalPart ? ".".concat(decimalPart) : '');
|
|
75
|
+
};
|
|
76
|
+
var C = 12.012;
|
|
77
|
+
var CA = 40.078;
|
|
78
|
+
var H = 1.008;
|
|
79
|
+
var K = 39.098;
|
|
80
|
+
var N = 14.007;
|
|
81
|
+
var O = 15.999;
|
|
82
|
+
var P = 30.974;
|
|
83
|
+
export var ConvertUnits;
|
|
84
|
+
(function (ConvertUnits) {
|
|
85
|
+
ConvertUnits["m3"] = "m3";
|
|
86
|
+
ConvertUnits["kg"] = "kg";
|
|
87
|
+
ConvertUnits["L"] = "L";
|
|
88
|
+
ConvertUnits["MJ"] = "MJ";
|
|
89
|
+
ConvertUnits["kWh"] = "kWh";
|
|
90
|
+
ConvertUnits["kgCa"] = "kg Ca";
|
|
91
|
+
ConvertUnits["kgCaCO3"] = "kg CaCO3";
|
|
92
|
+
ConvertUnits["kgCaO"] = "kg CaO";
|
|
93
|
+
ConvertUnits["kgCaMg_CO3_2"] = "kg CaMg(CO3)2";
|
|
94
|
+
ConvertUnits["kgCa_OH_2"] = "kg Ca(OH)2";
|
|
95
|
+
ConvertUnits["kgCH4"] = "kg CH4";
|
|
96
|
+
ConvertUnits["kgCH4C"] = "kg CH4-C";
|
|
97
|
+
ConvertUnits["kgCO2"] = "kg CO2";
|
|
98
|
+
ConvertUnits["kgCO2C"] = "kg CO2-C";
|
|
99
|
+
ConvertUnits["kgK"] = "kg K";
|
|
100
|
+
ConvertUnits["kgK2O"] = "kg K2O";
|
|
101
|
+
ConvertUnits["kgMgCO3"] = "kg MgCO3";
|
|
102
|
+
ConvertUnits["kgN2"] = "kg N2";
|
|
103
|
+
ConvertUnits["kgN2N"] = "kg N2-N";
|
|
104
|
+
ConvertUnits["kgN2O"] = "kg N2O";
|
|
105
|
+
ConvertUnits["kgN2ON"] = "kg N2O-N";
|
|
106
|
+
ConvertUnits["kgNH3"] = "kg NH3";
|
|
107
|
+
ConvertUnits["kgNH3N"] = "kg NH3-N";
|
|
108
|
+
ConvertUnits["kgNH4"] = "kg NH4";
|
|
109
|
+
ConvertUnits["kgNH4N"] = "kg NH4-N";
|
|
110
|
+
ConvertUnits["kgNO2"] = "kg NO2";
|
|
111
|
+
ConvertUnits["kgNO2N"] = "kg NO2-N";
|
|
112
|
+
ConvertUnits["kgNO3"] = "kg NO3";
|
|
113
|
+
ConvertUnits["kgNO3N"] = "kg NO3-N";
|
|
114
|
+
ConvertUnits["kgNOx"] = "kg NOx";
|
|
115
|
+
ConvertUnits["kgNOxN"] = "kg NOx-N";
|
|
116
|
+
ConvertUnits["kgP"] = "kg P";
|
|
117
|
+
ConvertUnits["kgP2O5"] = "kg P2O5";
|
|
118
|
+
ConvertUnits["kgPO43"] = "kg PO43";
|
|
119
|
+
})(ConvertUnits || (ConvertUnits = {}));
|
|
120
|
+
export var converters = (_a = {},
|
|
121
|
+
_a[ConvertUnits.m3] = (_b = {},
|
|
122
|
+
_b[ConvertUnits.kg] = function (value, args) { return value * args.density; },
|
|
123
|
+
_b[ConvertUnits.L] = function (value) { return value * 1000; },
|
|
124
|
+
_b),
|
|
125
|
+
_a[ConvertUnits.kg] = (_c = {},
|
|
126
|
+
_c[ConvertUnits.m3] = function (value, args) { return value / args.density; },
|
|
127
|
+
_c[ConvertUnits.L] = function (value, args) { return (value / args.density) * 1000; },
|
|
128
|
+
_c),
|
|
129
|
+
_a[ConvertUnits.L] = (_d = {},
|
|
130
|
+
_d[ConvertUnits.kg] = function (value, args) { return (value * args.density) / 1000; },
|
|
131
|
+
_d[ConvertUnits.m3] = function (value) { return value / 1000; },
|
|
132
|
+
_d),
|
|
133
|
+
_a[ConvertUnits.kWh] = (_e = {},
|
|
134
|
+
_e[ConvertUnits.MJ] = function (value) { return value * 3.6; },
|
|
135
|
+
_e),
|
|
136
|
+
_a[ConvertUnits.MJ] = (_f = {},
|
|
137
|
+
_f[ConvertUnits.kWh] = function (value) { return value / 3.6; },
|
|
138
|
+
_f),
|
|
139
|
+
_a[ConvertUnits.kgP] = (_g = {},
|
|
140
|
+
_g[ConvertUnits.kgP2O5] = function (value) { return (value * (P * 2)) / (P * 2 + O * 5); },
|
|
141
|
+
_g[ConvertUnits.kgPO43] = function (value) { return (value * P) / ((P + O * 4) * 3); },
|
|
142
|
+
_g),
|
|
143
|
+
_a[ConvertUnits.kgPO43] = (_h = {},
|
|
144
|
+
_h[ConvertUnits.kgP2O5] = function (value) { return (value * ((P + O * 4) * 3)) / (P * 2 + O * 5); },
|
|
145
|
+
_h),
|
|
146
|
+
_a[ConvertUnits.kgK] = (_j = {},
|
|
147
|
+
_j[ConvertUnits.kgK2O] = function (value) { return (value * (K * 2)) / (K * 2 + O); },
|
|
148
|
+
_j),
|
|
149
|
+
_a[ConvertUnits.kgCa] = (_k = {},
|
|
150
|
+
_k[ConvertUnits.kgCaO] = function (value) { return (value * CA) / (CA + O); },
|
|
151
|
+
_k),
|
|
152
|
+
_a[ConvertUnits.kgCaO] = (_l = {},
|
|
153
|
+
_l[ConvertUnits.kgCaCO3] = function (value) { return (value * (CA + O)) / (CA + C + O * 3); },
|
|
154
|
+
_l),
|
|
155
|
+
_a[ConvertUnits.kgCaMg_CO3_2] = (_m = {},
|
|
156
|
+
_m[ConvertUnits.kgCaCO3] = function (value) { return value; },
|
|
157
|
+
_m),
|
|
158
|
+
_a[ConvertUnits.kgCa_OH_2] = (_o = {},
|
|
159
|
+
_o[ConvertUnits.kgCaCO3] = function (value) { return value; },
|
|
160
|
+
_o),
|
|
161
|
+
_a[ConvertUnits.kgCaCO3] = (_p = {},
|
|
162
|
+
_p[ConvertUnits.kgCO2] = function (value) { return value * 0.12; },
|
|
163
|
+
_p),
|
|
164
|
+
_a[ConvertUnits.kgMgCO3] = (_q = {},
|
|
165
|
+
_q[ConvertUnits.kgCO2] = function (value) { return value * 0.13; },
|
|
166
|
+
_q),
|
|
167
|
+
_a[ConvertUnits.kgCH4] = (_r = {},
|
|
168
|
+
_r[ConvertUnits.kgCH4C] = function (value) { return (value * (C + H * 4)) / C; },
|
|
169
|
+
_r),
|
|
170
|
+
_a[ConvertUnits.kgCO2] = (_s = {},
|
|
171
|
+
_s[ConvertUnits.kgCO2C] = function (value) { return (value * (C + O * 2)) / C; },
|
|
172
|
+
_s),
|
|
173
|
+
_a[ConvertUnits.kgNOx] = (_t = {},
|
|
174
|
+
_t[ConvertUnits.kgNOxN] = function (value) { return (value * (N + O)) / N; },
|
|
175
|
+
_t),
|
|
176
|
+
_a[ConvertUnits.kgN2] = (_u = {},
|
|
177
|
+
_u[ConvertUnits.kgN2N] = function (value) { return value * 1; },
|
|
178
|
+
_u),
|
|
179
|
+
_a[ConvertUnits.kgN2O] = (_v = {},
|
|
180
|
+
_v[ConvertUnits.kgN2ON] = function (value) { return (value * (N * 2 + O)) / (N * 2); },
|
|
181
|
+
_v),
|
|
182
|
+
_a[ConvertUnits.kgNO2] = (_w = {},
|
|
183
|
+
_w[ConvertUnits.kgNO2N] = function (value) { return (value * (N + O * 2)) / N; },
|
|
184
|
+
_w),
|
|
185
|
+
_a[ConvertUnits.kgNO3] = (_x = {},
|
|
186
|
+
_x[ConvertUnits.kgNO3N] = function (value) { return (value * (N + O * 3)) / N; },
|
|
187
|
+
_x),
|
|
188
|
+
_a[ConvertUnits.kgNH3] = (_y = {},
|
|
189
|
+
_y[ConvertUnits.kgNH3N] = function (value) { return (value * (N + H * 3)) / N; },
|
|
190
|
+
_y),
|
|
191
|
+
_a[ConvertUnits.kgNH4] = (_z = {},
|
|
192
|
+
_z[ConvertUnits.kgNH4N] = function (value) { return (value * (N + H * 4)) / N; },
|
|
193
|
+
_z),
|
|
194
|
+
_a);
|
|
195
|
+
/**
|
|
196
|
+
* Converts a value of unit into a different unit.
|
|
197
|
+
* Depending on the destination unit, additional arguments might be provided.
|
|
198
|
+
*
|
|
199
|
+
* @param n The value to convert, usually a float or an integer.
|
|
200
|
+
* @param fromUnit The unit the value is specified in.
|
|
201
|
+
* @param toUnit The unit the converted value should be.
|
|
202
|
+
* @param args Optional arguments to provide depending on the conversion.
|
|
203
|
+
* @returns The converted value.
|
|
204
|
+
*/
|
|
205
|
+
export var convertValue = function (n, fromUnit, toUnit, args) {
|
|
206
|
+
return converters[fromUnit][toUnit](n, args);
|
|
207
|
+
};
|
|
208
|
+
export var sum = function (values) {
|
|
209
|
+
if (values === void 0) { values = []; }
|
|
210
|
+
return values.reduce(function (p, c) { return p + c; }, 0);
|
|
211
|
+
};
|
|
212
|
+
export var mean = function (values) {
|
|
213
|
+
if (values === void 0) { values = []; }
|
|
214
|
+
return ((values === null || values === void 0 ? void 0 : values.length) ? sum(values) / values.length : 0);
|
|
215
|
+
};
|
|
216
|
+
export var min = function (values) { return Math.min.apply(Math.min, values); };
|
|
217
|
+
export var max = function (values) { return Math.max.apply(Math.max, values); };
|
|
218
|
+
export var median = function (values) { return values.sort()[Math.round(values.length / 2)]; };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trims the string to a certain max length and replace with `...`.
|
|
3
|
+
*
|
|
4
|
+
* @param text The text to ellipsize.
|
|
5
|
+
* @param maxlength The maximum length of the text (including `...`).
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare const ellipsis: (text?: string, maxlength?: number) => string;
|
|
9
|
+
export declare const keyToLabel: (key: string) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Convert a string to dash-case.
|
|
12
|
+
*
|
|
13
|
+
* @param value The original value.
|
|
14
|
+
* @returns A dash case version of the value.
|
|
15
|
+
*/
|
|
16
|
+
export declare const toDashCase: (value?: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Convert a string to camelCase.
|
|
19
|
+
*
|
|
20
|
+
* @param value The original value.
|
|
21
|
+
* @returns A camel case version of the value.
|
|
22
|
+
*/
|
|
23
|
+
export declare const toCamelCase: (value?: string) => string;
|
|
24
|
+
export declare const dateToString: (date: Date) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Returns current date in YYYY-MM-DD format.
|
|
27
|
+
*
|
|
28
|
+
* @returns Date as a string.
|
|
29
|
+
*/
|
|
30
|
+
export declare const now: () => string;
|
|
31
|
+
/**
|
|
32
|
+
* Parse a markdown link.
|
|
33
|
+
*
|
|
34
|
+
* @param link
|
|
35
|
+
* @returns Title and link if found.
|
|
36
|
+
*/
|
|
37
|
+
export declare const parseMarkdownLink: (link: string) => {
|
|
38
|
+
title: string;
|
|
39
|
+
link: string;
|
|
40
|
+
} | {
|
|
41
|
+
title: string;
|
|
42
|
+
link?: undefined;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Parse a citation.
|
|
46
|
+
*
|
|
47
|
+
* @param value
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
export declare const parseCitation: (value: string) => {
|
|
51
|
+
original: string;
|
|
52
|
+
name: string;
|
|
53
|
+
title: string;
|
|
54
|
+
link: string;
|
|
55
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
/**
|
|
18
|
+
* Trims the string to a certain max length and replace with `...`.
|
|
19
|
+
*
|
|
20
|
+
* @param text The text to ellipsize.
|
|
21
|
+
* @param maxlength The maximum length of the text (including `...`).
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
export var ellipsis = function (text, maxlength) {
|
|
25
|
+
if (text === void 0) { text = ''; }
|
|
26
|
+
if (maxlength === void 0) { maxlength = 20; }
|
|
27
|
+
return text.length > maxlength ? "".concat(text.substring(0, maxlength), "...") : text;
|
|
28
|
+
};
|
|
29
|
+
export var keyToLabel = function (key) {
|
|
30
|
+
return "".concat(key[0].toUpperCase()).concat(key
|
|
31
|
+
.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
32
|
+
.replace(/([_])([a-zA-Z])/g, function (g) { return " ".concat(g[1].toUpperCase()); })
|
|
33
|
+
.substring(1));
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Convert a string to dash-case.
|
|
37
|
+
*
|
|
38
|
+
* @param value The original value.
|
|
39
|
+
* @returns A dash case version of the value.
|
|
40
|
+
*/
|
|
41
|
+
export var toDashCase = function (value) {
|
|
42
|
+
return value
|
|
43
|
+
? value
|
|
44
|
+
// handle dates followed by capital letter
|
|
45
|
+
.replace(/([\d]{4})([A-Z]{1})/g, function (g) { return "".concat(g.substring(0, 4), "-").concat(g[4].toLowerCase()); })
|
|
46
|
+
// handle words followed by dates
|
|
47
|
+
.replace(/([A-Za-z]{1})([\d]{4})/g, '$1-$2')
|
|
48
|
+
// handle molecules
|
|
49
|
+
.replace(/([\d]{1}[A-Z]{1}?)([A-Z]{1})/g, function (_g) {
|
|
50
|
+
var args = [];
|
|
51
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
52
|
+
args[_i - 1] = arguments[_i];
|
|
53
|
+
}
|
|
54
|
+
return "".concat(args[0], "-").concat(args[1]).toLowerCase();
|
|
55
|
+
})
|
|
56
|
+
// handle words followed by numbers
|
|
57
|
+
.replace(/([A-Z][a-z]+)([0-9]{2,})/g, '$1-$2')
|
|
58
|
+
// handle all capital letters
|
|
59
|
+
.replace(/([A-Z])/g, function (g) { return "-".concat(g[0].toLowerCase()); })
|
|
60
|
+
// handle underscores
|
|
61
|
+
.replace(/[\_\.]/g, '-')
|
|
62
|
+
: null;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Convert a string to camelCase.
|
|
66
|
+
*
|
|
67
|
+
* @param value The original value.
|
|
68
|
+
* @returns A camel case version of the value.
|
|
69
|
+
*/
|
|
70
|
+
export var toCamelCase = function (value) {
|
|
71
|
+
return value
|
|
72
|
+
? value
|
|
73
|
+
.replace(/^[_.\- ]+/, '')
|
|
74
|
+
.toLowerCase()
|
|
75
|
+
.replace(/[_.\- ]+(\w|$)/g, function (_, p1) { return p1.toUpperCase(); })
|
|
76
|
+
.replace(/\d+(\w|$)/g, function (m) { return m.toUpperCase(); })
|
|
77
|
+
.replace(/[(),\s]/g, '')
|
|
78
|
+
.replace(/[>:]/g, '-')
|
|
79
|
+
.normalize('NFD')
|
|
80
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
81
|
+
: null;
|
|
82
|
+
};
|
|
83
|
+
export var dateToString = function (date) { return date.toJSON().substring(0, 10); };
|
|
84
|
+
/**
|
|
85
|
+
* Returns current date in YYYY-MM-DD format.
|
|
86
|
+
*
|
|
87
|
+
* @returns Date as a string.
|
|
88
|
+
*/
|
|
89
|
+
export var now = function () { return dateToString(new Date()); };
|
|
90
|
+
/**
|
|
91
|
+
* Parse a markdown link.
|
|
92
|
+
*
|
|
93
|
+
* @param link
|
|
94
|
+
* @returns Title and link if found.
|
|
95
|
+
*/
|
|
96
|
+
export var parseMarkdownLink = function (link) {
|
|
97
|
+
var regex = /\[(.*?)\]\((.*?)\)/;
|
|
98
|
+
var match = link.match(regex);
|
|
99
|
+
return match && match.length === 3 ? { title: match[1], link: match[2] } : { title: link };
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Parse a citation.
|
|
103
|
+
*
|
|
104
|
+
* @param value
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
export var parseCitation = function (value) {
|
|
108
|
+
var _a, _b;
|
|
109
|
+
var parsed = parseMarkdownLink(value);
|
|
110
|
+
var _c = __read(parsed.title.match(/(.*[(]\d+[)][\.|\s])?(.*)/), 3), original = _c[0], name = _c[1], title = _c[2];
|
|
111
|
+
return {
|
|
112
|
+
original: original,
|
|
113
|
+
name: (_a = name === null || name === void 0 ? void 0 : name.trim()) === null || _a === void 0 ? void 0 : _a.replace(/\.$/, ''),
|
|
114
|
+
title: (_b = title === null || title === void 0 ? void 0 : title.trim()) === null || _b === void 0 ? void 0 : _b.replace(/\.$/, ''),
|
|
115
|
+
link: parsed.link
|
|
116
|
+
};
|
|
117
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare enum ArrayTreatment {
|
|
2
|
+
mean = "mean",
|
|
3
|
+
sum = "sum",
|
|
4
|
+
first = "first"
|
|
5
|
+
}
|
|
6
|
+
export type propertyValueType = string | number | boolean | null;
|
|
7
|
+
export declare const arrayValue: (values?: propertyValueType[], arrayTreatment?: ArrayTreatment) => propertyValueType;
|
|
8
|
+
/**
|
|
9
|
+
* Calculate the final value of a property.
|
|
10
|
+
*
|
|
11
|
+
* @param value The value as an array or a string/number/boolean.
|
|
12
|
+
* @param termId Optional - us if the term should handle an array in a specific way.
|
|
13
|
+
*/
|
|
14
|
+
export declare const propertyValue: (value: propertyValueType | propertyValueType[], termId?: string) => propertyValueType;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the value is empty or if the property value is empty.
|
|
17
|
+
*
|
|
18
|
+
* @param value
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export declare const emptyValue: (value: propertyValueType, termId?: string) => boolean;
|
package/dist/esm/term.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getArrayTreatment } from '@hestia-earth/glossary';
|
|
2
|
+
import { isEmpty, isUndefined } from './utils';
|
|
3
|
+
import { isNumber } from './number';
|
|
4
|
+
import { isBoolean } from './boolean';
|
|
5
|
+
export var ArrayTreatment;
|
|
6
|
+
(function (ArrayTreatment) {
|
|
7
|
+
ArrayTreatment["mean"] = "mean";
|
|
8
|
+
ArrayTreatment["sum"] = "sum";
|
|
9
|
+
ArrayTreatment["first"] = "first";
|
|
10
|
+
})(ArrayTreatment || (ArrayTreatment = {}));
|
|
11
|
+
var arrayValueByTreatment = {
|
|
12
|
+
mean: function (values) { return values.reduce(function (prev, curr) { return prev + parseFloat("".concat(curr)); }, 0) / values.length; },
|
|
13
|
+
sum: function (values) { return values.reduce(function (prev, curr) { return prev + parseFloat("".concat(curr)); }, 0); },
|
|
14
|
+
first: function (values) { return values[0]; }
|
|
15
|
+
};
|
|
16
|
+
export var arrayValue = function (values, arrayTreatment) {
|
|
17
|
+
if (values === void 0) { values = []; }
|
|
18
|
+
if (arrayTreatment === void 0) { arrayTreatment = ArrayTreatment.sum; }
|
|
19
|
+
var filteredValues = values.filter(function (v) { return !isEmpty(v); });
|
|
20
|
+
var treatmentFunction = arrayValueByTreatment[arrayTreatment] || arrayValueByTreatment.first;
|
|
21
|
+
return filteredValues.length === 0
|
|
22
|
+
? null
|
|
23
|
+
: filteredValues.every(isNumber)
|
|
24
|
+
? treatmentFunction(filteredValues.filter(function (v) { return typeof v !== 'undefined'; }))
|
|
25
|
+
: filteredValues.every(isBoolean)
|
|
26
|
+
? filteredValues.every(Boolean)
|
|
27
|
+
: filteredValues.join(';');
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Calculate the final value of a property.
|
|
31
|
+
*
|
|
32
|
+
* @param value The value as an array or a string/number/boolean.
|
|
33
|
+
* @param termId Optional - us if the term should handle an array in a specific way.
|
|
34
|
+
*/
|
|
35
|
+
export var propertyValue = function (value, termId) {
|
|
36
|
+
return isUndefined(value)
|
|
37
|
+
? null
|
|
38
|
+
: Array.isArray(value)
|
|
39
|
+
? arrayValue(value, termId ? getArrayTreatment(termId) : undefined)
|
|
40
|
+
: isNumber(value)
|
|
41
|
+
? parseFloat("".concat(value))
|
|
42
|
+
: value;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Checks if the value is empty or if the property value is empty.
|
|
46
|
+
*
|
|
47
|
+
* @param value
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
export var emptyValue = function (value, termId) {
|
|
51
|
+
return isEmpty(value) || isNaN(propertyValue(value, termId));
|
|
52
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const isEmpty: (value: any, minKeys?: number) => boolean;
|
|
2
|
+
export declare const isIri: (value?: string) => boolean;
|
|
3
|
+
export declare const isUndefined: <T>(value: T, allowNull?: boolean) => boolean;
|
|
4
|
+
export declare const reduceUndefinedValues: <T>(obj: T, allowNull?: boolean) => Partial<T>;
|
|
5
|
+
export declare const filterUndefinedValues: <T>(values: T[], allowNull?: boolean) => T[];
|
|
@@ -0,0 +1,58 @@
|
|
|
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 __read = (this && this.__read) || function (o, n) {
|
|
13
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
14
|
+
if (!m) return o;
|
|
15
|
+
var i = m.call(o), r, ar = [], e;
|
|
16
|
+
try {
|
|
17
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
18
|
+
}
|
|
19
|
+
catch (error) { e = { error: error }; }
|
|
20
|
+
finally {
|
|
21
|
+
try {
|
|
22
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
+
}
|
|
24
|
+
finally { if (e) throw e.error; }
|
|
25
|
+
}
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
export var isEmpty = function (value, minKeys) {
|
|
29
|
+
if (minKeys === void 0) { minKeys = 1; }
|
|
30
|
+
return value === null ||
|
|
31
|
+
typeof value === 'undefined' ||
|
|
32
|
+
(typeof value === 'object'
|
|
33
|
+
? Array.isArray(value)
|
|
34
|
+
? !value.length
|
|
35
|
+
: Object.keys(value).filter(function (key) { return key !== 'type'; }).length < minKeys
|
|
36
|
+
: value === '');
|
|
37
|
+
};
|
|
38
|
+
export var isIri = function (value) { return (value || '').startsWith('http'); };
|
|
39
|
+
/* eslint-disable complexity */
|
|
40
|
+
export var isUndefined = function (value, allowNull) {
|
|
41
|
+
if (allowNull === void 0) { allowNull = false; }
|
|
42
|
+
return (value === null && !allowNull) ||
|
|
43
|
+
typeof value === 'undefined' ||
|
|
44
|
+
(typeof value === 'object' && value !== null && !(value instanceof Date) && !Object.keys(value).length);
|
|
45
|
+
};
|
|
46
|
+
/* eslint-enable complexity */
|
|
47
|
+
export var reduceUndefinedValues = function (obj, allowNull) {
|
|
48
|
+
if (allowNull === void 0) { allowNull = false; }
|
|
49
|
+
return Object.entries(obj).reduce(function (prev, _a) {
|
|
50
|
+
var _b;
|
|
51
|
+
var _c = __read(_a, 2), key = _c[0], value = _c[1];
|
|
52
|
+
return __assign(__assign({}, prev), (isUndefined(value, allowNull) ? {} : (_b = {}, _b[key] = value, _b)));
|
|
53
|
+
}, {});
|
|
54
|
+
};
|
|
55
|
+
export var filterUndefinedValues = function (values, allowNull) {
|
|
56
|
+
if (allowNull === void 0) { allowNull = false; }
|
|
57
|
+
return values.filter(function (value) { return !isUndefined(value, allowNull); });
|
|
58
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare class Term {
|
|
2
|
+
'@id': string;
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validate a list of Terms.
|
|
7
|
+
*
|
|
8
|
+
* @param terms The list of Terms to validate.
|
|
9
|
+
* @returns The list of ids/names that could not be found (invalid).
|
|
10
|
+
*/
|
|
11
|
+
export declare const validateTerms: (terms: Term[]) => Promise<string[]>;
|
|
12
|
+
export declare const run: () => Promise<string[]>;
|
|
13
|
+
export {};
|