From e0daf4dc323d722edd5a707c7104348c22e67a03 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Mon, 15 Jan 2024 12:18:28 +0100 Subject: [PATCH] refactor typeof calls --- sysend.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/sysend.js b/sysend.js index 919badb..0d70691 100644 --- a/sysend.js +++ b/sysend.js @@ -87,7 +87,7 @@ return sysend; }, serializer: function(to, from) { - if (typeof to !== 'function' || typeof from !== 'function') { + if (!(is_function(to) && is_function(from))) { throw new Error('sysend::serializer: Invalid argument, expecting' + ' function'); } @@ -97,7 +97,7 @@ }, proxy: function(...args) { args.forEach(function(url) { - if (typeof url === 'string' && host(url) !== window.location.host) { + if (is_string(url) && host(url) !== window.location.host) { domains = domains || []; domains.push(origin(url)); var iframe = document.createElement('iframe'); @@ -324,11 +324,6 @@ }; })(); // ------------------------------------------------------------------------- - function is_promise(obj) { - return obj && typeof object == 'object' && - typeof object.then === 'function'; - } - // ------------------------------------------------------------------------- function unpromise(obj, callback, error = null) { if (is_promise(obj)) { var ret = obj.then(callback); @@ -412,6 +407,18 @@ return uniq_prefix + name; } // ------------------------------------------------------------------------- + function is_promise(obj) { + return obj && typeof object == 'object' && is_function(object.then); + } + // ------------------------------------------------------------------------- + function is_function(o) { + return typeof o === 'function'; + } + // ------------------------------------------------------------------------- + function is_string(o) { + return typeof o === 'string'; + } + // ------------------------------------------------------------------------- function is_internal(name) { return name.match(prefix_re); } @@ -589,7 +596,7 @@ hidden = 'webkitHidden'; visibilityChange = 'webkitvisibilitychange'; } - if (typeof document.addEventListener === 'function' && hidden) { + if (is_function(document.addEventListener) && hidden) { document.addEventListener(visibilityChange, function() { trigger(handlers.visbility, !document[hidden]); }, false);