qrono 1.3.0 → 1.4.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.
package/src/qrono.js CHANGED
@@ -1,4 +1,4 @@
1
- /* @ts-self-types="../types/qrono.d.ts" */
1
+ /* @ts-self-types="../types/qrono.d.ts" */
2
2
  import {
3
3
  has,
4
4
  given,
@@ -146,15 +146,12 @@ function valid() {
146
146
  return isValidDate(this.nativeDate)
147
147
  }
148
148
 
149
- function context(context) {
150
- if (!context) {
151
- return
152
- }
153
- for (const key of fields(defaultContext)) {
154
- if (has(context, key)) {
155
- this[key] = context[key]
156
- }
149
+ function context(arg) {
150
+ if (!arg) {
151
+ return { localtime: this.localtime, disambiguation: this.disambiguation }
157
152
  }
153
+ if ('localtime' in arg) this.localtime = arg.localtime
154
+ if ('disambiguation' in arg) this.disambiguation = arg.disambiguation
158
155
  return this
159
156
  }
160
157
 
@@ -184,7 +181,7 @@ function set(values) {
184
181
  }
185
182
  const dateOnly = !has(values, 'hour', 'minute', 'second', 'millisecond')
186
183
  const disambig = dateOnly ? 'later' : this.disambiguation
187
- const baseDate = this.nativeDate ?? new Date(0, 0)
184
+ const baseDate = this.nativeDate ?? new Date(0, 0) // 1900-01-01T00:00:00.000Z
188
185
  const newDate = new Date(initialSafeDate.getTime())
189
186
  const requested = {
190
187
  year: args.year ?? baseDate.getFullYear(),
@@ -301,7 +298,7 @@ function parse(str) {
301
298
  // -----------------------------------------------------------------------------
302
299
  // Public methods
303
300
  // -----------------------------------------------------------------------------
304
- const p = (v, n) => String(v).padStart(n, '0')
301
+ const pad0 = (v, n) => String(v).padStart(n, '0')
305
302
 
306
303
  // Basic
307
304
  Qrono.prototype.toString = function () {
@@ -310,16 +307,16 @@ Qrono.prototype.toString = function () {
310
307
  const offset = -t.getTimezoneOffset()
311
308
  const offsetAbs = Math.abs(offset)
312
309
  return (
313
- `${p(t.getFullYear(), 4)}-` +
314
- `${p(t.getMonth() + 1, 2)}-` +
315
- `${p(t.getDate(), 2)}T` +
316
- `${p(t.getHours(), 2)}:` +
317
- `${p(t.getMinutes(), 2)}:` +
318
- `${p(t.getSeconds(), 2)}.` +
319
- `${p(t.getMilliseconds(), 3)}` +
310
+ `${pad0(t.getFullYear(), 4)}-` +
311
+ `${pad0(t.getMonth() + 1, 2)}-` +
312
+ `${pad0(t.getDate(), 2)}T` +
313
+ `${pad0(t.getHours(), 2)}:` +
314
+ `${pad0(t.getMinutes(), 2)}:` +
315
+ `${pad0(t.getSeconds(), 2)}.` +
316
+ `${pad0(t.getMilliseconds(), 3)}` +
320
317
  `${offset >= 0 ? '+' : '-'}` +
321
- `${p(Math.trunc(offsetAbs / minutesPerHour), 2)}:` +
322
- `${p(offsetAbs % minutesPerHour, 2)}`
318
+ `${pad0(Math.trunc(offsetAbs / minutesPerHour), 2)}:` +
319
+ `${pad0(offsetAbs % minutesPerHour, 2)}`
323
320
  )
324
321
  }
325
322
  return this[internal].nativeDate.toISOString()
@@ -386,7 +383,7 @@ Qrono.prototype.toDate = function (...args) {
386
383
  }
387
384
 
388
385
  // Accessor
389
- for (const [field, native, offset] of [
386
+ for (const [field, native, base] of [
390
387
  ['year', 'FullYear', 0],
391
388
  ['month', 'Month', 1],
392
389
  ['day', 'Date', 0],
@@ -398,7 +395,7 @@ for (const [field, native, offset] of [
398
395
  Qrono.prototype[field] = function (value) {
399
396
  return given(value)
400
397
  ? this.clone({ [field]: value })
401
- : this[internal].getNative(native) + offset
398
+ : this[internal].getNative(native) + base
402
399
  }
403
400
  }
404
401
 
@@ -435,7 +432,7 @@ Qrono.prototype.isLeapYear = function () {
435
432
  return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
436
433
  }
437
434
 
438
- Qrono.prototype.hasDstInYear = function () {
435
+ Qrono.prototype.hasOffsetChangeInYear = function () {
439
436
  if (!this[internal].localtime) {
440
437
  return false
441
438
  }
@@ -446,18 +443,19 @@ Qrono.prototype.hasDstInYear = function () {
446
443
  }
447
444
 
448
445
  Qrono.prototype.isInDst = function () {
449
- if (!this[internal].localtime) {
450
- return false
446
+ if (!this[internal].localtime) return false
447
+ const offset = this.offset()
448
+ let past = false,
449
+ future = false
450
+ for (let i = 1; i <= 5; i += 2) {
451
+ if (this.month(-i).offset() < offset) past = true
452
+ if (this.month(i).offset() < offset) future = true
453
+ if (past && future) return true
451
454
  }
452
- const offsets = Array.from({ length: 12 }, (_, index) =>
453
- this.month(index + 1).offset()
454
- )
455
- const minOffset = Math.min(...offsets)
456
- const maxOffset = Math.max(...offsets)
457
- return minOffset !== maxOffset && this.offset() === maxOffset
455
+ return false
458
456
  }
459
457
 
460
- Qrono.prototype.isDstTransitionDay = function () {
458
+ Qrono.prototype.hasOffsetChangeInDay = function () {
461
459
  if (!this[internal].localtime) {
462
460
  return false
463
461
  }
@@ -518,24 +516,30 @@ Qrono.prototype.startOfDay = function () {
518
516
  return this.clone(timestamp)
519
517
  }
520
518
 
521
- Qrono.prototype.isSame = function (another) {
522
- return +this === +another
523
- }
524
- Qrono.prototype.isBefore = function (another) {
525
- return this < another
526
- }
527
- Qrono.prototype.isAfter = function (another) {
528
- return this > another
529
- }
530
- Qrono.prototype.isSameOrBefore = function (another) {
531
- return this <= another
532
- }
533
- Qrono.prototype.isSameOrAfter = function (another) {
534
- return this >= another
535
- }
536
- Qrono.prototype.isBetween = function (a, b) {
537
- return (a <= this && this <= b) || (b <= this && this <= a)
538
- }
519
+ function implementComparison(prototype) {
520
+ Object.assign(prototype, {
521
+ isSame(another) {
522
+ return +this === +another
523
+ },
524
+ isBefore(another) {
525
+ return this < another
526
+ },
527
+ isAfter(another) {
528
+ return this > another
529
+ },
530
+ isSameOrBefore(another) {
531
+ return this <= another
532
+ },
533
+ isSameOrAfter(another) {
534
+ return this >= another
535
+ },
536
+ isBetween(a, b) {
537
+ return (a <= this && this <= b) || (b <= this && this <= a)
538
+ },
539
+ })
540
+ }
541
+
542
+ implementComparison(Qrono.prototype)
539
543
 
540
544
  // Calculation
541
545
  Qrono.prototype.plus = function (...args) {
@@ -678,12 +682,10 @@ QronoDate.prototype.toArray = function () {
678
682
  return [this.year(), this.month(), this.day()]
679
683
  }
680
684
 
681
- QronoDate.prototype.startOfYear = function () {
682
- return new QronoDate(this[internalDate].datetime.startOfYear())
683
- }
684
-
685
- QronoDate.prototype.startOfMonth = function () {
686
- return new QronoDate(this[internalDate].datetime.startOfMonth())
685
+ for (const name of ['Year', 'Month']) {
686
+ QronoDate.prototype[`startOf${name}`] = function () {
687
+ return new QronoDate(this[internalDate].datetime[`startOf${name}`]())
688
+ }
687
689
  }
688
690
 
689
691
  for (const field of ['year', 'month', 'day']) {
@@ -708,7 +710,11 @@ for (const method of [
708
710
  return this[internalDate].datetime[method]()
709
711
  }
710
712
  }
711
- for (const method of ['minutesInDay', 'hasDstInYear', 'isDstTransitionDay']) {
713
+ for (const method of [
714
+ 'minutesInDay',
715
+ 'hasOffsetChangeInYear',
716
+ 'hasOffsetChangeInDay',
717
+ ]) {
712
718
  QronoDate.prototype[method] = function () {
713
719
  return qrono(
714
720
  { disambiguation: 'later' },
@@ -725,24 +731,7 @@ QronoDate.prototype.endOfMonth = function () {
725
731
  return this.clone({ day: this.daysInMonth() })
726
732
  }
727
733
 
728
- QronoDate.prototype.isSame = function (another) {
729
- return +this === +another
730
- }
731
- QronoDate.prototype.isBefore = function (another) {
732
- return this < another
733
- }
734
- QronoDate.prototype.isAfter = function (another) {
735
- return this > another
736
- }
737
- QronoDate.prototype.isSameOrBefore = function (another) {
738
- return this <= another
739
- }
740
- QronoDate.prototype.isSameOrAfter = function (another) {
741
- return this >= another
742
- }
743
- QronoDate.prototype.isBetween = function (a, b) {
744
- return (a <= this && this <= b) || (b <= this && this <= a)
745
- }
734
+ implementComparison(QronoDate.prototype)
746
735
 
747
736
  QronoDate.prototype.plus = function (...args) {
748
737
  return plusDate.call(this, 1, ...args)
package/types/qrono.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { qrono }
1
+ export { qrono }
2
2
 
3
3
  /**
4
4
  * Creates a new Qrono instance.
@@ -426,7 +426,7 @@ declare namespace qrono {
426
426
  /**
427
427
  * Returns whether the Qrono instance has daylight saving time in the year.
428
428
  */
429
- hasDstInYear(): boolean
429
+ hasOffsetChangeInYear(): boolean
430
430
 
431
431
  /**
432
432
  * Returns whether the Qrono instance is in daylight saving time.
@@ -436,7 +436,7 @@ declare namespace qrono {
436
436
  /**
437
437
  * Returns whether the Qrono instance is on a daylight saving time transition day.
438
438
  */
439
- isDstTransitionDay(): boolean
439
+ hasOffsetChangeInDay(): boolean
440
440
 
441
441
  /**
442
442
  * Returns the number of minutes in the day of the Qrono instance.
@@ -689,12 +689,12 @@ declare namespace qrono {
689
689
  /**
690
690
  * Returns whether the QronoDate instance has daylight saving time in the year.
691
691
  */
692
- hasDstInYear(): boolean
692
+ hasOffsetChangeInYear(): boolean
693
693
 
694
694
  /**
695
695
  * Returns whether the QronoDate instance is on a daylight saving time transition day.
696
696
  */
697
- isDstTransitionDay(): boolean
697
+ hasOffsetChangeInDay(): boolean
698
698
 
699
699
  /**
700
700
  * Returns the number of minutes in the day of the QronoDate instance.