UNPKG

@botonic/core

Version:
116 lines 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTwitter = exports.isInstagram = exports.isFacebook = exports.isTelegram = exports.isWhatsapp = exports.isWebchat = exports.isDev = exports.dirname = exports.join = exports.params2queryString = exports.cloneObject = exports.isFunction = exports.isMobile = exports.isBrowser = exports.isNode = void 0; const legacy_types_1 = require("./models/legacy-types"); const isNode = () => { // @ts-ignore return typeof IS_NODE !== 'undefined' ? // @ts-ignore IS_NODE : typeof process !== 'undefined' && process.versions !== null && process.versions.node !== null; }; exports.isNode = isNode; const isBrowser = () => { var _a, _b; // @ts-ignore return typeof IS_BROWSER !== 'undefined' ? // @ts-ignore IS_BROWSER : typeof window !== 'undefined' && typeof window.document !== 'undefined' && !((_b = (_a = window.process) === null || _a === void 0 ? void 0 : _a.versions) === null || _b === void 0 ? void 0 : _b.node); // In Jest window.process?.versions?.node is defined }; exports.isBrowser = isBrowser; const isMobile = (mobileBreakpoint = 460) => { if ((0, exports.isBrowser)()) { const w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); if (w < mobileBreakpoint) { return true; } } return false; }; exports.isMobile = isMobile; function isFunction(o) { return typeof o === 'function'; } exports.isFunction = isFunction; function cloneObject(object) { if (!object) return {}; return Object.assign({}, object); } exports.cloneObject = cloneObject; const params2queryString = (params) => Object.entries(params) .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`) .join('&'); exports.params2queryString = params2queryString; // Joins path segments. Preserves initial "/" and resolves ".." and "." // Does not support using ".." to go above/outside the root. // This means that join("foo", "../../bar") will not resolve to "../bar" const join = (...segments) => { // Split the inputs into a list of path commands. let parts = []; for (let i = 0, l = segments.length; i < l; i++) { parts = parts.concat(segments[i].split('/')); } // Interpret the path commands to get the new resolved path. const newParts = []; for (let i = 0, l = parts.length; i < l; i++) { const part = parts[i]; // Remove leading and trailing slashes // Also remove "." segments if (!part || part === '.') continue; // Interpret ".." to pop the last segment if (part === '..') newParts.pop(); // Push new path segments. else newParts.push(part); } // Preserve the initial slash if there was one. // @ts-ignore if (parts[0] === '') newParts.unshift(''); // Turn back into a single string path. return newParts.join('/') || (newParts.length ? '/' : '.'); }; exports.join = join; // A simple function to get the dirname of a path // Trailing slashes are ignored. Leading slash is preserved. const dirname = (path) => (0, exports.join)(path, '..'); exports.dirname = dirname; // To check Provider in session function isDev(session) { return session.user.provider === legacy_types_1.PROVIDER.DEV; } exports.isDev = isDev; function isWebchat(session) { // When the provider is DEV should we return true because it is a webchat? return session.user.provider === legacy_types_1.PROVIDER.WEBCHAT; } exports.isWebchat = isWebchat; function isWhatsapp(session) { return session.user.provider === legacy_types_1.PROVIDER.WHATSAPP; } exports.isWhatsapp = isWhatsapp; function isTelegram(session) { return session.user.provider === legacy_types_1.PROVIDER.TELEGRAM; } exports.isTelegram = isTelegram; function isFacebook(session) { return session.user.provider === legacy_types_1.PROVIDER.FACEBOOK; } exports.isFacebook = isFacebook; function isInstagram(session) { return session.user.provider === legacy_types_1.PROVIDER.INSTAGRAM; } exports.isInstagram = isInstagram; function isTwitter(session) { return session.user.provider === legacy_types_1.PROVIDER.TWITTER; } exports.isTwitter = isTwitter; //# sourceMappingURL=utils.js.map