danholibraryjs 1.5.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/.gitattributes +2 -2
  2. package/README.md +12 -304
  3. package/Time.xlsx +0 -0
  4. package/dist/Classes/{Event.d.ts → Events/Event.d.ts} +9 -8
  5. package/dist/Classes/{Event.js → Events/Event.js} +17 -9
  6. package/dist/Classes/{EventCollection.d.ts → Events/EventCollection.d.ts} +10 -10
  7. package/dist/Classes/{EventCollection.js → Events/EventCollection.js} +28 -29
  8. package/dist/Classes/{EventEmitter.d.ts → Events/EventEmitter.d.ts} +33 -7
  9. package/dist/Classes/{EventEmitter.js → Events/EventEmitter.js} +33 -3
  10. package/dist/Classes/Events/index.d.ts +3 -0
  11. package/dist/Classes/Events/index.js +19 -0
  12. package/dist/Classes/Time/Date.d.ts +147 -0
  13. package/dist/Classes/Time/Date.js +238 -0
  14. package/dist/Classes/Time/Time.d.ts +65 -0
  15. package/dist/Classes/Time/Time.js +117 -0
  16. package/dist/Classes/Time/TimeProperties.d.ts +3 -0
  17. package/dist/{Interfaces/BaseEventInterface.js → Classes/Time/TimeProperties.js} +0 -0
  18. package/dist/Classes/Time/TimeSpan.d.ts +123 -0
  19. package/dist/Classes/Time/TimeSpan.js +179 -0
  20. package/dist/Classes/Time/index.d.ts +4 -0
  21. package/dist/Classes/Time/index.js +20 -0
  22. package/dist/Classes/index.d.ts +2 -3
  23. package/dist/Classes/index.js +7 -4
  24. package/dist/Classes/store.d.ts +79 -0
  25. package/dist/Classes/store.js +85 -0
  26. package/dist/Extensions/Array.d.ts +31 -0
  27. package/dist/Extensions/Array.js +26 -0
  28. package/dist/Extensions/Map.d.ts +38 -0
  29. package/dist/Extensions/Map.js +31 -0
  30. package/dist/Extensions/Object.d.ts +16 -0
  31. package/dist/Extensions/Object.js +8 -0
  32. package/dist/Extensions/String.d.ts +26 -0
  33. package/dist/Extensions/String.js +17 -0
  34. package/dist/Extensions/index.d.ts +29 -0
  35. package/dist/Extensions/index.js +71 -0
  36. package/dist/Functions/GetCSSProperty.d.ts +15 -0
  37. package/dist/Functions/GetCSSProperty.js +26 -0
  38. package/dist/Functions/HTMLEvent.d.ts +6 -0
  39. package/dist/Functions/HTMLEvent.js +6 -0
  40. package/dist/Functions/SetNavigationSelected.js +2 -0
  41. package/dist/Functions/index.d.ts +1 -0
  42. package/dist/Functions/index.js +6 -1
  43. package/dist/Interfaces/ElementOptions.d.ts +12 -15
  44. package/dist/Interfaces/IReplacement.d.ts +1 -1
  45. package/dist/Interfaces/index.d.ts +0 -1
  46. package/dist/Interfaces/index.js +5 -2
  47. package/dist/Types/BetterTypes.d.ts +9 -0
  48. package/dist/{Interfaces/IHTMLEvent.js → Types/BetterTypes.js} +0 -0
  49. package/dist/Types/Date.d.ts +6 -0
  50. package/dist/Types/{EventHandler.js → Date.js} +0 -0
  51. package/dist/Types/Events.d.ts +10 -0
  52. package/dist/Types/{IElement.js → Events.js} +0 -0
  53. package/dist/Types/PropertiesWith.d.ts +13 -0
  54. package/dist/Types/{StringRegex.js → PropertiesWith.js} +0 -0
  55. package/dist/Types/TransformTypes.d.ts +13 -0
  56. package/dist/Types/TransformTypes.js +2 -0
  57. package/dist/Types/index.d.ts +31 -3
  58. package/dist/Types/index.js +10 -4
  59. package/dist/index.d.ts +5 -5
  60. package/dist/index.js +19 -10
  61. package/docs/Classes.md +488 -0
  62. package/docs/Extensions.md +146 -0
  63. package/docs/Functions.md +53 -0
  64. package/docs/Interfaces.md +34 -0
  65. package/docs/Types.md +137 -0
  66. package/docs/index.md +29 -0
  67. package/package.json +22 -21
  68. package/src/Classes/{Event.ts → Events/Event.ts} +19 -14
  69. package/src/Classes/Events/EventCollection.ts +109 -0
  70. package/src/Classes/{EventEmitter.ts → Events/EventEmitter.ts} +37 -11
  71. package/src/Classes/Events/index.ts +3 -0
  72. package/src/Classes/Time/Date.ts +264 -0
  73. package/src/Classes/Time/Time.ts +134 -0
  74. package/src/Classes/Time/TimeProperties.ts +3 -0
  75. package/src/Classes/Time/TimeSpan.ts +195 -0
  76. package/src/Classes/Time/index.ts +4 -0
  77. package/src/Classes/index.ts +2 -3
  78. package/src/Classes/store.ts +95 -0
  79. package/src/Extensions/Array.ts +57 -0
  80. package/src/Extensions/Map.ts +73 -0
  81. package/src/Extensions/Object.ts +25 -0
  82. package/src/Extensions/String.ts +43 -0
  83. package/src/Extensions/index.ts +83 -0
  84. package/src/Functions/GetCSSProperty.ts +27 -0
  85. package/src/Functions/HTMLEvent.ts +6 -0
  86. package/src/Functions/SetNavigationSelected.ts +1 -0
  87. package/src/Functions/index.ts +2 -1
  88. package/src/Interfaces/ElementOptions.ts +18 -14
  89. package/src/Interfaces/IReplacement.ts +1 -1
  90. package/src/Interfaces/index.ts +1 -2
  91. package/src/Types/BetterTypes.ts +10 -0
  92. package/src/Types/Date.ts +7 -0
  93. package/src/Types/Events.ts +15 -0
  94. package/src/Types/PropertiesWith.ts +14 -0
  95. package/src/Types/TransformTypes.ts +18 -0
  96. package/src/Types/index.ts +36 -3
  97. package/src/index.ts +5 -6
  98. package/tsconfig.json +99 -7
  99. package/dist/Extensions.d.ts +0 -85
  100. package/dist/Extensions.js +0 -84
  101. package/dist/Interfaces/BaseEventInterface.d.ts +0 -4
  102. package/dist/Interfaces/IHTMLEvent.d.ts +0 -4
  103. package/dist/Types/EventHandler.d.ts +0 -7
  104. package/dist/Types/IElement.d.ts +0 -9
  105. package/dist/Types/StringRegex.d.ts +0 -2
  106. package/src/Classes/EventCollection.ts +0 -116
  107. package/src/Extensions.ts +0 -185
  108. package/src/Interfaces/BaseEventInterface.ts +0 -4
  109. package/src/Types/EventHandler.ts +0 -12
  110. package/src/Types/IElement.ts +0 -9
  111. package/src/Types/StringRegex.ts +0 -2
  112. package/test.js +0 -20
