mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 18:29:15 +03:00
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { UserRoleEnum } from '@educt/enums';
|
|
import { IUserRole } from '@educt/interfaces';
|
|
|
|
export function userHasRoles(userRoles: IUserRole[], roles: UserRoleEnum[]): boolean {
|
|
for (let i = 0; i < roles.length; i += 1) {
|
|
if (!userRoles.map(r => r.slug).includes(roles[i])) {
|
|
/**
|
|
* Unable to find current role in user roles
|
|
*/
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Return true if user roles contain any one
|
|
* of roles in second param
|
|
*
|
|
* @param userRoles Current user roles
|
|
* @param roles Roles to check
|
|
*/
|
|
export function userContainRoles(userRoles: IUserRole[], roles: UserRoleEnum[]): boolean {
|
|
return userRoles.map(r => r.slug).some(r => roles.includes(r));
|
|
}
|
|
|
|
/**
|
|
* Clean object values
|
|
*
|
|
* @param obj Current object that have null values
|
|
* @returns Clean object that don't have null values
|
|
*/
|
|
export function removeEmptyValues(obj: object) {
|
|
return Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== null));
|
|
}
|
|
|
|
/**
|
|
* Transform object data to FormData
|
|
*
|
|
* @param data Input data
|
|
* @returns Data formatted in FormData
|
|
*/
|
|
export function transformToFormData(data: object): FormData {
|
|
const formData = new FormData();
|
|
|
|
/**
|
|
* Append value to form-data format
|
|
*/
|
|
Object.entries(data).forEach(([key, value]) => value && formData.append(key, value));
|
|
|
|
return formData;
|
|
}
|
|
|
|
/**
|
|
* Transform bytes to readable format
|
|
*
|
|
* @param bytes Number of bytes
|
|
* @param decimals
|
|
* @returns string
|
|
*/
|
|
export function transformBytes(bytes: number, decimals = 2): string {
|
|
if (bytes === 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
|
}
|