Last active
July 28, 2022 15:47
-
-
Save otnansirk/64cb2665db91b7210c139264f7c7b205 to your computer and use it in GitHub Desktop.
This is a helper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get domain from string url | |
* @param {*} url | |
* @returns string | |
*/ | |
export const getDomain = url => { | |
const urlParts = getApiUrl(url) | |
.split('/') | |
.slice(0, 3) | |
.join('/') | |
.split('.') | |
return urlParts | |
.slice(0) | |
.slice(-(urlParts.length === 4 ? 3 : 2)) | |
.join('.') | |
} | |
/** | |
* Second to string | |
* @param {number} seconds | |
* @returns string | |
*/ | |
export const secToStr = (seconds) => { | |
const dateTime = new Date(seconds * 1000).toISOString() | |
return seconds > 3600 ? dateTime.slice(11, -5): dateTime.slice(14, -5) | |
} | |
/** | |
* Random array with unique and length | |
* @param {array} array => data you want to randomized | |
* @param {number} n => the length of the array what you want to randomized | |
* @param {float} unique => Unique number used for random array | |
* @returns string | |
*/ | |
export const randomArray = (array, n, unique) => { | |
n = n == null ? 1 : n | |
const length = array == null ? 0 : array.length | |
if (!length || n < 1) { | |
return [] | |
} | |
n = n > length ? length : n | |
let index = -1 | |
const lastIndex = length - 1 | |
const result = [...array] | |
while (++index < n) { | |
const rand = index + Math.floor(unique * (lastIndex - index + 1)) | |
const value = result[rand] | |
result[rand] = result[index] | |
result[index] = value | |
} | |
return _.slice(result, 0, n) | |
} | |
/** | |
* Format file size | |
* @param {number} bytes | |
* @param {number} decimalPoint | |
* @returns string | |
*/ | |
export const formatFileSize = (bytes, decimalPoint) => { | |
if (bytes === 0) return '0 Bytes'; | |
var k = 1000, | |
dm = decimalPoint || 2, | |
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], | |
i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | |
} | |
/** | |
* Countdown | |
* @param deadline new Date() | |
* @return object | |
*/ | |
export const countdown = (deadline) => { | |
const now = new Date().getTime(); | |
const t = deadline - now; | |
const dd = Math.floor(t / (1000 * 60 * 60 * 24)); | |
const hh = Math.floor((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | |
const mm = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); | |
const ss = Math.floor((t % (1000 * 60)) / 1000); | |
const getTrueNumber = (x) => { | |
if (x<10) return '0'+x; | |
else return x; | |
} | |
return { | |
come : t, | |
day : getTrueNumber(dd), | |
hour : getTrueNumber(hh), | |
minute : getTrueNumber(mm), | |
second : getTrueNumber(ss) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment