From 392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 13 Jun 2018 00:02:55 +1000 Subject: Utils broken down into seperate files and exports --- src/js/utils/is.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/js/utils/is.js (limited to 'src/js/utils/is.js') diff --git a/src/js/utils/is.js b/src/js/utils/is.js new file mode 100644 index 00000000..d34d3aed --- /dev/null +++ b/src/js/utils/is.js @@ -0,0 +1,64 @@ +// ========================================================================== +// Type checking utils +// ========================================================================== + +const getConstructor = input => (input !== null && typeof input !== 'undefined' ? input.constructor : null); + +const instanceOf = (input, constructor) => Boolean(input && constructor && input instanceof constructor); + +const is = { + object(input) { + return getConstructor(input) === Object; + }, + number(input) { + return getConstructor(input) === Number && !Number.isNaN(input); + }, + string(input) { + return getConstructor(input) === String; + }, + boolean(input) { + return getConstructor(input) === Boolean; + }, + function(input) { + return getConstructor(input) === Function; + }, + array(input) { + return !is.nullOrUndefined(input) && Array.isArray(input); + }, + weakMap(input) { + return instanceOf(input, WeakMap); + }, + nodeList(input) { + return instanceOf(input, NodeList); + }, + element(input) { + return instanceOf(input, Element); + }, + textNode(input) { + return getConstructor(input) === Text; + }, + event(input) { + return instanceOf(input, Event); + }, + cue(input) { + return instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); + }, + track(input) { + return instanceOf(input, TextTrack) || (!is.nullOrUndefined(input) && is.string(input.kind)); + }, + url(input) { + return !is.nullOrUndefined(input) && /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input); + }, + nullOrUndefined(input) { + return input === null || typeof input === 'undefined'; + }, + empty(input) { + return ( + is.nullOrUndefined(input) || + ((is.string(input) || is.array(input) || is.nodeList(input)) && !input.length) || + (is.object(input) && !Object.keys(input).length) + ); + }, +}; + +export default is; -- cgit v1.2.3 From d4abb4b1438cb316aacae480e7b7e9b055a60b24 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 17 Jun 2018 01:04:55 +1000 Subject: 120 line width, package upgrade --- src/js/utils/is.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/js/utils/is.js') diff --git a/src/js/utils/is.js b/src/js/utils/is.js index d34d3aed..cb2c07c6 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -47,7 +47,10 @@ const is = { return instanceOf(input, TextTrack) || (!is.nullOrUndefined(input) && is.string(input.kind)); }, url(input) { - return !is.nullOrUndefined(input) && /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input); + return ( + !is.nullOrUndefined(input) && + /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input) + ); }, nullOrUndefined(input) { return input === null || typeof input === 'undefined'; -- cgit v1.2.3 From e04b90c9c030bf5629e034f616a636245770a8d1 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Thu, 21 Jun 2018 09:06:28 +1000 Subject: Ads only on HTML5 and .is cleanup --- src/js/utils/is.js | 112 ++++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 58 deletions(-) (limited to 'src/js/utils/is.js') diff --git a/src/js/utils/is.js b/src/js/utils/is.js index cb2c07c6..b4760da4 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -3,65 +3,61 @@ // ========================================================================== const getConstructor = input => (input !== null && typeof input !== 'undefined' ? input.constructor : null); - const instanceOf = (input, constructor) => Boolean(input && constructor && input instanceof constructor); +const isNullOrUndefined = input => input === null || typeof input === 'undefined'; +const isObject = input => getConstructor(input) === Object; +const isNumber = input => getConstructor(input) === Number && !Number.isNaN(input); +const isString = input => getConstructor(input) === String; +const isBoolean = input => getConstructor(input) === Boolean; +const isFunction = input => getConstructor(input) === Function; +const isArray = input => Array.isArray(input); +const isWeakMap = input => instanceOf(input, WeakMap); +const isNodeList = input => instanceOf(input, NodeList); +const isElement = input => instanceOf(input, Element); +const isTextNode = input => getConstructor(input) === Text; +const isEvent = input => instanceOf(input, Event); +const isCue = input => instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); +const isTrack = input => instanceOf(input, TextTrack) || (!isNullOrUndefined(input) && isString(input.kind)); + +const isEmpty = input => + isNullOrUndefined(input) || + ((isString(input) || isArray(input) || isNodeList(input)) && !input.length) || + (isObject(input) && !Object.keys(input).length); + +const isUrl = input => { + // Accept a URL object + if (instanceOf(input, window.URL)) { + return true; + } -const is = { - object(input) { - return getConstructor(input) === Object; - }, - number(input) { - return getConstructor(input) === Number && !Number.isNaN(input); - }, - string(input) { - return getConstructor(input) === String; - }, - boolean(input) { - return getConstructor(input) === Boolean; - }, - function(input) { - return getConstructor(input) === Function; - }, - array(input) { - return !is.nullOrUndefined(input) && Array.isArray(input); - }, - weakMap(input) { - return instanceOf(input, WeakMap); - }, - nodeList(input) { - return instanceOf(input, NodeList); - }, - element(input) { - return instanceOf(input, Element); - }, - textNode(input) { - return getConstructor(input) === Text; - }, - event(input) { - return instanceOf(input, Event); - }, - cue(input) { - return instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); - }, - track(input) { - return instanceOf(input, TextTrack) || (!is.nullOrUndefined(input) && is.string(input.kind)); - }, - url(input) { - return ( - !is.nullOrUndefined(input) && - /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input) - ); - }, - nullOrUndefined(input) { - return input === null || typeof input === 'undefined'; - }, - empty(input) { - return ( - is.nullOrUndefined(input) || - ((is.string(input) || is.array(input) || is.nodeList(input)) && !input.length) || - (is.object(input) && !Object.keys(input).length) - ); - }, + // Add the protocol if required + let string = input; + if (!input.startsWith('http://') || !input.startsWith('https://')) { + string = `http://${input}`; + } + + try { + return !isEmpty(new URL(string).hostname); + } catch (e) { + return false; + } }; -export default is; +export default { + nullOrUndefined: isNullOrUndefined, + object: isObject, + number: isNumber, + string: isString, + boolean: isBoolean, + function: isFunction, + array: isArray, + weakMap: isWeakMap, + nodeList: isNodeList, + element: isElement, + textNode: isTextNode, + event: isEvent, + cue: isCue, + track: isTrack, + url: isUrl, + empty: isEmpty, +}; -- cgit v1.2.3 From c8db1e55ddff51a1eb4ff08887cbed134116cd88 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 1 Aug 2018 01:26:15 +1000 Subject: Escape closes menu --- src/js/utils/is.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/js/utils/is.js') diff --git a/src/js/utils/is.js b/src/js/utils/is.js index b4760da4..2952d486 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -16,6 +16,7 @@ const isNodeList = input => instanceOf(input, NodeList); const isElement = input => instanceOf(input, Element); const isTextNode = input => getConstructor(input) === Text; const isEvent = input => instanceOf(input, Event); +const isKeyboardEvent = input => instanceOf(input, KeyboardEvent); const isCue = input => instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); const isTrack = input => instanceOf(input, TextTrack) || (!isNullOrUndefined(input) && isString(input.kind)); @@ -56,6 +57,7 @@ export default { element: isElement, textNode: isTextNode, event: isEvent, + keyboardEvent: isKeyboardEvent, cue: isCue, track: isTrack, url: isUrl, -- cgit v1.2.3 From 03c9b53232aeab78a7c592e1bcf387312f77a569 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 24 Oct 2018 22:31:35 +1100 Subject: Allow custom download URL (for streaming, etc) --- src/js/utils/is.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/js/utils/is.js') diff --git a/src/js/utils/is.js b/src/js/utils/is.js index 2952d486..ab28f2ab 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -31,6 +31,11 @@ const isUrl = input => { return true; } + // Must be string from here + if (!isString(input)) { + return false; + } + // Add the protocol if required let string = input; if (!input.startsWith('http://') || !input.startsWith('https://')) { -- cgit v1.2.3