mybase 1.1.21 → 1.1.23

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.21",
3
+ "version": "1.1.23",
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);
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"
@@ -1,6 +1,3 @@
1
-
2
-
3
-
4
1
  export class Timespan {
5
2
  constructor(private readonly ms: number){
6
3
  }
@@ -35,9 +35,15 @@ class Unixtime {
35
35
  // set the value
36
36
  this.value = new Date(value);
37
37
  }
38
- else {
38
+ else if (value instanceof Date) {
39
39
  this.value = value;
40
40
  }
41
+ else {
42
+ throw new Error('Invalid value');
43
+ }
44
+ if (isNaN(this.value.getTime())) {
45
+ throw new Error('Invalid date');
46
+ }
41
47
  }
42
48
  addTimespan(timespan) {
43
49
  this.value = new Date(this.value.getTime() + timespan.miliseconds);
@@ -93,6 +99,9 @@ class Unixtime {
93
99
  (hh > 9 ? '' : '0') + hh
94
100
  ].join('');
95
101
  }
102
+ getDay() {
103
+ return this.value.getDay();
104
+ }
96
105
  static from(value) {
97
106
  return new Unixtime(value);
98
107
  }
@@ -114,6 +123,14 @@ class Unixtime {
114
123
  elapsedMinutes() {
115
124
  return Math.round(this.elapsedSeconds() / 60);
116
125
  }
126
+ static try(value) {
127
+ try {
128
+ return new Unixtime(value);
129
+ }
130
+ catch (e) {
131
+ return null;
132
+ }
133
+ }
117
134
  }
118
135
  exports.Unixtime = Unixtime;
119
136
  Unixtime.EMPTY = new Unixtime(new Date(0));
@@ -7,6 +7,10 @@ describe('Unixtime', () => {
7
7
  expect(unixtime.toDate()).toEqual(date);
8
8
  });
9
9
 
10
+ it('create invalid date should throw error', () => {
11
+ expect(() => new Unixtime('invalid date')).toThrow();
12
+ })
13
+
10
14
  it('should create Unixtime object from string', () => {
11
15
  const dateString = '2022-01-01T00:00:00Z';
12
16
  const date = new Date(dateString);
@@ -42,8 +42,16 @@ export class Unixtime {
42
42
  value = Math.round(value)
43
43
  // set the value
44
44
  this.value = new Date(value)
45
- } else {
45
+ }
46
+ else if (value instanceof Date) {
46
47
  this.value = value
48
+ }
49
+ else {
50
+ throw new Error('Invalid value')
51
+ }
52
+
53
+ if (isNaN(this.value.getTime())) {
54
+ throw new Error('Invalid date')
47
55
  }
48
56
  }
49
57
 
@@ -113,6 +121,10 @@ export class Unixtime {
113
121
  }
114
122
 
115
123
 
124
+ public getDay() : number {
125
+ return this.value.getDay()
126
+ }
127
+
116
128
  static from(value: Date | string | number) {
117
129
  return new Unixtime(value)
118
130
  }
@@ -141,4 +153,12 @@ export class Unixtime {
141
153
  return Math.round(this.elapsedSeconds() / 60)
142
154
  }
143
155
 
156
+
157
+ static try(value: Date | string | number): Unixtime | null {
158
+ try {
159
+ return new Unixtime(value)
160
+ } catch (e) {
161
+ return null
162
+ }
163
+ }
144
164
  }