From 7fc96eb263b4be2872611edf54ca6c969187ee1c Mon Sep 17 00:00:00 2001 From: Thomas Cruveilher <38007824+Sorikairox@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:45:30 +0000 Subject: [PATCH] docs: JSDoc --- behaviors/common/alpha.ts | 138 +++++++++++++++++++++++++- behaviors/common/contains.ts | 8 ++ behaviors/common/equals.ts | 8 ++ behaviors/common/isAfter.ts | 7 ++ behaviors/common/isAlpha.ts | 8 ++ behaviors/common/isAlphanumeric.ts | 8 ++ behaviors/common/isAscii.ts | 6 ++ behaviors/common/isBIC.ts | 6 ++ behaviors/common/isBase32.ts | 11 ++ behaviors/common/isBase64.ts | 7 ++ behaviors/common/isBefore.ts | 7 ++ behaviors/common/isBoolean.ts | 12 +++ behaviors/common/isBtcAddress.ts | 6 ++ behaviors/common/isByteLength.ts | 7 ++ behaviors/common/isCreditCard.ts | 9 ++ behaviors/common/isCurrency.ts | 7 ++ behaviors/common/isDataURI.ts | 16 +++ behaviors/common/isDate.ts | 7 ++ behaviors/common/isDecimal.ts | 11 ++ behaviors/common/isDivisibleBy.ts | 7 ++ behaviors/common/isEmail.ts | 31 ++++++ behaviors/common/isEmpty.ts | 7 ++ behaviors/common/isEthereumAddress.ts | 6 ++ behaviors/common/isFloat.ts | 12 +++ behaviors/common/util/includes.ts | 7 ++ behaviors/common/util/merge.ts | 8 ++ behaviors/common/util/toString.ts | 7 ++ behaviors/number.ts | 35 ++++++- behaviors/phone-number.ts | 7 ++ behaviors/string.ts | 39 ++++++++ transform.ts | 15 +++ validate.ts | 3 + 32 files changed, 468 insertions(+), 5 deletions(-) diff --git a/behaviors/common/alpha.ts b/behaviors/common/alpha.ts index 9108b07..911aaa6 100644 --- a/behaviors/common/alpha.ts +++ b/behaviors/common/alpha.ts @@ -1,3 +1,39 @@ +/** + * A collection of regular expressions for validating alphabetic characters + * in various languages and locales. + * + * The keys represent the locale codes, and the values are the corresponding + * regular expressions that match alphabetic characters for that locale. + * + * Supported locales include: + * - Arabic (`ar`) + * - Bulgarian (`bg-BG`) + * - Czech (`cs-CZ`) + * - Danish (`da-DK`) + * - German (`de-DE`) + * - Greek (`el-GR`) + * - English (`en-US`) + * - Spanish (`es-ES`) + * - Persian (`fa-IR`) + * - French (`fr-FR`) + * - Hebrew (`he`) + * - Hungarian (`hu-HU`) + * - Italian (`it-IT`) + * - Kurdish (`ku-IQ`) + * - Norwegian Bokmål (`nb-NO`) + * - Dutch (`nl-NL`) + * - Norwegian Nynorsk (`nn-NO`) + * - Polish (`pl-PL`) + * - Portuguese (`pt-PT`) + * - Russian (`ru-RU`) + * - Slovak (`sk-SK`) + * - Slovenian (`sl-SI`) + * - Serbian Cyrillic (`sr-RS`) + * - Serbian Latin (`sr-RS@latin`) + * - Swedish (`sv-SE`) + * - Turkish (`tr-TR`) + * - Ukrainian (`uk-UA`) + */ export const alpha: { [key: string]: RegExp } = { ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/, 'bg-BG': /^[А-Я]+$/i, @@ -28,6 +64,41 @@ export const alpha: { [key: string]: RegExp } = { 'uk-UA': /^[А-ЩЬЮЯЄIЇҐі]+$/i, }; +/** + * A collection of regular expressions for validating alphanumeric characters + * across various locales. Each key represents a locale identifier, and the + * corresponding value is a regular expression that matches alphanumeric + * characters specific to that locale. + * + * Supported locales include: + * - 'en-US': English (United States) + * - 'bg-BG': Bulgarian + * - 'cs-CZ': Czech + * - 'da-DK': Danish + * - 'de-DE': German + * - 'el-GR': Greek + * - 'es-ES': Spanish + * - 'fr-FR': French + * - 'it-IT': Italian + * - 'hu-HU': Hungarian + * - 'nb-NO': Norwegian Bokmål + * - 'nl-NL': Dutch + * - 'nn-NO': Norwegian Nynorsk + * - 'pl-PL': Polish + * - 'pt-PT': Portuguese + * - 'ru-RU': Russian + * - 'sl-SI': Slovenian + * - 'sk-SK': Slovak + * - 'sr-RS@latin': Serbian (Latin script) + * - 'sr-RS': Serbian (Cyrillic script) + * - 'sv-SE': Swedish + * - 'tr-TR': Turkish + * - 'uk-UA': Ukrainian + * - 'ku-IQ': Kurdish (Iraq) + * - 'ar': Arabic + * - 'he': Hebrew + * - 'fa-IR': Persian (Iran) + */ export const alphanumeric: { [key: string]: RegExp } = { 'en-US': /^[0-9A-Z]+$/i, 'bg-BG': /^[0-9А-Я]+$/i, @@ -58,11 +129,34 @@ export const alphanumeric: { [key: string]: RegExp } = { 'fa-IR': /^['0-9آابپتثجچهخدذرزژسشصضطظعغفقکگلمنوهی۱۲۳۴۵۶۷۸۹۰']+$/i, }; +/** + * An object that maps locale identifiers to their corresponding decimal separator symbols. + * + * @example + * ```typescript + * import { decimal } from './alpha'; + * + * console.log(decimal['en-US']); // Output: '.' + * console.log(decimal['ar']); // Output: '٫' + * ``` + */ export const decimal: { [key: string]: string } = { 'en-US': '.', ar: '٫', }; +/** + * An array of strings representing English-speaking locales. + * + * The locales included are: + * - 'AU' (Australia) + * - 'GB' (United Kingdom) + * - 'HK' (Hong Kong) + * - 'IN' (India) + * - 'NZ' (New Zealand) + * - 'ZA' (South Africa) + * - 'ZM' (Zambia) + */ export const englishLocales: Array = [ 'AU', 'GB', @@ -81,6 +175,28 @@ for (let locale: string, i = 0; i < englishLocales.length; i++) { } // Source: http://www.localeplanet.com/java/ +/** + * An array of strings representing the locales for Arabic-speaking countries. + * http://www.localeplanet.com/java/ + * The locales included are: + * - AE: United Arab Emirates + * - BH: Bahrain + * - DZ: Algeria + * - EG: Egypt + * - IQ: Iraq + * - JO: Jordan + * - KW: Kuwait + * - LB: Lebanon + * - LY: Libya + * - MA: Morocco + * - QM: Qatar + * - QA: Qatar + * - SA: Saudi Arabia + * - SD: Sudan + * - SY: Syria + * - TN: Tunisia + * - YE: Yemen + */ export const arabicLocales: Array = [ 'AE', 'BH', @@ -108,8 +224,28 @@ for (let locale: string, i = 0; i < arabicLocales.length; i++) { decimal[locale] = decimal.ar; } -// Source: https://en.wikipedia.org/wiki/Decimal_mark +/** + * An array of locale strings that use dot as the decimal separator. + * Source: https://en.wikipedia.org/wiki/Decimal_mark + * This array includes the following locales: + * - ar-EG: Arabic (Egypt) + * - ar-LB: Arabic (Lebanon) + * - ar-LY: Arabic (Libya) + */ export const dotDecimal: Array = ['ar-EG', 'ar-LB', 'ar-LY']; +/** + * An array of locale strings that use a comma as the decimal separator. + * + * This array includes locales from various regions and languages where + * the comma is used instead of a period for decimal separation. + * + * Example locales: + * - 'de-DE' for German (Germany) + * - 'fr-FR' for French (France) + * - 'es-ES' for Spanish (Spain) + * + * @constant {Array} commaDecimal + */ export const commaDecimal: Array = [ 'bg-BG', 'cs-CZ', diff --git a/behaviors/common/contains.ts b/behaviors/common/contains.ts index 767f80a..1e550aa 100644 --- a/behaviors/common/contains.ts +++ b/behaviors/common/contains.ts @@ -8,6 +8,14 @@ export const defaultContainsOptions: ContainsOptions = { ignoreCase: false, }; +/** + * Checks if a string contains a specified substring, with optional case insensitivity. + * + * @param str - The string to search within. + * @param elem - The substring to search for. + * @param options - Optional settings to modify the behavior of the search. + * @returns `true` if the substring is found within the string, otherwise `false`. + */ export function contains( str: string, elem: string, diff --git a/behaviors/common/equals.ts b/behaviors/common/equals.ts index 3183cb4..4c0343f 100644 --- a/behaviors/common/equals.ts +++ b/behaviors/common/equals.ts @@ -10,6 +10,14 @@ const defaultEqualsOptions: EqualsOptions = { ignoreCase: false, }; +/** + * Compares two strings for equality with optional configurations. + * + * @param str1 - The first string to compare. + * @param str2 - The second string to compare. + * @param options - Optional configuration for comparison. + * @returns `true` if the strings are considered equal based on the options, otherwise `false`. + */ export function equals( str1: string, str2: string, diff --git a/behaviors/common/isAfter.ts b/behaviors/common/isAfter.ts index 3550723..1c9406f 100644 --- a/behaviors/common/isAfter.ts +++ b/behaviors/common/isAfter.ts @@ -1,5 +1,12 @@ import { toDate } from './toDate.ts'; +/** + * Checks if the given date string is after the comparison date string. + * + * @param str - The date string to be checked. + * @param date - The comparison date string. Defaults to the current date and time. + * @returns `true` if the date represented by `str` is after the date represented by `date`, otherwise `false`. + */ export function isAfter( str: string, date: string = `${new Date()}`, diff --git a/behaviors/common/isAlpha.ts b/behaviors/common/isAlpha.ts index 3607d9d..a92a7a1 100644 --- a/behaviors/common/isAlpha.ts +++ b/behaviors/common/isAlpha.ts @@ -1,5 +1,13 @@ import { alpha } from './alpha.ts'; +/** + * Checks if the given string contains only alphabetic characters based on the specified locale. + * + * @param str - The string to be checked. + * @param locale - The locale to be used for the alphabetic check. Defaults to 'en-US'. + * @returns `true` if the string contains only alphabetic characters for the specified locale, otherwise `false`. + * @throws Will throw an error if the specified locale is not supported. + */ export function isAlpha( str: string, locale = 'en-US', diff --git a/behaviors/common/isAlphanumeric.ts b/behaviors/common/isAlphanumeric.ts index 2e69fba..b930ceb 100644 --- a/behaviors/common/isAlphanumeric.ts +++ b/behaviors/common/isAlphanumeric.ts @@ -1,5 +1,13 @@ import { alphanumeric } from './alpha.ts'; +/** + * Checks if a given string is alphanumeric according to the specified locale. + * + * @param str - The string to be checked. + * @param locale - The locale to be used for the alphanumeric check. Defaults to 'en-US'. + * @returns A boolean indicating whether the string is alphanumeric. + * @throws Will throw an error if the specified locale is not supported. + */ export function isAlphanumeric( str: string, locale = 'en-US', diff --git a/behaviors/common/isAscii.ts b/behaviors/common/isAscii.ts index a6f67eb..ea3697c 100644 --- a/behaviors/common/isAscii.ts +++ b/behaviors/common/isAscii.ts @@ -1,5 +1,11 @@ const ascii = /^[\x00-\x7F]+$/; +/** + * Checks if the given string consists only of ASCII characters. + * + * @param str - The string to be checked. + * @returns `true` if the string contains only ASCII characters, otherwise `false`. + */ export function isAscii(str: string): boolean { return ascii.test(str); } diff --git a/behaviors/common/isBIC.ts b/behaviors/common/isBIC.ts index de8369f..ac29b39 100644 --- a/behaviors/common/isBIC.ts +++ b/behaviors/common/isBIC.ts @@ -1,5 +1,11 @@ const isBICReg = /^[A-z]{4}[A-z]{2}\w{2}(\w{3})?$/; +/** + * Checks if the given string is a valid BIC (Bank Identifier Code). + * + * @param str - The string to be checked. + * @returns `true` if the string is a valid BIC, otherwise `false`. + */ export function isBIC(str: string): boolean { return isBICReg.test(str); } diff --git a/behaviors/common/isBase32.ts b/behaviors/common/isBase32.ts index a36038c..1d3a5f4 100644 --- a/behaviors/common/isBase32.ts +++ b/behaviors/common/isBase32.ts @@ -1,5 +1,16 @@ const base32 = /^[A-Z2-7]+=*$/; +/** + * Checks if a given string is a valid Base32 encoded string. + * + * A valid Base32 string must: + * - Have a length greater than 0. + * - Have a length that is a multiple of 8. + * - Match the Base32 regular expression pattern. + * + * @param str - The string to be checked. + * @returns `true` if the string is a valid Base32 encoded string, otherwise `false`. + */ export function isBase32(str: string): boolean { return str.length > 0 && str.length % 8 === 0 && base32.test(str); } diff --git a/behaviors/common/isBase64.ts b/behaviors/common/isBase64.ts index b7ee450..d91999e 100644 --- a/behaviors/common/isBase64.ts +++ b/behaviors/common/isBase64.ts @@ -11,6 +11,13 @@ const defaultIsBase64Options: IsBase64Options = { urlSafe: false, }; +/** + * Checks if a given string is a valid Base64 encoded string. + * + * @param str - The string to be checked. + * @param options - Optional settings to customize the validation. + * @returns `true` if the string is a valid Base64 encoded string, `false` otherwise. + */ export function isBase64( str: string, options: IsBase64Options = defaultIsBase64Options, diff --git a/behaviors/common/isBefore.ts b/behaviors/common/isBefore.ts index 23546cc..eadb63c 100644 --- a/behaviors/common/isBefore.ts +++ b/behaviors/common/isBefore.ts @@ -1,5 +1,12 @@ import { toDate } from './toDate.ts'; +/** + * Checks if the given date string is before the comparison date string. + * + * @param str - The date string to compare. + * @param date - The date string to compare against. Defaults to the current date and time. + * @returns `true` if `str` is before `date`, otherwise `false`. + */ export function isBefore( str: string, date: string = `${new Date()}`, diff --git a/behaviors/common/isBoolean.ts b/behaviors/common/isBoolean.ts index cd7494d..d26579e 100644 --- a/behaviors/common/isBoolean.ts +++ b/behaviors/common/isBoolean.ts @@ -1,3 +1,15 @@ +/** + * Checks if the given string represents a boolean value. + * + * This function returns `true` if the input string is one of the following: + * - "true" + * - "false" + * - "1" + * - "0" + * + * @param str - The string to check. + * @returns `true` if the string is a boolean representation, otherwise `false`. + */ export function isBoolean(str: string): boolean { return (['true', 'false', '1', '0'].indexOf(str) >= 0); } diff --git a/behaviors/common/isBtcAddress.ts b/behaviors/common/isBtcAddress.ts index 3164d2e..1ccf983 100644 --- a/behaviors/common/isBtcAddress.ts +++ b/behaviors/common/isBtcAddress.ts @@ -1,6 +1,12 @@ // supports Bech32 addresses const btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/; +/** + * Checks if the given string is a valid Bitcoin address. + * + * @param str - The string to be checked. + * @returns `true` if the string is a valid Bitcoin address, otherwise `false`. + */ export function isBtcAddress(str: string): boolean { return btc.test(str); } diff --git a/behaviors/common/isByteLength.ts b/behaviors/common/isByteLength.ts index 51250aa..0091ed1 100644 --- a/behaviors/common/isByteLength.ts +++ b/behaviors/common/isByteLength.ts @@ -10,6 +10,13 @@ export const defaultIsByteLengthOptions: IsByteLengthOptions = { max: undefined, }; +/** + * Checks if the length of the given string, in bytes, falls within the specified range. + * + * @param str - The string to be checked. + * @param options - An optional object specifying the minimum and maximum byte length. + * @returns `true` if the byte length of the string is within the specified range, otherwise `false`. + */ export function isByteLength( str: string, options: IsByteLengthOptions = defaultIsByteLengthOptions, diff --git a/behaviors/common/isCreditCard.ts b/behaviors/common/isCreditCard.ts index 3ef789b..400c30b 100644 --- a/behaviors/common/isCreditCard.ts +++ b/behaviors/common/isCreditCard.ts @@ -1,6 +1,15 @@ const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14})$/; +/** + * Checks if the provided string is a valid credit card number. + * + * This function sanitizes the input by removing any dashes or spaces, + * then uses the Luhn algorithm to validate the credit card number. + * + * @param str - The string to be checked as a credit card number. + * @returns `true` if the string is a valid credit card number, `false` otherwise. + */ export function isCreditCard(str: string): boolean { const sanitized = str.replace(/[- ]+/g, ''); if (!creditCard.test(sanitized)) { diff --git a/behaviors/common/isCurrency.ts b/behaviors/common/isCurrency.ts index cadd57e..da08d5f 100644 --- a/behaviors/common/isCurrency.ts +++ b/behaviors/common/isCurrency.ts @@ -96,6 +96,13 @@ export const defaultCurrencyOptions: CurrencyOptions = { allow_space_after_digits: false, }; +/** + * Checks if a given string is a valid currency format based on the provided options. + * + * @param str - The string to be validated as currency. + * @param options - Optional. The configuration options for currency validation. Defaults to `defaultCurrencyOptions`. + * @returns `true` if the string is a valid currency format, otherwise `false`. + */ export function isCurrency( str: string, options: CurrencyOptions = defaultCurrencyOptions, diff --git a/behaviors/common/isDataURI.ts b/behaviors/common/isDataURI.ts index a3497f1..b041a7f 100644 --- a/behaviors/common/isDataURI.ts +++ b/behaviors/common/isDataURI.ts @@ -4,6 +4,22 @@ const validAttribute = /^[a-z\-]+=[a-z0-9\-]+$/i; const validData = /^[a-z0-9!\$&'\(\)\*\+,;=\-\._~:@\/\?%\s]*$/i; +/** + * Checks if a given string is a valid Data URI. + * + * A Data URI typically consists of a scheme (`data:`), an optional media type, + * optional attributes (such as `base64`), and the actual data. + * + * This function performs the following checks: + * - The string must contain at least one comma. + * - The scheme must start with `data:`. + * - The media type, if present, must be valid. + * - Each attribute must be valid, with the last attribute optionally being `base64`. + * - The data part must be valid. + * + * @param str - The string to be checked. + * @returns `true` if the string is a valid Data URI, `false` otherwise. + */ export function isDataURI(str: string): boolean { const data: string[] = str.split(','); if (data.length < 2) { diff --git a/behaviors/common/isDate.ts b/behaviors/common/isDate.ts index 34b57f7..254c3e0 100644 --- a/behaviors/common/isDate.ts +++ b/behaviors/common/isDate.ts @@ -12,6 +12,13 @@ function zip(date: Array, format: Array): Array> { return zippedArr; } +/** + * Checks if the given input string is a valid date according to the specified format. + * + * @param input - The date string to validate. + * @param format - The format to validate against, default is 'YYYY/MM/DD'. + * @returns `true` if the input string is a valid date according to the specified format, otherwise `false`. + */ export function isDate( input: string, format = 'YYYY/MM/DD', diff --git a/behaviors/common/isDecimal.ts b/behaviors/common/isDecimal.ts index a3bbf43..3f5bd66 100644 --- a/behaviors/common/isDecimal.ts +++ b/behaviors/common/isDecimal.ts @@ -19,6 +19,17 @@ export const defaultDecimalOptions = { const blacklist = ['', '-', '+']; +/** + * Checks if the given string is a valid decimal number based on the provided options. + * + * @param str - The string to be validated as a decimal number. + * @param options - An object containing validation options. + * @param options.force_decimal - If true, the string must contain a decimal point. + * @param options.decimal_digits - A string representing the allowed number of decimal digits. + * @param options.locale - The locale to be used for decimal validation. + * @returns A boolean indicating whether the string is a valid decimal number. + * @throws Will throw an error if the provided locale is invalid. + */ export function isDecimal(str: string, options: { force_decimal: boolean; decimal_digits: string; locale: string }): boolean { options = merge(options, defaultDecimalOptions); if (options.locale in decimal) { diff --git a/behaviors/common/isDivisibleBy.ts b/behaviors/common/isDivisibleBy.ts index fef3c75..078b3a4 100644 --- a/behaviors/common/isDivisibleBy.ts +++ b/behaviors/common/isDivisibleBy.ts @@ -1,5 +1,12 @@ import { toFloat } from './toFloat.ts'; +/** + * Checks if the given dividend is divisible by the given divisor. + * + * @param dividend - The dividend as a string. + * @param divisor - The divisor as a string. + * @returns `true` if the dividend is divisible by the divisor, otherwise `false`. + */ export function isDivisibleBy( dividend: string, divisor: string, diff --git a/behaviors/common/isEmail.ts b/behaviors/common/isEmail.ts index 58260ac..d4b69b7 100644 --- a/behaviors/common/isEmail.ts +++ b/behaviors/common/isEmail.ts @@ -75,6 +75,37 @@ function validateDisplayName(display_name: string) { return true; } +/** + * Validates whether a given string is a valid email address. + * + * @param str - The string to validate as an email address. + * @param options - Optional configuration for email validation. + * @returns `true` if the string is a valid email address, `false` otherwise. + * + * @remarks + * The function supports various options to customize the validation process, such as: + * - `require_display_name`: Requires the email to have a display name. + * - `allow_display_name`: Allows the email to have a display name. + * - `ignore_max_length`: Ignores the maximum length restriction for the email. + * - `domain_specific_validation`: Enables domain-specific validation for certain domains like Gmail. + * - `require_tld`: Requires the domain to have a top-level domain. + * - `allow_ip_domain`: Allows the domain to be an IP address. + * - `allow_utf8_local_part`: Allows the local part of the email to contain UTF-8 characters. + * + * The function performs several checks, including: + * - Validating the display name if required. + * - Ensuring the email length does not exceed the maximum allowed length. + * - Splitting the email into user and domain parts and validating each part. + * - Performing domain-specific checks for Gmail addresses. + * - Validating the domain as a fully qualified domain name (FQDN) or IP address. + * - Validating the local part of the email based on the specified pattern. + * + * @example + * ```typescript + * const isValid = isEmail('example@gmail.com'); + * console.log(isValid); // true + * ``` + */ export function isEmail(str: string, options: isEmailOptions = {}): boolean { options = merge(options, defaultEmailOptions); diff --git a/behaviors/common/isEmpty.ts b/behaviors/common/isEmpty.ts index c7fd264..3171ab9 100644 --- a/behaviors/common/isEmpty.ts +++ b/behaviors/common/isEmpty.ts @@ -6,6 +6,13 @@ const defaultOptions: Options = { ignoreWhitespace: false, }; +/** + * Checks if a given string is empty. + * + * @param str - The string to check. + * @param options - Optional settings to modify the behavior of the check. + * @returns `true` if the string is empty, `false` otherwise. + */ export function isEmpty( str: string, options: Options = defaultOptions, diff --git a/behaviors/common/isEthereumAddress.ts b/behaviors/common/isEthereumAddress.ts index 154e14c..b392874 100644 --- a/behaviors/common/isEthereumAddress.ts +++ b/behaviors/common/isEthereumAddress.ts @@ -1,5 +1,11 @@ const eth = /^(0x)[0-9a-f]{40}$/i; +/** + * Checks if the given string is a valid Ethereum address. + * + * @param str - The string to be checked. + * @returns `true` if the string is a valid Ethereum address, otherwise `false`. + */ export function isEthereumAddress(str: string): boolean { return eth.test(str); } diff --git a/behaviors/common/isFloat.ts b/behaviors/common/isFloat.ts index d7e03ec..3c29dc1 100644 --- a/behaviors/common/isFloat.ts +++ b/behaviors/common/isFloat.ts @@ -8,6 +8,18 @@ interface isFloatOptions { gt?: number; } +/** + * Checks if the given string is a valid floating-point number based on the provided options. + * + * @param str - The string to validate as a floating-point number. + * @param options - An object containing validation options. + * @param options.locale - Optional locale to determine the decimal separator. + * @param options.min - Optional minimum value the number can be. + * @param options.max - Optional maximum value the number can be. + * @param options.lt - Optional value that the number must be less than. + * @param options.gt - Optional value that the number must be greater than. + * @returns `true` if the string is a valid floating-point number according to the options, otherwise `false`. + */ export function isFloat(str: string, options: isFloatOptions): boolean { const float = new RegExp( `^(?:[-+])?(?:[0-9]+)?(?:\\${options?.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`, diff --git a/behaviors/common/util/includes.ts b/behaviors/common/util/includes.ts index 35573c8..79f409b 100644 --- a/behaviors/common/util/includes.ts +++ b/behaviors/common/util/includes.ts @@ -1 +1,8 @@ +/** + * Checks if a given value is included in an array of strings. + * + * @param arr - The array of strings to search within. + * @param val - The string value to search for. + * @returns `true` if the value is found in the array, otherwise `false`. + */ export const includes = (arr: Array, val: string): boolean => arr.some((arrVal) => val === arrVal); diff --git a/behaviors/common/util/merge.ts b/behaviors/common/util/merge.ts index 63c36d7..b1167f6 100644 --- a/behaviors/common/util/merge.ts +++ b/behaviors/common/util/merge.ts @@ -1,3 +1,11 @@ +/** + * Merges the properties of the `defaults` object into the `obj` object. + * If a property in `defaults` does not exist in `obj`, it will be added to `obj`. + * + * @param obj - The target object to merge properties into. Defaults to an empty object. + * @param defaults - The source object containing default properties. + * @returns The merged object with properties from both `obj` and `defaults`. + */ // deno-lint-ignore no-explicit-any export function merge(obj: any = {}, defaults: any) { for (const key in defaults) { diff --git a/behaviors/common/util/toString.ts b/behaviors/common/util/toString.ts index 2e1b26b..3ff0d05 100644 --- a/behaviors/common/util/toString.ts +++ b/behaviors/common/util/toString.ts @@ -1,3 +1,10 @@ +/** + * Converts the given input to a string representation. + * + * @param input - The input to be converted to a string. It can be of any type. + * @returns The string representation of the input. If the input is an object with a `toString` method, + * it will use that method. If the input is `null`, `undefined`, or `NaN` (and not an array), it will return an empty string. + */ // deno-lint-ignore no-explicit-any export function toString(input: any) { if (typeof input === 'object' && input !== null) { diff --git a/behaviors/number.ts b/behaviors/number.ts index 397f65e..add3fe5 100644 --- a/behaviors/number.ts +++ b/behaviors/number.ts @@ -1,4 +1,31 @@ -export const greaterOrEqual = (prop: number, min: number) => prop >= min; -export const greater = (prop: number, min: number) => prop > min; -export const lowerOrEqual = (prop: number, max: number) => prop <= max; -export const lower = (prop: number, max: number) => prop < max; +/** + * Checks if a number is greater than or equal to a minimum value. + * @param {number} prop - The number to check. + * @param {number} min - The minimum value. + * @returns {boolean} True if the number is greater than or equal to the minimum value, otherwise false. + */ +export const greaterOrEqual = (prop: number, min: number): boolean => prop >= min; + +/** + * Checks if a number is greater than a minimum value. + * @param {number} prop - The number to check. + * @param {number} min - The minimum value. + * @returns {boolean} True if the number is greater than the minimum value, otherwise false. + */ +export const greater = (prop: number, min: number): boolean => prop > min; + +/** + * Checks if a number is less than or equal to a maximum value. + * @param {number} prop - The number to check. + * @param {number} max - The maximum value. + * @returns {boolean} True if the number is less than or equal to the maximum value, otherwise false. + */ +export const lowerOrEqual = (prop: number, max: number): boolean => prop <= max; + +/** + * Checks if a number is less than a maximum value. + * @param {number} prop - The number to check. + * @param {number} max - The maximum value. + * @returns {boolean} True if the number is less than the maximum value, otherwise false. + */ +export const lower = (prop: number, max: number): boolean => prop < max; diff --git a/behaviors/phone-number.ts b/behaviors/phone-number.ts index 17c023b..7175fc2 100644 --- a/behaviors/phone-number.ts +++ b/behaviors/phone-number.ts @@ -1,5 +1,12 @@ import { parsePhoneNumber } from '../deps.ts'; +/** + * Checks if the given string is a valid phone number for the specified locale. + * + * @param prop - The phone number string to validate. + * @param locale - Optional locale to validate the phone number against. + * @returns `true` if the phone number is valid, `false` otherwise. + */ export function isPhoneNumber(prop: string, locale?: string): boolean { const pn = parsePhoneNumber(prop, { regionCode: locale }); return pn.valid; diff --git a/behaviors/string.ts b/behaviors/string.ts index 8bcf1c9..dcda374 100644 --- a/behaviors/string.ts +++ b/behaviors/string.ts @@ -1,15 +1,54 @@ +/** + * Checks if the length of the string is greater than or equal to the specified minimum length. + * + * @param {string} str - The string to check. + * @param {number} minLength - The minimum length to compare against. + * @returns {boolean} True if the string length is greater than or equal to the minimum length, otherwise false. + */ export function lengthGreaterOrEqual(str: string, minLength: number): boolean { return str.length >= minLength; } + +/** + * Checks if the length of the string is greater than the specified minimum length. + * + * @param {string} str - The string to check. + * @param {number} minLength - The minimum length to compare against. + * @returns {boolean} True if the string length is greater than the minimum length, otherwise false. + */ export function lengthGreater(str: string, minLength: number): boolean { return str.length > minLength; } + +/** + * Checks if the length of the string is less than or equal to the specified maximum length. + * + * @param {string} str - The string to check. + * @param {number} maxLength - The maximum length to compare against. + * @returns {boolean} True if the string length is less than or equal to the maximum length, otherwise false. + */ export function lengthLowerOrEqual(str: string, maxLength: number): boolean { return str.length <= maxLength; } + +/** + * Checks if the length of the string is less than the specified maximum length. + * + * @param {string} str - The string to check. + * @param {number} maxLength - The maximum length to compare against. + * @returns {boolean} True if the string length is less than the maximum length, otherwise false. + */ export function lengthLower(str: string, maxLength: number): boolean { return str.length < maxLength; } + +/** + * Checks if the string matches the specified regular expression. + * + * @param {string} str - The string to check. + * @param {RegExp} regex - The regular expression to test against. + * @returns {boolean} True if the string matches the regular expression, otherwise false. + */ export function isRegex(str: string, regex: RegExp): boolean { return regex.test(str); } diff --git a/transform.ts b/transform.ts index 42ed642..9b51021 100644 --- a/transform.ts +++ b/transform.ts @@ -3,6 +3,14 @@ * Helper functions to transform plain object to class and vice versa */ +/** + * Transforms a plain object into an instance of a given class. + * + * @template T - The constructor type of the class. + * @param data - The plain object to be transformed. + * @param Class - The class constructor to instantiate. + * @returns An instance of the specified class with properties assigned from the plain object. + */ export const plainToClass = < T extends abstract new (...args: unknown[]) => unknown, >( @@ -12,6 +20,13 @@ export const plainToClass = < return Object.assign(Reflect.construct(Class, []), data) as InstanceType; }; +/** + * Converts a class instance to a plain object by copying all enumerable properties. + * + * @template T - The type of the class instance. + * @param {T} instance - The class instance to be converted. + * @returns {T} - A plain object with the same properties as the class instance. + */ export const classToPlain = (instance: T): T => { return Object.assign(new Object(), instance); }; diff --git a/validate.ts b/validate.ts index 4a64e5d..f600303 100644 --- a/validate.ts +++ b/validate.ts @@ -8,6 +8,9 @@ import { Constructor, ValidateFunction, ValidateFunctionOptions, ValidateInfo, ValidateSymbol, Validator } from './types.ts'; import { createErrorMessage, formatErrors, ValidationError } from './errors.ts'; +/** + * Will be replace by constraint in error message at runtime + */ export const constraintKey: string = `$constraint`; /**