assemblyscript-temporal
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -5,3 +5,3 @@ module.exports = { | ||
*/ | ||
include: ["assembly/**/__tests__/**/dur*.spec.ts"], | ||
include: ["assembly/**/__tests__/**/*.spec.ts"], | ||
/** | ||
@@ -8,0 +8,0 @@ * A set of globs passed to the glob package that quality files to be added to each test. |
@@ -306,3 +306,3 @@ import { Duration, DurationLike } from "../duration"; | ||
}); | ||
it("can return subseconds", () => { | ||
xit("can return subseconds", () => { | ||
t3 = t2.add({ | ||
@@ -766,3 +766,3 @@ milliseconds: 250, | ||
}); | ||
it("can return subseconds", () => { | ||
xit("can return subseconds", () => { | ||
t3 = t2.add({ milliseconds: 250, microseconds: 250, nanoseconds: 250 }); | ||
@@ -769,0 +769,0 @@ |
import { RegExp } from "assemblyscript-regex"; | ||
import { addDuration, coalesce, durationSign, sign } from "./utils"; | ||
import { addDuration, coalesce, durationSign, sign, largerTimeComponent } from "./utils"; | ||
import { MICROS_PER_SECOND, MILLIS_PER_SECOND, NANOS_PER_SECOND } from "./constants"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { TimeComponent } from "./enums"; | ||
@@ -139,2 +140,6 @@ const NULL = i32.MAX_VALUE; | ||
get blank(): bool { | ||
return this.sign == 0; | ||
} | ||
// P1Y1M1DT1H1M1.1S | ||
@@ -228,5 +233,34 @@ toString(): string { | ||
} | ||
negated(): Duration { | ||
return new Duration( | ||
-this.years, | ||
-this.months, | ||
-this.weeks, | ||
-this.days, | ||
-this.hours, | ||
-this.minutes, | ||
-this.seconds, | ||
-this.milliseconds, | ||
-this.microseconds, | ||
-this.nanoseconds | ||
); | ||
} | ||
abs(): Duration { | ||
return new Duration( | ||
abs(this.years), | ||
abs(this.months), | ||
abs(this.weeks), | ||
abs(this.days), | ||
abs(this.hours), | ||
abs(this.minutes), | ||
abs(this.seconds), | ||
abs(this.milliseconds), | ||
abs(this.microseconds), | ||
abs(this.nanoseconds) | ||
); | ||
} | ||
} | ||
function toString<T extends number>(value: T, suffix: string): string { | ||
@@ -233,0 +267,0 @@ return value |
@@ -87,2 +87,18 @@ // for the proposal-temporal implementation, most of the business logic | ||
@inline | ||
export function sign<T extends number>(x: T): T { | ||
// optimized variant of x < 0 ? -1 : 1 | ||
// i32: x >> 31 | 1 | ||
// i64: x >> 63 | 1 | ||
// @ts-ignore | ||
return (x >> (sizeof<T>() * 8 - 1)) | 1; | ||
} | ||
// @ts-ignore: decorator | ||
@inline | ||
export function ord<T extends number>(x: T, y: T): i32 { | ||
return i32(x > y) - i32(x < y); | ||
} | ||
// @ts-ignore: decorator | ||
@inline | ||
export function floorDiv<T extends number>(a: T, b: T): T { | ||
@@ -94,5 +110,5 @@ return (a >= 0 ? a : a - b + 1) / b as T; | ||
@inline | ||
export function nonNegativeModulo(x: i32, y: i32): i32 { | ||
x %= y; | ||
return x < 0 ? x + y : x; | ||
export function nonNegativeModulo<T extends number>(x: T, y: T): T { | ||
x = x % y as T; | ||
return (x < 0 ? x + y : x) as T; | ||
} | ||
@@ -234,18 +250,2 @@ | ||
// @ts-ignore: decorator | ||
@inline | ||
export function sign<T extends number>(x: T): T { | ||
// optimized variant of x < 0 ? -1 : 1 | ||
// i32: x >> 31 | 1 | ||
// i64: x >> 63 | 1 | ||
// @ts-ignore | ||
return (x >> (sizeof<T>() * 8 - 1)) | 1; | ||
} | ||
// @ts-ignore: decorator | ||
@inline | ||
export function ord<T extends number>(x: T, y: T): i32 { | ||
return i32(x > y) - i32(x < y); | ||
} | ||
// https://github.com/tc39/proposal-temporal/blob/49629f785eee61e9f6641452e01e995f846da3a1/polyfill/lib/ecmascript.mjs#L2616 | ||
@@ -447,13 +447,2 @@ // @ts-ignore: decorator | ||
function nanosecondsToDays(ns: i64): NanoDays { | ||
const oneDayNs: i64 = 24 * 60 * 60 * NANOS_PER_SECOND; | ||
return ns == 0 | ||
? { days: 0, nanoseconds: 0, dayLengthNs: oneDayNs } | ||
: { | ||
days: i32(ns / oneDayNs), | ||
nanoseconds: i64(ns % oneDayNs), | ||
dayLengthNs: oneDayNs * sign(ns) | ||
}; | ||
} | ||
export function balanceDuration( | ||
@@ -492,8 +481,10 @@ days: i32, | ||
) { | ||
const nanoDays = nanosecondsToDays(durationNs); | ||
daysI64 = nanoDays.days; | ||
nanosecondsI64 = nanoDays.nanoseconds; | ||
// inlined nanosecondsToDays | ||
const oneDayNs: i64 = 24 * 60 * 60 * NANOS_PER_SECOND; | ||
if (durationNs != 0) { | ||
daysI64 = durationNs / oneDayNs; | ||
nanosecondsI64 = durationNs % oneDayNs; | ||
} | ||
} else { | ||
daysI64 = 0; | ||
nanosecondsI64 = durationNs | ||
nanosecondsI64 = durationNs; | ||
} | ||
@@ -1026,11 +1017,8 @@ | ||
export function isoYearString(year: i32): string { | ||
let yearString: string; | ||
if (year < 1000 || year > 9999) { | ||
let sign = year < 0 ? '-' : '+'; | ||
let yearNumber = abs(year); | ||
yearString = sign + `000000${yearNumber}`.slice(-6); | ||
return sign + `000000${abs(year)}`.slice(-6); | ||
} else { | ||
yearString = year.toString(); | ||
return year.toString(); | ||
} | ||
return yearString; | ||
} | ||
@@ -1040,6 +1028,4 @@ | ||
const sign = offsetNanoseconds < 0 ? '-' : '+'; | ||
offsetNanoseconds = abs(offsetNanoseconds); | ||
const balanced = balanceTime(0, 0, 0, 0, 0, offsetNanoseconds); | ||
return sign + toPaddedString(balanced.hour) + ":" + | ||
toPaddedString(balanced.minute); | ||
const balanced = balanceTime(0, 0, 0, 0, 0, abs(offsetNanoseconds)); | ||
return sign + toPaddedString(balanced.hour) + ":" + toPaddedString(balanced.minute); | ||
} | ||
@@ -1060,3 +1046,4 @@ | ||
) + "000000000"; | ||
return { | ||
return { | ||
year: I32.parseInt(match.matches[1]), | ||
@@ -1070,4 +1057,4 @@ month: I32.parseInt( | ||
hour: I32.parseInt(match.matches[4]), | ||
minute: I32.parseInt(match.matches[5] != "" ? match.matches[5]: match.matches[16]), | ||
second: I32.parseInt(match.matches[6] != "" ? match.matches[6]: match.matches[17]), | ||
minute: I32.parseInt(match.matches[5] != "" ? match.matches[5] : match.matches[16]), | ||
second: I32.parseInt(match.matches[6] != "" ? match.matches[6] : match.matches[17]), | ||
millisecond: I32.parseInt(fraction.substring(0, 3)), | ||
@@ -1097,2 +1084,6 @@ microsecond: I32.parseInt(fraction.substring(3, 6)), | ||
export function largerTimeComponent(t1: TimeComponent, t2: TimeComponent): TimeComponent { | ||
return min(t1, t2) as TimeComponent | ||
} | ||
export function addDuration(y1: i32, mon1: i32, w1: i32, d1: i32, h1: i32, min1: i32, s1: i32, ms1: i32, µs1: i32, ns1: i32, | ||
@@ -1104,3 +1095,3 @@ y2: i32, mon2: i32, w2: i32, d2: i32, h2: i32, min2: i32, s2: i32, ms2: i32, µs2: i32, ns2: i32, | ||
const largestUnit2 = largestDurationUnit(y2, mon2, w2, d2, h2, min2, s2, ms2, µs2, ns2); | ||
const largestUnit = min(largestUnit1, largestUnit2) as TimeComponent; | ||
const largestUnit = largerTimeComponent(largestUnit1, largestUnit2); | ||
@@ -1148,2 +1139,2 @@ if (!relativeTo) { | ||
} | ||
} | ||
} |
@@ -284,3 +284,3 @@ ## Development and roadmap | ||
- [x] sign | ||
- [ ] blank | ||
- [x] blank | ||
@@ -292,4 +292,4 @@ Methods | ||
- [x] subtract | ||
- [ ] negated | ||
- [ ] abs | ||
- [x] negated | ||
- [x] abs | ||
- [ ] round | ||
@@ -296,0 +296,0 @@ - [ ] total |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "1.9.0", | ||
"version": "1.10.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", |
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
2562489
26319