@@ -1,11 +1,37 @@
1
- import BaseEvent from '../Interfaces/BaseEventInterface';
2
- import EventHandler from '../Types/EventHandler';
1
+ import { BaseEvent, EventHandler } from '../../Types';
3
2
  /**
4
3
  * Traditional Node.js EventEmitter for vanilla JavaScript
4
+ * @borrows EventCollection
5
+ * @borrows BaseEvent
6
+ * @borrows EventHandler
7
+ *
8
+ * @example ```ts
9
+ * import { EventEmitter } from 'danholibraryjs';
10
+ *
11
+ * type EventTypes = {
12
+ * create: [username: string, password: string],
13
+ * update: [id: string, user: User],
14
+ * delete: [id: string, reason?: string],
15
+ * }
16
+ *
17
+ * const emitter = new EventEmitter<EventTypes>(new Map([
18
+ * ['create', (username, password) => {
19
+ * return { username, password };
20
+ * }],
21
+ * ['update', (id, user) => {
22
+ * return { id, ...user };
23
+ * }]
24
+ * ]));
25
+ *
26
+ * const onDelete = (id: string, reason?: string) => console.log(`User ${id} was deleted because ${reason}`);
27
+ * emitter.on('delete', onDelete);
28
+ * emitter.emit('delete', '1', 'No longer needed');
29
+ * emitter.off('delete', onDelete);
30
+ * ```
5
31
  */
