oakscriptjs 0.15.1 → 0.15.3

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 (103) hide show
  1. package/dist/cjs/callsite/index.d.ts.map +1 -1
  2. package/dist/cjs/callsite/index.js +61 -7
  3. package/dist/cjs/callsite/index.js.map +2 -2
  4. package/dist/cjs/drawing/registry.d.ts +31 -3
  5. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  6. package/dist/cjs/drawing/registry.js +188 -68
  7. package/dist/cjs/drawing/registry.js.map +2 -2
  8. package/dist/cjs/index.d.ts +1 -1
  9. package/dist/cjs/index.js +1 -1
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/linefill/index.d.ts.map +1 -1
  12. package/dist/cjs/linefill/index.js +14 -5
  13. package/dist/cjs/linefill/index.js.map +2 -2
  14. package/dist/cjs/session/bars.d.ts +3 -0
  15. package/dist/cjs/session/bars.d.ts.map +1 -1
  16. package/dist/cjs/session/bars.js +10 -3
  17. package/dist/cjs/session/bars.js.map +2 -2
  18. package/dist/cjs/session/calendar.d.ts +19 -6
  19. package/dist/cjs/session/calendar.d.ts.map +1 -1
  20. package/dist/cjs/session/calendar.js +57 -18
  21. package/dist/cjs/session/calendar.js.map +2 -2
  22. package/dist/cjs/session/context.d.ts.map +1 -1
  23. package/dist/cjs/session/context.js +25 -8
  24. package/dist/cjs/session/context.js.map +2 -2
  25. package/dist/cjs/str/floatingdecimal.d.ts +3 -0
  26. package/dist/cjs/str/floatingdecimal.d.ts.map +1 -1
  27. package/dist/cjs/str/floatingdecimal.js +25 -12
  28. package/dist/cjs/str/floatingdecimal.js.map +2 -2
  29. package/dist/cjs/str/messageformat.d.ts.map +1 -1
  30. package/dist/cjs/str/messageformat.js +27 -8
  31. package/dist/cjs/str/messageformat.js.map +2 -2
  32. package/dist/cjs/str/numberformat.d.ts.map +1 -1
  33. package/dist/cjs/str/numberformat.js +9 -16
  34. package/dist/cjs/str/numberformat.js.map +3 -3
  35. package/dist/cjs/ta/index.d.ts.map +1 -1
  36. package/dist/cjs/ta/index.js +27 -0
  37. package/dist/cjs/ta/index.js.map +2 -2
  38. package/dist/cjs/ta/running-sum.d.ts +8 -1
  39. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  40. package/dist/cjs/ta/running-sum.js +13 -1
  41. package/dist/cjs/ta/running-sum.js.map +2 -2
  42. package/dist/cjs/time/timezone.d.ts +13 -1
  43. package/dist/cjs/time/timezone.d.ts.map +1 -1
  44. package/dist/cjs/time/timezone.js +64 -12
  45. package/dist/cjs/time/timezone.js.map +2 -2
  46. package/dist/esm/callsite/index.d.ts.map +1 -1
  47. package/dist/esm/callsite/index.js +76 -7
  48. package/dist/esm/callsite/index.js.map +1 -1
  49. package/dist/esm/drawing/registry.d.ts +31 -3
  50. package/dist/esm/drawing/registry.d.ts.map +1 -1
  51. package/dist/esm/drawing/registry.js +237 -80
  52. package/dist/esm/drawing/registry.js.map +1 -1
  53. package/dist/esm/index.d.ts +1 -1
  54. package/dist/esm/index.js +1 -1
  55. package/dist/esm/linefill/index.d.ts.map +1 -1
  56. package/dist/esm/linefill/index.js +17 -7
  57. package/dist/esm/linefill/index.js.map +1 -1
  58. package/dist/esm/session/bars.d.ts +3 -0
  59. package/dist/esm/session/bars.d.ts.map +1 -1
  60. package/dist/esm/session/bars.js +11 -3
  61. package/dist/esm/session/bars.js.map +1 -1
  62. package/dist/esm/session/calendar.d.ts +19 -6
  63. package/dist/esm/session/calendar.d.ts.map +1 -1
  64. package/dist/esm/session/calendar.js +74 -21
  65. package/dist/esm/session/calendar.js.map +1 -1
  66. package/dist/esm/session/context.d.ts.map +1 -1
  67. package/dist/esm/session/context.js +34 -9
  68. package/dist/esm/session/context.js.map +1 -1
  69. package/dist/esm/str/floatingdecimal.d.ts +3 -0
  70. package/dist/esm/str/floatingdecimal.d.ts.map +1 -1
  71. package/dist/esm/str/floatingdecimal.js +27 -11
  72. package/dist/esm/str/floatingdecimal.js.map +1 -1
  73. package/dist/esm/str/messageformat.d.ts.map +1 -1
  74. package/dist/esm/str/messageformat.js +35 -9
  75. package/dist/esm/str/messageformat.js.map +1 -1
  76. package/dist/esm/str/numberformat.d.ts.map +1 -1
  77. package/dist/esm/str/numberformat.js +12 -18
  78. package/dist/esm/str/numberformat.js.map +1 -1
  79. package/dist/esm/ta/index.d.ts.map +1 -1
  80. package/dist/esm/ta/index.js +35 -0
  81. package/dist/esm/ta/index.js.map +1 -1
  82. package/dist/esm/ta/running-sum.d.ts +8 -1
  83. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  84. package/dist/esm/ta/running-sum.js +16 -1
  85. package/dist/esm/ta/running-sum.js.map +1 -1
  86. package/dist/esm/time/timezone.d.ts +13 -1
  87. package/dist/esm/time/timezone.d.ts.map +1 -1
  88. package/dist/esm/time/timezone.js +97 -16
  89. package/dist/esm/time/timezone.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/callsite/index.ts +65 -7
  92. package/src/drawing/registry.ts +249 -79
  93. package/src/index.ts +1 -1
  94. package/src/linefill/index.ts +16 -7
  95. package/src/session/bars.ts +11 -3
  96. package/src/session/calendar.ts +84 -21
  97. package/src/session/context.ts +36 -9
  98. package/src/str/floatingdecimal.ts +27 -11
  99. package/src/str/messageformat.ts +40 -9
  100. package/src/str/numberformat.ts +13 -17
  101. package/src/ta/index.ts +41 -0
  102. package/src/ta/running-sum.ts +13 -1
  103. package/src/time/timezone.ts +99 -16
