@botonic/core
Version:
Build Chatbots using React
98 lines • 3.52 kB
JavaScript
import { PROVIDER } from './models/legacy-types';
export const isNode = () => {
// @ts-ignore
return typeof IS_NODE !== 'undefined'
? // @ts-ignore
IS_NODE
: typeof process !== 'undefined' &&
process.versions !== null &&
process.versions.node !== null;
};
export 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
};
export const isMobile = (mobileBreakpoint = 460) => {
if (isBrowser()) {
const w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if (w < mobileBreakpoint) {
return true;
}
}
return false;
};
export function isFunction(o) {
return typeof o === 'function';
}
export function cloneObject(object) {
if (!object)
return {};
return Object.assign({}, object);
}
export const params2queryString = (params) => Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
// 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"
export 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 ? '/' : '.');
};
// A simple function to get the dirname of a path
// Trailing slashes are ignored. Leading slash is preserved.
export const dirname = (path) => join(path, '..');
// To check Provider in session
export function isDev(session) {
return session.user.provider === PROVIDER.DEV;
}
export function isWebchat(session) {
// When the provider is DEV should we return true because it is a webchat?
return session.user.provider === PROVIDER.WEBCHAT;
}
export function isWhatsapp(session) {
return session.user.provider === PROVIDER.WHATSAPP;
}
export function isTelegram(session) {
return session.user.provider === PROVIDER.TELEGRAM;
}
export function isFacebook(session) {
return session.user.provider === PROVIDER.FACEBOOK;
}
export function isInstagram(session) {
return session.user.provider === PROVIDER.INSTAGRAM;
}
export function isTwitter(session) {
return session.user.provider === PROVIDER.TWITTER;
}
//# sourceMappingURL=utils.js.map