-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathfunctions.js
More file actions
executable file
·153 lines (133 loc) · 4.2 KB
/
functions.js
File metadata and controls
executable file
·153 lines (133 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @module lib/functions
*/
/**
* Check to see this is a node environment.
* @type {Boolean}
*/
/* global global */
export const isNode = typeof global !== 'undefined' &&
({}).toString.call(global) === '[object global]';
/**
* Get the name of the method for a given getter or setter.
*
* @param {string} prop The name of the property.
* @param {string} type Either “get” or “set”.
* @return {string}
*/
export function getMethodName(prop, type) {
if (prop.indexOf(type.toLowerCase()) === 0) {
return prop;
}
return `${type.toLowerCase()}${prop.substr(0, 1).toUpperCase()}${prop.substr(1)}`;
}
/**
* Check to see if the object is a DOM Element.
*
* @param {*} element The object to check.
* @return {boolean}
*/
export function isDomElement(element) {
return Boolean(
element && element.nodeType === 1 && 'nodeName' in element &&
element.ownerDocument && element.ownerDocument.defaultView
);
}
/**
* Check to see whether the value is a number.
*
* @see http://dl.dropboxusercontent.com/u/35146/js/tests/isNumber.html
* @param {*} value The value to check.
* @param {boolean} integer Check if the value is an integer.
* @return {boolean}
*/
export function isInteger(value) {
// eslint-disable-next-line eqeqeq
return !isNaN(parseFloat(value)) && isFinite(value) && Math.floor(value) == value;
}
/**
* Check to see if the URL is a Vimeo url.
*
* @param {string} url The url string.
* @return {boolean}
*/
export function isVimeoUrl(url) {
return (/^(https?:)?\/\/((((player|www)\.)?vimeo\.com)|((player\.)?[a-zA-Z0-9-]+\.(videoji\.(hk|cn)|vimeo\.work)))(?=$|\/)/).test(url);
}
/**
* Check to see if the URL is for a Vimeo embed.
*
* @param {string} url The url string.
* @return {boolean}
*/
export function isVimeoEmbed(url) {
const expr = /^https:\/\/player\.((vimeo\.com)|([a-zA-Z0-9-]+\.(videoji\.(hk|cn)|vimeo\.work)))\/video\/\d+/;
return expr.test(url);
}
export function getOembedDomain(url) {
const match = (url || '').match(/^(?:https?:)?(?:\/\/)?([^/?]+)/);
const domain = ((match && match[1]) || '').replace('player.', '');
const customDomains = [
'.videoji.hk',
'.vimeo.work',
'.videoji.cn'
];
for (const customDomain of customDomains) {
if (domain.endsWith(customDomain)) {
return domain;
}
}
return 'vimeo.com';
}
/**
* Get the Vimeo URL from an element.
* The element must have either a data-vimeo-id or data-vimeo-url attribute.
*
* @param {object} oEmbedParameters The oEmbed parameters.
* @return {string}
*/
export function getVimeoUrl(oEmbedParameters = {}) {
const id = oEmbedParameters.id;
const url = oEmbedParameters.url;
const idOrUrl = id || url;
if (!idOrUrl) {
throw new Error('An id or url must be passed, either in an options object or as a data-vimeo-id or data-vimeo-url attribute.');
}
if (isInteger(idOrUrl)) {
return `https://vimeo.com/${idOrUrl}`;
}
if (isVimeoUrl(idOrUrl)) {
return idOrUrl.replace('http:', 'https:');
}
if (id) {
throw new TypeError(`“${id}” is not a valid video id.`);
}
throw new TypeError(`“${idOrUrl}” is not a vimeo.com url.`);
}
/* eslint-disable max-params */
/**
* A utility method for attaching and detaching event handlers
*
* @param {EventTarget} target
* @param {string | string[]} eventName
* @param {function} callback
* @param {'addEventListener' | 'on'} onName
* @param {'removeEventListener' | 'off'} offName
* @return {{cancel: (function(): void)}}
*/
export const subscribe = (target, eventName, callback, onName = 'addEventListener', offName = 'removeEventListener') => {
const eventNames = typeof eventName === 'string' ? [eventName] : eventName;
eventNames.forEach((evName) => {
target[onName](evName, callback);
});
return {
cancel: () => eventNames.forEach((evName) => target[offName](evName, callback))
};
};
export const logSurveyLink = () => {
console.log(
'\n%cVimeo is looking for feedback!\n%cComplete our survey about the Player SDK: https://t.maze.co/393567477',
'color:#00adef;font-size:1.2em;',
'color:#aaa;font-size:0.8em;'
);
};