UNPKG

@mopinion/deploy

Version:

Deploy your Mopinion feedback forms with ease

200 lines (199 loc) 8.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._get = _get; exports.appendScript = appendScript; exports.debounce = debounce; exports.flattenArray = flattenArray; exports.getCookie = getCookie; exports.schedule = schedule; exports.setCookie = setCookie; exports.setRecursiveTimeout = setRecursiveTimeout; exports.strContains = strContains; exports.timeInSeconds = timeInSeconds; function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function parseRegex() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var unescapedStr = str.replace(/\\\//g, '/'); while (unescapedStr.endsWith('\\')) { unescapedStr = unescapedStr.slice(0, -1); } var splitString = unescapedStr.split('/'); var pattern = splitString.slice(0, -1).join('/'); var flags = splitString.slice(-1)[0] || ''; var allowedFlags = ['g', 'i', 'm', 's', 'u', 'y']; if (flags.split('').every(function (flag) { return allowedFlags.includes(flag); })) { return { pattern: pattern, flags: flags }; } return { pattern: unescapedStr, flags: '' }; } function strContains() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var search = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var regex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; if (!regex) { return (str === null || str === void 0 ? void 0 : str.indexOf(search)) > -1; } try { var _parseRegex = parseRegex(search), pattern = _parseRegex.pattern, flags = _parseRegex.flags; return new RegExp(pattern, flags).test(str); } catch (e) { return new RegExp(search).test(str); } } function getCookie() { var cookieKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var _iterator = _createForOfIteratorHelper(document.cookie.split('; ')), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var cookie = _step.value; var _cookie$split = cookie.split('='), _cookie$split2 = _slicedToArray(_cookie$split, 2), key = _cookie$split2[0], val = _cookie$split2[1]; if (cookieKey === key) { return decodeURIComponent(val); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return; } function rootDomain() { var parts = window.location.hostname.split('.'); if (parts.length <= 2) { return window.location.hostname; } var sliced = parts.slice(-3); if (['co', 'com'].indexOf(sliced[1]) > -1) return sliced.join('.'); return sliced.slice(-2).join('.'); } function setCookie(name, value, days) { var expires = ''; if (days) { var date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = "; expires=".concat(date.toUTCString()); } var secure = document.location.protocol === 'https:' ? '; Secure' : ''; var cookie = "".concat(name, "=").concat(value || '').concat(expires, ";domain=.").concat(rootDomain(), ";path=/").concat(secure, ";"); document.cookie = cookie; } function _get(value, path, defaultValue) { var ignoreWindow = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; return String(path).replace(/\["|\['|\[/g, '.').replace(/"]|']|]/g, '').split('.').reduce(function (acc, current, index, currentArr) { if (index === 0 && current === 'window' && ignoreWindow) { return acc; } try { var key = current.replace(/[()]/g, ''); var wildcard = key === '*'; var lastWildcard = key === '*LAST'; if ((wildcard || lastWildcard) && Array.isArray(acc)) { var _acc$, _acc$pop; var nextSearchKey = currentArr[index + 1]; acc = acc.filter(function (item) { return item === null || item === void 0 ? void 0 : item[nextSearchKey]; }); return wildcard ? (_acc$ = acc[0]) !== null && _acc$ !== void 0 ? _acc$ : defaultValue : (_acc$pop = acc.pop()) !== null && _acc$pop !== void 0 ? _acc$pop : defaultValue; } else { acc = typeof acc[key] === 'function' ? acc[key]() : acc[key]; return acc !== null && acc !== void 0 ? acc : defaultValue; } } catch (e) { return defaultValue; } }, value); } function appendScript(_ref) { var src = _ref.src, _ref$id = _ref.id, id = _ref$id === void 0 ? 'mopinionFeedbackScript' : _ref$id, _ref$onLoad = _ref.onLoad, onLoad = _ref$onLoad === void 0 ? function () {} : _ref$onLoad; var script = document.createElement('script'); script.async = 'async'; script.id = id; script.src = src; script.onload = script.onreadystatechange = function () { if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') { try { onLoad(); } catch (e) {} } }; document.head.appendChild(script); } function debounce(fn) { var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var timerId; return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (timerId) { clearTimeout(timerId); } timerId = setTimeout(function () { fn.apply(void 0, args); timerId = null; }, delay); }; } function flattenArray() { var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return array.reduce(function (acc, item) { return acc.concat(item); }, []); } function setRecursiveTimeout(callback) { var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var timeoutId; var canceled = false; var _recursiveFn = function recursiveFn() { if (canceled) { return; } requestAnimationFrame(function () { callback(); timeoutId = setTimeout(_recursiveFn, delay); }); }; _recursiveFn(); var cancel = function cancel() { canceled = true; clearTimeout(timeoutId); }; return cancel; } function schedule(fn) { var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; requestAnimationFrame(function () { setTimeout(fn, delay); }); } function timeInSeconds() { return Math.floor(Date.now() / 1000); } //# sourceMappingURL=utils.js.map