Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
48 lines
1.1 KiB
Plaintext
48 lines
1.1 KiB
Plaintext
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
|
|
};
|