mybase 1.1.47 → 1.1.49

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.47",
3
+ "version": "1.1.49",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -15,6 +15,7 @@ export declare class Timespan {
15
15
  static days(days: number): Timespan;
16
16
  static weeks(weeks: number): Timespan;
17
17
  static months(months: number): Timespan;
18
+ static years(years: number): Timespan;
18
19
  add(timespan: Timespan): Timespan;
19
20
  subtract(timespan: Timespan): Timespan;
20
21
  toJSON(): {
@@ -47,6 +47,9 @@ class Timespan {
47
47
  static months(months) {
48
48
  return new Timespan(months * 30 * 24 * 60 * 60 * 1000);
49
49
  }
50
+ static years(years) {
51
+ return new Timespan(years * 365 * 24 * 60 * 60 * 1000);
52
+ }
50
53
  add(timespan) {
51
54
  return new Timespan(this.ms + timespan.ms);
52
55
  }
@@ -58,6 +58,10 @@ export class Timespan {
58
58
  return new Timespan(months * 30 * 24 * 60 * 60 * 1000)
59
59
  }
60
60
 
61
+ static years(years: number): Timespan {
62
+ return new Timespan(years * 365 * 24 * 60 * 60 * 1000)
63
+ }
64
+
61
65
  add(timespan: Timespan): Timespan {
62
66
  return new Timespan(this.ms + timespan.ms)
63
67
  }
@@ -22,8 +22,8 @@ export declare class Unixtime {
22
22
  addDays(days?: number): Unixtime;
23
23
  addMonths(months?: number): Unixtime;
24
24
  addYears(years?: number): Unixtime;
25
- yyyymmdd(): string;
26
- yyymmddhh(): string;
25
+ yyyymmdd(seperator?: string): string;
26
+ yyyymmddhh(seperator?: string): string;
27
27
  /**
28
28
  @deprecated use 'day' instead
29
29
  */
@@ -42,6 +42,16 @@ export declare class Unixtime {
42
42
  elapsedSeconds(): number;
43
43
  elapsedMinutes(): number;
44
44
  clone(): Unixtime;
45
+ /**
46
+ * @param max maximum timespan to subtract
47
+ * @returns returns random time in the past within given timespan
48
+ */
49
+ static randomPast(max?: Timespan, base?: Unixtime): Unixtime;
50
+ /**
51
+ * @param max maximum timespan to add
52
+ * @returns returns random time in the future within given timespan
53
+ */
54
+ static randomFuture(max?: Timespan, base?: Unixtime): Unixtime;
45
55
  static fromYYYYMMDD(value: string): Unixtime;
46
56
  static try(value: Date | string | number): Unixtime | null;
47
57
  }
@@ -87,23 +87,23 @@ class Unixtime {
87
87
  this.value.setUTCFullYear(this.year + years);
88
88
  return this;
89
89
  }
90
- yyyymmdd() {
90
+ yyyymmdd(seperator = "") {
91
91
  let mm = this.month;
92
92
  let dd = this.day;
93
93
  return [this.value.getFullYear(),
94
94
  (mm > 9 ? '' : '0') + mm,
95
- (dd > 9 ? '' : '0') + dd
96
- ].join('');
95
+ (dd > 9 ? '' : '0') + dd,
96
+ ].join(seperator);
97
97
  }
98
- yyymmddhh() {
98
+ yyyymmddhh(seperator = "") {
99
99
  let mm = this.month;
100
100
  let dd = this.day;
101
101
  let hh = this.hours;
102
102
  return [this.year,
103
103
  (mm > 9 ? '' : '0') + mm,
104
104
  (dd > 9 ? '' : '0') + dd,
105
- (hh > 9 ? '' : '0') + hh
106
- ].join('');
105
+ (hh > 9 ? '' : '0') + hh,
106
+ ].join(seperator);
107
107
  }
108
108
  /**
109
109
  @deprecated use 'day' instead
@@ -139,6 +139,20 @@ class Unixtime {
139
139
  clone() {
140
140
  return new Unixtime(this.toLongUnixtime());
141
141
  }
142
+ /**
143
+ * @param max maximum timespan to subtract
144
+ * @returns returns random time in the past within given timespan
145
+ */
146
+ static randomPast(max = Timespan_1.Timespan.years(1), base = Unixtime.now()) {
147
+ return base.addSeconds(Math.random() * max.seconds * -1);
148
+ }
149
+ /**
150
+ * @param max maximum timespan to add
151
+ * @returns returns random time in the future within given timespan
152
+ */
153
+ static randomFuture(max = Timespan_1.Timespan.years(1), base = Unixtime.now()) {
154
+ return base.addSeconds(Math.random() * max.seconds);
155
+ }
142
156
  static fromYYYYMMDD(value) {
143
157
  if (value.length != 8)
144
158
  throw new Error('Invalid yyyymmdd value');
@@ -1,3 +1,4 @@
1
+ import { Timespan } from './Timespan';
1
2
  import { Unixtime } from './Unixtime';
2
3
 
3
4
  describe('Unixtime', () => {
@@ -123,6 +124,40 @@ describe('Unixtime', () => {
123
124
  expect(unixtime.toISOString()).toEqual('2024-10-11T00:00:00.000Z');
124
125
  })
125
126
 
127
+ it('yyyymmddhh should return correct values',() => {
128
+ const unixtime = new Unixtime("2024-10-11T12:13:14Z");
129
+ expect(unixtime.yyyymmdd()).toEqual('20241011');
130
+ expect(unixtime.yyyymmddhh()).toEqual('2024101112');
131
+ // should also work with optional seperators
132
+ expect(unixtime.yyyymmddhh('-')).toEqual('2024-10-11-12');
133
+ })
134
+
135
+ it('randomPast should return random time in the past', () => {
136
+ const end = Unixtime.now()
137
+ const begin = end.clone().addYears(-1)
138
+ for(let i=0;i<100000;i++) {
139
+ const rand = Unixtime.randomPast(Timespan.years(1))
140
+ expect(rand.toLongUnixtime()).toBeGreaterThan(begin.toLongUnixtime());
141
+ expect(rand.toLongUnixtime()).toBeLessThan(end.toLongUnixtime());
142
+ }
143
+ })
144
+
145
+ it('randomFuture should return random time in the future within given timespan of 1 year', () => {
146
+ const begin = Unixtime.now()
147
+ const end = begin.clone().addYears(1)
148
+ for(let i=0;i<100000;i++) {
149
+ const rand = Unixtime.randomFuture(Timespan.years(1))
150
+ expect(rand.toLongUnixtime()).toBeGreaterThan(begin.toLongUnixtime());
151
+ expect(rand.toLongUnixtime()).toBeLessThan(end.toLongUnixtime());
152
+ }
153
+ })
154
+
155
+ it('yyyymmdd should work as designed',() => {
156
+ const unixtime = new Unixtime("2024-10-11T12:13:14Z");
157
+ expect(unixtime.yyyymmdd()).toEqual('20241011');
158
+ expect(unixtime.yyyymmdd('-')).toEqual('2024-10-11');
159
+ })
160
+
126
161
  it('fromYYYYMMDD should throw error when invalid date', () => {
127
162
  expect(() => Unixtime.fromYYYYMMDD('20243131')).toThrow('Invalid month');
128
163
  expect(() => Unixtime.fromYYYYMMDD('20241233')).toThrow('Invalid day');
@@ -107,24 +107,24 @@ export class Unixtime {
107
107
  return this
108
108
  }
109
109
 
110
- public yyyymmdd(): string {
110
+ public yyyymmdd(seperator: string=""): string {
111
111
  let mm = this.month
112
112
  let dd = this.day
113
113
  return [this.value.getFullYear(),
114
114
  (mm > 9 ? '' : '0') + mm,
115
- (dd > 9 ? '' : '0') + dd
116
- ].join('')
115
+ (dd > 9 ? '' : '0') + dd,
116
+ ].join(seperator)
117
117
  }
118
118
 
119
- public yyymmddhh(): string {
119
+ public yyyymmddhh(seperator: string=""): string {
120
120
  let mm = this.month
121
121
  let dd = this.day
122
122
  let hh = this.hours
123
123
  return [this.year,
124
124
  (mm > 9 ? '' : '0') + mm,
125
125
  (dd > 9 ? '' : '0') + dd,
126
- (hh > 9 ? '' : '0') + hh
127
- ].join('')
126
+ (hh > 9 ? '' : '0') + hh,
127
+ ].join(seperator)
128
128
  }
129
129
 
130
130
  /**
@@ -171,6 +171,21 @@ export class Unixtime {
171
171
  return new Unixtime(this.toLongUnixtime())
172
172
  }
173
173
 
174
+ /**
175
+ * @param max maximum timespan to subtract
176
+ * @returns returns random time in the past within given timespan
177
+ */
178
+ static randomPast(max: Timespan=Timespan.years(1), base: Unixtime=Unixtime.now()): Unixtime {
179
+ return base.addSeconds(Math.random()*max.seconds*-1)
180
+ }
181
+
182
+ /**
183
+ * @param max maximum timespan to add
184
+ * @returns returns random time in the future within given timespan
185
+ */
186
+ static randomFuture(max: Timespan=Timespan.years(1), base: Unixtime=Unixtime.now()): Unixtime {
187
+ return base.addSeconds(Math.random()*max.seconds)
188
+ }
174
189
 
175
190
  static fromYYYYMMDD(value: string): Unixtime {
176
191
  if (value.length != 8) throw new Error('Invalid yyyymmdd value')