@hestia-earth/utils
Advanced tools
Comparing version 0.10.30 to 0.10.31
@@ -0,3 +1,20 @@ | ||
/** | ||
* Return list of unique values. | ||
* | ||
* @param values List of values | ||
*/ | ||
export declare const unique: <T>(values: T[]) => T[]; | ||
/** | ||
* Return an array containing all values that appear in both arrays. | ||
* | ||
* @param array1 List of values | ||
* @param array2 List of values | ||
*/ | ||
export declare const intersection: <T>(array1: T[], array2: T[]) => T[]; | ||
/** | ||
* Checks if both arrays are equal. | ||
* | ||
* @param array1 List of values | ||
* @param array2 List of values | ||
*/ | ||
export declare const isEqual: <T>(array1: T[], array2: T[]) => boolean; |
@@ -25,2 +25,7 @@ "use strict"; | ||
exports.isEqual = exports.intersection = exports.unique = void 0; | ||
/** | ||
* Return list of unique values. | ||
* | ||
* @param values List of values | ||
*/ | ||
var unique = function (values) { | ||
@@ -31,5 +36,17 @@ return values.some(function (v) { return typeof v === 'object'; }) ? | ||
exports.unique = unique; | ||
/** | ||
* Return an array containing all values that appear in both arrays. | ||
* | ||
* @param array1 List of values | ||
* @param array2 List of values | ||
*/ | ||
var intersection = function (array1, array2) { return array1.filter(function (x) { return array2.includes(x); }); }; | ||
exports.intersection = intersection; | ||
/** | ||
* Checks if both arrays are equal. | ||
* | ||
* @param array1 List of values | ||
* @param array2 List of values | ||
*/ | ||
var isEqual = function (array1, array2) { return JSON.stringify(array1) === JSON.stringify(array2); }; | ||
exports.isEqual = isEqual; |
#!/usr/bin/env node | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable no-console */ | ||
var validate_terms_1 = require("../validate-terms"); | ||
@@ -5,0 +6,0 @@ validate_terms_1.run() |
@@ -5,6 +5,46 @@ export declare const secondMs = 1000; | ||
export declare const dayMs: number; | ||
/** | ||
* Get the difference in days between two dates. | ||
* | ||
* @param date1 The left date | ||
* @param date2 The right date | ||
* | ||
* @returns The difference between date1 and date2 in days (rounded). | ||
*/ | ||
export declare const diffInDays: (date1: Date | string, date2: Date | string) => number; | ||
/** | ||
* Get the difference in years between two dates. | ||
* | ||
* @param date1 The left date | ||
* @param date2 The right date | ||
* | ||
* @returns The difference between date1 and date2 in years (precision: 1). | ||
*/ | ||
export declare const diffInYears: (date1: Date | string, date2: Date | string) => number; | ||
/** | ||
* Remove days on the date. | ||
* | ||
* @param date The date. | ||
* @param days How many days to remove. Note: you can use a negative number to add days. | ||
* | ||
* @returns New date. | ||
*/ | ||
export declare const daysBefore: (date?: Date, days?: number) => Date; | ||
/** | ||
* Remove hours on the date. | ||
* | ||
* @param date The date. | ||
* @param minutes How many hours to remove. Note: you can use a negative number to add hours. | ||
* | ||
* @returns New date. | ||
*/ | ||
export declare const hoursBefore: (date?: Date, hours?: number) => Date; | ||
/** | ||
* Remove minutes on the date. | ||
* | ||
* @param date The date. | ||
* @param minutes How many minutes to remove. Note: you can use a negative number to add minutes. | ||
* | ||
* @returns New date. | ||
*/ | ||
export declare const minutesBefore: (date?: Date, minutes?: number) => Date; |
@@ -9,2 +9,10 @@ "use strict"; | ||
var year = 365.2425; | ||
/** | ||
* Get the difference in days between two dates. | ||
* | ||
* @param date1 The left date | ||
* @param date2 The right date | ||
* | ||
* @returns The difference between date1 and date2 in days (rounded). | ||
*/ | ||
var diffInDays = function (date1, date2) { | ||
@@ -14,2 +22,10 @@ return Math.abs(Math.round((new Date(date2).getTime() - new Date(date1).getTime()) / exports.dayMs)); | ||
exports.diffInDays = diffInDays; | ||
/** | ||
* Get the difference in years between two dates. | ||
* | ||
* @param date1 The left date | ||
* @param date2 The right date | ||
* | ||
* @returns The difference between date1 and date2 in years (precision: 1). | ||
*/ | ||
var diffInYears = function (date1, date2) { | ||
@@ -19,2 +35,10 @@ return Math.round((exports.diffInDays(date1, date2) / year) * 10) / 10; | ||
exports.diffInYears = diffInYears; | ||
/** | ||
* Remove days on the date. | ||
* | ||
* @param date The date. | ||
* @param days How many days to remove. Note: you can use a negative number to add days. | ||
* | ||
* @returns New date. | ||
*/ | ||
var daysBefore = function (date, days) { | ||
@@ -28,2 +52,10 @@ if (date === void 0) { date = new Date(); } | ||
exports.daysBefore = daysBefore; | ||
/** | ||
* Remove hours on the date. | ||
* | ||
* @param date The date. | ||
* @param minutes How many hours to remove. Note: you can use a negative number to add hours. | ||
* | ||
* @returns New date. | ||
*/ | ||
var hoursBefore = function (date, hours) { | ||
@@ -37,2 +69,10 @@ if (date === void 0) { date = new Date(); } | ||
exports.hoursBefore = hoursBefore; | ||
/** | ||
* Remove minutes on the date. | ||
* | ||
* @param date The date. | ||
* @param minutes How many minutes to remove. Note: you can use a negative number to add minutes. | ||
* | ||
* @returns New date. | ||
*/ | ||
var minutesBefore = function (date, minutes) { | ||
@@ -39,0 +79,0 @@ if (date === void 0) { date = new Date(); } |
@@ -11,3 +11,13 @@ export declare enum DeltaDisplayType { | ||
}; | ||
/** | ||
* Calculate the delta between 2 values. | ||
* | ||
* @param value | ||
* @param originalValue | ||
* @param displayType | ||
* @param mapping | ||
* @param termId If calculating delta for a specific Term. | ||
* @returns | ||
*/ | ||
export declare const delta: (value: any, originalValue: any, displayType?: DeltaDisplayType, mapping?: deltaTypeMapping, termId?: string) => number; | ||
export {}; |
@@ -38,2 +38,3 @@ "use strict"; | ||
_c[PercentDeltaConditions.recalculated0] = function (original, recalculated) { return (recalculated - original) / (original + 1); }, | ||
// Always considered an error so deliberately exceed SUCCESS_CRITERION_MAX_DELTA_PERCENT | ||
_c[PercentDeltaConditions.original0] = function (original, recalculated) { return Math.sign(recalculated - original); }, | ||
@@ -51,2 +52,12 @@ _c.default = function (original, recalculated) { return (recalculated - original) / original; }, | ||
_d); | ||
/** | ||
* Calculate the delta between 2 values. | ||
* | ||
* @param value | ||
* @param originalValue | ||
* @param displayType | ||
* @param mapping | ||
* @param termId If calculating delta for a specific Term. | ||
* @returns | ||
*/ | ||
var delta = function (value, originalValue, displayType, mapping, termId) { | ||
@@ -53,0 +64,0 @@ if (displayType === void 0) { displayType = DeltaDisplayType.percent; } |
@@ -0,3 +1,21 @@ | ||
/** | ||
* Check if the value is a number. | ||
* | ||
* @param n Number as string or number | ||
* @returns true if the value is a number, false otherwise | ||
*/ | ||
export declare const isNumber: (n: string | number) => boolean; | ||
/** | ||
* Returns a number with significant figures. | ||
* Example: 3645.875 with 3 significant figures will return 3650. | ||
* | ||
* @param n The value | ||
* @param precision The number of significant figures | ||
*/ | ||
export declare const toPrecision: (n: number, precision?: number) => number; | ||
/** | ||
* Returns the number formatted with commas every thousand. | ||
* | ||
* @param n The value | ||
*/ | ||
export declare const toComma: (n: number) => string; | ||
@@ -48,2 +66,12 @@ export declare enum ConvertUnits { | ||
}; | ||
/** | ||
* Converts a value of unit into a different unit. | ||
* Depending on the destination unit, additional arguments might be provided. | ||
* | ||
* @param n The value to convert, usually a float or an integer. | ||
* @param fromUnit The unit the value is specified in. | ||
* @param toUnit The unit the converted value should be. | ||
* @param args Optional arguments to provide depending on the conversion. | ||
* @returns The converted value. | ||
*/ | ||
export declare const convertValue: (n: number, fromUnit: ConvertUnits, toUnit: ConvertUnits, args?: IConvertArgs) => number; |
@@ -21,2 +21,8 @@ "use strict"; | ||
exports.convertValue = exports.converters = exports.ConvertUnits = exports.toComma = exports.toPrecision = exports.isNumber = void 0; | ||
/** | ||
* Check if the value is a number. | ||
* | ||
* @param n Number as string or number | ||
* @returns true if the value is a number, false otherwise | ||
*/ | ||
var isNumber = function (n) { | ||
@@ -31,9 +37,25 @@ var _a; | ||
exports.isNumber = isNumber; | ||
/** | ||
* Returns a number with significant figures. | ||
* Example: 3645.875 with 3 significant figures will return 3650. | ||
* | ||
* @param n The value | ||
* @param precision The number of significant figures | ||
*/ | ||
var toPrecision = function (n, precision) { | ||
if (precision === void 0) { precision = 3; } | ||
var mult = Math.pow(10, precision - Math.floor(Math.log(n < 0 ? -n : n) / Math.LN10) - 1); | ||
var multiplier = +("" + mult).replace(/[0]([1-9])\1+/g, function (v) { return "" + (+(v.substring(0, 1)) + 1); }); | ||
var multiplier = Math.max( | ||
// handle floating errors like 0.00009999999999999999 | ||
+("" + mult).replace(/[0]([1-9])\1+/g, function (v) { return "" + (+(v.substring(0, 1)) + 1); }), | ||
// dividing by 0.00001 will give floating point error | ||
0.0001); | ||
return n === 0 ? 0 : Math.round(n * multiplier) / multiplier; | ||
}; | ||
exports.toPrecision = toPrecision; | ||
/** | ||
* Returns the number formatted with commas every thousand. | ||
* | ||
* @param n The value | ||
*/ | ||
var toComma = function (n) { | ||
@@ -164,2 +186,12 @@ var _a = __read(n.toString().split('.'), 2), numberPart = _a[0], decimalPart = _a[1]; | ||
_a); | ||
/** | ||
* Converts a value of unit into a different unit. | ||
* Depending on the destination unit, additional arguments might be provided. | ||
* | ||
* @param n The value to convert, usually a float or an integer. | ||
* @param fromUnit The unit the value is specified in. | ||
* @param toUnit The unit the converted value should be. | ||
* @param args Optional arguments to provide depending on the conversion. | ||
* @returns The converted value. | ||
*/ | ||
var convertValue = function (n, fromUnit, toUnit, args) { | ||
@@ -166,0 +198,0 @@ return exports.converters[fromUnit][toUnit](n, args); |
@@ -0,4 +1,22 @@ | ||
/** | ||
* Trims the string to a certain max length and replace with `...`. | ||
* | ||
* @param text The text to ellipsize. | ||
* @param maxlength The maximum length of the text (including `...`). | ||
* @returns | ||
*/ | ||
export declare const ellipsis: (text?: string, maxlength?: number) => string; | ||
export declare const keyToLabel: (key: string) => string; | ||
/** | ||
* Convert a Term ID into a dash string. Use it to generate link to documentation. | ||
* | ||
* @param value The original value. | ||
* @returns A dash case version of the value. | ||
*/ | ||
export declare const toDashCase: (value?: string) => string; | ||
/** | ||
* Returns current date in YYYY-MM-DD format. | ||
* | ||
* @returns Date as a string. | ||
*/ | ||
export declare const now: () => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.now = exports.toDashCase = exports.keyToLabel = exports.ellipsis = void 0; | ||
/** | ||
* Trims the string to a certain max length and replace with `...`. | ||
* | ||
* @param text The text to ellipsize. | ||
* @param maxlength The maximum length of the text (including `...`). | ||
* @returns | ||
*/ | ||
var ellipsis = function (text, maxlength) { | ||
@@ -14,6 +21,14 @@ if (text === void 0) { text = ''; } | ||
exports.keyToLabel = keyToLabel; | ||
/** | ||
* Convert a Term ID into a dash string. Use it to generate link to documentation. | ||
* | ||
* @param value The original value. | ||
* @returns A dash case version of the value. | ||
*/ | ||
var toDashCase = function (value) { | ||
return value | ||
? value | ||
// handle dates followed by capital letter | ||
.replace(/([\d]{4})([A-Z]{1})/g, function (g) { return g.substring(0, 4) + "-" + g[4].toLowerCase(); }) | ||
// handle molecules | ||
.replace(/([\d]{1}[A-Z]{1}?)([A-Z]{1})/g, function (_g) { | ||
@@ -26,4 +41,7 @@ var args = []; | ||
}) | ||
// handle all capital letters | ||
.replace(/([A-Z])/g, function (g) { return "-" + g[0].toLowerCase(); }) | ||
// handle years | ||
.replace(/([0-9]{4})/g, function (g) { return "-" + g; }) | ||
// handle underscores | ||
.replace(/[\_\.]/g, '-') | ||
@@ -33,3 +51,8 @@ : null; | ||
exports.toDashCase = toDashCase; | ||
/** | ||
* Returns current date in YYYY-MM-DD format. | ||
* | ||
* @returns Date as a string. | ||
*/ | ||
var now = function () { return new Date().toJSON().substring(0, 10); }; | ||
exports.now = now; |
export declare const arrayValue: (values?: any[], isAverage?: boolean) => number; | ||
/** | ||
* Calculate the final value of a property. | ||
* | ||
* @param value The value as an array or a string/number. | ||
* @param termId Optional - us if the term should handle an array in a specific way. | ||
* @returns The value as a number. | ||
*/ | ||
export declare const propertyValue: (value: string | number | ((string | number)[]), termId?: string) => number; | ||
/** | ||
* Checks if the value is empty or if the property value is empty. | ||
* | ||
* @param value | ||
* @returns | ||
*/ | ||
export declare const emptyValue: (value: any, termId?: string) => boolean; |
@@ -14,2 +14,9 @@ "use strict"; | ||
exports.arrayValue = arrayValue; | ||
/** | ||
* Calculate the final value of a property. | ||
* | ||
* @param value The value as an array or a string/number. | ||
* @param termId Optional - us if the term should handle an array in a specific way. | ||
* @returns The value as a number. | ||
*/ | ||
var propertyValue = function (value, termId) { | ||
@@ -23,3 +30,9 @@ return typeof value === 'undefined' || value === null | ||
exports.propertyValue = propertyValue; | ||
/** | ||
* Checks if the value is empty or if the property value is empty. | ||
* | ||
* @param value | ||
* @returns | ||
*/ | ||
var emptyValue = function (value, termId) { return utils_1.isEmpty(value) || isNaN(exports.propertyValue(value, termId)); }; | ||
exports.emptyValue = emptyValue; |
@@ -5,4 +5,10 @@ declare class Term { | ||
} | ||
/** | ||
* Validate a list of Terms. | ||
* | ||
* @param terms The list of Terms to validate. | ||
* @returns The list of ids/names that could not be found (invalid). | ||
*/ | ||
export declare const validateTerms: (terms: Term[]) => Promise<(string | true)[]>; | ||
export declare const run: () => Promise<string[]>; | ||
export {}; |
@@ -69,3 +69,3 @@ "use strict"; | ||
var encoding = 'utf8'; | ||
var Term = (function () { | ||
var Term = /** @class */ (function () { | ||
function Term() { | ||
@@ -95,22 +95,22 @@ } | ||
_b = (_a = Promise).all; | ||
return [4, readdir(directory)]; | ||
case 1: return [4, _b.apply(_a, [(_c.sent()).map(function (entry) { return __awaiter(void 0, void 0, void 0, function () { | ||
return [4 /*yield*/, readdir(directory)]; | ||
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).map(function (entry) { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4, lstat(path_1.join(directory, entry))]; | ||
case 0: return [4 /*yield*/, lstat(path_1.join(directory, entry))]; | ||
case 1: | ||
if (!(_b.sent()).isDirectory()) return [3, 3]; | ||
return [4, recursiveFiles(path_1.join(directory, entry))]; | ||
if (!(_b.sent()).isDirectory()) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, recursiveFiles(path_1.join(directory, entry))]; | ||
case 2: | ||
_a = _b.sent(); | ||
return [3, 4]; | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_a = (path_1.join(directory, entry).endsWith('.jsonld') ? [path_1.join(directory, entry)] : []); | ||
_b.label = 4; | ||
case 4: return [2, _a]; | ||
case 4: return [2 /*return*/, _a]; | ||
} | ||
}); | ||
}); })])]; | ||
case 2: return [2, (_c.sent()).flat()]; | ||
case 2: return [2 /*return*/, (_c.sent()).flat()]; | ||
} | ||
@@ -129,2 +129,3 @@ }); | ||
}; | ||
// search is limited to 1000 parameters | ||
var searchLimit = 500; | ||
@@ -137,4 +138,4 @@ var searchTerms = function (terms) { return __awaiter(void 0, void 0, void 0, function () { | ||
searchedTerms = terms.slice(0, searchLimit); | ||
if (!searchedTerms.length) return [3, 2]; | ||
return [4, search({ | ||
if (!searchedTerms.length) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, search({ | ||
limit: searchLimit, | ||
@@ -158,3 +159,3 @@ fields: ['@id', 'name'], | ||
_a = _d.sent(); | ||
return [3, 3]; | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
@@ -166,14 +167,20 @@ _a = { results: [] }; | ||
_b = [__spreadArray([], __read(results))]; | ||
if (!(terms.length > searchLimit)) return [3, 5]; | ||
return [4, searchTerms(terms.slice(searchLimit))]; | ||
if (!(terms.length > searchLimit)) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, searchTerms(terms.slice(searchLimit))]; | ||
case 4: | ||
_c = _d.sent(); | ||
return [3, 6]; | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
_c = []; | ||
_d.label = 6; | ||
case 6: return [2, __spreadArray.apply(void 0, _b.concat([__read.apply(void 0, [(_c)])]))]; | ||
case 6: return [2 /*return*/, __spreadArray.apply(void 0, _b.concat([__read.apply(void 0, [(_c)])]))]; | ||
} | ||
}); | ||
}); }; | ||
/** | ||
* Validate a list of Terms. | ||
* | ||
* @param terms The list of Terms to validate. | ||
* @returns The list of ids/names that could not be found (invalid). | ||
*/ | ||
var validateTerms = function (terms) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -183,6 +190,6 @@ var results; | ||
switch (_a.label) { | ||
case 0: return [4, searchTerms(terms)]; | ||
case 0: return [4 /*yield*/, searchTerms(terms)]; | ||
case 1: | ||
results = _a.sent(); | ||
return [2, terms.map(function (_a) { | ||
return [2 /*return*/, terms.map(function (_a) { | ||
var id = _a["@id"], name = _a.name; | ||
@@ -219,4 +226,4 @@ var exists = results.find(function (res) { return res['@id'] === id || res.name === name; }); | ||
_c = (_b = JSON).parse; | ||
return [4, readFile(filepath, encoding)]; | ||
case 1: return [2, (_d.terms = _a.apply(void 0, [_c.apply(_b, [_e.sent()])]), | ||
return [4 /*yield*/, readFile(filepath, encoding)]; | ||
case 1: return [2 /*return*/, (_d.terms = _a.apply(void 0, [_c.apply(_b, [_e.sent()])]), | ||
_d)]; | ||
@@ -230,6 +237,6 @@ } | ||
switch (_a.label) { | ||
case 0: return [4, recursiveFiles(folder)]; | ||
case 0: return [4 /*yield*/, recursiveFiles(folder)]; | ||
case 1: | ||
filepaths = _a.sent(); | ||
return [4, Promise.all(filepaths.map(loadFile))]; | ||
return [4 /*yield*/, Promise.all(filepaths.map(loadFile))]; | ||
case 2: | ||
@@ -241,3 +248,3 @@ fileData = _a.sent(); | ||
})); | ||
return [4, searchTerms(allTerms)]; | ||
return [4 /*yield*/, searchTerms(allTerms)]; | ||
case 3: | ||
@@ -256,3 +263,3 @@ existingTerms = _a.sent(); | ||
} | ||
return [2, filepaths]; | ||
return [2 /*return*/, filepaths]; | ||
} | ||
@@ -259,0 +266,0 @@ }); |
{ | ||
"name": "@hestia-earth/utils", | ||
"version": "0.10.30", | ||
"version": "0.10.31", | ||
"description": "Hestia Utils library", | ||
@@ -28,4 +28,4 @@ "main": "dist/index.js", | ||
], | ||
"author": "Guillaume Royer <guillaumeroyer@gmail.com>", | ||
"license": "GPL-3.0-or-later", | ||
"author": "Guillaume Royer <guillaume@hestia.earth>", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -32,0 +32,0 @@ "url": "https://gitlab.com/hestia-earth/hestia-utils/issues" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
44429
0
100
1080