@@ -36,13 +36,18 @@ type Zone =
36
36
  | {
37
37
  readonly kind: 'iana';
38
38
  readonly format: Intl.DateTimeFormat;
39
- /** Offset of each UTC hour (`floor(time / 1 h)`); null for an hour with an offset change inside */
39
+ /** Offset at the start of each UTC day (`floor(time / 1 day)`) */
40
+ readonly dayStarts: Map<number, number>;
41
+ /** Offset of each UTC hour (`floor(time / 1 h)`) of a day with an offset change; null for an hour with the
42
+ * change inside */
40
43
  readonly hours: Map<number, number | null>;
41
44
  };
42
45
 
43
46
  const MS_PER_HOUR = 3_600_000;
44
47
  /** Cached hours per zone before the cache restarts (about 11 years of hours) */
45
48
  const MAX_CACHED_HOURS = 100_000;
49
+ /** Cached days per zone before the cache restarts (about 270 years) */
50
+ const MAX_CACHED_DAYS = 100_000;
46
51
 
47
52
  const zones = new Map<string, Zone>();
48
53
 
@@ -80,19 +85,58 @@ function resolveZone(timezone: string): Zone {
80
85
  } catch {
81
86
  throw invalid();
82
87
  }
83
- zone = { kind: 'iana', format, hours: new Map() };
88
+ zone = { kind: 'iana', format, dayStarts: new Map(), hours: new Map() };
84
89
  }
85
90
  zones.set(timezone, zone);
86
91
  return zone;
87
92
  }
88
93
 
