UNPKG

date-manip

Version:

A lightweight JavaScript date utility library that provides modularity, high performance, and additional features. It supports various date operations, including date addition and subtraction, formatting, comparison, etc.

48 lines (47 loc) 1.16 kB
import units from "./units.mjs"; import { M as MS_IN_DAY } from "./constants-BuFquuHl.mjs"; import { n as normalizeUnit } from "./normalizeUnit-bp5SODxw.mjs"; const { DATE, DAY, HOUR, MILLISECOND, MINUTE, MONTH, SECOND, TIME, YEAR } = units; const mapping = { [YEAR]: "FullYear", [MONTH]: "Month", [DATE]: "Date", [DAY]: "Day", [HOUR]: "Hours", [MINUTE]: "Minutes", [SECOND]: "Seconds", [MILLISECOND]: "Milliseconds", [TIME]: "Time" }; const setting = {}; const getting = {}; Object.entries(mapping).forEach(([key, method]) => { if (key === DAY) { setting[key] = function(date, val) { const day = date.getDay(); if (val !== day) { date.setTime(+date + (val - day) * MS_IN_DAY); } }; } else { setting[key] = function(date, val) { date[`set${method}`](val); }; } getting[key] = function(date) { return date[`get${method}`](); }; }); function get(date, unit) { const fn = getting[normalizeUnit(unit)]; return fn ? fn(date) : 0; } function set(date, unit, val) { const fn = setting[normalizeUnit(unit)]; fn && fn(date, val); return date; } export { get as g, set as s };