diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
commit | e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch) | |
tree | 2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /app/assets/javascripts/lib/utils/forms.js | |
parent | ffda4e7bcac36987f936b4ba515995a6698698f0 (diff) |
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'app/assets/javascripts/lib/utils/forms.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/forms.js | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/app/assets/javascripts/lib/utils/forms.js b/app/assets/javascripts/lib/utils/forms.js index 1d8c6ee23fc..652ae337506 100644 --- a/app/assets/javascripts/lib/utils/forms.js +++ b/app/assets/javascripts/lib/utils/forms.js @@ -18,18 +18,69 @@ export const serializeForm = (form) => { }; /** + * Like trim but without the error for non-string values. + * + * @param {String, Number, Array} - value + * @returns {String, Number, Array} - the trimmed string or the value if it isn't a string + */ +export const safeTrim = (value) => (typeof value === 'string' ? value.trim() : value); + +/** * Check if the value provided is empty or not * * It is being used to check if a form input - * value has been set or not + * value has been set or not. * * @param {String, Number, Array} - Any form value * @returns {Boolean} - returns false if a value is set * * @example - * returns true for '', [], null, undefined + * returns true for '', ' ', [], null, undefined + */ +export const isEmptyValue = (value) => value == null || safeTrim(value).length === 0; + +/** + * Check if the value has a minimum string length + * + * @param {String, Number, Array} - Any form value + * @param {Number} - minLength + * @returns {Boolean} + */ +export const hasMinimumLength = (value, minLength) => + !isEmptyValue(value) && value.length >= minLength; + +/** + * Checks if the given value can be parsed as an integer as it is (without cutting off decimals etc.) + * + * @param {String, Number, Array} - Any form value + * @returns {Boolean} + */ +export const isParseableAsInteger = (value) => + !isEmptyValue(value) && Number.isInteger(Number(safeTrim(value))); + +/** + * Checks if the parsed integer value from the given input is greater than a certain number + * + * @param {String, Number, Array} - Any form value + * @param {Number} - greaterThan + * @returns {Boolean} + */ +export const isIntegerGreaterThan = (value, greaterThan) => + isParseableAsInteger(value) && parseInt(value, 10) > greaterThan; + +/** + * Regexp that matches email structure. + * Taken from app/models/service_desk_setting.rb custom_email + */ +export const EMAIL_REGEXP = /^[\w\-._]+@[\w\-.]+\.[a-zA-Z]{2,}$/; + +/** + * Checks if the input is a valid email address + * + * @param {String} - value + * @returns {Boolean} */ -export const isEmptyValue = (value) => value == null || value.length === 0; +export const isEmail = (value) => EMAIL_REGEXP.test(value); /** * A form object serializer |