From 27ebf55bb71b9f28cbd81da57c36fa9cec510e73 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sun, 28 Nov 2021 23:44:06 -0600 Subject: [PATCH] Use check-types helper --- helpers/app-files-scanner.js | 19 +------------------ helpers/check-types.js | 10 +++++++--- pages-eleventy/app.11ty.js | 5 +---- test/main.js | 4 +--- 4 files changed, 10 insertions(+), 28 deletions(-) diff --git a/helpers/app-files-scanner.js b/helpers/app-files-scanner.js index 57a45af..eacdb3c 100644 --- a/helpers/app-files-scanner.js +++ b/helpers/app-files-scanner.js @@ -1,6 +1,7 @@ import plist from 'plist' import axios from 'axios' +import { isString } from './check-types.js' import parseMacho from './macho/index.js' const prettyBytes = require('pretty-bytes') @@ -27,24 +28,6 @@ const knownAppExtensions = new Set([ '.app.zip' ]) -function isString( maybeString ) { - return (typeof maybeString === 'string' || maybeString instanceof String) -} - -function isValidHttpUrl( string ) { - if ( !isString( string ) ) return false - - let url - - try { - url = new URL(string) - } catch (_) { - return false - } - - return url.protocol === "http:" || url.protocol === "https:" -} - function callWithTimeout(timeout, func) { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject(new Error("timeout")), timeout) diff --git a/helpers/check-types.js b/helpers/check-types.js index 65c2ded..ef13e3b 100644 --- a/helpers/check-types.js +++ b/helpers/check-types.js @@ -4,16 +4,20 @@ export function isString( maybeString ) { return (typeof maybeString === 'string' || maybeString instanceof String) } -export function isValidHttpUrl( string ) { - if ( !isString( string ) ) return false +export function isValidHttpUrl( maybeUrl, allowUnsecure = false ) { + if ( !isString( maybeUrl ) ) return false let url try { - url = new URL(string) + url = new URL(maybeUrl) } catch (_) { return false } return url.protocol === "http:" || url.protocol === "https:" } + +export function isObject( maybeObject ) { + return maybeObject === Object( maybeObject ) +} diff --git a/pages-eleventy/app.11ty.js b/pages-eleventy/app.11ty.js index 84d39fb..3970e4a 100644 --- a/pages-eleventy/app.11ty.js +++ b/pages-eleventy/app.11ty.js @@ -6,6 +6,7 @@ import { getAppType, getRouteType } from '../helpers/app-derived.js' import { deviceSupportsApp } from '../helpers/devices.js' import { makeLastUpdatedFriendly } from '../helpers/parse-date.js' import { getStatusOfScan } from '../helpers/statuses.js' +import { isString } from '../helpers/check-types.js' import VideoRow from '../components-eleventy/video/row.js' @@ -16,10 +17,6 @@ import VideoRow from '../components-eleventy/video/row.js' // Setup dotenv dotenv.config() -function isString( maybeString ) { - return (typeof maybeString === 'string' || maybeString instanceof String) -} - export const makeTitle = function ( app ) { return `Does ${app.name} work on Apple Silicon? - ${ config.head.title }` } diff --git a/test/main.js b/test/main.js index 80e7318..be4a4e2 100644 --- a/test/main.js +++ b/test/main.js @@ -7,12 +7,10 @@ import parser from 'fast-xml-parser' import axios from 'axios' import { structuredDataTest } from 'structured-data-testing-tool' import { Google, Twitter } from 'structured-data-testing-tool/presets' +// import { isString } from '../helpers/check-types.js' require('dotenv').config() -function isString( maybeString ) { - return (typeof maybeString === 'string' || maybeString instanceof String) -} async function pageContains ( needle, pageUrlString ) { const pageUrlInstance = new URL( pageUrlString )