Add input type checks

This commit is contained in:
JeremyStar™ 2024-10-27 22:04:37 +01:00
parent b706526e99
commit 5be44a87bf
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 48 additions and 0 deletions

View file

@ -164,6 +164,23 @@ function is_command_file() { _bashutils_reset_environment; [[ "$(type -t "${1}")
## => Arguments: <str:command name>
## => Returns: 0 if it is, 1 otherwise
function is_command_defined() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == "" ]]; _bashutils_return_environment ${?}; }
## Input type
## => Checks whether the provided input is a bool.
## => Arguments: <str:input>
## => Returns: 0 if it is, 1 otherwise
function is_input_bool() { _bashutils_reset_environment; [ "${1}" == "true" ] || [ "${1}" == "false" ]; _bashutils_return_environment "${?}"; }
## => Checks whether the provided input is a byte.
## => Arguments: <str:input>
## => Returns: 0 if it is, 1 otherwise
function is_input_byte() { _bashutils_reset_environment; [[ "${1}" =~ ^[0-9]+$ ]] && [[ "${1}" -gt -1 ]] && [["${1}" -lt 256 ]]; _bashutils_return_environment "${?}"; }
## => Checks whether the provided input is a integer/number.
## => Arguments: <str:input>
## => Returns: 0 if it is, 1 otherwise
function is_input_int() { _bashutils_reset_environment; [[ "${1}" =~ ^[0-9]+$ ]]; _bashutils_return_environment "${?}"; }
## => Checks whether the provided input is a char.
## => Arguments: <str:input>
## => Returns: 0 if it is, 1 otherwise
function is_input_char() { _bashutils_reset_environment; [[ "${1}" =~ ^.$ ]]; _bashutils_return_environment "${?}"; }
# Variable definition
## => Sets the specified variable to the specified value if it is undefined.
@ -185,3 +202,4 @@ set_undefined "BASHUTILS_LOGLEVEL" "3"
################################
### BASHUTILS CODE ENDS HERE ###
################################

View file

@ -57,3 +57,33 @@ These checks check the type of the passed command and return `0` if it matches o
<td><code>is_command_defined &lt;str:command name&gt;</code></td>
</tr>
</table>
## Input type
These checks check the type of the specified input and return `0` if the type matches or `1` if it isn't.
<table>
<tr>
<td>Checks for</td>
<td>Usage</td>
</tr>
<tr>
<td><code>bool</code></td>
<td><code>is_input_bool &lt;?:input&gt;</code></td>
</tr>
<tr>
<td><code>byte</code></td>
<td><code>is_input_byte &lt;?:input&gt;</code></td>
</tr>
<tr>
<td><code>int</code></td>
<td><code>is_input_int &lt;?:input&gt;</code></td>
</tr>
<tr>
<td><code>char</code></td>
<td><code>is_input_char &lt;?:input&gt;</code></td>
</tr>
<tr>
<td><code>str</code></td>
<td>Are you serious?</td>
</tr>
</table>