Skip to content

Instantly share code, notes, and snippets.

View ehsaneona's full-sized avatar
🏠
Working from home

Ehsan Akbarzadeh ehsaneona

🏠
Working from home
View GitHub Profile
@ehsaneona
ehsaneona / MakeCountFriendly.js
Created December 7, 2022 08:56
Make follower count friendly
const intlFormat = (num) => {
return new Intl.NumberFormat().format(Math.round(num * 10) / 10);
};
export const makeCountFriendly = (num) => {
if (num >= 1000000) return intlFormat(num / 1000000) + "M";
if (num >= 1000) return intlFormat(num / 1000) + "k";
return intlFormat(num);
};
@ehsaneona
ehsaneona / getExtention.js
Created November 28, 2022 08:29
Get file extentions
export const getExtention = (path) => {
return path.split("?")[0].split("#")[0].split(".").pop();
};
@ehsaneona
ehsaneona / objectUrlToFile.js
Last active November 17, 2022 10:28
ObjectUrl to js file
import StringHelper from "~/classess/Helpers/StringHelper";
export const objectUrlToFile = async (blob) => {
return new Promise(async (resolve) => {
let blb = await fetch(blob).then((r) => r.blob());
resolve(
new File([blb], StringHelper.getRandomString(100), {
type: blb.type,
})
);
@ehsaneona
ehsaneona / getHeightAndWidthFromFile.js
Created November 13, 2022 11:54
get height and width from js file
export const getHeightAndWidthFromFile = (file) =>
new Promise((resolve) => {
const dataURL = URL.createObjectURL(file);
const img = new Image();
img.onload = () => {
resolve({
height: img.height,
width: img.width,
});
};
@ehsaneona
ehsaneona / swapArrayIndex.js
Created November 13, 2022 08:39
Moving an Index from the Array and moving to the specified Index
export const swapArrayIndex = (array, currentIndex, moveIndex) => {
if (!array[moveIndex] || !array[currentIndex]) return array;
let changedArray = [...array];
changedArray[currentIndex] = array[moveIndex];
changedArray[moveIndex] = array[currentIndex];
return changedArray;
};
@ehsaneona
ehsaneona / getFileVideoFirstFrame.js
Created November 12, 2022 13:44
Get the first frame of the uploaded video via HTML form for thumbnail.
const getRandomString = (len) => {
let result = "";
const characters = "abcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < len; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
@ehsaneona
ehsaneona / getVideoDuration.js
Created November 12, 2022 13:41
Get the time of the uploaded video via HTML form
export const getVideoDuration = async (file) => {
const url = URL.createObjectURL(file);
return new Promise((resolve) => {
const video = document.createElement("video");
const source = document.createElement("source");
source.src = url;
video.preload = "metadata";
video.appendChild(source);
video.onloadedmetadata = () => {
resolve(video.duration);
@ehsaneona
ehsaneona / asyncMap.js
Last active November 13, 2022 13:13
async map for js
export const asyncMap = async (array, callback) => {
const results = array.map(async (item, index) => await callback(item, index));
return await Promise.all(results);
};