Skip to content

Commit

Permalink
fix: DateTime -> XDate
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jun 15, 2023
1 parent 5bb9ac6 commit bd37d02
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 81 deletions.
48 changes: 0 additions & 48 deletions packages/date/src/DateTime.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/date/src/Locale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DateConfig} from "./interface.js";
import {XDateConfig} from "./interface.js";

function normalizeLocale(locale: string): string {
if (/^[a-z]{2}_.+$/.test(locale)) {
Expand All @@ -8,7 +8,7 @@ function normalizeLocale(locale: string): string {
return locale;
}

export function getDefaultConfig(): DateConfig {
export function getDefaultConfig(): XDateConfig {
const system = Intl.DateTimeFormat().resolvedOptions();
const timeZone = process.env.TZ ?? process.env.TIMEZONE ?? system.timeZone;
const locale = normalizeLocale(process.env.LANG ?? process.env.LANGUAGE ?? system.locale);
Expand All @@ -21,7 +21,7 @@ export function getDefaultConfig(): DateConfig {
return {locale, timeZone, weekBegins};
}

const config: DateConfig = getDefaultConfig();
const config: XDateConfig = getDefaultConfig();

export function setLocale(locale: string | string[]): void {
const [supported] = Intl.DateTimeFormat.supportedLocalesOf(locale);
Expand Down
53 changes: 53 additions & 0 deletions packages/date/src/XDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {assert} from "@bunt/assert";
import {XDateMutation} from "./XDateMutation.js";
import {XDateIntervalKind} from "./interface.js";

export class XDate {
readonly #date: Date;

constructor(date: string | number | Date = new Date()) {
this.#date = new Date(date);
assert(this.#date.getTime() > 0);
}

public get date(): Date {
return new Date(this.#date);
}

public static from(date: string | number | Date = new Date()): XDate {
return new this(date);
}

public getTime(): number {
return this.#date.getTime();
}

public getDate(): Date {
return this.date;
}

public toString(): string {
return this.date.toString();
}

public begins(kind: Exclude<XDateIntervalKind, "ms">): XDate {
return new XDate(XDateMutation.begins(kind, this.getTime()));
}

public ends(kind: Exclude<XDateIntervalKind, "ms">): XDate {
return new XDate(XDateMutation.ends(kind, this.getTime()));
}

public mutate(...mutations: [XDateIntervalKind, number][]): XDate {
return new XDate(XDateMutation.mutate(this.getTime(), ...mutations));
}

public set(...intervals: [XDateIntervalKind, number][]): XDate {
return new XDate(XDateMutation.set(this.getTime(), ...intervals));
}
}

/**
* @deprecated
*/
export const DateTime = XDate;
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import {isFunction} from "@bunt/is";
import {assert} from "@bunt/assert";
import {DateTimeKind, TimeMutateFn} from "./interface.js";
import {XDateIntervalKind, XTimeMutateFn} from "./interface.js";
import begins from "./mutators/begins.js";
import ends from "./mutators/ends.js";
import mutators from "./mutators/mutators.js";
import setters from "./mutators/setters.js";

export class DateTimeMutation {
public static begins(kind: Exclude<DateTimeKind, "ms">, time: number): number {
export class XDateMutation {
public static begins(kind: Exclude<XDateIntervalKind, "ms">, time: number): number {
assert(kind in begins);

return begins[kind](time);
}

public static ends(kind: Exclude<DateTimeKind, "ms">, time: number): number {
public static ends(kind: Exclude<XDateIntervalKind, "ms">, time: number): number {
assert(kind in begins);

return ends[kind](time);
}

public static set(time: number, ...intervals: [DateTimeKind, number][]): number {
public static set(time: number, ...intervals: [XDateIntervalKind, number][]): number {
return this.apply(setters, time, intervals);
}

public static mutate(time: number, ...mutations: [DateTimeKind, number][]): number {
public static mutate(time: number, ...mutations: [XDateIntervalKind, number][]): number {
return this.apply(mutators, time, mutations);
}

protected static apply(map: Map<DateTimeKind, TimeMutateFn>,
protected static apply(map: Map<XDateIntervalKind, XTimeMutateFn>,
time: number,
values: [DateTimeKind, number][]): number {
values: [XDateIntervalKind, number][]): number {
for (const [interval, value] of values) {
const fn = map.get(interval);
assert(isFunction(fn));
Expand All @@ -39,3 +39,8 @@ export class DateTimeMutation {
return time;
}
}

/**
* @deprecated
*/
export const DateTimeMutation = XDateMutation;
4 changes: 2 additions & 2 deletions packages/date/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./DateTime.js";
export * from "./DateTimeMutation.js";
export * from "./XDate.js";
export * from "./XDateMutation.js";
export * from "./Locale.js";
export * from "./interface.js";
8 changes: 4 additions & 4 deletions packages/date/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type DateTimeKind =
export type XDateIntervalKind =
"ms"
| "sec"
| "min"
Expand All @@ -8,16 +8,16 @@ export type DateTimeKind =
| "week"
| "year";

export type TimeMutateFn = (date: number, value: number) => number;
export type XTimeMutateFn = (date: number, value: number) => number;

export enum TimeMultiply {
export enum XTimeMultiply {
SEC = 1000,
MIN = SEC * 60,
HOUR = MIN * 60,
DAY = HOUR * 24,
}

export type DateConfig = {
export type XDateConfig = {
locale: string;
timeZone: string;
weekBegins: number;
Expand Down
18 changes: 9 additions & 9 deletions packages/date/src/mutators/mutators.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import {DateTimeKind, TimeMultiply, TimeMutateFn} from "../interface.js";
import {XDateIntervalKind, XTimeMultiply, XTimeMutateFn} from "../interface.js";

const hour: TimeMutateFn = (time, value) => time + (value * TimeMultiply.HOUR);
const day: TimeMutateFn = (time, value) => time + (value * TimeMultiply.DAY);
const hour: XTimeMutateFn = (time, value) => time + (value * XTimeMultiply.HOUR);
const day: XTimeMutateFn = (time, value) => time + (value * XTimeMultiply.DAY);

const week: TimeMutateFn = (time, value) => {
const week: XTimeMutateFn = (time, value) => {
const date = new Date(time);

return date.setDate(date.getDate() + (value * 7));
};

const month: TimeMutateFn = (time, value) => {
const month: XTimeMutateFn = (time, value) => {
const date = new Date(time);

return date.setMonth(date.getMonth() + value);
};

const year: TimeMutateFn = (time, value) => {
const year: XTimeMutateFn = (time, value) => {
const date = new Date(time);

return date.setFullYear(date.getFullYear() + value);
};

export default new Map<DateTimeKind, TimeMutateFn>([
export default new Map<XDateIntervalKind, XTimeMutateFn>([
["ms", (time: number, value: number): number => time + value],
["sec", (time: number, value: number): number => time + (value * TimeMultiply.SEC)],
["min", (time: number, value: number): number => time + (value * TimeMultiply.MIN)],
["sec", (time: number, value: number): number => time + (value * XTimeMultiply.SEC)],
["min", (time: number, value: number): number => time + (value * XTimeMultiply.MIN)],
["hour", hour],
["day", day],
["week", week],
Expand Down
14 changes: 7 additions & 7 deletions packages/date/src/mutators/setters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {DateTimeKind, TimeMutateFn} from "../interface.js";
import {XDateIntervalKind, XTimeMutateFn} from "../interface.js";

const hour: TimeMutateFn = (time, value) => new Date(time).setHours(value);
const day: TimeMutateFn = (time, value) => new Date(time).setDate(value);
const week: TimeMutateFn = (time, value) => new Date(time).setDate(value);
const month: TimeMutateFn = (time, value) => new Date(time).setDate(value);
const year: TimeMutateFn = (time, value) => new Date(time).setFullYear(value);
const hour: XTimeMutateFn = (time, value) => new Date(time).setHours(value);
const day: XTimeMutateFn = (time, value) => new Date(time).setDate(value);
const week: XTimeMutateFn = (time, value) => new Date(time).setDate(value);
const month: XTimeMutateFn = (time, value) => new Date(time).setDate(value);
const year: XTimeMutateFn = (time, value) => new Date(time).setFullYear(value);

export default new Map<DateTimeKind, TimeMutateFn>([
export default new Map<XDateIntervalKind, XTimeMutateFn>([
["ms", (time: number, value: number): number => new Date(time).setMilliseconds(value)],
["sec", (time: number, value: number): number => new Date(time).setSeconds(value)],
["min", (time: number, value: number): number => new Date(time).setMinutes(value)],
Expand Down

0 comments on commit bd37d02

Please sign in to comment.