wrappedjs
Version:
This package provides wrapped versions of basic JavaScript blocks like switch or try-catch, making them usable in a more inline fashion for flexible usage inside function parameters or chained calls. I created it out of frustration with not being able to
87 lines (86 loc) • 4.03 kB
JavaScript
;
/**
* Inline tools for JavaScript and TypeScript.
*
* This file contains a set of functions that try to improve basic JavaScript code blocks by wrapping or mimicking its behaviour and making them more readable or usable.
*
* The focus is on making these blocks usable in a more inline fashion, allowing them to be used directly inside arguments or on the return of a function, or simply used in a single line of code.
*
* @author Miguel <miguel12105marcos@gmail.com>
* @version 1.0.0
* @license MIT License
*
* @repository https://github.com/mriioos/snippets/tree/main/libs/wrappedjs
*
* @note Feel free to use, modify, and distribute it under the terms of the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.try_catch = try_catch;
exports.select = select;
/**
* MIT License
*
* Copyright (c) 2024 Miguel Ríos Marcos
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following condition:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Wrapper function for a try-catch block.
* Helps you escape the 5 lines minimum size of a try-catch hell with Promises.
* Simplifies handling both async functions and promises by returning the result or error in a tuple.
*
* @param func_or_promise A function returning a promise or a direct promise to be awaited for.
* @returns Array containing both:
* - The error of the catch (first element).
* - The result of a successful try (second element)
*
* Note: Even with all of this boilerplate code solved, I still can't debug those errors for you, sorry. As the prophecy tells: "One day, all errors will be autodebugged". So cheer up, have a good coding!
*/
async function try_catch(func_or_promise) {
try {
const promise = typeof func_or_promise === 'function' ? func_or_promise() : func_or_promise;
const result = await promise;
return [null, result];
}
catch (err) {
return [err, null];
}
}
function select(key, switcher, separator) {
// Return a function called 'from' (to select-from) that accepts the switcher if it is not defined
if (!switcher)
return { from: (switcher, separator) => select(key, switcher, separator) }; // T can be casted because switcher is defined
// Get value directly if separator is not defined
if (!separator)
return switcher[key] ?? switcher['default'];
// Check if key can be accessed directly without using the separator
if (key in switcher)
return switcher[key];
// Find first key in switcher that includes the search-by key if separator is defined
const found_key = Object.keys(switcher).find((switcher_key) => {
// Only split key by separator if it is a string
if (typeof switcher_key === 'string') {
// Check if case key is included in switcher_key
return switcher_key.split(separator).includes(key);
}
// Manage non-string keys by comparing them directly
return switcher_key === key;
});
return switcher[found_key ?? 'default'];
}