aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/is.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
committerSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
commitcc3c0b54484e6f5a7b4dba8a36a44f345e462f26 (patch)
tree5fe2838546d9f981b21572dee88ee7a1c3195477 /src/js/utils/is.js
parent4811e3333f1417bc9e14f6cc38afc789e9051c4c (diff)
parent3c9c1b4cdcd0eb9076c3f0bafbabb057ee140c42 (diff)
downloadplyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.lz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.xz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.zip
Merge branch 'develop'
Diffstat (limited to 'src/js/utils/is.js')
-rw-r--r--src/js/utils/is.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/js/utils/is.js b/src/js/utils/is.js
new file mode 100644
index 00000000..cb2c07c6
--- /dev/null
+++ b/src/js/utils/is.js
@@ -0,0 +1,67 @@
+// ==========================================================================
+// 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;