@mopinion/deploy
Version:
Deploy your Mopinion feedback forms with ease
221 lines (176 loc) • 8.4 kB
JavaScript
;
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(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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;
}
var findFlag = search.replace(/\\/g, '').split('/');
if (findFlag[findFlag.length - 1] === 'i') {
var extractSearch = findFlag[0] === '' ? findFlag.slice(1, -1).join('/') : findFlag.slice(0, -1).join('/');
var fixedRegex = new RegExp(extractSearch, 'i');
return fixedRegex.test(str);
}
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 {
var _acc;
acc = typeof acc[key] === 'function' ? acc[key]() : acc[key];
return (_acc = 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