6
- export declare class EventEmitter<Events extends BaseEvent> {
32
+ export declare class EventEmitter<Events extends BaseEvent<string, Array<any>>> {
7
33
  /**@param events Map<name: string, handlers: EventHandler[]>*/
8
- constructor(events?: Map<keyof Events, EventHandler<Events, keyof Events>[]>);
34
+ constructor(events?: Map<keyof Events, EventHandler<Events>>);
9
35
  /**@private Internal event collection*/
10
36
  private _events;
11
37
  /**
@@ -21,14 +47,14 @@ export declare class EventEmitter<Events extends BaseEvent> {
21
47
  * @param listener Callback function to run, when event occurs
22
48
  * @returns this
23
49
  */
24
- once<Return extends any, Event extends keyof Events>(event: keyof Events, listener: EventHandler<Events, Event, Return>): this;
50
+ once<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>): this;
25
51
  /**
26
52
  * Removes listener(s) from event
27
53
  * @param event Event to get collection of listeners | "all"
28
54
  * @param listener If left null, removes all listeners tied to event, else only removes listener from event
29
55
  * @returns this
30
56
  */
31
- off<Event extends keyof Events>(event?: Event | string, listener?: EventHandler<Events, Event>): this;
57
+ off<Return extends any, Event extends keyof Events>(event?: Event | 'all', listener?: EventHandler<Events, Event, Return>): this;
32
58
  /**
33
59
  * Emits event and runs all listeners tied to event
34
60
  * @param event Event to emit
@@ -36,7 +62,7 @@ export declare class EventEmitter<Events extends BaseEvent> {
36
62
  * @fires event
37
63
  * @returns Array of listeners' reponses
38
64
  */
39
- emit<ReturnType extends any, Event extends keyof Events>(event: Event, args: Events[Event]): ReturnType[];
65
+ emit<Return extends any, Event extends keyof Events>(event: Event, ...args: Events[Event]): Array<Return>;
40
66
  /**
41
67
  * Limits how many events to accept using EventEmitter#on or EventEmitter#once
42
68
  * @param event: Specific event to limit, or by default, 'all'
@@ -1,9 +1,39 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.EventEmitter = void 0;
4
- const EventCollection_1 = require("./EventCollection");
7
+ const EventCollection_1 = __importDefault(require("./EventCollection"));
5
8
  /**
6
9
  * Traditional Node.js EventEmitter for vanilla JavaScript
10
+ * @borrows EventCollection
11
+ * @borrows BaseEvent
12
+ * @borrows EventHandler
13
+ *
14
+ * @example ```ts
15
+ * import { EventEmitter } from 'danholibraryjs';
16
+ *
17
+ * type EventTypes = {
18
+ * create: [username: string, password: string],
19
+ * update: [id: string, user: User],
20
+ * delete: [id: string, reason?: string],
21
+ * }
22
+ *
23
+ * const emitter = new EventEmitter<EventTypes>(new Map([
24
+ * ['create', (username, password) => {
25
+ * return { username, password };
26
+ * }],
27
+ * ['update', (id, user) => {
28
+ * return { id, ...user };
29
+ * }]
30
+ * ]));
31
+ *
32
+ * const onDelete = (id: string, reason?: string) => console.log(`User ${id} was deleted because ${reason}`);
33
+ * emitter.on('delete', onDelete);
34
+ * emitter.emit('delete', '1', 'No longer needed');
35
+ * emitter.off('delete', onDelete);
36
+ * ```
7
37
  */
8
38
  class EventEmitter {
9
39
  /**@param events Map<name: string, handlers: EventHandler[]>*/
@@ -49,8 +79,8 @@ class EventEmitter {
49
79
  * @fires event
50
80
  * @returns Array of listeners' reponses
51
81
  */
52
- emit(event, args) {
53
- return this._events.emit(event, args);
82
+ emit(event, ...args) {
83
+ return this._events.emit(event, ...args);
54
84
  }
55
85
  /**
56
86
  * Limits how many events to accept using EventEmitter#on or EventEmitter#once
@@ -0,0 +1,3 @@
1
+ export * from './Event';
2
+ export * from './EventCollection';
3
+ export * from './EventEmitter';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Event"), exports);
18
+ __exportStar(require("./EventCollection"), exports);
19
+ __exportStar(require("./EventEmitter"), exports);
@@ -0,0 +1,147 @@
1
+ import { BetterOmit, PartialExcept, LongMonth, LongDay, ShortDay, ShortMonth } from "../../Types";
2
+ import TimeProperties from "./TimeProperties";
3
+ import TimeSpan, { TimeSpanFormat } from "./TimeSpan";
4
+ declare type Data = PartialExcept<BetterOmit<TimeProperties<true>, 'weeks'>, 'years' | 'months'>;
5
+ declare type Double = `${number}${number}`;
6
+ declare type Quadruple = `${Double}${Double}`;
7
+ declare type DateFormat = `${Double}/${Double}/${Quadruple}`;
8
+ /**
9
+ * Type used to construct DanhoDate.
10
+ * @Data Partial TimeProperties except years & months
11
+ * @DateFormat string as dd/MM/yyyy
12
+ */
13
+ export declare type DanhoDateConstructor = Data | DateFormat | number | Date;
14
+ declare class DanhoDate {
15
+ /**
16
+ * Returns the value of the current irl time
17
+ */
18
+ static get now(): number;
19
+ constructor(data?: DanhoDateConstructor);
20
+ private _formats;
21
+ private _format;
22
+ date: Date;
23
+ /**
24
+ * Year of the date
25
+ */
26
+ get year(): number;
27
+ set year(value: number);
28
+ /**
29
+ * Month of the date
30
+ */
31
+ get month(): number;
32
+ set month(value: number);
33
+ /**
34
+ * Days in the month of the date
35
+ */
36
+ get daysInMonth(): number;
37
+ /**
38
+ * Week of the year the day is in
39
+ */
40
+ get week(): number;
41
+ set week(value: number);
42
+ /**
43
+ * Week of the month the day is in
44
+ */
45
+ get weekOfMonth(): number;
46
+ /**
47
+ * Day of the date
48
+ */
49
+ get day(): number;
50
+ set day(value: number);
51
+ get dayOfWeek(): number;
52
+ set dayOfWeek(value: number);
53
+ /**
54
+ * Hours of the date
55
+ */
56
+ get hours(): number;
57
+ set hours(value: number);
58
+ get isPM(): boolean;
59
+ /**
60
+ * Minutes of the date
61
+ */
62
+ get minutes(): number;
63
+ set minutes(value: number);
64
+ /**
65
+ * Seconds of the date
66
+ */
67
+ get seconds(): number;
68
+ set seconds(value: number);
69
+ /**
70
+ * Milliseconds of the date
71
+ */
72
+ get milliseconds(): number;
73
+ set milliseconds(value: number);
74
+ /**
75
+ * Millisecond value of internal time
76
+ */
77
+ get time(): number;
78
+ set time(value: number);
79
+ /**
80
+ * Week day i.e. Monday
81
+ */
82
+ get weekDay(): LongDay;
83
+ /**
84
+ * Short week day i.e. Mon
85
+ */
86
+ get weekDayShort(): ShortDay;
87
+ /**
88
+ * Month name i.e. February
89
+ */
90
+ get monthName(): LongMonth;
91
+ /**
92
+ * Short month name i.e. Feb
93
+ */
94
+ get monthNameShort(): ShortMonth;
95
+ /**
96
+ * Sets internal date property
97
+ * @param data Time properties to set - replacement of i.e. Date.setHours(value: number): number
98
+ * @returns This, with updated properties
99
+ */
100
+ set(data: Partial<Data>): this;
101
+ /**
102
+ * Calculates the time between this and provided date
103
+ * @param date Date information
104
+ * @returns TimeSpan between this and provided date
105
+ */
106
+ between(date?: DanhoDate | DanhoDateConstructor): TimeSpan;
107
+ /**
108
+ * String representation of this
109
+ * @param format String format of date
110
+ * @returns String representation of this
111
+ *
112
+ * @$year Replaced with year of the date i.e. 2022
113
+ *
114
+ * @$month Replaced with month name i.e. March
115
+ * @$daysInMonth Replaced with the amount of days in the month i.e. 31
116
+ * @$MM replaced with double digit month i.e. 01
117
+ * @$M replaced with single digit month i.e. 1
118
+ *
119
+ * @$week Replaced with week of the year i.e. 32
120
+ * @$weekOfMonth Replaced with the week of the month i.e. 3
121
+ *
122
+ * @$weekday Replaced with day of the week i.e. Wednesday
123
+ * @$dd Replaced with double digit day i.e. 02
124
+ * @$d Replaced with single digit day i.e. 2
125
+ * @$ddth Replaced with double digit day + "th" i.e. 05th
126
+ * @$dth Replaced with double digit day + "th" i.e. 5th
127
+ *
128
+ * @$hh12 Replaced with double digit hour in 12-hour format i.e. 09
129
+ * @$hh24 Replaced with double digit hour in 24-hour format i.e. 21
130
+ * @$h12 Replaced with single digit hour in 12-hour format i.e. 9
131
+ * @$h24 Replaced with single digit hour in 24-hour format i.e. 9
132
+ *
133
+ * @$mm Replaced with double digit minute i.e. 05
134
+ * @$m Replaced with single digit minute i.e. 5
135
+ *
136
+ * @$ss Replaced with double digit second i.e. 03
137
+ * @$s Replaced with single digit second i.e. 3
138
+ *
139
+ * @msms Replaced with double digit millisecond i.e. 02
140
+ * @ms Replaced with single digit millisecond i.e. 2
141
+ *
142
+ * @$relative Replaced with relative timeformat as TimeSpan
143
+ */
144
+ toString(format?: string, relativeFormat?: TimeSpanFormat): string;
145
+ }
146
+ export { DanhoDate as Date };
147
+ export default DanhoDate;
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Date = void 0;
7
+ const Time_1 = __importDefault(require("./Time"));
8
+ const TimeSpan_1 = __importDefault(require("./TimeSpan"));
9
+ class DanhoDate {
10
+ /**
11
+ * Returns the value of the current irl time
12
+ */
13
+ static get now() {
14
+ return Date.now();
15
+ }
16
+ constructor(data) {
17
+ // data not provided
18
+ if (!data)
19
+ this.date = new Date();
20
+ // data is typeof Date
21
+ else if (typeof data === 'object' && data instanceof Date) {
22
+ this.date = new Date(data);
23
+ }
24
+ // data is typeof Data
25
+ else if (typeof data === 'object') {
26
+ this.date = new Date(data.years, data.months - 1);
27
+ if (data.days)
28
+ this.date.setDate(data.days);
29
+ if (data.hours)
30
+ this.date.setHours(data.hours);
31
+ if (data.minutes)
32
+ this.date.setMinutes(data.minutes);
33
+ if (data.seconds)
34
+ this.date.setSeconds(data.seconds);
35
+ if (data.milliseconds)
36
+ this.date.setMilliseconds(data.milliseconds);
37
+ }
38
+ // data is string or number
39
+ else
40
+ this.date = new Date(data);
41
+ this._formats = new Map([
42
+ ['year', this.year],
43
+ ['daysInMonth', this.daysInMonth],
44
+ ['monthShort', this.monthNameShort],
45
+ ['month', this.monthName],
46
+ ['MM', Time_1.default.DoubleDigit(this.month)],
47
+ ['M', this.month],
48
+ ['weekMonth', this.weekOfMonth],
49
+ ['weekDay', this.weekDay],
50
+ ['weekDayShort', this.weekDayShort],
51
+ ['week', this.week],
52
+ ['dd', Time_1.default.DoubleDigit(this.day)],
53
+ ['ddth', `${Time_1.default.DoubleDigit(this.day)}${Time_1.default.th(this.day)}`],
54
+ ['d', this.day],
55
+ ['dth', Time_1.default.th(this.day, true)],
56
+ ['hh12', `${Time_1.default.DoubleDigit(this.hours)}${this.hours < 12 ? 'AM' : 'PM'}`],
57
+ ['h12', `${this.hours > 12 ? 12 - this.hours : this.hours}${this.hours < 12 ? 'AM' : 'PM'}`],
58
+ ['hh24', Time_1.default.DoubleDigit(this.hours)],
59
+ ['h24', this.hours],
60
+ ['mm', Time_1.default.DoubleDigit(this.minutes)],
61
+ ['m', this.minutes],
62
+ ['ss', Time_1.default.DoubleDigit(this.seconds)],
63
+ ['s', this.seconds],
64
+ ['msms', Time_1.default.DoubleDigit(this.milliseconds)],
65
+ ['ms', this.milliseconds]
66
+ ]).map((v, k) => [k, v.toString()]);
67
+ }
68
+ _formats;
69
+ _format(format) {
70
+ return format
71
+ .split(/\$/)
72
+ .map(v => v.split(/\W/)[0])
73
+ .filter(v => v)
74
+ .reduce((result, key) => result.replaceAll(key, this._formats.get(key)), format.replaceAll('$', ''));
75
+ }
76
+ date;
77
+ /**
78
+ * Year of the date
79
+ */
80
+ get year() { return this.date.getFullYear(); }
81
+ set year(value) { this.date.setFullYear(value); }
82
+ /**
83
+ * Month of the date
84
+ */
85
+ get month() { return this.date.getMonth() + 1; }
86
+ set month(value) { this.date.setMonth(value); }
87
+ /**
88
+ * Days in the month of the date
89
+ */
90
+ get daysInMonth() { return Time_1.default.daysInMonth[this.month - 1]; }
91
+ /**
92
+ * Week of the year the day is in
93
+ */
94
+ get week() {
95
+ const firstMonday = new Date(this.year, 1, 1);
96
+ if (firstMonday.getDay() !== 1) {
97
+ const daysToMon = Time_1.default.DayNames.reverse().reduce((result, day, i) => result.set(day, i + 1), new Map()).get(Time_1.default.DayNames[firstMonday.getDay() - 1]);
98
+ firstMonday.setDate(firstMonday.getDate() + daysToMon);
99
+ }
100
+ const timeDiff = Math.round(firstMonday.getTime() - this.time);
101
+ const timeSince = Time_1.default.millisecond % timeDiff;
102
+ const result = Math.ceil(timeSince / Time_1.default.week);
103
+ return result;
104
+ }
105
+ set week(value) { this.date.setDate(value * Time_1.default.week / Time_1.default.day); }
106
+ /**
107
+ * Week of the month the day is in
108
+ */
109
+ get weekOfMonth() { return Math.round(this.daysInMonth / (Time_1.default.week / Time_1.default.day)); }
110
+ /**
111
+ * Day of the date
112
+ */
113
+ get day() { return this.date.getDate(); }
114
+ set day(value) { this.date.setDate(value); }
115
+ get dayOfWeek() { return this.date.getDay(); }
116
+ set dayOfWeek(value) {
117
+ const current = this.dayOfWeek;
118
+ if (value > current)
119
+ var diff = value - current;
120
+ else
121
+ diff = current - value;
122
+ this.day -= diff;
123
+ }
124
+ /**
125
+ * Hours of the date
126
+ */
127
+ get hours() { return this.date.getHours(); }
128
+ set hours(value) { this.date.setHours(value); }
129
+ get isPM() {
130
+ return this.hours > 11;
131
+ }
132
+ /**
133
+ * Minutes of the date
134
+ */
135
+ get minutes() { return this.date.getMinutes(); }
136
+ set minutes(value) { this.date.setMinutes(value); }
137
+ /**
138
+ * Seconds of the date
139
+ */
140
+ get seconds() { return this.date.getSeconds(); }
141
+ set seconds(value) { this.date.setSeconds(value); }
142
+ /**
143
+ * Milliseconds of the date
144
+ */
145
+ get milliseconds() { return this.date.getMilliseconds(); }
146
+ set milliseconds(value) { this.date.setMilliseconds(value); }
147
+ /**
148
+ * Millisecond value of internal time
149
+ */
150
+ get time() { return this.date.getTime(); }
151
+ set time(value) { this.date.setTime(value); }
152
+ /**
153
+ * Week day i.e. Monday
154
+ */
155
+ get weekDay() { return Time_1.default.DayNames[this.dayOfWeek - 1] || Time_1.default.DayNames.at(-1); }
156
+ /**
157
+ * Short week day i.e. Mon
158
+ */
159
+ get weekDayShort() { return this.weekDay.substring(0, 3); }
160
+ /**
161
+ * Month name i.e. February
162
+ */
163
+ get monthName() { return Time_1.default.MonthNames[this.month - 1] || Time_1.default.MonthNames.at(-1); }
164
+ /**
165
+ * Short month name i.e. Feb
166
+ */
167
+ get monthNameShort() { return this.monthName.substring(0, 3); }
168
+ /**
169
+ * Sets internal date property
170
+ * @param data Time properties to set - replacement of i.e. Date.setHours(value: number): number
171
+ * @returns This, with updated properties
172
+ */
173
+ set(data) {
174
+ const { years, months, days, hours, minutes, seconds, milliseconds } = data;
175
+ const ymd = this.date.setFullYear(years || this.year, months, days);
176
+ const hmsms = new Date(ymd).setHours(hours || this.hours, minutes, seconds, milliseconds);
177
+ this.date = new Date(hmsms);
178
+ return this;
179
+ }
180
+ /**
181
+ * Calculates the time between this and provided date
182
+ * @param date Date information
183
+ * @returns TimeSpan between this and provided date
184
+ */
185
+ between(date) {
186
+ if (!date)
187
+ return new TimeSpan_1.default(this.date, new Date());
188
+ else if (date instanceof DanhoDate)
189
+ return new TimeSpan_1.default(this.date, date.date);
190
+ else if (date instanceof Date)
191
+ return new TimeSpan_1.default(this.date, date);
192
+ else if (typeof date === 'object')
193
+ return new DanhoDate(date).between(this.date);
194
+ return new TimeSpan_1.default(this.date, new Date(date));
195
+ }
196
+ /**
197
+ * String representation of this
198
+ * @param format String format of date
199
+ * @returns String representation of this
200
+ *
201
+ * @$year Replaced with year of the date i.e. 2022
202
+ *
203
+ * @$month Replaced with month name i.e. March
204
+ * @$daysInMonth Replaced with the amount of days in the month i.e. 31
205
+ * @$MM replaced with double digit month i.e. 01
206
+ * @$M replaced with single digit month i.e. 1
207
+ *
208
+ * @$week Replaced with week of the year i.e. 32
209
+ * @$weekOfMonth Replaced with the week of the month i.e. 3
210
+ *
211
+ * @$weekday Replaced with day of the week i.e. Wednesday
212
+ * @$dd Replaced with double digit day i.e. 02
213
+ * @$d Replaced with single digit day i.e. 2
214
+ * @$ddth Replaced with double digit day + "th" i.e. 05th
215
+ * @$dth Replaced with double digit day + "th" i.e. 5th
216
+ *
217
+ * @$hh12 Replaced with double digit hour in 12-hour format i.e. 09
218
+ * @$hh24 Replaced with double digit hour in 24-hour format i.e. 21
219
+ * @$h12 Replaced with single digit hour in 12-hour format i.e. 9
220
+ * @$h24 Replaced with single digit hour in 24-hour format i.e. 9
221
+ *
222
+ * @$mm Replaced with double digit minute i.e. 05
223
+ * @$m Replaced with single digit minute i.e. 5
224
+ *
225
+ * @$ss Replaced with double digit second i.e. 03
226
+ * @$s Replaced with single digit second i.e. 3
227
+ *
228
+ * @msms Replaced with double digit millisecond i.e. 02
229
+ * @ms Replaced with single digit millisecond i.e. 2
230
+ *
231
+ * @$relative Replaced with relative timeformat as TimeSpan
232
+ */
233
+ toString(format = "$dd/$MM/$year", relativeFormat) {
234
+ return this._format(format.replaceAll('$relative', this.between(new Date()).toString(relativeFormat)));
235
+ }
236
+ }
237
+ exports.Date = DanhoDate;
238
+ exports.default = DanhoDate;
@@ -0,0 +1,65 @@
1
+ import { LongMonth } from "../../Types/Date";
2
+ /** '2s' or 2000 */
3
+ export declare type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
4
+ export declare type TimeString = `${number}${TimeUnit}`;
5
+ export declare type TimeDelay = number | TimeString;
6
+ export declare const ValidTime: RegExp;
7
+ /**
8
+ * Converts input into milliseconds
9
+ * @param input Input to convert to ms. 1s | 2m | 3h | 1M | 60000
10
+ * @returns Millisecond value of input
11
+ */
12
+ export declare function ms(input: TimeDelay): number;
13
+ /**
14
+ * Time utility class
15
+ * @borrows TimeDelay
16
+ * @borrows ms
17
+ */
18
+ export declare class Time {
19
+ /**
20
+ * Array of amount of days in the months. 0 indexed
21
+ */
22
+ static get daysInMonth(): number[];
23
+ /**
24
+ * Amount of weeks per year
25
+ */
26
+ static get weeksInYear(): number;
27
+ /**
28
+ * Amount of days per year
29
+ */
30
+ static get daysInYear(): number;
31
+ /**
32
+ * Returns function that converts value into double digit string
33
+ * @returns (value: number): string
34
+ */
35
+ static DoubleDigit(value: number): string;
36
+ static th(value: number, includeValue?: boolean): string;
37
+ /**
38
+ * Array of names of the months. 0 idnexed
39
+ */
40
+ static get MonthNames(): LongMonth[];
41
+ /**
42
+ * Array of names of the days of the week. 0 indexed
43
+ */
44
+ static get DayNames(): ("Monday" | "Friday" | "Sunday" | "Tuesday" | "Wednesday" | "Thursday" | "Saturday")[];
45
+ /** Millisecond in milliseconds (I know that sounds weird but the others make sense) */
46
+ static get millisecond(): number;
47
+ /** Second in milliseconds */
48
+ static get second(): number;
49
+ /** Minute in milliseconds */
50
+ static get minute(): number;
51
+ /** Hour in milliseconds */
52
+ static get hour(): number;
53
+ /** Day in milliseconds */
54
+ static get day(): number;
55
+ /** Week in milliseconds */
56
+ static get week(): number;
57
+ /** Month in milliseconds */
58
+ static get month(): number;
59
+ /** Year in milliseconds */
60
+ static get year(): number;
61
+ /** Average month in milliseconds */
62
+ static get avgMonth(): number;
63
+ static ms(input: TimeDelay): number;
64
+ }
65
+ export default Time;