94
+ /** Days whose starts are read together (see `dayStartOffset`) */
95
+ const DAYS_PER_BLOCK = 3;
96
+ /** Last day whose block and the next one are inside the `Date` range (8.64e15 ms on each side of 1970) */
97
+ const LAST_BLOCK_DAY = 100_000_000 - DAYS_PER_BLOCK;
98
+
99
+ /** Offset of the zone at the start of UTC day `day`, from `Intl`, cached. */
100
+ function formattedDayStart(zone: Zone & { kind: 'iana' }, day: number): number {
101
+ let offset = zone.dayStarts.get(day);
102
+ if (offset === undefined) {
103
+ offset = formattedOffset(zone.format, day * MS_PER_DAY);
104
+ zone.dayStarts.set(day, offset);
105
+ }
106
+ return offset;
107
+ }
108
+
109
+ /**
110
+ * Offset of the zone at the start of UTC day `day`, cached. The days are read by blocks of three: two offset changes
111
+ * are always more than three days apart (167 hours at least, see `offsetAt`), so when the starts of a block and of
112
+ * the next one have the same offset, no change happens in between and the days of the block have it: one `Intl`
113
+ * call for three days instead of one per day.
114
+ */
115
+ function dayStartOffset(zone: Zone & { kind: 'iana' }, day: number): number {
116
+ const cached = zone.dayStarts.get(day);
117
+ if (cached !== undefined) return cached;
118
+ if (zone.dayStarts.size >= MAX_CACHED_DAYS) zone.dayStarts.clear();
119
+ if (!(Math.abs(day) <= LAST_BLOCK_DAY)) return formattedDayStart(zone, day);
120
+ const first = Math.floor(day / DAYS_PER_BLOCK) * DAYS_PER_BLOCK;
121
+ const offset = formattedDayStart(zone, first);
122
+ if (offset !== formattedDayStart(zone, first + DAYS_PER_BLOCK)) return formattedDayStart(zone, day);
123
+ for (let d = first + 1; d < first + DAYS_PER_BLOCK; d++) zone.dayStarts.set(d, offset);
124
+ return offset;
125
+ }
126
+
89
127
  /**
90
128
  * Offset of the zone from UTC at instant `time`, in milliseconds (local = UTC + offset).
91
- * An offset change happens at most once in a UTC hour, so when the first and the last second of the hour have the
92
- * same offset, the whole hour has it: it is computed once and cached.
129
+ *
130
+ * Two offset changes of a zone are always more than a day apart (the closest ones of the time zone database are 167
131
+ * hours apart), so when a UTC day starts with the offset of the next day, the whole day has it: daily bars cost one
132
+ * lookup per day. In a day with a change, an hour whose first and last second have the same offset has it (an offset
133
+ * change happens at most once in an hour): it is computed once and cached.
93
134
  */
