utils
recaptcha_check
¶
Checks the validity of a reCAPTCHA response.
Parameters
Name | Type | Description |
---|---|---|
captcha |
any | The |
Returns: A Promise that resolves to the reCAPTCHA verification result.
md5
¶
This function converts txt
into an MD5 string.
If do_check
is true, the original string is checked against a Regular Expression to
verify if it is already an MD5 string, and (in that case) it is just returned without hashing.
Parameters
Name | Type | Description |
---|---|---|
txt |
any | The |
do_check |
any | If |
Returns: ``
md5File
¶
Calculates the MD5 hash of a file.
Parameters
Name | Type | Description |
---|---|---|
fname |
any | The path to the file. |
Returns: The MD5 hash of the file.
sha512
¶
Calculates the SHA512 hash of a given string.
Parameters
Name | Type | Description |
---|---|---|
txt |
any | The string to be hashed. |
do_check |
any | Optional. Specifies whether to check if the input string is already a valid SHA512 hash. Default is true. |
Returns: The SHA512 hash of the input string.
send_error
¶
This function returns an error to Express
Parameters
Name | Type | Description |
---|---|---|
req The |
any | Express |
error |
any | an |
error_code |
any | the |
Returns: ``
send_ok
¶
This function is used when an Express request succedees
Parameters
Name | Type | Description |
---|---|---|
res |
any | the |
payload |
any | the |
status_code |
any | the |
Returns: ``
send_html
¶
This function returns a simple HTML instead of the standard JSON returned by send_ok()
Parameters
Name | Type | Description |
---|---|---|
res |
any | the |
payload |
any | the |
status_code |
any | the |
Returns: ``
send_binary
¶
Sends a binary response with the specified buffer, content type, and filename.
Parameters
Name | Type | Description |
---|---|---|
res |
any | The express response object. |
buffer |
any | The binary buffer to send. |
content_type |
any | The content type of the response. |
filename |
any | The filename for the attachment. |
Returns: ``
rand_int
¶
Generates a random integer number from min
to max
Parameters
Name | Type | Description |
---|---|---|
min |
any | Random |
max |
any | Random |
Returns: an integer between the specified min / max range
unique_code
¶
unique_code = ( simple: boolean = true, prefix: string = null, second_slice: boolean = true ): string
Generates an unique string code (up to 37 chars long)
Parameters
Name | Type | Description |
---|---|---|
simple If |
any | the |
prefix The |
any | prefix |
second_slice If |
any | a |
Returns: the unique string generated
unique_code_numbers
¶
Generates a unique code number string based on the current timestamp.
Parameters
Name | Type | Description |
---|---|---|
length |
number | The length of the code number string to generate. |
[second_slice=0] |
number | The length of the second slice to append to the code number string. |
Returns: {string} - The unique code number string.
mkid
¶
@description This function returns an unique id, the id starts with the prefix and can optionally contain an extension
Parameters
Name | Type | Description |
---|---|---|
prefix |
any | The prefix to add to the string |
ext |
any | The extension to add to the string |
Returns: ``
random_string
¶
returns a random string of specified length
using a randomizer of iterations
.
Parameters
Name | Type | Description |
---|---|---|
length: |
any | |
iterations: |
any | the |
Returns: the created random string
fetch_file
¶
Fetch a file from the given url
into dest_local_path
Parameters
Name | Type | Description |
---|---|---|
url |
any | |
dest_local_path |
any | The |
Returns: ``
jwt_crypt
¶
Generates a JWT token with the provided payload, secret, and expiration time.
Parameters
Name | Type | Description |
---|---|---|
payload |
any | The data to be included in the token. |
secret |
any | The secret key used to sign the token. |
expires |
any | The expiration time for the token in seconds. |
Returns: The generated JWT token.
jwt_decrypt
¶
Decrypts a JWT token using the provided secret.
Parameters
Name | Type | Description |
---|---|---|
tok |
any | The JWT token to decrypt. |
secret |
any | The secret used to decrypt the JWT token. |
Returns: The decrypted payload if the token is valid, otherwise null.
delete_folder
¶
Deletes a folder and all its contents recursively.
Parameters
Name | Type | Description |
---|---|---|
path |
any | The path of the folder to delete. |
Returns: ``
shell
¶
Executes a shell command asynchronously.
Parameters
Name | Type | Description |
---|---|---|
command |
any | The shell command to execute. |
cback |
any | Optional callback function to handle the result or error. |
Returns: A promise that resolves with the result of the command or rejects with an error.
progressive_fetch_file
¶
Download the file specified by the given url
using progressive events
The function gets 3 cbacks:
-
update_cback ( chunk, perc, size, total )
-
chunk: the chunk to write ( eg. fs.syncWrite ( fd, chunk, 0, chunk.len ) );
- perc: the percentual of the file downloaded (float)
- size: the size (in MB) of the file downloaded
-
total: total size in MB
-
end_cback ()
This cback is called at the end of the download
- err_cback ( err )
This cback is called when something goes wrong
Returns: ``
template_render
¶
Renders a template file with Handlebars syntax
Parameters
Name | Type | Description |
---|---|---|
template_full_path |
str | Full path for the file containing the template |
dct |
object | An object with all key / values needed |
Returns: ``
typed_dict
¶
Converts a dictionary object into a typed object based on the provided field descriptions.
Parameters
Name | Type | Description |
---|---|---|
dct |
any | The |
fields_descr |
any | The |
Returns: The typed object with converted values and error information.
const isValidDate
¶
Checks if a given date is valid.
Parameters
Name | Type | Description |
---|---|---|
d |
any | The date to be checked. |
Returns: A boolean indicating whether the date is valid or not.
date_format
¶
converts a date into a string with the desired format
yyyy - year mm - month dd - day
HH - hour MM - minutes SS - seconds
Returns: ``
isValidEmail
¶
Checks if the given email is valid.
Parameters
Name | Type | Description |
---|---|---|
email |
any | The email to be validated. |
Returns: True if the email is valid, false otherwise.
int
¶
Converts the input value to an integer. If the input value is falsy or undefined, returns 0.
Parameters
Name | Type | Description |
---|---|---|
s |
any | The value to convert to an integer. |
Returns: The converted integer value.
float
¶
Converts a value to a floating-point number. If the value is falsy, returns 0.0.
Parameters
Name | Type | Description |
---|---|---|
s |
any | The value to convert. |
Returns: The converted floating-point number.
keys_remove
¶
remove keys specified by keys
in obj
This is a 'change in place' function.
The object is modified in memory
Returns: ``
set_attr
¶
sets an attribute to obj
only if val
is not undefined.
Returns: ``
set_attrs
¶
sets multiple attributes to obj
only if val
is not undefined.
Returns: ``
keys_filter
¶
Filters the keys of an object obj
based on the
specified fields in type_def
.
If a key is not in type_def
, it is removed from obj
.
Returns: ``
get_date
¶
Returns a date in international format 'YYYY-MM-DD'
Returns: ``
list_add
¶
adds a new element (el) to a string list (lst)
only if el
does not exists in lst
Parameters
Name | Type | Description |
---|---|---|
lst the |
any | string |
el element |
any | to |
Returns: the new modified list
list_del
¶
removes an element (el) from the list (lst)
Parameters
Name | Type | Description |
---|---|---|
lst the |
any | string |
el element |
any | to |
Returns: the new modified list
keys_valid
¶
returns a new object with only the keys that are not undefined
Parameters
Name | Type | Description |
---|---|---|
dct the |
any | object |
Returns: a new object with valid keys
export function isObject ( item: any )
¶
Simple object check.
Parameters
Name | Type | Description |
---|---|---|
item |
any |
Returns: {boolean}
export function merge ( target: any, ...sources: any[] ): any
¶
Deep merge two objects.
Parameters
Name | Type | Description |
---|---|---|
target |
any | |
...sources |
any |
Returns: ``
list_random_pick
¶
Picks a random element from the given list.
Parameters
Name | Type | Description |
---|---|---|
lst |
any | The list to pick from. |
Returns: The randomly picked element from the list.
list_random_pick_n
¶
Picks n random elements from the given list.
Parameters
Name | Type | Description |
---|---|---|
lst |
any | The list from which to pick random elements. |
n |
any | The number of random elements to pick. |
Returns: An array containing n random elements from the list.
challenge_create
¶
takes a list of strings and returns a valid challenge
Returns: ``
challenge_check
¶
takes a list of strings and check it agains the provided challenge
Parameters
Name | Type | Description |
---|---|---|
challenge the |
any | challenge |
params the |
any | list |
Returns: true if the challenge is valid
slugify
¶
Converts a string into a slug by removing special characters, converting to lowercase, and replacing spaces with dashes.
Parameters
Name | Type | Description |
---|---|---|
str |
any | The string to be slugified. |
Returns: The slugified string.
decimalToBase96
¶
Converts a decimal number to a base-96 string representation.
Parameters
Name | Type | Description |
---|---|---|
decimal |
any | The |
Returns: The base-96 string representation of the decimal number.
base96ToDecimal
¶
Converts a base96 number to decimal.
Parameters
Name | Type | Description |
---|---|---|
base96 |
any | The base96 number to convert. |
Returns: The decimal representation of the base96 number.
formatCurrency
¶
Formats a number as a currency string.
Parameters
Name | Type | Description |
---|---|---|
number |
any | The number to format. |
options |
any | Optional formatting options. |
options.thousandSeparator |
any | The character used as a thousand separator. Default is '.'. |
options.decimalSeparator |
any | The character used as a decimal separator. Default is ','. |
Returns: The formatted currency string.