@magnusopera/fscript 0.86.0 → 0.86.2

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.
Files changed (97) hide show
  1. package/dist/FScript.Language/Ast.js +2 -2
  2. package/dist/FScript.Language/BuiltinFunctions.js +31 -19
  3. package/dist/FScript.Language/BuiltinSignatures.js +6 -6
  4. package/dist/FScript.Language/BuiltinTypes.js +1 -1
  5. package/dist/FScript.Language/Error.js +2 -2
  6. package/dist/FScript.Language/Eval.js +11 -11
  7. package/dist/FScript.Language/Execution.js +6 -6
  8. package/dist/FScript.Language/ExportSignatures.js +5 -5
  9. package/dist/FScript.Language/HostInterop.js +8 -8
  10. package/dist/FScript.Language/IncludeResolver.js +10 -10
  11. package/dist/FScript.Language/Interactive.js +7 -7
  12. package/dist/FScript.Language/Lexer.js +9 -9
  13. package/dist/FScript.Language/Parser.js +10 -10
  14. package/dist/FScript.Language/Pretty.js +4 -4
  15. package/dist/FScript.Language/Span.js +2 -2
  16. package/dist/FScript.Language/TaskFunctions.js +2 -2
  17. package/dist/FScript.Language/TaskSharingAnalysis.js +17 -17
  18. package/dist/FScript.Language/Token.js +2 -2
  19. package/dist/FScript.Language/TypeInfer.js +12 -12
  20. package/dist/FScript.Language/Types.js +8 -8
  21. package/dist/FScript.Language/Value.js +2 -2
  22. package/dist/Library.js +13 -13
  23. package/dist/fable_modules/fable-library-js.5.24.0/Async.js +363 -0
  24. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/AsyncBuilder.js +70 -10
  25. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/CHANGELOG.md +90 -1
  26. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/CollectionUtil.js +10 -16
  27. package/dist/fable_modules/fable-library-js.5.24.0/DateOnlyTemporal.js +146 -0
  28. package/dist/fable_modules/fable-library-js.5.24.0/DateTimeFormat.js +523 -0
  29. package/dist/fable_modules/fable-library-js.5.24.0/DateTimeOffsetTemporal.js +283 -0
  30. package/dist/fable_modules/fable-library-js.5.24.0/DateTimeTemporal.js +243 -0
  31. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Decimal.js +100 -11
  32. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Encoding.js +23 -16
  33. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Guid.js +3 -1
  34. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/MailboxProcessor.js +23 -3
  35. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/MutableMap.js +5 -1
  36. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/MutableSet.js +5 -1
  37. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Numeric.js +38 -1
  38. package/dist/fable_modules/fable-library-js.5.24.0/Stopwatch.js +65 -0
  39. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/String.js +43 -9
  40. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/System.Text.js +9 -9
  41. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Task.js +22 -0
  42. package/dist/fable_modules/fable-library-js.5.24.0/TimeOnlyTemporal.js +142 -0
  43. package/dist/fable_modules/fable-library-js.5.24.0/TimeSpanTemporal.js +259 -0
  44. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Util.js +15 -3
  45. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/big.d.ts +1 -0
  46. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/lib/big.js +111 -64
  47. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/package.json +1 -1
  48. package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/quotation.js +47 -0
  49. package/dist/fable_modules/project_cracked.json +1 -1
  50. package/package.json +1 -1
  51. package/dist/fable_modules/fable-library-js.5.13.0/Async.js +0 -163
  52. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Array.js +0 -0
  53. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/BigInt.js +0 -0
  54. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/BitConverter.js +0 -0
  55. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Boolean.js +0 -0
  56. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Char.js +0 -0
  57. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Choice.js +0 -0
  58. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/ConditionalWeakTable.js +0 -0
  59. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Date.js +0 -0
  60. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/DateOffset.js +0 -0
  61. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/DateOnly.js +0 -0
  62. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Directory.js +0 -0
  63. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Double.js +0 -0
  64. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Environment.js +0 -0
  65. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Event.js +0 -0
  66. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/FSharp.Collections.js +0 -0
  67. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/FSharp.Core.CompilerServices.js +0 -0
  68. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/FSharp.Core.js +0 -0
  69. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/File.js +0 -0
  70. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Global.js +0 -0
  71. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Int32.js +0 -0
  72. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/List.js +0 -0
  73. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Long.js +0 -0
  74. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Map.js +0 -0
  75. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/MapUtil.js +0 -0
  76. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Native.js +0 -0
  77. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Observable.js +0 -0
  78. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Option.js +0 -0
  79. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Path.js +0 -0
  80. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/README.md +0 -0
  81. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Random.js +0 -0
  82. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Range.js +0 -0
  83. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Reflection.js +0 -0
  84. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/RegExp.js +0 -0
  85. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Result.js +0 -0
  86. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Seq.js +0 -0
  87. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Seq2.js +0 -0
  88. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Set.js +0 -0
  89. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/System.Collections.Generic.js +0 -0
  90. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/System.js +0 -0
  91. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/TaskBuilder.js +0 -0
  92. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/TimeOnly.js +0 -0
  93. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/TimeSpan.js +0 -0
  94. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Timer.js +0 -0
  95. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Types.js +0 -0
  96. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Unicode.13.0.0.js +0 -0
  97. /package/dist/fable_modules/{fable-library-js.5.13.0 → fable-library-js.5.24.0}/Uri.js +0 -0
