mybase 1.1.20 → 1.1.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getWeekNumber = void 0;
4
+ // credit: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
5
+ /* For a given date, get the ISO week number
6
+
7
+ Based on information at:
8
+
9
+ THIS PAGE (DOMAIN EVEN) DOESN'T EXIST ANYMORE UNFORTUNATELY
10
+ http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
11
+
12
+ Algorithm is to find nearest thursday, it's year
13
+ is the year of the week number. Then get weeks
14
+ between that date and the first day of that year.
15
+
16
+ Note that dates in one year can be weeks of previous
17
+ or next year, overlap is up to 3 days.
18
+
19
+ e.g. 2014/12/29 is Monday in week 1 of 2015
20
+ 2012/1/1 is Sunday in week 52 of 2011
21
+ */
22
+ function getWeekNumber(d = new Date()) {
23
+ // Copy date so don't modify original
24
+ const date = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
25
+ // Set to nearest Thursday: current date + 4 - current day number
26
+ // Make Sunday's day number 7
27
+ date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
28
+ // Get first day of year
29
+ const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
30
+ // Calculate full weeks to nearest Thursday
31
+ const weekNo = Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
32
+ // Return the week number
33
+ return weekNo;
34
+ }
35
+ exports.getWeekNumber = getWeekNumber;
36
+ //# sourceMappingURL=getWeekNumber.js.map
@@ -0,0 +1,9 @@
1
+ import { getWeekNumber } from './getWeekNumber'
2
+
3
+
4
+ describe('getWeekNumber', () => {
5
+ it('should return the correct week number', () => {
6
+ expect(getWeekNumber(new Date('2012-01-01'))).toBe(52)
7
+ expect(getWeekNumber(new Date('2014-12-30'))).toBe(1)
8
+ })
9
+ })
@@ -0,0 +1,31 @@
1
+ // credit: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
2
+ /* For a given date, get the ISO week number
3
+
4
+ Based on information at:
5
+
6
+ THIS PAGE (DOMAIN EVEN) DOESN'T EXIST ANYMORE UNFORTUNATELY
7
+ http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
8
+
9
+ Algorithm is to find nearest thursday, it's year
10
+ is the year of the week number. Then get weeks
11
+ between that date and the first day of that year.
12
+
13
+ Note that dates in one year can be weeks of previous
14
+ or next year, overlap is up to 3 days.
15
+
16
+ e.g. 2014/12/29 is Monday in week 1 of 2015
17
+ 2012/1/1 is Sunday in week 52 of 2011
18
+ */
19
+ export function getWeekNumber(d: Date = new Date()): number {
20
+ // Copy date so don't modify original
21
+ const date = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
22
+ // Set to nearest Thursday: current date + 4 - current day number
23
+ // Make Sunday's day number 7
24
+ date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
25
+ // Get first day of year
26
+ const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
27
+ // Calculate full weeks to nearest Thursday
28
+ const weekNo = Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
29
+ // Return the week number
30
+ return weekNo;
31
+ }
package/ts/index.js CHANGED
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./funcs/getWeekNumber"), exports);
17
18
  __exportStar(require("./funcs/Geoip2Paths"), exports);
18
19
  __exportStar(require("./funcs/isLocal"), exports);
19
20
  __exportStar(require("./funcs/randomString"), exports);
@@ -39,4 +40,7 @@ __exportStar(require("./funcs/getMysql2"), exports);
39
40
  __exportStar(require("./funcs/initMysql2Pool"), exports);
40
41
  __exportStar(require("./funcs/randomUTFString"), exports);
41
42
  __exportStar(require("./funcs/MaxRuntimeHours"), exports);
43
+ // models
44
+ __exportStar(require("./models/Unixtime"), exports);
45
+ __exportStar(require("./models/Timespan"), exports);
42
46
  //# sourceMappingURL=index.js.map
package/ts/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./funcs/getWeekNumber"
1
2
  export * from "./funcs/Geoip2Paths";
2
3
  export * from "./funcs/isLocal"
3
4
  export * from "./funcs/randomString"
@@ -22,4 +23,10 @@ export * from "./funcs/getMysql1"
22
23
  export * from "./funcs/getMysql2"
23
24
  export * from "./funcs/initMysql2Pool"
24
25
  export * from "./funcs/randomUTFString"
