assemblyscript-temporal
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -7,4 +7,4 @@ import { RegExp } from "assemblyscript-regex"; | ||
import { | ||
MICROS_PER_SECOND, | ||
MILLIS_PER_SECOND, | ||
NANOS_PER_DAY, | ||
NANOS_PER_SECOND | ||
@@ -173,40 +173,57 @@ } from "./util/constants"; | ||
private get largestDurationUnit(): TimeComponent { | ||
if (this.years) return TimeComponent.Years; | ||
if (this.months) return TimeComponent.Months; | ||
if (this.weeks) return TimeComponent.Weeks; | ||
if (this.days) return TimeComponent.Days; | ||
if (this.hours) return TimeComponent.Hours; | ||
if (this.minutes) return TimeComponent.Minutes; | ||
if (this.seconds) return TimeComponent.Seconds; | ||
if (this.milliseconds) return TimeComponent.Milliseconds; | ||
if (this.microseconds) return TimeComponent.Microseconds; | ||
return TimeComponent.Nanoseconds; | ||
} | ||
// P1Y1M1DT1H1M1.1S | ||
toString(): string { | ||
const date = | ||
toString(abs(this.years), "Y") + | ||
toString(abs(this.months), "M") + | ||
toString(abs(this.weeks), "W") + | ||
toString(abs(this.days), "D"); | ||
let nanoseconds: i64 = | ||
this.nanoseconds + | ||
this.microseconds * 1000 + | ||
this.milliseconds * 1000_000; | ||
const seconds: i64 = i64(this.seconds) + nanoseconds / NANOS_PER_SECOND; | ||
const negative = this.sign < 0; | ||
nanoseconds %= NANOS_PER_SECOND; | ||
const time = | ||
toString(abs(this.hours), "H") + | ||
toString(abs(this.minutes), "M") + | ||
toString(abs( | ||
// sort in ascending order for better sum precision | ||
f64(this.nanoseconds) / NANOS_PER_SECOND + | ||
f64(this.microseconds) / MICROS_PER_SECOND + | ||
f64(this.milliseconds) / MILLIS_PER_SECOND + | ||
f64(this.seconds) | ||
), | ||
"S" | ||
); | ||
if ( | ||
this.years == 0 && | ||
this.months == 0 && | ||
this.weeks == 0 && | ||
this.days == 0 && | ||
this.hours == 0 && | ||
this.minutes == 0 && | ||
seconds == 0 && | ||
nanoseconds == 0 | ||
) { | ||
return "PT0S"; | ||
} | ||
if (!date.length && !time.length) return "PT0S"; | ||
return ( | ||
(this.sign < 0 ? "-" : "") + "P" + date + (time.length ? "T" + time : "") | ||
); | ||
let isoString = negative ? "-P" : "P"; | ||
if (this.years) isoString += abs(this.years).toString() + "Y"; | ||
if (this.months) isoString += abs(this.months).toString() + "M"; | ||
if (this.weeks) isoString += abs(this.weeks).toString() + "W"; | ||
if (this.days) isoString += abs(this.days).toString() + "D"; | ||
if (!this.hours && !this.minutes && !seconds && !nanoseconds) { | ||
return isoString; | ||
} | ||
isoString += "T"; | ||
if (this.hours) isoString += abs(this.hours).toString() + "H"; | ||
if (this.minutes) isoString += abs(this.minutes).toString() + "M"; | ||
if (seconds || nanoseconds) { | ||
let decimalPart: string | null = null; | ||
if (nanoseconds) { | ||
const fraction = abs(nanoseconds); | ||
decimalPart = fraction.toString().padStart(9, "0"); | ||
// precision would truncate this string here | ||
while (decimalPart.endsWith("0")) { | ||
decimalPart = decimalPart.slice(0, -1); | ||
} | ||
} | ||
if (decimalPart != null) { | ||
isoString += abs(seconds).toString() + "." + decimalPart! + "S"; | ||
} else { | ||
isoString += abs(seconds).toString() + "S"; | ||
} | ||
} | ||
return isoString; | ||
} | ||
@@ -216,3 +233,3 @@ | ||
const duration = Duration.from(durationToAdd); | ||
const largestUnit = min(this.largestDurationUnit, duration.largestDurationUnit); | ||
const largestUnit = min(largestUnitOf(this), largestUnitOf(duration)); | ||
@@ -303,13 +320,2 @@ if (!relativeTo) { | ||
function toString<T extends number>(value: T, suffix: string): string { | ||
if (value) return (isFloat<T>() ? stringify(value) : value.toString()) + suffix; | ||
return ""; | ||
} | ||
// @ts-ignore: decorator | ||
@inline | ||
function stringify(value: f64): string { | ||
return F64.isSafeInteger(value) ? i64(value).toString() : value.toString(); | ||
} | ||
function totalDurationNanoseconds( | ||
@@ -365,7 +371,5 @@ days: i64, | ||
) { | ||
// inlined nanosecondsToDays | ||
const oneDayNs: i64 = 24 * 60 * 60 * NANOS_PER_SECOND; | ||
if (durationNs != 0) { | ||
daysI64 = durationNs / oneDayNs; | ||
nanosecondsI64 = durationNs % oneDayNs; | ||
daysI64 = durationNs / NANOS_PER_DAY; | ||
nanosecondsI64 = durationNs % NANOS_PER_DAY; | ||
} | ||
@@ -457,2 +461,16 @@ } else { | ||
//@ts-ignore: decorator | ||
@inline | ||
function largestUnitOf(duration: Duration): TimeComponent { | ||
if (duration.years) return TimeComponent.Years; | ||
if (duration.months) return TimeComponent.Months; | ||
if (duration.weeks) return TimeComponent.Weeks; | ||
if (duration.days) return TimeComponent.Days; | ||
if (duration.hours) return TimeComponent.Hours; | ||
if (duration.minutes) return TimeComponent.Minutes; | ||
if (duration.seconds) return TimeComponent.Seconds; | ||
if (duration.milliseconds) return TimeComponent.Milliseconds; | ||
if (duration.microseconds) return TimeComponent.Microseconds; | ||
if (duration.nanoseconds) return TimeComponent.Nanoseconds; | ||
return TimeComponent.Nanoseconds; | ||
} |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "An implementation of temporal within AssemblyScript, with an initial focus on non-timezone-aware classes and functionality.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
502561
11720