@bckd00r/time-parse
Version:
✨ A simple time parser module
73 lines • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const standartOptions = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12,
daysPerYear: 365.25,
};
const smallValues = {
ms: 0.001,
s: 1,
m: 60,
h: 3600,
};
const TimeAbbreve = {
ms: ['ms', 'milli', 'millisecond', 'milliseconds', 'milisaniye'],
s: ['s', 'sec', 'secs', 'second', 'seconds', 'saniye'],
m: ['m', 'min', 'mins', 'minute', 'minutes', 'dak', 'dakika'],
h: ['h', 'hr', 'hrs', 'hour', 'hours', 'saat', 'saatler'],
d: ['d', 'day', 'days', 'gun', 'gunler'],
w: ['w', 'week', 'weeks', 'hafta', 'haftalar'],
mth: ['mon', 'mth', 'mths', 'month', 'months', 'ay', 'aylar'],
y: ['y', 'yr', 'yrs', 'year', 'years', 'yil'],
};
function parse(timeString, timeName, options) {
options = Object.assign({}, standartOptions, options || {});
let total = 0;
const timeValues = getTimeValues(options);
const list = timeString
.toLowerCase()
.replace(/[^.\w+-]+/g, '')
.match(/[-+]?[0-9.]+[a-zşğiüöç]+/g);
if (!list || list.length < 1)
throw new Error(`Unexpected time string, ${timeString}`);
list.forEach((e) => {
const value = e.match(/[0-9.]+/g)[0];
const time = e.match(/[a-zşğiüöç]+/g)[0].replace(/ü/g, "u").replace(/ı/g, "i").replace(/ö/g, "o").replace(/ç/g, "c").replace(/ş/, "s");
total += getSeconds(value, time, timeValues);
});
if (timeName)
return convert(total, timeName, timeValues);
return total * 1000;
}
exports.default = parse;
function getTimeValues(options) {
const day = options.hoursPerDay * smallValues.h;
const week = options.daysPerWeek * day;
const month = (options.daysPerYear / options.monthsPerYear) * day;
const year = options.daysPerYear * day;
const timeValues = {
d: day,
w: week,
mth: month,
y: year,
};
return Object.assign(Object.assign({}, smallValues), timeValues);
}
function getStringKey(timeName) {
for (const key in TimeAbbreve) {
const abbreve = TimeAbbreve[key];
if (abbreve.includes(timeName))
return key;
}
throw new Error(`Unexpected abbreve, ${timeName}`);
}
function getSeconds(value, time, values) {
return value * values[getStringKey(time)];
}
function convert(value, time, timeValues) {
return value / timeValues[getStringKey(time)];
}
//# sourceMappingURL=index.js.map