94
135
  function offsetAt(zone: Zone, time: number): number {
95
136
  if (zone.kind === 'fixed') return zone.offsetMs;
137
+ const day = Math.floor(time / MS_PER_DAY);
138
+ const start = dayStartOffset(zone, day);
139
+ if (start === dayStartOffset(zone, day + 1)) return start;
96
140
  const hour = Math.floor(time / MS_PER_HOUR);
97
141
  let offset = zone.hours.get(hour);
98
142
  if (offset === undefined) {
@@ -135,17 +179,48 @@ export function zoneOffset(time: number, timezone: string): number {
135
179
  return offsetAt(resolveZone(timezone), time);
136
180
  }
137
181
 
138
- /** Splits a UNIX time (ms) into calendar fields in `timezone`. */
182
+ /** Last valid `Date` time on each side of 01/01/1970 (ms) */
183
+ const MAX_DATE_MS = 8.64e15;
184
+
185
+ /**
186
+ * Year, month (1-12) and day of the month of a day count since 01/01/1970, in the proleptic Gregorian calendar as
187
+ * `Date` reads it, by integer arithmetic (years counted from March, the leap day last).
188
+ */
189
+ export function civilFromDays(days: number): { year: number; month: number; day: number } {
190
+ const z = days + 719_468;
191
+ const era = Math.floor(z / 146_097);
192
+ const dayOfEra = z - era * 146_097;
193
+ const yearOfEra = Math.floor(
194
+ (dayOfEra - Math.floor(dayOfEra / 1460) + Math.floor(dayOfEra / 36_524) - Math.floor(dayOfEra / 146_096)) / 365
195
+ );
196
+ const dayOfYear = dayOfEra - (yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
197
+ const mp = Math.floor((5 * dayOfYear + 2) / 153);
198
+ const month = mp < 10 ? mp + 3 : mp - 9;
199
+ return { year: yearOfEra + era * 400 + (month <= 2 ? 1 : 0), month, day: dayOfYear - Math.floor((153 * mp + 2) / 5) + 1 };
200
+ }
201
+
202
+ /**
203
+ * Splits a UNIX time (ms) into calendar fields in `timezone`: the fields `Date` gives for the wall clock (whole ms,
204
+ * NaN outside its range), computed without one as every bar reads them.
205
+ */
139
206
  export function zonedFields(time: number, timezone: string): ZonedFields {
140
- const local = new Date(time + offsetAt(resolveZone(timezone), time));
207
+ const wall = time + offsetAt(resolveZone(timezone), time);
208
+ if (!(Math.abs(wall) <= MAX_DATE_MS)) {
209
+ return { year: NaN, month: NaN, day: NaN, hour: NaN, minute: NaN, second: NaN, dayOfWeek: NaN };
210
+ }
211
+ const ms = Math.trunc(wall);
212
+ const days = Math.floor(ms / MS_PER_DAY);
213
+ const inDay = ms - days * MS_PER_DAY;
214
+ const { year, month, day } = civilFromDays(days);
141
215
  return {
142
- year: local.getUTCFullYear(),
143
- month: local.getUTCMonth() + 1,
144
- day: local.getUTCDate(),
145
- hour: local.getUTCHours(),
146
- minute: local.getUTCMinutes(),
147
- second: local.getUTCSeconds(),
148
- dayOfWeek: local.getUTCDay() + 1,
216
+ year,
217
+ month,
218
+ day,
219
+ hour: Math.floor(inDay / MS_PER_HOUR),
220
+ minute: Math.floor(inDay / 60_000) % 60,
221
+ second: Math.floor(inDay / MS_PER_SECOND) % 60,
222
+ // 01/01/1970 is a Thursday (5)
223
+ dayOfWeek: ((((days + 4) % 7) + 7) % 7) + 1,
149
224
  };
150
225
  }
151
226
 
@@ -163,6 +238,9 @@ export function zonedFields(time: number, timezone: string): ZonedFields {
163
238
  */
164
239
  export function civilToUnix(year: number, month: number, day: number, hour = 0, minute = 0, second = 0): number {
165
240
  const gregorian = Date.UTC(year, month - 1, day, hour, minute, second);
241
+ // a month of a year after 1582 (and before the end of the Date range) is Gregorian whatever the day: no need to
242
+ // normalize the date
243
+ if (year >= 1583 && year < 275_760 && month >= 1 && month < 13) return gregorian;
166
244
  // normalized year / month / day of the Gregorian reading decides the calendar
167
245
  const d = new Date(Date.UTC(year, month - 1, 1));
168
246
  const y = d.getUTCFullYear();
@@ -200,8 +278,13 @@ export function zonedToUnix(
200
278
  // one day before and one day after are the two candidates.
201
279
  const before = offsetAt(zone, local - MS_PER_DAY);
202
280
  const after = offsetAt(zone, local + MS_PER_DAY);
203
- const candidates = [local - before, local - after].filter((t) => local - offsetAt(zone, t) === t);
281
+ // the same offset on both sides: no change near `local`, the wall time is read with it
282
+ if (before === after) return local - before;
283
+ const withBefore = local - before;
284
+ const withAfter = local - after;
285
+ const beforeFits = local - offsetAt(zone, withBefore) === withBefore;
286
+ const afterFits = local - offsetAt(zone, withAfter) === withAfter;
204
287
  // a wall time that occurs twice: the later instant, or the earlier one with `earlier`
205
- if (candidates.length) return earlier ? Math.min(...candidates) : Math.max(...candidates);
206
- return local - before;
288
+ if (beforeFits && afterFits) return earlier ? Math.min(withBefore, withAfter) : Math.max(withBefore, withAfter);
289
+ return afterFits ? withAfter : withBefore;
207
290
  }