25
- export * from "./funcs/MaxRuntimeHours"
26
+ export * from "./funcs/MaxRuntimeHours"
27
+
28
+
29
+
30
+ // models
31
+ export * from "./models/Unixtime"
32
+ export * from "./models/Timespan"
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Timespan = void 0;
4
+ class Timespan {
5
+ constructor(ms) {
6
+ this.ms = ms;
7
+ }
8
+ get miliseconds() {
9
+ return this.ms;
10
+ }
11
+ get seconds() {
12
+ return this.ms / 1000;
13
+ }
14
+ get minutes() {
15
+ return this.ms / 1000 / 60;
16
+ }
17
+ get hours() {
18
+ return this.ms / 1000 / 60 / 60;
19
+ }
20
+ get days() {
21
+ return this.ms / 1000 / 60 / 60 / 24;
22
+ }
23
+ get weeks() {
24
+ return this.ms / 1000 / 60 / 60 / 24 / 7;
25
+ }
26
+ get months() {
27
+ return this.ms / 1000 / 60 / 60 / 24 / 30;
28
+ }
29
+ static miliseconds(amount) {
30
+ return new Timespan(amount);
31
+ }
32
+ static seconds(amount) {
33
+ return new Timespan(amount * 1000);
34
+ }
35
+ static minutes(mins) {
36
+ return new Timespan(mins * 60 * 1000);
37
+ }
38
+ static hours(hours) {
39
+ return new Timespan(hours * 60 * 60 * 1000);
40
+ }
41
+ static days(days) {
42
+ return new Timespan(days * 24 * 60 * 60 * 1000);
43
+ }
44
+ static weeks(weeks) {
45
+ return new Timespan(weeks * 7 * 24 * 60 * 60 * 1000);
46
+ }
47
+ static months(months) {
48
+ return new Timespan(months * 30 * 24 * 60 * 60 * 1000);
49
+ }
50
+ }
51
+ exports.Timespan = Timespan;
52
+ //# sourceMappingURL=Timespan.js.map
@@ -0,0 +1,63 @@
1
+ export class Timespan {
2
+ constructor(private readonly ms: number){
3
+ }
4
+
5
+ get miliseconds() : number {
6
+ return this.ms
7
+ }
8
+
9
+ get seconds() : number {
10
+ return this.ms / 1000
11
+ }
12
+
13
+ get minutes() : number {
14
+ return this.ms / 1000 / 60
15
+ }
16
+
17
+ get hours() : number {
18
+ return this.ms / 1000 / 60 / 60
19
+ }
20
+
21
+ get days() : number {
22
+ return this.ms / 1000 / 60 / 60 / 24
23
+ }
24
+
25
+ get weeks() : number {
26
+ return this.ms / 1000 / 60 / 60 / 24 / 7
27
+ }
28
+
29
+ get months() : number {
30
+ return this.ms / 1000 / 60 / 60 / 24 / 30
31
+ }
32
+
33
+ static miliseconds(amount: number): Timespan {
34
+ return new Timespan(amount)
35
+ }
36
+
37
+ static seconds(amount: number): Timespan {
38
+ return new Timespan(amount * 1000)
39
+ }
40
+
41
+ static minutes(mins: number): Timespan {
42
+ return new Timespan(mins * 60 * 1000)
43
+ }
44
+
45
+ static hours(hours: number): Timespan {
46
+ return new Timespan(hours * 60 * 60 * 1000)
47
+ }
48
+
49
+ static days(days: number): Timespan {
50
+ return new Timespan(days * 24 * 60 * 60 * 1000)
51
+ }
52
+
53
+ static weeks(weeks: number): Timespan {
54
+ return new Timespan(weeks * 7 * 24 * 60 * 60 * 1000)
55
+ }
56
+
57
+ static months(months: number): Timespan {
58
+ return new Timespan(months * 30 * 24 * 60 * 60 * 1000)
59
+ }
60
+
61
+
62
+
63
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Unixtime = void 0;
4
+ const Timespan_1 = require("./Timespan");
5
+ // this will be a class to work with short and long version of unixtime format
6
+ class Unixtime {
7
+ static NOW() {
8
+ return new Unixtime(Date.now());
9
+ }
10
+ static now() {
11
+ return new Unixtime(Date.now());
12
+ }
13
+ greaterThan(other) {
14
+ return this.toLongUnixtime() > other.toLongUnixtime();
15
+ }
16
+ isEmpty() {
17
+ return this.toLongUnixtime() == 0;
18
+ }
19
+ lessThan(other) {
20
+ return this.toLongUnixtime() < other.toLongUnixtime();
21
+ }
22
+ get elapsed() {
23
+ return new Timespan_1.Timespan(Date.now() - this.toLongUnixtime());
24
+ }
25
+ constructor(value) {
26
+ if (typeof value === 'string') {
27
+ this.value = new Date(value);
28
+ }
29
+ else if (typeof value === 'number') {
30
+ // the number might be short or long form(from nodejs Date.now()
31
+ if (value < 10000000000)
32
+ value *= 1000;
33
+ else
34
+ value = Math.round(value);
35
+ // set the value
36
+ this.value = new Date(value);
37
+ }
38
+ else {
39
+ this.value = value;
40
+ }
41
+ }
42
+ addTimespan(timespan) {
43
+ this.value = new Date(this.value.getTime() + timespan.miliseconds);
44
+ return this;
45
+ }
46
+ subtractTimespan(timespan) {
47
+ this.value = new Date(this.value.getTime() - timespan.miliseconds);
48
+ return this;
49
+ }
50
+ addMiliseconds(miliseconds = 1) {
51
+ this.value = new Date(this.value.getTime() + miliseconds);
52
+ return this;
53
+ }
54
+ addSeconds(seconds = 1) {
55
+ this.value = new Date(this.value.getTime() + seconds * 1000);
56
+ return this;
57
+ }
58
+ addMinutes(minutes = 1) {
59
+ this.value = new Date(this.value.getTime() + minutes * 60000);
60
+ return this;
61
+ }
62
+ addHours(hours = 1) {
63
+ this.value = new Date(this.value.getTime() + hours * 3600000);
64
+ return this;
65
+ }
66
+ addDays(days = 1) {
67
+ this.value = new Date(this.value.getTime() + days * 86400000);
68
+ return this;
69
+ }
70
+ addMonths(months = 1) {
71
+ this.value.setMonth(this.value.getMonth() + months);
72
+ return this;
73
+ }
74
+ addYears(years = 1) {
75
+ this.value.setFullYear(this.value.getFullYear() + years);
76
+ return this;
77
+ }
78
+ yyyymmdd() {
79
+ let mm = this.value.getMonth() + 1;
80
+ let dd = this.value.getDate();
81
+ return [this.value.getFullYear(),
82
+ (mm > 9 ? '' : '0') + mm,
83
+ (dd > 9 ? '' : '0') + dd
84
+ ].join('');
85
+ }
86
+ yyymmddhh() {
87
+ let mm = this.value.getMonth() + 1;
88
+ let dd = this.value.getDate();
89
+ let hh = this.value.getHours();
90
+ return [this.value.getFullYear(),
91
+ (mm > 9 ? '' : '0') + mm,
92
+ (dd > 9 ? '' : '0') + dd,
93
+ (hh > 9 ? '' : '0') + hh
94
+ ].join('');
95
+ }
96
+ static from(value) {
97
+ return new Unixtime(value);
98
+ }
99
+ toShortUnixtime() {
100
+ return Math.round(this.toLongUnixtime() / 1000);
101
+ }
102
+ toLongUnixtime() {
103
+ return this.value.getTime();
104
+ }
105
+ toDate() {
106
+ return this.value;
107
+ }
108
+ toISOString() {
109
+ return this.value.toISOString();
110
+ }
111
+ elapsedSeconds() {
112
+ return Math.round((Date.now() - this.toLongUnixtime()) / 1000);
113
+ }
114
+ elapsedMinutes() {
115
+ return Math.round(this.elapsedSeconds() / 60);
116
+ }
117
+ }
118
+ exports.Unixtime = Unixtime;
119
+ Unixtime.EMPTY = new Unixtime(new Date(0));
120
+ //# sourceMappingURL=Unixtime.js.map
@@ -0,0 +1,98 @@
1
+ import { Unixtime } from './Unixtime';
2
+
3
+ describe('Unixtime', () => {
4
+ it('should create Unixtime object from Date', () => {
5
+ const date = new Date();
6
+ const unixtime = new Unixtime(date);
7
+ expect(unixtime.toDate()).toEqual(date);
8
+ });
9
+
10
+ it('should create Unixtime object from string', () => {
11
+ const dateString = '2022-01-01T00:00:00Z';
12
+ const date = new Date(dateString);
13
+ const unixtime = new Unixtime(dateString);
14
+ expect(unixtime.toDate()).toEqual(date);
15
+ });
16
+
17
+ it('should create Unixtime object from number (short form)', () => {
18
+ const timestamp = 1640995200; // January 1, 2022 00:00:00 UTC
19
+ const date = new Date(timestamp * 1000);
20
+ const unixtime = new Unixtime(timestamp);
21
+ expect(unixtime.toDate()).toEqual(date);
22
+ });
23
+
24
+ it('should create Unixtime object from number (long form)', () => {
25
+ const timestamp = 1640995200000; // January 1, 2022 00:00:00 UTC
26
+ const date = new Date(timestamp);
27
+ const unixtime = new Unixtime(timestamp);
28
+ expect(unixtime.toDate()).toEqual(date);
29
+ });
30
+
31
+ it('should convert Unixtime to short Unixtime', () => {
32
+ const date = new Date();
33
+ const unixtime = new Unixtime(date);
34
+ const shortUnixtime = Math.round(date.getTime() / 1000);
35
+ expect(unixtime.toShortUnixtime()).toEqual(shortUnixtime);
36
+ });
37
+
38
+ it('should convert Unixtime to long Unixtime', () => {
39
+ const date = new Date();
40
+ const unixtime = new Unixtime(date);
41
+ expect(unixtime.toLongUnixtime()).toEqual(date.getTime());
42
+ });
43
+
44
+ it('should convert Unixtime to ISO string', () => {
45
+ const date = new Date();
46
+ const unixtime = new Unixtime(date);
47
+ expect(unixtime.toISOString()).toEqual(date.toISOString());
48
+ });
49
+
50
+ it('should add miliseconds to Unixtime', () => {
51
+ const date = new Date();
52
+ const unixtime = new Unixtime(date);
53
+ const addedUnixtime = unixtime.addMiliseconds(1000);
54
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 1000));
55
+ });
56
+
57
+ it('should add seconds to Unixtime', () => {
58
+ const date = new Date();
59
+ const unixtime = new Unixtime(date);
60
+ const addedUnixtime = unixtime.addSeconds(1);
61
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 1000));
62
+ });
63
+
64
+ it('should add minutes to Unixtime', () => {
65
+ const date = new Date();
66
+ const unixtime = new Unixtime(date);
67
+ const addedUnixtime = unixtime.addMinutes(1);
68
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 60000));
69
+ });
70
+
71
+ it('should add hours to Unixtime', () => {
72
+ const date = new Date();
73
+ const unixtime = new Unixtime(date);
74
+ const addedUnixtime = unixtime.addHours(1);
75
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 3600000));
76
+ });
77
+
78
+ it('should add days to Unixtime', () => {
79
+ const date = new Date();
80
+ const unixtime = new Unixtime(date);
81
+ const addedUnixtime = unixtime.addDays(1);
82
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 86400000));
83
+ });
84
+
85
+ it('should add months to Unixtime', () => {
86
+ const date = new Date();
87
+ const unixtime = new Unixtime(date);
88
+ const addedUnixtime = unixtime.addMonths(1);
89
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.setMonth(date.getMonth() + 1)));
90
+ });
91
+
92
+ it('should add years to Unixtime', () => {
93
+ const date = new Date();
94
+ const unixtime = new Unixtime(date);
95
+ const addedUnixtime = unixtime.addYears(1);
96
+ expect(addedUnixtime.toDate()).toEqual(new Date(date.setFullYear(date.getFullYear() + 1)));
97
+ });
98
+ });
@@ -0,0 +1,144 @@
1
+ import { Timespan } from "./Timespan"
2
+
3
+ // this will be a class to work with short and long version of unixtime format
4
+ export class Unixtime {
5
+ private value: Date
6
+
7
+
8
+ static EMPTY = new Unixtime(new Date(0))
9
+
10
+ static NOW() {
11
+ return new Unixtime(Date.now())
12
+ }
13
+
14
+ static now() {
15
+ return new Unixtime(Date.now())
16
+ }
17
+
18
+ greaterThan(other: Unixtime) {
19
+ return this.toLongUnixtime() > other.toLongUnixtime()
20
+ }
21
+
22
+ isEmpty() {
23
+ return this.toLongUnixtime() == 0
24
+ }
25
+
26
+ lessThan(other: Unixtime) {
27
+ return this.toLongUnixtime() < other.toLongUnixtime()
28
+ }
29
+
30
+ get elapsed():Timespan {
31
+ return new Timespan(Date.now() - this.toLongUnixtime())
32
+ }
33
+
34
+
35
+ constructor(value: Date | string | number) {
36
+ if (typeof value === 'string') {
37
+ this.value = new Date(value)
38
+ } else if (typeof value === 'number') {
39
+ // the number might be short or long form(from nodejs Date.now()
40
+ if (value < 10000000000) value *= 1000
41
+ else
42
+ value = Math.round(value)
43
+ // set the value
44
+ this.value = new Date(value)
45
+ } else {
46
+ this.value = value
47
+ }
48
+ }
49
+
50
+ public addTimespan(timespan: Timespan): Unixtime {
51
+ this.value = new Date(this.value.getTime() + timespan.miliseconds)
52
+ return this
53
+ }
54
+
55
+ public subtractTimespan(timespan: Timespan): Unixtime {
56
+ this.value = new Date(this.value.getTime() - timespan.miliseconds)
57
+ return this
58
+ }
59
+
60
+ public addMiliseconds(miliseconds: number = 1): Unixtime {
61
+ this.value = new Date(this.value.getTime() + miliseconds)
62
+ return this
63
+ }
64
+
65
+ public addSeconds(seconds: number = 1): Unixtime {
66
+ this.value = new Date(this.value.getTime() + seconds * 1000)
67
+ return this
68
+ }
69
+
70
+ public addMinutes(minutes: number = 1): Unixtime {
71
+ this.value = new Date(this.value.getTime() + minutes * 60000)
72
+ return this
73
+ }
74
+
75
+ public addHours(hours: number = 1): Unixtime {
76
+ this.value = new Date(this.value.getTime() + hours * 3600000)
77
+ return this
78
+ }
79
+
80
+ public addDays(days: number = 1): Unixtime {
81
+ this.value = new Date(this.value.getTime() + days * 86400000)
82
+ return this
83
+ }
84
+
85
+ public addMonths(months: number = 1): Unixtime {
86
+ this.value.setMonth(this.value.getMonth() + months)
87
+ return this
88
+ }
89
+
90
+ public addYears(years: number = 1): Unixtime {
91
+ this.value.setFullYear(this.value.getFullYear() + years)
92
+ return this
93
+ }
94
+
95
+ public yyyymmdd(): string {
96
+ let mm = this.value.getMonth() + 1
97
+ let dd = this.value.getDate()
98
+ return [this.value.getFullYear(),
99
+ (mm > 9 ? '' : '0') + mm,
100
+ (dd > 9 ? '' : '0') + dd
101
+ ].join('')
102
+ }
103
+
104
+ public yyymmddhh(): string {
105
+ let mm = this.value.getMonth() + 1
106
+ let dd = this.value.getDate()
107
+ let hh = this.value.getHours()
108
+ return [this.value.getFullYear(),
109
+ (mm > 9 ? '' : '0') + mm,
110
+ (dd > 9 ? '' : '0') + dd,
111
+ (hh > 9 ? '' : '0') + hh
112
+ ].join('')
113
+ }
114
+
115
+
116
+ static from(value: Date | string | number) {
117
+ return new Unixtime(value)
118
+ }
119
+
120
+ public toShortUnixtime(): number {
121
+ return Math.round(this.toLongUnixtime() / 1000)
122
+ }
123
+
124
+ public toLongUnixtime(): number {
125
+ return this.value.getTime()
126
+ }
127
+
128
+ public toDate(): Date {
129
+ return this.value
130
+ }
131
+
132
+ public toISOString(): string {
133
+ return this.value.toISOString()
134
+ }
135
+
136
+ public elapsedSeconds(): number {
137
+ return Math.round((Date.now() - this.toLongUnixtime()) / 1000)
138
+ }
139
+
140
+ public elapsedMinutes(): number {
141
+ return Math.round(this.elapsedSeconds() / 60)
142
+ }
143
+
144
+ }