From e333fe79590039e38fbf139b96b42eca916c250f Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 30 Apr 2022 14:46:16 -0500 Subject: [PATCH] Add more specfic string and url checkers --- helpers/check-types.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/helpers/check-types.js b/helpers/check-types.js index 6c9dc51..db4162f 100644 --- a/helpers/check-types.js +++ b/helpers/check-types.js @@ -4,6 +4,19 @@ export function isString( maybeString ) { return (typeof maybeString === 'string' || maybeString instanceof String) } +export function isNonEmptyString ( maybeString ) { + if ( !isString( maybeString ) ) return false + + return maybeString.length > 0 +} + +export function isPositiveNumberString ( maybeNumber ) { + if ( !isString( maybeNumber ) ) return false + + return /\d+$/.test( maybeNumber ) +} + + export function isValidHttpUrl( maybeUrl, allowUnsecure = false ) { if ( !isString( maybeUrl ) ) return false @@ -22,6 +35,17 @@ export function isValidHttpUrl( maybeUrl, allowUnsecure = false ) { return url.protocol === "https:" } +export function isValidImageUrl ( maybeUrl ) { + if ( !isValidHttpUrl( maybeUrl ) ) return false + + // Check if url has a file extension + const url = new URL(maybeUrl) + const fileExtension = url.pathname.split('.').pop() + + return isNonEmptyString( fileExtension ) +} + + export function isObject( maybeObject ) { return maybeObject === Object( maybeObject ) }