assemblyscript-temporal
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -74,6 +74,6 @@ import { durationSign } from "./utils"; | ||
// 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), | ||
f64(this.nanoseconds) / NANOS_PER_SECOND + | ||
f64(this.microseconds) / MICROS_PER_SECOND + | ||
f64(this.milliseconds) / MILLIS_PER_SECOND + | ||
f64(this.seconds), | ||
"S" | ||
@@ -95,4 +95,6 @@ ); | ||
// @ts-ignore: decorator | ||
@inline | ||
function stringify(value: f64): string { | ||
return F64.isSafeInteger(value) ? i64(value).toString() : value.toString(); | ||
} |
@@ -153,6 +153,3 @@ import { RegExp } from "assemblyscript-regex"; | ||
until( | ||
date: PlainDate, | ||
largestUnit: TimeComponent = TimeComponent.Days | ||
): Duration { | ||
until(date: PlainDate, largestUnit: TimeComponent = TimeComponent.Days): Duration { | ||
return differenceDate( | ||
@@ -169,6 +166,3 @@ this.year, | ||
since( | ||
date: PlainDate, | ||
largestUnit: TimeComponent = TimeComponent.Days | ||
): Duration { | ||
since(date: PlainDate, largestUnit: TimeComponent = TimeComponent.Days): Duration { | ||
return differenceDate( | ||
@@ -198,3 +192,3 @@ date.year, | ||
// @ts-ignore TS2352 | ||
: (durationToAdd as Duration); | ||
: durationToAdd as Duration; | ||
@@ -229,3 +223,3 @@ const balancedDuration = balanceDuration( | ||
// @ts-ignore TS2352 | ||
: (durationToSubtract as Duration); | ||
: durationToSubtract as Duration; | ||
@@ -259,5 +253,4 @@ const balancedDuration = balanceDuration( | ||
if (a === b) return 0; | ||
return compareTemporalDate(a.year, a.month, a.day, b.year, b.month, b.day); | ||
} | ||
} |
@@ -213,3 +213,2 @@ import { RegExp } from "assemblyscript-regex"; | ||
if (a === b) return 0; | ||
return compareTemporalDateTime( | ||
@@ -258,3 +257,3 @@ a.year, | ||
// @ts-ignore TS2352 | ||
: (durationToAdd as Duration); | ||
: durationToAdd as Duration; | ||
@@ -301,3 +300,3 @@ const newDate = addDateTime( | ||
// @ts-ignore TS2352 | ||
: (durationToSubtract as Duration); | ||
: durationToSubtract as Duration; | ||
@@ -304,0 +303,0 @@ const newDate = addDateTime( |
@@ -1,5 +0,9 @@ | ||
import { checkDateTimeRange, coalesce, toPaddedString } from "./utils"; | ||
import { RegExp } from "assemblyscript-regex"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { PlainDate } from "./plaindate"; | ||
import { | ||
coalesce, | ||
toPaddedString, | ||
checkDateTimeRange | ||
} from "./utils"; | ||
@@ -14,2 +18,19 @@ export class MonthDayLike { | ||
@inline | ||
static from<T = MonthDayLike>(monthDay: T): PlainMonthDay { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>monthDay); | ||
} else { | ||
if (isReference<T>()) { | ||
if (monthDay instanceof PlainMonthDay) { | ||
return this.fromPlainMonthDay(monthDay); | ||
} else if (monthDay instanceof MonthDayLike) { | ||
return this.fromMonthDayLike(monthDay); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
@inline | ||
private static fromPlainMonthDay(monthDay: PlainMonthDay): PlainMonthDay { | ||
@@ -48,19 +69,2 @@ return new PlainMonthDay( | ||
@inline | ||
static from<T = MonthDayLike>(monthDay: T): PlainMonthDay { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>monthDay); | ||
} else { | ||
if (isReference<T>()) { | ||
if (monthDay instanceof PlainMonthDay) { | ||
return this.fromPlainMonthDay(monthDay); | ||
} else if (monthDay instanceof MonthDayLike) { | ||
return this.fromMonthDayLike(monthDay); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
constructor( | ||
@@ -81,2 +85,3 @@ readonly month: i32, | ||
@inline | ||
toString(): string { | ||
@@ -86,2 +91,3 @@ return toPaddedString(this.month) + "-" + toPaddedString(this.day); | ||
@inline | ||
toPlainDate(year: i32): PlainDate { | ||
@@ -88,0 +94,0 @@ return new PlainDate(year, this.month, this.day); |
@@ -10,2 +10,3 @@ import { Duration, DurationLike } from "./duration"; | ||
checkRange, | ||
rejectTime, | ||
balanceDuration, | ||
@@ -153,10 +154,3 @@ addTime, | ||
) { | ||
if (!( | ||
checkRange(hour, 0, 23) && | ||
checkRange(minute, 0, 59) && | ||
checkRange(second, 0, 59) && | ||
checkRange(millisecond, 0, 999) && | ||
checkRange(microsecond, 0, 999) && | ||
checkRange(nanosecond, 0, 999) | ||
)) throw new RangeError("invalid plain time"); | ||
rejectTime(hour, minute, second, millisecond, microsecond, nanosecond); | ||
} | ||
@@ -219,8 +213,3 @@ | ||
): Duration { | ||
if ( | ||
largestUnit == TimeComponent.Years || | ||
largestUnit == TimeComponent.Months || | ||
largestUnit == TimeComponent.Weeks || | ||
largestUnit == TimeComponent.Days | ||
) { | ||
if (largestUnit >= TimeComponent.Years && largestUnit <= TimeComponent.Days) { | ||
throw new RangeError("higher units are not allowed"); | ||
@@ -260,8 +249,3 @@ } | ||
): Duration { | ||
if ( | ||
largestUnit == TimeComponent.Years || | ||
largestUnit == TimeComponent.Months || | ||
largestUnit == TimeComponent.Weeks || | ||
largestUnit == TimeComponent.Days | ||
) { | ||
if (largestUnit >= TimeComponent.Years && largestUnit <= TimeComponent.Days) { | ||
throw new RangeError("higher units are not allowed"); | ||
@@ -335,3 +319,3 @@ } | ||
// @ts-ignore TS2352 | ||
: (durationToAdd as Duration); | ||
: durationToAdd as Duration; | ||
@@ -377,3 +361,3 @@ const newTime = addTime( | ||
// @ts-ignore TS2352 | ||
: (durationToSubtract as Duration); | ||
: durationToSubtract as Duration; | ||
@@ -380,0 +364,0 @@ const newTime = addTime( |
@@ -27,2 +27,19 @@ import { RegExp } from "assemblyscript-regex"; | ||
@inline | ||
static from<T = YearMonthLike>(yearMonth: T): PlainYearMonth { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>yearMonth); | ||
} else { | ||
if (isReference<T>()) { | ||
if (yearMonth instanceof PlainYearMonth) { | ||
return this.fromPlainYearMonth(yearMonth); | ||
} else if (yearMonth instanceof YearMonthLike) { | ||
return this.fromYearMonthLike(yearMonth); | ||
} | ||
} | ||
throw new TypeError("invalid yearMonth type"); | ||
} | ||
} | ||
@inline | ||
private static fromPlainYearMonth(yearMonth: PlainYearMonth): PlainYearMonth { | ||
@@ -76,19 +93,2 @@ return new PlainYearMonth( | ||
@inline | ||
static from<T = YearMonthLike>(yearMonth: T): PlainYearMonth { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>yearMonth); | ||
} else { | ||
if (isReference<T>()) { | ||
if (yearMonth instanceof PlainYearMonth) { | ||
return this.fromPlainYearMonth(yearMonth); | ||
} else if (yearMonth instanceof YearMonthLike) { | ||
return this.fromYearMonthLike(yearMonth); | ||
} | ||
} | ||
throw new TypeError("invalid yearMonth type"); | ||
} | ||
} | ||
constructor( | ||
@@ -129,2 +129,3 @@ readonly year: i32, | ||
@inline | ||
toString(): string { | ||
@@ -134,2 +135,3 @@ return isoYearString(this.year) + "-" + toPaddedString(this.month); | ||
@inline | ||
toPlainDate(day: i32): PlainDate { | ||
@@ -143,12 +145,9 @@ return new PlainDate(this.year, this.month, day); | ||
return ( | ||
this.referenceISODay == other.referenceISODay && | ||
this.month == other.month && | ||
this.year == other.year | ||
this.year == other.year && | ||
this.referenceISODay == other.referenceISODay | ||
); | ||
} | ||
until( | ||
yearMonth: PlainYearMonth, | ||
largestUnit: TimeComponent = TimeComponent.Years | ||
): Duration { | ||
until(yearMonth: PlainYearMonth, largestUnit: TimeComponent = TimeComponent.Years): Duration { | ||
if (largestUnit > TimeComponent.Months) | ||
@@ -167,6 +166,3 @@ throw new RangeError("lower units are not allowed"); | ||
since( | ||
yearMonth: PlainYearMonth, | ||
largestUnit: TimeComponent = TimeComponent.Years | ||
): Duration { | ||
since(yearMonth: PlainYearMonth, largestUnit: TimeComponent = TimeComponent.Years): Duration { | ||
if (largestUnit > TimeComponent.Months) | ||
@@ -193,6 +189,3 @@ throw new RangeError("lower units are not allowed"); | ||
add<T = DurationLike>( | ||
durationToAdd: T, | ||
overflow: Overflow = Overflow.Constrain | ||
): PlainYearMonth { | ||
add<T = DurationLike>(durationToAdd: T, overflow: Overflow = Overflow.Constrain): PlainYearMonth { | ||
const duration = | ||
@@ -202,3 +195,3 @@ durationToAdd instanceof DurationLike | ||
// @ts-ignore TS2352 | ||
: (durationToAdd as Duration); | ||
: durationToAdd as Duration; | ||
@@ -220,26 +213,17 @@ const balancedDuration = balanceDuration( | ||
duration.weeks, | ||
balancedDuration.days, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0 | ||
balancedDuration.days | ||
); | ||
const day = sign < 0 ? daysInMonth(this.year, this.month) : 1; | ||
const startdate = new PlainDate(this.year, this.month, day); | ||
const addedDate = startdate.add(duration, overflow); | ||
const startDate = new PlainDate(this.year, this.month, day); | ||
const addedDate = startDate.add(duration, overflow); | ||
return new PlainYearMonth(addedDate.year, addedDate.month); | ||
} | ||
subtract<T = DurationLike>( | ||
durationToAdd: T, | ||
overflow: Overflow = Overflow.Constrain | ||
): PlainYearMonth { | ||
subtract<T = DurationLike>(durationToAdd: T, overflow: Overflow = Overflow.Constrain): PlainYearMonth { | ||
let duration = | ||
durationToAdd instanceof DurationLike | ||
? durationToAdd.toDuration() | ||
: // @ts-ignore TS2352 | ||
(durationToAdd as Duration); | ||
// @ts-ignore TS2352 | ||
: durationToAdd as Duration; | ||
@@ -274,9 +258,3 @@ duration = new Duration( | ||
duration.weeks, | ||
balancedDuration.days, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0 | ||
balancedDuration.days | ||
); | ||
@@ -292,3 +270,2 @@ | ||
if (a === b) return 0; | ||
return compareTemporalDate( | ||
@@ -295,0 +272,0 @@ a.year, |
@@ -250,2 +250,20 @@ // for the proposal-temporal implementation, most of the business logic | ||
export function rejectTime( | ||
hour: i32, | ||
minute: i32, | ||
second: i32, | ||
millisecond: i32, | ||
microsecond: i32, | ||
nanosecond: i32 | ||
): void { | ||
if (!( | ||
checkRange(hour, 0, 23) && | ||
checkRange(minute, 0, 59) && | ||
checkRange(second, 0, 59) && | ||
checkRange(millisecond, 0, 999) && | ||
checkRange(microsecond, 0, 999) && | ||
checkRange(nanosecond, 0, 999) | ||
)) throw new RangeError("time out of range"); | ||
} | ||
export function rejectDate(year: i32, month: i32, day: i32): void { | ||
@@ -349,8 +367,8 @@ if (!checkRange(month, 1, 12)) { | ||
days: i32, | ||
hours: i32, | ||
minutes: i32, | ||
seconds: i32, | ||
milliseconds: i32, | ||
microseconds: i32, | ||
nanoseconds: i32 | ||
hours: i32 = 0, | ||
minutes: i32 = 0, | ||
seconds: i32 = 0, | ||
milliseconds: i32 = 0, | ||
microseconds: i32 = 0, | ||
nanoseconds: i32 = 0 | ||
): i32 { | ||
@@ -676,3 +694,3 @@ if (years) return sign(years); | ||
while (years > 0) { | ||
days += 365 + i32(leapYear(smallerYear + years - 1)); | ||
days += daysInYear(smallerYear + years - 1); | ||
years -= 1; | ||
@@ -687,6 +705,3 @@ } | ||
weeks *= sign; | ||
days *= sign; | ||
return new Duration(0, 0, weeks, days); | ||
return new Duration(0, 0, weeks * sign, days * sign); | ||
} | ||
@@ -867,3 +882,3 @@ | ||
case Overflow.Reject: | ||
// rejectTime(hour, minute, second, millisecond, microsecond, nanosecond); | ||
rejectTime(hour, minute, second, millisecond, microsecond, nanosecond); | ||
break; | ||
@@ -982,2 +997,2 @@ | ||
return yearString; | ||
} | ||
} |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"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", |
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
350177
8743