2023-11-28 15:24:41 +01:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const FALSE_VALUES = [
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
'0',
|
|
|
|
'f',
|
|
|
|
'F',
|
|
|
|
'false',
|
|
|
|
'FALSE',
|
|
|
|
'off',
|
|
|
|
'OFF',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} value
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export function isTruthy(value) {
|
|
|
|
return value && !FALSE_VALUES.includes(value);
|
|
|
|
}
|
2024-01-22 11:02:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See app/lib/ascii_folder.rb for the canon definitions
|
|
|
|
* of these constants
|
|
|
|
*/
|
|
|
|
const NON_ASCII_CHARS = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž';
|
|
|
|
const EQUIVALENT_ASCII_CHARS = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export function foldToASCII(str) {
|
2024-01-22 11:02:26 +01:00
|
|
|
const regex = new RegExp(NON_ASCII_CHARS.split('').join('|'), 'g');
|
|
|
|
|
|
|
|
return str.replace(regex, function(match) {
|
|
|
|
const index = NON_ASCII_CHARS.indexOf(match);
|
|
|
|
return EQUIVALENT_ASCII_CHARS[index];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export function normalizeHashtag(str) {
|
2024-01-22 11:02:26 +01:00
|
|
|
return foldToASCII(str.normalize('NFKC').toLowerCase()).replace(/[^\p{L}\p{N}_\u00b7\u200c]/gu, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string|string[]} arrayOrString
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export function firstParam(arrayOrString) {
|
2024-01-22 11:02:26 +01:00
|
|
|
if (Array.isArray(arrayOrString)) {
|
|
|
|
return arrayOrString[0];
|
|
|
|
} else {
|
|
|
|
return arrayOrString;
|
|
|
|
}
|
|
|
|
}
|
2024-08-27 10:40:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an environment variable that should be an integer, attempts to parse
|
|
|
|
* it falling back to a default if not set, and handles errors parsing.
|
|
|
|
* @param {string|undefined} value
|
|
|
|
* @param {number} defaultValue
|
|
|
|
* @param {string} variableName
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
export function parseIntFromEnvValue(value, defaultValue, variableName) {
|
|
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
|
|
const parsedValue = parseInt(value, 10);
|
|
|
|
if (isNaN(parsedValue)) {
|
|
|
|
throw new Error(`Invalid ${variableName} environment variable: ${value}`);
|
|
|
|
}
|
|
|
|
return parsedValue;
|
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|