slate/common/utilities.js

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-06-09 01:53:30 +03:00
import BCrypt from "bcryptjs";
import * as Strings from "~/common/strings";
import * as Validations from "~/common/validations";
import * as Constants from "~/common/constants";
2021-01-21 00:50:29 +03:00
//NOTE(martina): this file is for utility functions that do not involve API calls
//For API related utility functions, see common/user-behaviors.js
//And for uploading related utility functions, see common/file-utilities.js
export const getImageUrlIfExists = (file, sizeLimit = null) => {
if (!file) return;
const coverImage = file.data?.coverImage;
if (coverImage) {
if (sizeLimit && coverImage.data.size && coverImage.data.size > sizeLimit) {
return;
}
if (coverImage?.data.url) {
return coverImage.data.url;
}
if (coverImage?.cid) {
return Strings.getURLfromCID(coverImage.cid);
}
}
if (Validations.isPreviewableImage(file.data.type)) {
if (sizeLimit && file.data.size > sizeLimit) {
return;
}
return Strings.getURLfromCID(file.cid);
}
};
2021-01-21 00:50:29 +03:00
export const getPublicAndPrivateFiles = ({ viewer }) => {
let publicFileIds = [];
for (let slate of viewer.slates) {
if (slate.isPublic) {
publicFileIds.push(...slate.objects.map((obj) => obj.id));
2021-01-21 00:50:29 +03:00
}
}
let publicFiles = [];
let privateFiles = [];
let library = viewer.library || [];
2021-01-21 00:50:29 +03:00
for (let file of library) {
if (file.isPublic || publicFileIds.includes(file.id)) {
2021-01-21 00:50:29 +03:00
publicFiles.push(file);
} else {
privateFiles.push(file);
}
}
return { publicFiles, privateFiles };
};
2021-03-19 19:10:34 +03:00
2021-03-30 17:21:54 +03:00
export const generateNumberByStep = ({ min, max, step = 1 }) => {
var numbers = [];
for (var n = min; n <= max; n += step) {
numbers.push(n);
}
const randomIndex = Math.floor(Math.random() * numbers.length);
return numbers[randomIndex];
};
2021-06-09 01:53:30 +03:00
export const encryptPasswordClient = async (text) => {
const salt = "$2a$06$Yl.tEYt9ZxMcem5e6AbeUO";
let hash = text;
const rounds = 5;
for (let i = 1; i <= rounds; i++) {
hash = await BCrypt.hash(text, salt);
}
return hash;
};
2021-06-01 02:41:00 +03:00
export const coerceToArray = (input) => {
if (!input) {
return [];
}
if (Array.isArray(input)) {
return input;
} else {
return [input];
}
};
2021-05-27 11:20:34 +03:00
export const getFileExtension = (filename) => filename?.split(".").pop();
export const getTimeDifferenceFromNow = (date, format = {}) => {
const defaultFormats = {
seconds: (time) => time + "s",
minutes: (time) => time + "m",
hours: (time) => time + "h",
days: (time) => time + "d",
currentYear: (month, day) => `${month} ${day}`,
default: (month, day, year) => `${month} ${day}, ${year}`,
};
const formatDate = { ...defaultFormats, ...format };
2021-05-27 11:20:34 +03:00
const pastDate = new Date(date);
const now = new Date();
const differenceInSeconds = Math.floor((now - pastDate) / 1000);
if (differenceInSeconds < 60) {
return formatDate.seconds(differenceInSeconds);
2021-05-27 11:20:34 +03:00
}
const differenceInMinutes = Math.floor(differenceInSeconds / 60);
if (differenceInMinutes < 60) {
return formatDate.minutes(differenceInMinutes);
2021-05-27 11:20:34 +03:00
}
const differenceInHours = Math.floor(differenceInMinutes / 60);
if (differenceInHours < 24) {
return formatDate.hours(differenceInHours);
2021-05-27 11:20:34 +03:00
}
const differenceInDays = Math.floor(differenceInHours / 24);
if (differenceInDays < 24) {
return formatDate.days(differenceInDays);
2021-05-27 11:20:34 +03:00
}
const currentYear = now.getFullYear();
const day = pastDate.getDay();
const month = pastDate.toLocaleString("default", { month: "short" });
2021-05-27 11:20:34 +03:00
const year = pastDate.getFullYear();
if (year === currentYear) {
return formatDate.currentYear(month, day);
2021-05-27 11:20:34 +03:00
}
return formatDate.default(month, day, year);
2021-05-27 11:20:34 +03:00
};
const isObject = (val) => val instanceof Object;
/**
NOTE(amine): This will take a prop and return a responsive object that we can use with emotion.
Let's say we have a size prop with current values {base: 64, mobile: 120}, and a mapper function
(size)=> ({width: size, height: size}).
It will return a responsive object
{ width: 64, height: 64,
'@media (min-width: 768px':{ width: 120, height: 120 } }
*/
export function mapResponsiveProp(prop, mapper) {
if (isObject(prop)) {
const { base, ...restProps } = prop;
let initialStyles = mapper(base) || {};
return Object.keys(restProps).reduce((styles, size) => {
const media = `@media (min-width: ${Constants.sizes[size]}px)`;
const mediaStyles = mapper(restProps[size]);
styles[media] = mediaStyles;
return styles;
}, initialStyles);
}
if (prop !== null) {
return mapper(prop);
}
return null;
}