Inline tools for JavaScript and TypeScript.
This package provides wrapped versions of basic JavaScript code blocks like switch
and try-catch
, making them usable in a more inline fashion for flexible usage inside function parameters, chained calls, or simply in a single line of code. I created it out of frustration with not being able to use switch inline—hope it softens your coding experience :).
You can install the package via npm:
npm install wrappedjs
-
Inline
switch
statement: Use aswitch
-like behavior with more flexibility, including a separator for grouping cases together. -
Inline
try-catch
block: A wrapped version oftry-catch
that simplifies error handling for async functions or promises.
This function acts as a flexible switch-case
statement and can be used inline, similar to a ternary operator. It allows you to define multiple keys for a single case using a separator.
const result = select('case1', {
'case1:case2': 'value1',
'case3': 'value2'
}, ':');
console.log(result); // Output: 'value1'
const result = select('case1', {
'case1': 'value1',
'case2': 'value1',
'case3': 'value2'
});
console.log(result); // Output: 'value1'
const result = select('case1')
.from({
'case1': 'value1',
'case2': 'value2'
});
console.log(result); // Output: 'value1'
This function wraps your try-catch
block, making it more concise and easier to use inline. It returns a tuple containing either the result or the error.
const [error, result] = await try_catch(async () => {
// Some async code that might fail
return await fetchData();
});
if (error) {
console.error('Error:', error);
} else {
console.log('Result:', result);
}
MIT License. Feel free to use, modify, and distribute this package under the terms of the MIT License.
Miguel Ríos Marcos miguel12105marcos@gmail.com
GitHub: @mriioos
npm: @miguelrios