@@ -90,6 +90,27 @@ function utf8_decode(bytes) {
90
90
  }
91
91
  return chars.join("");
92
92
  }
93
+ function toUint8Array(bytes, index, count) {
94
+ if (ArrayBuffer.isView(bytes)) {
95
+ const buffer = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
96
+ if (index != null && count != null) {
97
+ return buffer.subarray(index, index + count);
98
+ }
99
+ else if (index != null) {
100
+ return buffer.subarray(index);
101
+ }
102
+ return buffer;
103
+ }
104
+ else {
105
+ if (index != null && count != null) {
106
+ return Uint8Array.from(Array.prototype.slice.call(bytes, index, index + count));
107
+ }
108
+ else if (index != null) {
109
+ return Uint8Array.from(Array.prototype.slice.call(bytes, index));
110
+ }
111
+ return Uint8Array.from(bytes);
112
+ }
113
+ }
93
114
  class UTF16LE {
94
115
  getBytes(str, index, count) {
95
116
  str = Array.isArray(str) ? str.join("") : str;
@@ -111,14 +132,7 @@ class UTF16LE {
111
132
  }
112
133
  }
113
134
  getString(bytes, index, count) {
114
- const array = ArrayBuffer.isView(bytes) ? bytes : Uint8Array.from(bytes);
115
- let buffer = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
116
- if (index != null && count != null) {
117
- buffer = buffer.subarray(index, index + count);
118
- }
119
- else if (index != null) {
120
- buffer = buffer.subarray(index);
121
- }
135
+ const buffer = toUint8Array(bytes, index, count);
122
136
  // Allow to compile for Browser environment without @types/node being installed
123
137
  // See https://github.com/fable-compiler/Fable/issues/4368
124
138
  const g = globalThis;
@@ -157,14 +171,7 @@ class UTF8 {
157
171
  }
158
172
  }
159
173
  getString(bytes, index, count) {
160
- const array = ArrayBuffer.isView(bytes) ? bytes : Uint8Array.from(bytes);
161
- let buffer = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
162
- if (index != null && count != null) {
163
- buffer = buffer.subarray(index, index + count);
164
- }
165
- else if (index != null) {
166
- buffer = buffer.subarray(index);
167
- }
174
+ const buffer = toUint8Array(bytes, index, count);
168
175
  // Allow to compile for Browser environment without @types/node being installed
169
176
  // See https://github.com/fable-compiler/Fable/issues/4368
170
177
  const g = globalThis;
@@ -66,7 +66,9 @@ export function newGuid() {
66
66
  }
67
67
  // RFC 9562 UUID v7
68
68
  export function createVersion7(timestamp) {
69
- const ms = timestamp != null ? timestamp.getTime() : Date.now();
69
+ // A number/bigint timestamp is Unix milliseconds (used by the Temporal representation,
70
+ // whose DateTimeOffset is not a JS Date); a Date uses getTime().
71
+ const ms = timestamp == null ? Date.now() : typeof timestamp === "object" ? timestamp.getTime() : Number(timestamp);
70
72
  // 48-bit timestamp as hex
71
73
  const msHex = Math.floor(ms).toString(16).padStart(12, "0");
72
74
  // random bits
@@ -1,6 +1,7 @@
1
1
  import { defaultCancellationToken } from "./Async.js";
2
2
  import { fromContinuations } from "./Async.js";
3
3
  import { startImmediate } from "./Async.js";
4
+ import { OperationCanceledException, protectedCont } from "./AsyncBuilder.js";
4
5
  import { Exception } from "./Util.js";
5
6
  class QueueCell {
6
7
  constructor(message) {
@@ -55,12 +56,27 @@ export function startInstance($this) {
55
56
  startImmediate($this.body($this), $this.cancellationToken);
56
57
  }
57
58
  export function receive($this) {
58
- return fromContinuations((conts) => {
59
+ return protectedCont((ctx) => {
59
60
  if ($this.continuation) {
60
61
  throw new Exception("Receive can only be called once!");
61
62
  }
62
- $this.continuation = conts[0];
63
- __processEvents($this);
63
+ let tokenId;
64
+ const continuation = (message) => {
65
+ if (tokenId !== undefined) {
66
+ ctx.cancelToken.removeListener(tokenId);
67
+ }
68
+ ctx.onSuccess(message);
69
+ };
70
+ $this.continuation = continuation;
71
+ tokenId = ctx.cancelToken.addListener(() => {
72
+ if ($this.continuation === continuation) {
73
+ delete $this.continuation;
74
+ ctx.onCancel(new OperationCanceledException());
75
+ }
76
+ });
77
+ if ($this.continuation === continuation) {
78
+ __processEvents($this);
79
+ }
64
80
  });
65
81
  }
66
82
  export function post($this, message) {
@@ -80,6 +96,10 @@ export function postAndAsyncReply($this, buildMessage) {
80
96
  }
81
97
  const reply = {
82
98
  reply: (res) => {
99
+ // .NET AsyncReplyChannel.Reply ignores a second reply
100
+ if (replied) {
101
+ return;
102
+ }
83
103
  result = res;
84
104
  replied = true;
85
105
  checkCompletion();
@@ -330,7 +330,11 @@ export function Dictionary__Remove_2B595(this$, k) {
330
330
  }
331
331
  switch (matchResult) {
332
332
  case 0: {
333
- getItemFromDict(this$.hashMap, matchValue[1]).splice(matchValue[2], 1);
333
+ const h_1 = matchValue[1] | 0;
334
+ getItemFromDict(this$.hashMap, h_1).splice(matchValue[2], 1);
335
+ if (getItemFromDict(this$.hashMap, h_1).length === 0) {
336
+ this$.hashMap.delete(h_1);
337
+ }
334
338
  return true;
335
339
  }
336
340
  default:
@@ -224,7 +224,11 @@ export function HashSet__Remove_2B595(this$, k) {
224
224
  }
225
225
  switch (matchResult) {
226
226
  case 0: {
227
- getItemFromDict(this$.hashMap, matchValue[1]).splice(matchValue[2], 1);
227
+ const h_1 = matchValue[1] | 0;
228
+ getItemFromDict(this$.hashMap, h_1).splice(matchValue[2], 1);
229
+ if (getItemFromDict(this$.hashMap, h_1).length === 0) {
230
+ this$.hashMap.delete(h_1);
231
+ }
228
232
  return true;
229
233
  }
230
234
  default:
@@ -28,6 +28,18 @@ export function multiply(x, y) {
28
28
  return x[symbol]().multiply(y);
29
29
  }
30
30
  }
31
+ export function divide(x, y) {
32
+ if (typeof x === "number") {
33
+ return x / y;
34
+ }
35
+ else if (typeof x === "bigint") {
36
+ // Keep the fractional part so the caller can round it like .NET does
37
+ return Number(x) / y;
38
+ }
39
+ else {
40
+ return x[symbol]().multiply(1 / y);
41
+ }
42
+ }
31
43
  export function toFixed(x, dp) {
32
44
  if (typeof x === "number") {
33
45
  return x.toFixed(dp);
@@ -50,12 +62,37 @@ export function toPrecision(x, sd) {
50
62
  return x[symbol]().toPrecision(sd);
51
63
  }
52
64
  }
65
+ // A bigint can hold more significant digits than a double, so the mantissa is rounded on the
66
+ // decimal digits themselves instead of going through Number
67
+ function bigintToExponential(x, dp) {
68
+ const sign = x < 0n ? "-" : "";
69
+ let digits = (x < 0n ? -x : x).toString();
70
+ if (x === 0n) {
71
+ return sign + (dp ? "0." + "0".repeat(dp) : "0") + "e+0";
72
+ }
73
+ dp = dp ?? digits.length - 1;
74
+ let exponent = digits.length - 1;
75
+ if (digits.length > dp + 1) {
76
+ // .NET rounds the discarded digits away from zero
77
+ const roundUp = digits.charCodeAt(dp + 1) >= 53;
78
+ digits = (BigInt(digits.slice(0, dp + 1)) + (roundUp ? 1n : 0n)).toString();
79
+ if (digits.length > dp + 1) {
80
+ digits = digits.slice(0, dp + 1);
81
+ exponent += 1;
82
+ }
83
+ }
84
+ else {
85
+ digits = digits.padEnd(dp + 1, "0");
86
+ }
87
+ const mantissa = dp > 0 ? digits[0] + "." + digits.slice(1) : digits[0];
88
+ return sign + mantissa + "e+" + exponent;
89
+ }
53
90
  export function toExponential(x, dp) {
54
91
  if (typeof x === "number") {
55
92
  return x.toExponential(dp);
56
93
  }
57
94
  else if (typeof x === "bigint") {
58
- return x;
95
+ return bigintToExponential(x, dp);
59
96
  }
60
97
  else {
61
98
  return x[symbol]().toExponential(dp);
@@ -0,0 +1,65 @@
1
+ // Timestamps are in microseconds, the resolution performance.now() can offer
2
+ const ticksPerMillisecond = 1000;
3
+ export class Stopwatch {
4
+ constructor() {
5
+ // Milliseconds measured before the last Start
6
+ this.elapsed = 0;
7
+ // performance.now() at the last Start, while running
8
+ this.startedAt = undefined;
9
+ }
10
+ }
11
+ export function create() {
12
+ return new Stopwatch();
13
+ }
14
+ export function startNew() {
15
+ const sw = new Stopwatch();
16
+ start(sw);
17
+ return sw;
18
+ }
19
+ export function start(sw) {
20
+ if (sw.startedAt === undefined) {
21
+ sw.startedAt = performance.now();
22
+ }
23
+ }
24
+ export function stop(sw) {
25
+ if (sw.startedAt !== undefined) {
26
+ sw.elapsed += performance.now() - sw.startedAt;
27
+ sw.startedAt = undefined;
28
+ }
29
+ }
30
+ export function reset(sw) {
31
+ sw.elapsed = 0;
32
+ sw.startedAt = undefined;
33
+ }
34
+ export function restart(sw) {
35
+ sw.elapsed = 0;
36
+ sw.startedAt = performance.now();
37
+ }
38
+ export function isRunning(sw) {
39
+ return sw.startedAt !== undefined;
40
+ }
41
+ function rawElapsedMilliseconds(sw) {
42
+ return sw.elapsed + (sw.startedAt === undefined ? 0 : performance.now() - sw.startedAt);
43
+ }
44
+ // In units of Frequency, like .NET (not TimeSpan ticks)
45
+ export function elapsedTicks(sw) {
46
+ return BigInt(Math.trunc(rawElapsedMilliseconds(sw) * ticksPerMillisecond));
47
+ }
48
+ export function elapsedMilliseconds(sw) {
49
+ return elapsedTicks(sw) / BigInt(ticksPerMillisecond);
50
+ }
51
+ export function elapsedTotalMilliseconds(sw) {
52
+ return Number(elapsedTicks(sw)) / ticksPerMillisecond;
53
+ }
54
+ export function frequency() {
55
+ return BigInt(ticksPerMillisecond * 1000);
56
+ }
57
+ export function isHighResolution() {
58
+ return true;
59
+ }
60
+ export function getTimestamp() {
61
+ return BigInt(Math.trunc(performance.now() * ticksPerMillisecond));
62
+ }
63
+ export function getElapsedTotalMilliseconds(startingTimestamp, endingTimestamp) {
64
+ return Number((endingTimestamp ?? getTimestamp()) - startingTimestamp) / ticksPerMillisecond;
65
+ }
@@ -1,8 +1,19 @@
1
1
  import { toString as dateToString } from "./Date.js";
2
- import { compare as numericCompare, isNumeric, isIntegral, multiply, toExponential, toFixed, toHex, toPrecision } from "./Numeric.js";
2
+ import { compare as numericCompare, divide, isNumeric, isIntegral, multiply, toExponential, toFixed, toHex, toPrecision } from "./Numeric.js";
3
3
  import { escape } from "./RegExp.js";
4
4
  import { toString } from "./Types.js";
5
5
  import { Exception } from "./Util.js";
6
+ // Temporal date/time values (only present under --test:js-temporal) are not JS Dates. To avoid
7
+ // importing the side-effecting Temporal runtime modules into this core file — which would pull
8
+ // them into every bundle — they advertise a .NET-style formatter through this well-known symbol,
9
+ // attached to their prototype when (and only when) those modules are loaded.
10
+ const dateTimeFormattableSymbol = Symbol.for("Fable.DateTimeFormattable");
11
+ function isNetDateFormattable(rep) {
12
+ return rep != null && typeof rep[dateTimeFormattableSymbol] === "function";
13
+ }
14
+ function formatDateLike(rep, format) {
15
+ return rep instanceof Date ? dateToString(rep, format) : rep[dateTimeFormattableSymbol](format);
16
+ }
6
17
  const fsFormatRegExp = /(^|[^%])%([0+\- ]*)(\*|\d+)?(?:\.(\d+))?(\w)/g;
7
18
  const interpolateRegExp = /(?:(^|[^%])%([0+\- ]*)(\d+)?(?:\.(\d+))?(\w))?%P\(\)/g;
8
19
  const formatRegExp = /\{(\d+)(,-?\d+)?(?:\:([a-zA-Z])(\d{0,2})|\:(.+?))?\}/g;
@@ -236,8 +247,8 @@ function formatReplacement(rep, flags, padLength, precision, format) {
236
247
  break;
237
248
  }
238
249
  }
239
- else if (rep instanceof Date) {
240
- rep = dateToString(rep);
250
+ else if (rep instanceof Date || isNetDateFormattable(rep)) {
251
+ rep = formatDateLike(rep, undefined);
241
252
  }
242
253
  else if (format === "A" && typeof rep === "string") {
243
254
  rep = "\"" + rep + "\"";
@@ -396,9 +407,14 @@ export function format(str, ...args) {
396
407
  }
397
408
  break;
398
409
  case "e":
399
- case "E":
400
- rep = precision != null ? toExponential(rep, precision) : toExponential(rep);
410
+ case "E": {
411
+ precision = precision != null ? precision : 6;
412
+ rep = String(toExponential(rep, precision));
413
+ // .NET always signs the exponent and pads it to at least three digits
414
+ const eIdx = rep.indexOf("e");
415
+ rep = rep.slice(0, eIdx) + format + rep[eIdx + 1] + padLeft(rep.slice(eIdx + 2), 3, "0");
401
416
  break;
417
+ }
402
418
  case "f":
403
419
  case "F":
404
420
  precision = precision != null ? precision : 2;
@@ -410,6 +426,10 @@ export function format(str, ...args) {
410
426
  break;
411
427
  case "g":
412
428
  case "G": {
429
+ if (precision == null && typeof rep !== "number" && typeof rep !== "bigint") {
430
+ rep = String(rep);
431
+ break;
432
+ }
413
433
  rep = precision != null ? toPrecision(rep, precision) : toPrecision(rep);
414
434
  // Handle exponential notation: only trim trailing zeros from mantissa, not from exponent.
415
435
  // .NET G format guarantees an exponent of at least 2 digits with an explicit sign (e.g. "E-07").
@@ -422,7 +442,7 @@ export function format(str, ...args) {
422
442
  const eChar = format === "G" ? "E" : "e";
423
443
  rep = mantissa + eChar + expSign + paddedExpDigits;
424
444
  }
425
- else {
445
+ else if (rep.indexOf(".") >= 0) {
426
446
  rep = trimEnd(trimEnd(rep, "0"), ".");
427
447
  }
428
448
  break;
@@ -473,7 +493,21 @@ export function format(str, ...args) {
473
493
  }
474
494
  if (pattern) {
475
495
  let sign = "";
476
- rep = pattern.replace(/([0#,]+)(\.[0#]+)?/, (_, intPart, decimalPart) => {
496
+ const patternStr = pattern;
497
+ // Each `%` scales the value by 100 and is kept as a literal in the output
498
+ const percents = (patternStr.match(/%/g) ?? []).length;
499
+ if (percents > 0) {
500
+ rep = multiply(rep, Math.pow(100, percents));
501
+ }
502
+ // .NET ignores commas placed after the decimal placeholders, hence the trailing group
503
+ rep = patternStr.replace(/([0#,]+)(\.[0#]+)?(,*)/, (_, intPart, decimalPart) => {
504
+ // Commas between the last integer placeholder and the decimal point scale the
505
+ // value down by 1000 each; commas anywhere else only turn on digit grouping
506
+ const scaleCommas = /,*$/.exec(intPart)[0].length;
507
+ if (scaleCommas > 0) {
508
+ intPart = intPart.substring(0, intPart.length - scaleCommas);
509
+ rep = divide(rep, Math.pow(1000, scaleCommas));
510
+ }
477
511
  if (isLessThan(rep, 0)) {
478
512
  rep = multiply(rep, -1);
479
513
  sign = "-";
@@ -507,8 +541,8 @@ export function format(str, ...args) {
507
541
  }
508
542
  }
509
543
  }
510
- else if (rep instanceof Date) {
511
- rep = dateToString(rep, pattern || format);
544
+ else if (rep instanceof Date || isNetDateFormattable(rep)) {
545
+ rep = formatDateLike(rep, pattern || format);
512
546
  }
513
547
  else {
514
548
  rep = toString(rep);
@@ -178,6 +178,13 @@ export function StringBuilder__set_Chars_413E0D0A(x, index, value) {
178
178
  setItem(x.buf, i, (item(i, x.buf).slice(0, (pos - 1) + 1) + value) + item(i, x.buf).slice(pos + 1, item(i, x.buf).length));
179
179
  }
180
180
  }
181
+ export function StringBuilder__get_Length(x) {
182
+ let len = 0;
183
+ for (let i = x.buf.length - 1; i >= 0; i--) {
184
+ len = ((len + item(i, x.buf).length) | 0);
185
+ }
186
+ return len | 0;
187
+ }
181
188
  export function StringBuilder__Replace_Z766F94C0(x, oldValue, newValue) {
182
189
  for (let i = x.buf.length - 1; i >= 0; i--) {
183
190
  setItem(x.buf, i, replace(item(i, x.buf), oldValue, newValue));
@@ -188,13 +195,6 @@ export function StringBuilder__Replace_Z384F8060(x, oldValue, newValue) {
188
195
  const str = replace(x.toString(), oldValue, newValue);
189
196
  return StringBuilder__Append_Z721C83C5(StringBuilder__Clear(x), str);
190
197
  }
191
- export function StringBuilder__get_Length(x) {
192
- let len = 0;
193
- for (let i = x.buf.length - 1; i >= 0; i--) {
194
- len = ((len + item(i, x.buf).length) | 0);
195
- }
196
- return len | 0;
197
- }
198
- export function StringBuilder__ToString_Z37302880(x, firstIndex, length) {
199
- return substring(x.toString(), firstIndex, length);
198
+ export function StringBuilder__ToString_Z37302880(x, startIndex, length) {
199
+ return substring(x.toString(), startIndex, length);
200
200
  }
@@ -17,6 +17,28 @@ export function fromResult(value) {
17
17
  export function zero() {
18
18
  return Promise.resolve();
19
19
  }
20
+ // Like .NET, -1 waits until the token is cancelled
21
+ export function delay(millisecondsDelay, cancellationToken) {
22
+ return new Promise((resolve, reject) => {
23
+ if (cancellationToken?.isCancelled) {
24
+ reject(new OperationCanceledException());
25
+ return;
26
+ }
27
+ let tokenId;
28
+ const timeoutId = millisecondsDelay === -1 ? undefined : setTimeout(() => {
29
+ if (tokenId !== undefined) {
30
+ cancellationToken.removeListener(tokenId);
31
+ }
32
+ resolve();
33
+ }, millisecondsDelay);
34
+ if (cancellationToken != null) {
35
+ tokenId = cancellationToken.addListener(() => {
36
+ clearTimeout(timeoutId);
37
+ reject(new OperationCanceledException());
38
+ });
39
+ }
40
+ });
41
+ }
20
42
  // Task<T> = Promise<T> in JS/TS. GetAwaiter/GetResult return the Promise itself;
21
43
  // callers should use Async.AwaitTask to extract the value in an async context.
22
44
  export function getAwaiter(t) {
@@ -0,0 +1,142 @@
1
+ import { toInt64, fromFloat64 } from "./BigInt.js";
2
+ import { fromTicks as TimeSpan_fromTicks, totalNanoseconds as TimeSpan_totalNanoseconds } from "./TimeSpanTemporal.js";
3
+ import { Exception } from "./Util.js";
4
+ export const PlainTime = Temporal.PlainTime;
5
+ // The generic equality/comparison/hashing helpers in Util.ts dispatch at runtime
6
+ // on .NET-style Equals/CompareTo/GetHashCode methods. Attach them so TimeOnly
7
+ // also works in generic contexts: records, tuples, erased generics, etc.
8
+ const proto = Temporal.PlainTime.prototype;
9
+ proto.Equals = function (other) { return this.equals(other); };
10
+ proto.CompareTo = function (other) { return Temporal.PlainTime.compare(this, other); };
11
+ proto.GetHashCode = function () { return hash(this); };
12
+ // Lets String.Format format this value without String.ts importing this module (see String.ts)
13
+ proto[Symbol.for("Fable.DateTimeFormattable")] = function (format) { return toString(this, format); };
14
+ const nanosecondsPerDay = 86400000000000;
15
+ const nanosecondsPerDayBig = 86400000000000n;
16
+ function totalNanoseconds(t) {
17
+ return ((t.hour * 60 + t.minute) * 60 + t.second) * 1000000000
18
+ + t.millisecond * 1000000 + t.microsecond * 1000 + t.nanosecond;
19
+ }
20
+ function fromNanoseconds(n) {
21
+ return new Temporal.PlainTime(Math.floor(n / 3600000000000), Math.floor(n / 60000000000) % 60, Math.floor(n / 1000000000) % 60, Math.floor(n / 1000000) % 1000, Math.floor(n / 1000) % 1000, n % 1000);
22
+ }
23
+ export function create(h = 0, m = 0, s = 0, ms = 0, us = 0) {
24
+ return new Temporal.PlainTime(h, m, s, ms, us);
25
+ }
26
+ export function fromTicks(ticks) {
27
+ return fromNanoseconds(Number(BigInt(ticks) * 100n));
28
+ }
29
+ export function fromTimeSpan(timeSpan) {
30
+ const ns = Number(TimeSpan_totalNanoseconds(timeSpan));
31
+ if (ns < 0 || ns >= nanosecondsPerDay)
32
+ throw new Exception("The TimeSpan describes an unrepresentable TimeOnly.");
33
+ return fromNanoseconds(ns);
34
+ }
35
+ export function fromDateTime(d) {
36
+ // Under the Temporal representation a DateTime is a PlainDateTime (kind-agnostic wall-clock)
37
+ return new Temporal.PlainTime(d.hour, d.minute, d.second, d.millisecond, d.microsecond, d.nanosecond);
38
+ }
39
+ export function minValue() {
40
+ return new Temporal.PlainTime();
41
+ }
42
+ export function maxValue() {
43
+ // This is "23:59:59.9999999" (.NET tick precision)
44
+ return new Temporal.PlainTime(23, 59, 59, 999, 999, 900);
45
+ }
46
+ export function ticks(t) {
47
+ return toInt64(fromFloat64(totalNanoseconds(t) / 100));
48
+ }
49
+ export function toTimeSpan(t) {
50
+ // TimeOnly is tick-precise, so the nanosecond count is always a multiple of 100.
51
+ return TimeSpan_fromTicks(Math.round(totalNanoseconds(t) / 100));
52
+ }
53
+ // Kept in bigint: a TimeOnly itself always fits a float64 exactly (under 2^47 ns),
54
+ // but the amount added does not — a TimeSpan of more than ~104 days already
55
+ // exceeds Number.MAX_SAFE_INTEGER in nanoseconds, and .NET wraps it modulo a day
56
+ // rather than saturating, so the low ticks are the part that must survive.
57
+ function addNanoseconds(t, deltaNs, wrappedDays) {
58
+ const totalNs = BigInt(totalNanoseconds(t)) + deltaNs;
59
+ let days = totalNs / nanosecondsPerDayBig;
60
+ let remainder = totalNs % nanosecondsPerDayBig;
61
+ // BigInt division truncates toward zero; .NET wraps toward the previous day.
62
+ if (remainder < 0n) {
63
+ remainder += nanosecondsPerDayBig;
64
+ days -= 1n;
65
+ }
66
+ if (wrappedDays !== undefined) {
67
+ wrappedDays.contents = Number(days);
68
+ }
69
+ return fromNanoseconds(Number(remainder));
70
+ }
71
+ export function add(t, ts, wrappedDays) {
72
+ return addNanoseconds(t, TimeSpan_totalNanoseconds(ts), wrappedDays);
73
+ }
74
+ export function addHours(t, h) {
75
+ // Scale to ticks first, as .NET does, then widen — `h` hours in nanoseconds
76
+ // overflows the exact float64 range two orders of magnitude sooner.
77
+ return addNanoseconds(t, BigInt(Math.round(h * 36000000000)) * 100n);
78
+ }
79
+ export function addMinutes(t, m) {
80
+ return addNanoseconds(t, BigInt(Math.round(m * 600000000)) * 100n);
81
+ }
82
+ export function isBetween(t, start, end) {
83
+ return Temporal.PlainTime.compare(start, end) <= 0
84
+ ? (Temporal.PlainTime.compare(start, t) <= 0 && Temporal.PlainTime.compare(end, t) > 0)
85
+ : (Temporal.PlainTime.compare(start, t) <= 0 || Temporal.PlainTime.compare(end, t) > 0);
86
+ }
87
+ export function equals(x, y) {
88
+ return x.equals(y);
89
+ }
90
+ export function compare(x, y) {
91
+ return Temporal.PlainTime.compare(x, y);
92
+ }
93
+ export function hash(t) {
94
+ return totalNanoseconds(t) % 2147483647;
95
+ }
96
+ export function op_Subtraction(left, right) {
97
+ // Returns the elapsed TimeSpan, wrapping around midnight
98
+ const ns = (totalNanoseconds(left) - totalNanoseconds(right) + nanosecondsPerDay) % nanosecondsPerDay;
99
+ return TimeSpan_fromTicks(Math.round(ns / 100));
100
+ }
101
+ export function toString(t, format = "t", _provider) {
102
+ switch (format) {
103
+ case "t":
104
+ return t.toString({ smallestUnit: "minute" });
105
+ case "r":
106
+ case "R":
107
+ case "T":
108
+ return t.toString({ smallestUnit: "second" });
109
+ case "o":
110
+ case "O":
111
+ // .NET tick precision is 7 fractional digits
112
+ return t.toString({ fractionalSecondDigits: 7 });
113
+ default:
114
+ throw new Exception("Custom formats are not supported");
115
+ }
116
+ }
117
+ export function parse(str) {
118
+ // Allowed format types:
119
+ // hh:mm
120
+ // hh:mm:ss
121
+ // hh:mm:ss.fffffff
122
+ const r = /^\s*([0-1]?\d|2[0-3])\s*:\s*([0-5]?\d)(\s*:\s*([0-5]?\d)(\.(\d+))?)?\s*$/.exec(str);
123
+ if (r != null && r[1] != null && r[2] != null) {
124
+ const h = +r[1];
125
+ const m = +r[2];
126
+ const s = r[4] != null ? +r[4] : 0;
127
+ // .NET resolves the fraction down to a single tick (7 digits) and truncates
128
+ // anything finer, so the digits are read as ticks rather than milliseconds.
129
+ const subSecondTicks = r[6] != null ? +r[6].padEnd(7, "0").substring(0, 7) : 0;
130
+ return fromNanoseconds(((h * 60 + m) * 60 + s) * 1000000000 + subSecondTicks * 100);
131
+ }
132
+ throw new Exception(`String '${str}' was not recognized as a valid TimeOnly.`);
133
+ }
134
+ export function tryParse(v, defValue) {
135
+ try {
136
+ defValue.contents = parse(v);
137
+ return true;
138
+ }
139
+ catch {
140
+ return false;
141
+ }
142
+ }