@hestia-earth/utils 0.17.15 → 0.17.17
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/esm/array.d.ts +20 -0
- package/esm/array.js +49 -0
- package/esm/bin/bin-validate-terms.d.ts +2 -0
- package/esm/bin/bin-validate-terms.js +14 -0
- package/esm/boolean.d.ts +1 -0
- package/esm/boolean.js +3 -0
- package/esm/date.d.ts +59 -0
- package/esm/date.js +108 -0
- package/esm/delta.d.ts +32 -0
- package/esm/delta.js +78 -0
- package/esm/form.js +18 -0
- package/esm/index.js +7 -0
- package/esm/number.js +218 -0
- package/esm/string.js +117 -0
- package/esm/term.js +52 -0
- package/esm/utils.js +58 -0
- package/esm/validate-terms.js +267 -0
- package/form.d.ts +1 -0
- package/index.d.ts +7 -0
- package/number.d.ts +89 -0
- package/package.json +24 -5
- package/string.d.ts +55 -0
- package/term.d.ts +21 -0
- package/utils.d.ts +5 -0
- package/validate-terms.d.ts +13 -0
- package/{dist/validate-terms.js → validate-terms.js} +1 -1
- /package/{dist/array.d.ts → array.d.ts} +0 -0
- /package/{dist/array.js → array.js} +0 -0
- /package/{dist/bin → bin}/bin-validate-terms.d.ts +0 -0
- /package/{dist/bin → bin}/bin-validate-terms.js +0 -0
- /package/{dist/boolean.d.ts → boolean.d.ts} +0 -0
- /package/{dist/boolean.js → boolean.js} +0 -0
- /package/{dist/date.d.ts → date.d.ts} +0 -0
- /package/{dist/date.js → date.js} +0 -0
- /package/{dist/delta.d.ts → delta.d.ts} +0 -0
- /package/{dist/delta.js → delta.js} +0 -0
- /package/{dist → esm}/form.d.ts +0 -0
- /package/{dist → esm}/index.d.ts +0 -0
- /package/{dist → esm}/number.d.ts +0 -0
- /package/{dist → esm}/string.d.ts +0 -0
- /package/{dist → esm}/term.d.ts +0 -0
- /package/{dist → esm}/utils.d.ts +0 -0
- /package/{dist → esm}/validate-terms.d.ts +0 -0
- /package/{dist/form.js → form.js} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/number.js → number.js} +0 -0
- /package/{dist/string.js → string.js} +0 -0
- /package/{dist/term.js → term.js} +0 -0
- /package/{dist/utils.js → utils.js} +0 -0
package/esm/string.js
ADDED
|
@@ -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
|
+
};
|
package/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
|
+
};
|
package/esm/utils.js
ADDED
|
@@ -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,267 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
38
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
39
|
+
if (!m) return o;
|
|
40
|
+
var i = m.call(o), r, ar = [], e;
|
|
41
|
+
try {
|
|
42
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
43
|
+
}
|
|
44
|
+
catch (error) { e = { error: error }; }
|
|
45
|
+
finally {
|
|
46
|
+
try {
|
|
47
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
48
|
+
}
|
|
49
|
+
finally { if (e) throw e.error; }
|
|
50
|
+
}
|
|
51
|
+
return ar;
|
|
52
|
+
};
|
|
53
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
54
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
55
|
+
if (ar || !(i in from)) {
|
|
56
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
57
|
+
ar[i] = from[i];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
61
|
+
};
|
|
62
|
+
import { promises } from 'fs';
|
|
63
|
+
import { join } from 'path';
|
|
64
|
+
import { request } from 'https';
|
|
65
|
+
import { unique } from './array';
|
|
66
|
+
var API_URL = process.env.API_URL || 'https://api.hestia.earth';
|
|
67
|
+
var _a = __read(process.argv.slice(2), 2), folder = _a[0], extension = _a[1];
|
|
68
|
+
var readdir = promises.readdir, lstat = promises.lstat, readFile = promises.readFile;
|
|
69
|
+
var encoding = 'utf8';
|
|
70
|
+
var Term = /** @class */ (function () {
|
|
71
|
+
function Term() {
|
|
72
|
+
}
|
|
73
|
+
return Term;
|
|
74
|
+
}());
|
|
75
|
+
var search = function (body) {
|
|
76
|
+
return new Promise(function (resolve, reject) {
|
|
77
|
+
var req = request("".concat(API_URL, "/terms/search"), {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: { 'Content-Type': 'application/json' }
|
|
80
|
+
}, function (res) {
|
|
81
|
+
res.setEncoding(encoding);
|
|
82
|
+
var data = [];
|
|
83
|
+
res.on('data', function (chunk) { return data.push(chunk); });
|
|
84
|
+
res.on('end', function () { return resolve(JSON.parse(data.join(''))); });
|
|
85
|
+
res.on('error', reject);
|
|
86
|
+
});
|
|
87
|
+
req.write(JSON.stringify(body));
|
|
88
|
+
req.end();
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
var recursiveFiles = function (directory) { return __awaiter(void 0, void 0, void 0, function () {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
return __generator(this, function (_c) {
|
|
94
|
+
switch (_c.label) {
|
|
95
|
+
case 0:
|
|
96
|
+
_b = (_a = Promise).all;
|
|
97
|
+
return [4 /*yield*/, readdir(directory)];
|
|
98
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).map(function (entry) { return __awaiter(void 0, void 0, void 0, function () {
|
|
99
|
+
var _a;
|
|
100
|
+
return __generator(this, function (_b) {
|
|
101
|
+
switch (_b.label) {
|
|
102
|
+
case 0: return [4 /*yield*/, lstat(join(directory, entry))];
|
|
103
|
+
case 1:
|
|
104
|
+
if (!(_b.sent()).isDirectory()) return [3 /*break*/, 3];
|
|
105
|
+
return [4 /*yield*/, recursiveFiles(join(directory, entry))];
|
|
106
|
+
case 2:
|
|
107
|
+
_a = _b.sent();
|
|
108
|
+
return [3 /*break*/, 4];
|
|
109
|
+
case 3:
|
|
110
|
+
_a = join(directory, entry).endsWith(".".concat(extension || 'jsonld'))
|
|
111
|
+
? [join(directory, entry)]
|
|
112
|
+
: [];
|
|
113
|
+
_b.label = 4;
|
|
114
|
+
case 4: return [2 /*return*/, _a];
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}); })])];
|
|
118
|
+
case 2: return [2 /*return*/, (_c.sent()).flat()];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}); };
|
|
122
|
+
var getAllTerms = function (data) {
|
|
123
|
+
return Object.keys(data)
|
|
124
|
+
.filter(function (key) { return Array.isArray(data[key]) || (typeof data[key] === 'object' && '@type' in data[key]); })
|
|
125
|
+
.flatMap(function (key) {
|
|
126
|
+
var value = data[key];
|
|
127
|
+
var isArray = Array.isArray(value);
|
|
128
|
+
return isArray ? value.flatMap(getAllTerms) : value['@type'] === 'Term' ? value : getAllTerms(value);
|
|
129
|
+
})
|
|
130
|
+
.filter(function (term) { return !!term && term['@type'] === 'Term' && (!!term['@id'] || !!term.name); });
|
|
131
|
+
};
|
|
132
|
+
// search is limited to 1000 parameters
|
|
133
|
+
var searchLimit = 500;
|
|
134
|
+
var searchTerms = function (terms) { return __awaiter(void 0, void 0, void 0, function () {
|
|
135
|
+
var searchedTerms, results, _a, _b, _c;
|
|
136
|
+
return __generator(this, function (_d) {
|
|
137
|
+
switch (_d.label) {
|
|
138
|
+
case 0:
|
|
139
|
+
searchedTerms = terms.slice(0, searchLimit);
|
|
140
|
+
if (!searchedTerms.length) return [3 /*break*/, 2];
|
|
141
|
+
return [4 /*yield*/, search({
|
|
142
|
+
limit: searchLimit,
|
|
143
|
+
fields: ['@id', 'name'],
|
|
144
|
+
query: {
|
|
145
|
+
bool: {
|
|
146
|
+
must: [
|
|
147
|
+
{
|
|
148
|
+
match: { '@type': 'Term' }
|
|
149
|
+
}
|
|
150
|
+
],
|
|
151
|
+
should: searchedTerms.map(function (_a) {
|
|
152
|
+
var id = _a["@id"], name = _a.name;
|
|
153
|
+
return ({
|
|
154
|
+
match: id ? { '@id.keyword': id } : { 'name.keyword': name }
|
|
155
|
+
});
|
|
156
|
+
}),
|
|
157
|
+
minimum_should_match: 1
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
})];
|
|
161
|
+
case 1:
|
|
162
|
+
_a = _d.sent();
|
|
163
|
+
return [3 /*break*/, 3];
|
|
164
|
+
case 2:
|
|
165
|
+
_a = { results: [] };
|
|
166
|
+
_d.label = 3;
|
|
167
|
+
case 3:
|
|
168
|
+
results = (_a).results;
|
|
169
|
+
_b = [__spreadArray([], __read(results), false)];
|
|
170
|
+
if (!(terms.length > searchLimit)) return [3 /*break*/, 5];
|
|
171
|
+
return [4 /*yield*/, searchTerms(terms.slice(searchLimit))];
|
|
172
|
+
case 4:
|
|
173
|
+
_c = _d.sent();
|
|
174
|
+
return [3 /*break*/, 6];
|
|
175
|
+
case 5:
|
|
176
|
+
_c = [];
|
|
177
|
+
_d.label = 6;
|
|
178
|
+
case 6: return [2 /*return*/, __spreadArray.apply(void 0, _b.concat([__read.apply(void 0, [(_c)]), false]))];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}); };
|
|
182
|
+
/**
|
|
183
|
+
* Validate a list of Terms.
|
|
184
|
+
*
|
|
185
|
+
* @param terms The list of Terms to validate.
|
|
186
|
+
* @returns The list of ids/names that could not be found (invalid).
|
|
187
|
+
*/
|
|
188
|
+
export var validateTerms = function (terms) { return __awaiter(void 0, void 0, void 0, function () {
|
|
189
|
+
var results;
|
|
190
|
+
return __generator(this, function (_a) {
|
|
191
|
+
switch (_a.label) {
|
|
192
|
+
case 0: return [4 /*yield*/, searchTerms(terms)];
|
|
193
|
+
case 1:
|
|
194
|
+
results = _a.sent();
|
|
195
|
+
return [2 /*return*/, terms
|
|
196
|
+
.map(function (_a) {
|
|
197
|
+
var id = _a["@id"], name = _a.name;
|
|
198
|
+
var exists = results.find(function (res) { return res['@id'] === id || res.name === name; });
|
|
199
|
+
return exists ? true : id || name;
|
|
200
|
+
})
|
|
201
|
+
.filter(function (val) { return val !== true; })];
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}); };
|
|
205
|
+
var validateFile = function (existingTerms) {
|
|
206
|
+
return function (_a) {
|
|
207
|
+
var filepath = _a.filepath, terms = _a.terms;
|
|
208
|
+
return ({
|
|
209
|
+
filepath: filepath,
|
|
210
|
+
missing: terms
|
|
211
|
+
.map(function (_a) {
|
|
212
|
+
var id = _a["@id"], name = _a.name;
|
|
213
|
+
return existingTerms.find(function (res) { return res['@id'] === id || res.name === name; }) ? true : id || name;
|
|
214
|
+
})
|
|
215
|
+
.filter(function (val) { return val !== true; })
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
var loadFile = function (filepath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
220
|
+
var _a, _b, _c;
|
|
221
|
+
var _d;
|
|
222
|
+
return __generator(this, function (_e) {
|
|
223
|
+
switch (_e.label) {
|
|
224
|
+
case 0:
|
|
225
|
+
_d = {
|
|
226
|
+
filepath: filepath
|
|
227
|
+
};
|
|
228
|
+
_a = getAllTerms;
|
|
229
|
+
_c = (_b = JSON).parse;
|
|
230
|
+
return [4 /*yield*/, readFile(filepath, encoding)];
|
|
231
|
+
case 1: return [2 /*return*/, (_d.terms = _a.apply(void 0, [_c.apply(_b, [_e.sent()])]),
|
|
232
|
+
_d)];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}); };
|
|
236
|
+
export var run = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
237
|
+
var filepaths, fileData, allTerms, existingTerms, results, missingFiles;
|
|
238
|
+
return __generator(this, function (_a) {
|
|
239
|
+
switch (_a.label) {
|
|
240
|
+
case 0: return [4 /*yield*/, recursiveFiles(folder)];
|
|
241
|
+
case 1:
|
|
242
|
+
filepaths = _a.sent();
|
|
243
|
+
return [4 /*yield*/, Promise.all(filepaths.map(loadFile))];
|
|
244
|
+
case 2:
|
|
245
|
+
fileData = _a.sent();
|
|
246
|
+
allTerms = unique(fileData.flatMap(function (_a) {
|
|
247
|
+
var terms = _a.terms;
|
|
248
|
+
return terms;
|
|
249
|
+
}));
|
|
250
|
+
return [4 /*yield*/, searchTerms(allTerms)];
|
|
251
|
+
case 3:
|
|
252
|
+
existingTerms = _a.sent();
|
|
253
|
+
results = fileData.map(validateFile(existingTerms)).filter(Boolean);
|
|
254
|
+
missingFiles = results.filter(function (_a) {
|
|
255
|
+
var missing = _a.missing;
|
|
256
|
+
return missing.length;
|
|
257
|
+
});
|
|
258
|
+
if (missingFiles.length) {
|
|
259
|
+
throw new Error("\n Terms not found:\n \t".concat(missingFiles.map(function (_a) {
|
|
260
|
+
var filepath = _a.filepath, missing = _a.missing;
|
|
261
|
+
return "- ".concat(filepath, ": ").concat(missing.join(', '));
|
|
262
|
+
}).join('\n\t'), "\n ").trim());
|
|
263
|
+
}
|
|
264
|
+
return [2 /*return*/, filepaths];
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}); };
|
package/form.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const filterParams: (obj: any) => {};
|
package/index.d.ts
ADDED
package/number.d.ts
ADDED
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hestia-earth/utils",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.17",
|
|
4
4
|
"description": "HESTIA Utils library",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "esm/index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./esm/index.js",
|
|
12
|
+
"require": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"./*.js": {
|
|
15
|
+
"types": "./*.d.ts",
|
|
16
|
+
"import": "./esm/*.js",
|
|
17
|
+
"require": "./*.js"
|
|
18
|
+
},
|
|
19
|
+
"./*": {
|
|
20
|
+
"types": "./*.d.ts",
|
|
21
|
+
"import": "./esm/*.js",
|
|
22
|
+
"require": "./*.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
7
26
|
"bin": {
|
|
8
|
-
"hestia-validate-terms": "
|
|
27
|
+
"hestia-validate-terms": "bin/bin-validate-terms.js"
|
|
9
28
|
},
|
|
10
29
|
"scripts": {
|
|
11
|
-
"build": "rm -rf dist && tsc -p tsconfig.dist.json",
|
|
30
|
+
"build": "rm -rf dist && tsc -p tsconfig.dist.json && tsc -p tsconfig.dist.esm.json && cp package.json README.md dist/",
|
|
12
31
|
"lint": "eslint \"src/**/*.ts\"",
|
|
13
32
|
"lint:fix": "npm run lint -- --fix",
|
|
14
33
|
"test": "jest",
|