Skip to content

Instantly share code, notes, and snippets.

@0xlkda
Last active May 14, 2024 12:17
Show Gist options
  • Save 0xlkda/fb2073e78abf1b5d92b6e9f704ca5a70 to your computer and use it in GitHub Desktop.
Save 0xlkda/fb2073e78abf1b5d92b6e9f704ca5a70 to your computer and use it in GitHub Desktop.
index of converters
import UnitOfDigital from './unit-of-digital.converter.js'
import UnitOfTime from './unit-of-time.converter.js'
export {
UnitOfDigital,
UnitOfTime,
}
let step = 1024
let denominations = ['B', 'KB', 'MB', 'GB', 'TB']
function convertUnitOfDigital(value, from, to) {
let indexFrom = denominations.findIndex((symbol) => from === symbol)
let indexTo = denominations.findIndex((symbol) => to === symbol)
if (indexFrom == -1 || indexTo == -1) return 'unknown'
let exponentiation = Math.abs(indexFrom - indexTo)
let convertUp = () => value * Math.pow(step, exponentiation)
let convertDown = () => value / Math.pow(step, exponentiation)
let equivalant = value
if (indexFrom < indexTo) equivalant = convertDown()
if (indexFrom > indexTo) equivalant = convertUp()
return equivalant
}
function createConverters(unit, value) {
return denominations.reduce((converters, denomination) =>
({ [denomination]: convertUnitOfDigital(value, unit, denomination), ...converters }), {})
}
let UnitOfDigital = {
Bytes: (bytes) => createConverters('B', bytes),
Kilobytes: (kilobytes) => createConverters('KB', kilobytes),
Megabytes: (megabytes) => createConverters('MB', megabytes),
Gigabytes: (gigabytes) => createConverters('GB', gigabytes),
Terabytes: (terabytes) => createConverters('TB', terabytes),
}
export default UnitOfDigital
let denominations = ['ms', 's', 'm', 'h', 'd', 'M', 'Y']
let Seconds = {
from(denomination, value) {
let units = {
ms: { from: (ms) => ms * 0.001 },
s: { from: (seconds) => seconds },
m: { from: (minutes) => minutes * 60 },
h: { from: (hours) => hours * 3600 },
d: { from: (days) => days * 86400 },
M: { from: (months) => months * 2.628e+6 },
Y: { from: (years) => years * 3.154e+7 },
}
return units[denomination].from(value)
},
to(denomination, seconds) {
let units = {
ms: { from: (seconds) => seconds * 1000 },
s: { from: (seconds) => seconds },
m: { from: (seconds) => seconds / 60 },
h: { from: (seconds) => seconds / 3600 },
d: { from: (seconds) => seconds / 86400 },
M: { from: (seconds) => seconds / 2.628e+6 },
Y: { from: (seconds) => seconds / 3.154e+7 },
}
return units[denomination].from(seconds)
},
}
function convertUnitOfTime(value, from, to) {
let seconds = Seconds.from(from, value)
let equivalant = Seconds.to(to, seconds)
return equivalant
}
function createConverters(unit, value) {
return denominations.reduce((converters, denomination) =>
({ [denomination]: convertUnitOfTime(value, unit, denomination), ...converters }), {})
}
let denominationsLongForm = {
ms: 'miliseconds',
s: 'seconds',
m: 'minutes',
h: 'hours',
d: 'days',
M: 'months',
Y: 'years',
}
function longForm(converter) {
for (const unit in converter) {
converter[denominationsLongForm[unit]] = converter[unit]
}
return converter
}
let UnitOfTime = {
Miliseconds: (ms) => longForm(createConverters('ms', ms)),
Seconds: (seconds) => longForm(createConverters('s', seconds)),
Minutes: (minutes) => longForm(createConverters('m', minutes)),
Hours: (hours) => longForm(createConverters('h', hours)),
Days: (days) => longForm(createConverters('d', days)),
Months: (months) => longForm(createConverters('M', months)),
Years: (years) => longForm(createConverters('Y', years)),
}
export default UnitOfTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment