50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
const require_conditions = require('./conditions.cjs');
|
|
const require_console_styling = require('./console-styling.cjs');
|
|
const require_errors = require('./errors.cjs');
|
|
const require_json_schema = require('./json-schema.cjs');
|
|
const require_random_id = require('./random-id.cjs');
|
|
const require_requests = require('./requests.cjs');
|
|
|
|
//#region src/utils/index.ts
|
|
/**
|
|
* Safely parses a JSON string into an object
|
|
* @param json The JSON string to parse
|
|
* @param fallback Optional fallback value to return if parsing fails. If not provided or set to "unset", returns null
|
|
* @returns The parsed JSON object, or the fallback value (or null) if parsing fails
|
|
*/
|
|
function parseJson(json, fallback = "unset") {
|
|
try {
|
|
return JSON.parse(json);
|
|
} catch (e) {
|
|
return fallback === "unset" ? null : fallback;
|
|
}
|
|
}
|
|
/**
|
|
* Maps an array of items to a new array, skipping items that throw errors during mapping
|
|
* @param items The array to map
|
|
* @param callback The mapping function to apply to each item
|
|
* @returns A new array containing only the successfully mapped items
|
|
*/
|
|
function tryMap(items, callback) {
|
|
return items.reduce((acc, item, index, array) => {
|
|
try {
|
|
acc.push(callback(item, index, array));
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
return acc;
|
|
}, []);
|
|
}
|
|
/**
|
|
* Checks if the current environment is macOS
|
|
* @returns {boolean} True if running on macOS, false otherwise
|
|
*/
|
|
function isMacOS() {
|
|
return /Mac|iMac|Macintosh/i.test(navigator.userAgent);
|
|
}
|
|
|
|
//#endregion
|
|
exports.isMacOS = isMacOS;
|
|
exports.parseJson = parseJson;
|
|
exports.tryMap = tryMap;
|
|
//# sourceMappingURL=index.cjs.map
|