jam-cloud/jam-ui/src/helpers/utils.js

337 lines
9.2 KiB
JavaScript

import moment from 'moment';
export const isIterableArray = array => Array.isArray(array) && !!array.length;
//===============================
// Breakpoints
//===============================
export const breakpoints = {
xs: 0,
sm: 576,
md: 768,
lg: 992,
xl: 1200,
xxl: 1540
};
//===============================
// Store
//===============================
export const getItemFromStore = (key, defaultValue, store = localStorage) =>
JSON.parse(store.getItem(key)) || defaultValue;
export const setItemToStore = (key, payload, store = localStorage) => store.setItem(key, JSON.stringify(payload));
export const getStoreSpace = (store = localStorage) =>
parseFloat((escape(encodeURIComponent(JSON.stringify(store))).length / (1024 * 1024)).toFixed(2));
//===============================
// Cookie
//===============================
export const getCookieValue = name => {
const value = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)');
return value ? value.pop() : null;
};
export const createCookie = (name, value, cookieExpireTime) => {
const date = new Date();
date.setTime(date.getTime() + cookieExpireTime);
const expires = '; expires=' + date.toUTCString();
document.cookie = name + '=' + value + expires + '; path=/';
};
//===============================
// Moment
//===============================
export const getDuration = (startDate, endDate) => {
if (!moment.isMoment(startDate)) throw new Error(`Start date must be a moment object, received ${typeof startDate}`);
if (endDate && !moment.isMoment(endDate))
throw new Error(`End date must be a moment object, received ${typeof startDate}`);
return `${startDate.format('ll')} - ${endDate ? endDate.format('ll') : 'Present'}${startDate.from(
endDate || moment(),
true
)}`;
};
export const prettyPrintSeconds = (seconds) => {
// from: http://stackoverflow.com/questions/3733227/javascript-seconds-to-minutes-and-seconds
// Minutes and seconds
var mins = ~~(seconds / 60);
var secs = seconds % 60;
// Hours, minutes and seconds
var hrs = ~~(seconds / 3600);
var mins = ~~((seconds % 3600) / 60);
var secs = seconds % 60;
// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0)
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
ret += "" + mins + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
export const numberFormatter = (number, fixed = 2) => {
// Nine Zeroes for Billions
return Math.abs(Number(number)) >= 1.0e9
? (Math.abs(Number(number)) / 1.0e9).toFixed(fixed) + 'B'
: // Six Zeroes for Millions
Math.abs(Number(number)) >= 1.0e6
? (Math.abs(Number(number)) / 1.0e6).toFixed(fixed) + 'M'
: // Three Zeroes for Thousands
Math.abs(Number(number)) >= 1.0e3
? (Math.abs(Number(number)) / 1.0e3).toFixed(fixed) + 'K'
: Math.abs(Number(number)).toFixed(fixed);
};
//===============================
// Colors
//===============================
export const hexToRgb = hexValue => {
let hex;
hexValue.indexOf('#') === 0 ? (hex = hexValue.substring(1)) : (hex = hexValue);
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(
hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b)
);
return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : null;
};
export const rgbColor = (color = colors[0]) => `rgb(${hexToRgb(color)})`;
export const rgbaColor = (color = colors[0], alpha = 0.5) => `rgba(${hexToRgb(color)},${alpha})`;
export const colors = [
'#2c7be5',
'#00d97e',
'#e63757',
'#39afd1',
'#fd7e14',
'#02a8b5',
'#727cf5',
'#6b5eae',
'#ff679b',
'#f6c343'
];
export const themeColors = {
primary: '#2c7be5',
secondary: '#748194',
success: '#00d27a',
info: '#27bcfd',
warning: '#f5803e',
danger: '#e63757',
light: '#f9fafd',
dark: '#0b1727'
};
export const grays = {
white: '#fff',
100: '#f9fafd',
200: '#edf2f9',
300: '#d8e2ef',
400: '#b6c1d2',
500: '#9da9bb',
600: '#748194',
700: '#5e6e82',
800: '#4d5969',
900: '#344050',
1000: '#232e3c',
1100: '#0b1727',
black: '#000'
};
export const darkGrays = {
white: '#fff',
1100: '#f9fafd',
1000: '#edf2f9',
900: '#d8e2ef',
800: '#b6c1d2',
700: '#9da9bb',
600: '#748194',
500: '#5e6e82',
400: '#4d5969',
300: '#344050',
200: '#232e3c',
100: '#0b1727',
black: '#000'
};
export const getGrays = isDark => (isDark ? darkGrays : grays);
export const rgbColors = colors.map(color => rgbColor(color));
export const rgbaColors = colors.map(color => rgbaColor(color));
//===============================
// Echarts
//===============================
export const getPosition = (pos, params, dom, rect, size) => ({
top: pos[1] - size.contentSize[1] - 10,
left: pos[0] - size.contentSize[0] / 2
});
//===============================
// E-Commerce
//===============================
export const calculateSale = (base, less = 0, fix = 2) => (base - base * (less / 100)).toFixed(fix);
export const getTotalPrice = (cart, baseItems) =>
cart.reduce((accumulator, currentValue) => {
const { id, quantity } = currentValue;
const { price, sale } = baseItems.find(item => item.id === id);
return accumulator + calculateSale(price, sale) * quantity;
}, 0);
//===============================
// Helpers
//===============================
export const getPaginationArray = (totalSize, sizePerPage) => {
const noOfPages = Math.ceil(totalSize / sizePerPage);
const array = [];
let pageNo = 1;
while (pageNo <= noOfPages) {
array.push(pageNo);
pageNo = pageNo + 1;
}
return array;
};
export const capitalize = str => (str.charAt(0).toUpperCase() + str.slice(1)).replace(/-/g, ' ');
export const titleize = (str) => {
return str.replace(
/\w\S*/g,
(txt) => {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
);
}
export const truncate = (str, length=100) => {
if(!str) return;
if (str.length <= length) {
return str;
} else {
return `${str.substring(0, length)}...`;
}
}
export const routesSlicer = ({ routes, columns = 3, rows }) => {
const routesCollection = [];
routes.map(route => {
if (route.children) {
return route.children.map(item => {
if (item.children) {
return routesCollection.push(...item.children);
}
return routesCollection.push(item);
});
}
return routesCollection.push(route);
});
const totalRoutes = routesCollection.length;
const calculatedRows = rows || Math.ceil(totalRoutes / columns);
const routesChunks = [];
for (let i = 0; i < totalRoutes; i += calculatedRows) {
routesChunks.push(routesCollection.slice(i, i + calculatedRows));
}
return routesChunks;
};
export const getPageName = pageName => {
return window.location.pathname.split('/').slice(-1)[0] === pageName;
};
export const copyToClipBoard = textFieldRef => {
const textField = textFieldRef.current;
textField.focus();
textField.select();
document.execCommand('copy');
};
export const currencyFormat = (num) => {
return new Intl.NumberFormat('en-US',
{ style: 'currency', currency: 'USD' }
).format(num);
}
const days = new Array("Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat");
const months = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
export const monthName = (monthNumber) => {
return months[monthNumber];
}
export const formatDateShort = (dateString) => {
const date = dateString instanceof Date ? dateString : new Date(dateString);
return months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();
}
// returns Fri May 20, 2013
export const formatDate = (dateString, options = {}) => {
if (!dateString) {
return 'N/A'
}
const suppressDay = options.suppressDay || false;
const date = new Date(dateString);
return (suppressDay ? '' : (days[date.getDay()] + ' ')) + months[date.getMonth()] + ' ' + padString(date.getDate(), 2) + ', ' + date.getFullYear();
}
export const groupByKey = (list, key) => list.reduce((hash, obj) => ({...hash, [obj[key]]:( hash[obj[key]] || [] ).concat(obj)}), {})
const padString = (str, max) => {
let retVal = '' + str;
while (retVal.length < max) {
retVal = '0' + retVal;
}
return retVal;
}
export const detectOS = () => {
let platform;
if(navigator.platform) {
platform = navigator.platform.toLowerCase();
}else if(navigator.userAgentData){
platform = navigator.userAgentData.platform.toLowerCase();
}
if (!platform) {
return null;
}
if (platform.indexOf('mac') !== -1) {
return "MacOSX";
}
if (platform.indexOf('win') !== -1) {
return "Win32"
}
if (platform.indexOf('linux') !== -1) {
return "Unix"
}
return null;
}
export const isAppleSilicon = () => {
const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl');
// Best guess if the device is an Apple Silicon
// https://stackoverflow.com/a/65412357
// @ts-expect-error - Object is possibly 'null'
return gl.getSupportedExtensions().indexOf('WEBGL_compressed_texture_etc') !== -1
}