@nubjs/nub-linux-x64 0.0.7 → 0.0.8

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 (387) hide show
  1. package/bin/nub +0 -0
  2. package/package.json +1 -1
  3. package/runtime/addons/nub-native.node +0 -0
  4. package/runtime/node_modules/@js-temporal/polyfill/CHANGELOG.md +453 -0
  5. package/runtime/node_modules/@js-temporal/polyfill/LICENSE +13 -0
  6. package/runtime/node_modules/@js-temporal/polyfill/README.md +90 -0
  7. package/runtime/node_modules/@js-temporal/polyfill/dist/index.cjs +2 -0
  8. package/runtime/node_modules/@js-temporal/polyfill/dist/index.cjs.map +1 -0
  9. package/runtime/node_modules/@js-temporal/polyfill/dist/index.esm.js +2 -0
  10. package/runtime/node_modules/@js-temporal/polyfill/dist/index.esm.js.map +1 -0
  11. package/runtime/node_modules/@js-temporal/polyfill/dist/index.umd.js +2 -0
  12. package/runtime/node_modules/@js-temporal/polyfill/dist/index.umd.js.map +1 -0
  13. package/runtime/node_modules/@js-temporal/polyfill/index.d.cts +1215 -0
  14. package/runtime/node_modules/@js-temporal/polyfill/index.d.ts +1215 -0
  15. package/runtime/node_modules/@js-temporal/polyfill/lib/assert.ts +8 -0
  16. package/runtime/node_modules/@js-temporal/polyfill/lib/bigintmath.ts +39 -0
  17. package/runtime/node_modules/@js-temporal/polyfill/lib/calendar.ts +2424 -0
  18. package/runtime/node_modules/@js-temporal/polyfill/lib/debug.ts +7 -0
  19. package/runtime/node_modules/@js-temporal/polyfill/lib/duration.ts +438 -0
  20. package/runtime/node_modules/@js-temporal/polyfill/lib/durationformat.d.ts +93 -0
  21. package/runtime/node_modules/@js-temporal/polyfill/lib/ecmascript.ts +4973 -0
  22. package/runtime/node_modules/@js-temporal/polyfill/lib/index.ts +34 -0
  23. package/runtime/node_modules/@js-temporal/polyfill/lib/init.ts +43 -0
  24. package/runtime/node_modules/@js-temporal/polyfill/lib/instant.ts +138 -0
  25. package/runtime/node_modules/@js-temporal/polyfill/lib/internaltypes.d.ts +242 -0
  26. package/runtime/node_modules/@js-temporal/polyfill/lib/intl.ts +758 -0
  27. package/runtime/node_modules/@js-temporal/polyfill/lib/intrinsicclass.ts +162 -0
  28. package/runtime/node_modules/@js-temporal/polyfill/lib/legacydate.ts +17 -0
  29. package/runtime/node_modules/@js-temporal/polyfill/lib/math.ts +96 -0
  30. package/runtime/node_modules/@js-temporal/polyfill/lib/now.ts +48 -0
  31. package/runtime/node_modules/@js-temporal/polyfill/lib/plaindate.ts +212 -0
  32. package/runtime/node_modules/@js-temporal/polyfill/lib/plaindatetime.ts +274 -0
  33. package/runtime/node_modules/@js-temporal/polyfill/lib/plainmonthday.ts +110 -0
  34. package/runtime/node_modules/@js-temporal/polyfill/lib/plaintime.ts +154 -0
  35. package/runtime/node_modules/@js-temporal/polyfill/lib/plainyearmonth.ts +157 -0
  36. package/runtime/node_modules/@js-temporal/polyfill/lib/regex.ts +59 -0
  37. package/runtime/node_modules/@js-temporal/polyfill/lib/slots.ts +341 -0
  38. package/runtime/node_modules/@js-temporal/polyfill/lib/temporal.ts +10 -0
  39. package/runtime/node_modules/@js-temporal/polyfill/lib/timeduration.ts +149 -0
  40. package/runtime/node_modules/@js-temporal/polyfill/lib/zoneddatetime.ts +492 -0
  41. package/runtime/node_modules/@js-temporal/polyfill/package.json +143 -0
  42. package/runtime/node_modules/@oxc-project/runtime/CHANGELOG.md +88 -0
  43. package/runtime/node_modules/@oxc-project/runtime/LICENSE +22 -0
  44. package/runtime/node_modules/@oxc-project/runtime/README.md +5 -0
  45. package/runtime/node_modules/@oxc-project/runtime/package.json +1066 -0
  46. package/runtime/node_modules/@oxc-project/runtime/src/helpers/AwaitValue.js +4 -0
  47. package/runtime/node_modules/@oxc-project/runtime/src/helpers/OverloadYield.js +4 -0
  48. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecoratedDescriptor.js +9 -0
  49. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs.js +236 -0
  50. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs2203.js +184 -0
  51. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs2203R.js +191 -0
  52. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs2301.js +222 -0
  53. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs2305.js +133 -0
  54. package/runtime/node_modules/@oxc-project/runtime/src/helpers/applyDecs2311.js +124 -0
  55. package/runtime/node_modules/@oxc-project/runtime/src/helpers/arrayLikeToArray.js +6 -0
  56. package/runtime/node_modules/@oxc-project/runtime/src/helpers/arrayWithHoles.js +4 -0
  57. package/runtime/node_modules/@oxc-project/runtime/src/helpers/arrayWithoutHoles.js +5 -0
  58. package/runtime/node_modules/@oxc-project/runtime/src/helpers/assertClassBrand.js +5 -0
  59. package/runtime/node_modules/@oxc-project/runtime/src/helpers/assertThisInitialized.js +5 -0
  60. package/runtime/node_modules/@oxc-project/runtime/src/helpers/asyncGeneratorDelegate.js +24 -0
  61. package/runtime/node_modules/@oxc-project/runtime/src/helpers/asyncIterator.js +45 -0
  62. package/runtime/node_modules/@oxc-project/runtime/src/helpers/asyncToGenerator.js +26 -0
  63. package/runtime/node_modules/@oxc-project/runtime/src/helpers/awaitAsyncGenerator.js +5 -0
  64. package/runtime/node_modules/@oxc-project/runtime/src/helpers/callSuper.js +7 -0
  65. package/runtime/node_modules/@oxc-project/runtime/src/helpers/checkInRHS.js +6 -0
  66. package/runtime/node_modules/@oxc-project/runtime/src/helpers/checkPrivateRedeclaration.js +4 -0
  67. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classApplyDescriptorDestructureSet.js +10 -0
  68. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classApplyDescriptorGet.js +4 -0
  69. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classApplyDescriptorSet.js +7 -0
  70. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classCallCheck.js +4 -0
  71. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classCheckPrivateStaticAccess.js +5 -0
  72. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classCheckPrivateStaticFieldDescriptor.js +4 -0
  73. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classExtractFieldDescriptor.js +5 -0
  74. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classNameTDZError.js +4 -0
  75. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldDestructureSet.js +7 -0
  76. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldGet.js +7 -0
  77. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldGet2.js +5 -0
  78. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldInitSpec.js +5 -0
  79. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldLooseBase.js +5 -0
  80. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldLooseKey.js +5 -0
  81. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldSet.js +7 -0
  82. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateFieldSet2.js +5 -0
  83. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateGetter.js +5 -0
  84. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateMethodGet.js +5 -0
  85. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateMethodInitSpec.js +5 -0
  86. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateMethodSet.js +4 -0
  87. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classPrivateSetter.js +5 -0
  88. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classStaticPrivateFieldDestructureSet.js +7 -0
  89. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classStaticPrivateFieldSpecGet.js +7 -0
  90. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classStaticPrivateFieldSpecSet.js +7 -0
  91. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classStaticPrivateMethodGet.js +5 -0
  92. package/runtime/node_modules/@oxc-project/runtime/src/helpers/classStaticPrivateMethodSet.js +4 -0
  93. package/runtime/node_modules/@oxc-project/runtime/src/helpers/construct.js +10 -0
  94. package/runtime/node_modules/@oxc-project/runtime/src/helpers/createClass.js +13 -0
  95. package/runtime/node_modules/@oxc-project/runtime/src/helpers/createForOfIteratorHelper.js +50 -0
  96. package/runtime/node_modules/@oxc-project/runtime/src/helpers/createForOfIteratorHelperLoose.js +19 -0
  97. package/runtime/node_modules/@oxc-project/runtime/src/helpers/createSuper.js +16 -0
  98. package/runtime/node_modules/@oxc-project/runtime/src/helpers/decorate.js +23 -0
  99. package/runtime/node_modules/@oxc-project/runtime/src/helpers/decorateMetadata.js +9 -0
  100. package/runtime/node_modules/@oxc-project/runtime/src/helpers/decorateParam.js +11 -0
  101. package/runtime/node_modules/@oxc-project/runtime/src/helpers/defaults.js +9 -0
  102. package/runtime/node_modules/@oxc-project/runtime/src/helpers/defineAccessor.js +8 -0
  103. package/runtime/node_modules/@oxc-project/runtime/src/helpers/defineEnumerableProperties.js +12 -0
  104. package/runtime/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js +10 -0
  105. package/runtime/node_modules/@oxc-project/runtime/src/helpers/dispose.js +28 -0
  106. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/AwaitValue.js +4 -0
  107. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/OverloadYield.js +4 -0
  108. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecoratedDescriptor.js +9 -0
  109. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs.js +236 -0
  110. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs2203.js +184 -0
  111. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs2203R.js +191 -0
  112. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs2301.js +222 -0
  113. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs2305.js +133 -0
  114. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/applyDecs2311.js +124 -0
  115. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/arrayLikeToArray.js +6 -0
  116. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/arrayWithHoles.js +4 -0
  117. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/arrayWithoutHoles.js +5 -0
  118. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/assertClassBrand.js +5 -0
  119. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/assertThisInitialized.js +5 -0
  120. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/asyncGeneratorDelegate.js +24 -0
  121. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/asyncIterator.js +45 -0
  122. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/asyncToGenerator.js +26 -0
  123. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/awaitAsyncGenerator.js +5 -0
  124. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/callSuper.js +7 -0
  125. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/checkInRHS.js +6 -0
  126. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/checkPrivateRedeclaration.js +4 -0
  127. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classApplyDescriptorDestructureSet.js +10 -0
  128. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classApplyDescriptorGet.js +4 -0
  129. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classApplyDescriptorSet.js +7 -0
  130. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classCallCheck.js +4 -0
  131. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classCheckPrivateStaticAccess.js +5 -0
  132. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classCheckPrivateStaticFieldDescriptor.js +4 -0
  133. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classExtractFieldDescriptor.js +5 -0
  134. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classNameTDZError.js +4 -0
  135. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldDestructureSet.js +7 -0
  136. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldGet.js +7 -0
  137. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldGet2.js +5 -0
  138. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldInitSpec.js +5 -0
  139. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldLooseBase.js +5 -0
  140. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldLooseKey.js +5 -0
  141. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldSet.js +7 -0
  142. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateFieldSet2.js +5 -0
  143. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateGetter.js +5 -0
  144. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateMethodGet.js +5 -0
  145. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateMethodInitSpec.js +5 -0
  146. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateMethodSet.js +4 -0
  147. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classPrivateSetter.js +5 -0
  148. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classStaticPrivateFieldDestructureSet.js +7 -0
  149. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classStaticPrivateFieldSpecGet.js +7 -0
  150. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classStaticPrivateFieldSpecSet.js +7 -0
  151. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classStaticPrivateMethodGet.js +5 -0
  152. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/classStaticPrivateMethodSet.js +4 -0
  153. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/construct.js +10 -0
  154. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/createClass.js +13 -0
  155. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/createForOfIteratorHelper.js +50 -0
  156. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/createForOfIteratorHelperLoose.js +19 -0
  157. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/createSuper.js +16 -0
  158. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/decorate.js +21 -0
  159. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/decorateMetadata.js +7 -0
  160. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/decorateParam.js +9 -0
  161. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/defaults.js +9 -0
  162. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/defineAccessor.js +8 -0
  163. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/defineEnumerableProperties.js +12 -0
  164. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/defineProperty.js +10 -0
  165. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/dispose.js +28 -0
  166. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/extends.js +10 -0
  167. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/get.js +11 -0
  168. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/getPrototypeOf.js +6 -0
  169. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/identity.js +4 -0
  170. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/importDeferProxy.js +27 -0
  171. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/inherits.js +14 -0
  172. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/inheritsLoose.js +5 -0
  173. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/initializerDefineProperty.js +9 -0
  174. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/initializerWarningHelper.js +4 -0
  175. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/instanceof.js +4 -0
  176. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/interopRequireDefault.js +6 -0
  177. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/interopRequireWildcard.js +27 -0
  178. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/isNativeFunction.js +8 -0
  179. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/isNativeReflectConstruct.js +9 -0
  180. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/iterableToArray.js +4 -0
  181. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/iterableToArrayLimit.js +28 -0
  182. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/jsx.js +22 -0
  183. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/maybeArrayLike.js +9 -0
  184. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/newArrowCheck.js +4 -0
  185. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/nonIterableRest.js +4 -0
  186. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/nonIterableSpread.js +4 -0
  187. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/nullishReceiverError.js +4 -0
  188. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/objectDestructuringEmpty.js +4 -0
  189. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/objectSpread.js +14 -0
  190. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/objectSpread2.js +23 -0
  191. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/objectWithoutProperties.js +13 -0
  192. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/objectWithoutPropertiesLoose.js +10 -0
  193. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/package.json +3 -0
  194. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/possibleConstructorReturn.js +8 -0
  195. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/readOnlyError.js +4 -0
  196. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/regeneratorRuntime.js +304 -0
  197. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/set.js +22 -0
  198. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/setFunctionName.js +12 -0
  199. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/setPrototypeOf.js +6 -0
  200. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/skipFirstGeneratorNext.js +7 -0
  201. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/slicedToArray.js +8 -0
  202. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/superPropBase.js +6 -0
  203. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/superPropGet.js +9 -0
  204. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/superPropSet.js +6 -0
  205. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/taggedTemplateLiteral.js +8 -0
  206. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/taggedTemplateLiteralLoose.js +4 -0
  207. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/tdz.js +4 -0
  208. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/temporalRef.js +6 -0
  209. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/temporalUndefined.js +2 -0
  210. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/toArray.js +8 -0
  211. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/toConsumableArray.js +8 -0
  212. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/toPrimitive.js +12 -0
  213. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/toPropertyKey.js +7 -0
  214. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/toSetter.js +10 -0
  215. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/typeof.js +10 -0
  216. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/unsupportedIterableToArray.js +9 -0
  217. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/using.js +12 -0
  218. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/usingCtx.js +59 -0
  219. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/wrapAsyncGenerator.js +69 -0
  220. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/wrapNativeSuper.js +27 -0
  221. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/wrapRegExp.js +51 -0
  222. package/runtime/node_modules/@oxc-project/runtime/src/helpers/esm/writeOnlyError.js +4 -0
  223. package/runtime/node_modules/@oxc-project/runtime/src/helpers/extends.js +10 -0
  224. package/runtime/node_modules/@oxc-project/runtime/src/helpers/get.js +11 -0
  225. package/runtime/node_modules/@oxc-project/runtime/src/helpers/getPrototypeOf.js +6 -0
  226. package/runtime/node_modules/@oxc-project/runtime/src/helpers/identity.js +4 -0
  227. package/runtime/node_modules/@oxc-project/runtime/src/helpers/importDeferProxy.js +27 -0
  228. package/runtime/node_modules/@oxc-project/runtime/src/helpers/inherits.js +14 -0
  229. package/runtime/node_modules/@oxc-project/runtime/src/helpers/inheritsLoose.js +5 -0
  230. package/runtime/node_modules/@oxc-project/runtime/src/helpers/initializerDefineProperty.js +9 -0
  231. package/runtime/node_modules/@oxc-project/runtime/src/helpers/initializerWarningHelper.js +4 -0
  232. package/runtime/node_modules/@oxc-project/runtime/src/helpers/instanceof.js +4 -0
  233. package/runtime/node_modules/@oxc-project/runtime/src/helpers/interopRequireDefault.js +6 -0
  234. package/runtime/node_modules/@oxc-project/runtime/src/helpers/interopRequireWildcard.js +27 -0
  235. package/runtime/node_modules/@oxc-project/runtime/src/helpers/isNativeFunction.js +8 -0
  236. package/runtime/node_modules/@oxc-project/runtime/src/helpers/isNativeReflectConstruct.js +9 -0
  237. package/runtime/node_modules/@oxc-project/runtime/src/helpers/iterableToArray.js +4 -0
  238. package/runtime/node_modules/@oxc-project/runtime/src/helpers/iterableToArrayLimit.js +28 -0
  239. package/runtime/node_modules/@oxc-project/runtime/src/helpers/jsx.js +22 -0
  240. package/runtime/node_modules/@oxc-project/runtime/src/helpers/maybeArrayLike.js +9 -0
  241. package/runtime/node_modules/@oxc-project/runtime/src/helpers/newArrowCheck.js +4 -0
  242. package/runtime/node_modules/@oxc-project/runtime/src/helpers/nonIterableRest.js +4 -0
  243. package/runtime/node_modules/@oxc-project/runtime/src/helpers/nonIterableSpread.js +4 -0
  244. package/runtime/node_modules/@oxc-project/runtime/src/helpers/nullishReceiverError.js +4 -0
  245. package/runtime/node_modules/@oxc-project/runtime/src/helpers/objectDestructuringEmpty.js +4 -0
  246. package/runtime/node_modules/@oxc-project/runtime/src/helpers/objectSpread.js +14 -0
  247. package/runtime/node_modules/@oxc-project/runtime/src/helpers/objectSpread2.js +23 -0
  248. package/runtime/node_modules/@oxc-project/runtime/src/helpers/objectWithoutProperties.js +13 -0
  249. package/runtime/node_modules/@oxc-project/runtime/src/helpers/objectWithoutPropertiesLoose.js +10 -0
  250. package/runtime/node_modules/@oxc-project/runtime/src/helpers/possibleConstructorReturn.js +8 -0
  251. package/runtime/node_modules/@oxc-project/runtime/src/helpers/readOnlyError.js +4 -0
  252. package/runtime/node_modules/@oxc-project/runtime/src/helpers/regeneratorRuntime.js +304 -0
  253. package/runtime/node_modules/@oxc-project/runtime/src/helpers/set.js +22 -0
  254. package/runtime/node_modules/@oxc-project/runtime/src/helpers/setFunctionName.js +12 -0
  255. package/runtime/node_modules/@oxc-project/runtime/src/helpers/setPrototypeOf.js +6 -0
  256. package/runtime/node_modules/@oxc-project/runtime/src/helpers/skipFirstGeneratorNext.js +7 -0
  257. package/runtime/node_modules/@oxc-project/runtime/src/helpers/slicedToArray.js +8 -0
  258. package/runtime/node_modules/@oxc-project/runtime/src/helpers/superPropBase.js +6 -0
  259. package/runtime/node_modules/@oxc-project/runtime/src/helpers/superPropGet.js +9 -0
  260. package/runtime/node_modules/@oxc-project/runtime/src/helpers/superPropSet.js +6 -0
  261. package/runtime/node_modules/@oxc-project/runtime/src/helpers/taggedTemplateLiteral.js +8 -0
  262. package/runtime/node_modules/@oxc-project/runtime/src/helpers/taggedTemplateLiteralLoose.js +4 -0
  263. package/runtime/node_modules/@oxc-project/runtime/src/helpers/tdz.js +4 -0
  264. package/runtime/node_modules/@oxc-project/runtime/src/helpers/temporalRef.js +6 -0
  265. package/runtime/node_modules/@oxc-project/runtime/src/helpers/temporalUndefined.js +2 -0
  266. package/runtime/node_modules/@oxc-project/runtime/src/helpers/toArray.js +8 -0
  267. package/runtime/node_modules/@oxc-project/runtime/src/helpers/toConsumableArray.js +8 -0
  268. package/runtime/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js +12 -0
  269. package/runtime/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js +7 -0
  270. package/runtime/node_modules/@oxc-project/runtime/src/helpers/toSetter.js +10 -0
  271. package/runtime/node_modules/@oxc-project/runtime/src/helpers/typeof.js +10 -0
  272. package/runtime/node_modules/@oxc-project/runtime/src/helpers/unsupportedIterableToArray.js +9 -0
  273. package/runtime/node_modules/@oxc-project/runtime/src/helpers/using.js +12 -0
  274. package/runtime/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js +59 -0
  275. package/runtime/node_modules/@oxc-project/runtime/src/helpers/wrapAsyncGenerator.js +69 -0
  276. package/runtime/node_modules/@oxc-project/runtime/src/helpers/wrapNativeSuper.js +27 -0
  277. package/runtime/node_modules/@oxc-project/runtime/src/helpers/wrapRegExp.js +51 -0
  278. package/runtime/node_modules/@oxc-project/runtime/src/helpers/writeOnlyError.js +4 -0
  279. package/runtime/node_modules/@oxc-project/types/LICENSE +22 -0
  280. package/runtime/node_modules/@oxc-project/types/README.md +3 -0
  281. package/runtime/node_modules/@oxc-project/types/package.json +26 -0
  282. package/runtime/node_modules/@oxc-project/types/types.d.ts +1912 -0
  283. package/runtime/node_modules/@petamoriken/float16/LICENSE +21 -0
  284. package/runtime/node_modules/@petamoriken/float16/README.md +413 -0
  285. package/runtime/node_modules/@petamoriken/float16/browser/float16.js +1340 -0
  286. package/runtime/node_modules/@petamoriken/float16/browser/float16.mjs +1325 -0
  287. package/runtime/node_modules/@petamoriken/float16/index.d.ts +546 -0
  288. package/runtime/node_modules/@petamoriken/float16/index.v5.6.d.ts +546 -0
  289. package/runtime/node_modules/@petamoriken/float16/index.v5.7.d.ts +555 -0
  290. package/runtime/node_modules/@petamoriken/float16/inspect/node.cjs +25 -0
  291. package/runtime/node_modules/@petamoriken/float16/inspect/node.mjs +23 -0
  292. package/runtime/node_modules/@petamoriken/float16/lib/DataView.cjs +16 -0
  293. package/runtime/node_modules/@petamoriken/float16/lib/Float16Array.cjs +690 -0
  294. package/runtime/node_modules/@petamoriken/float16/lib/_util/arrayIterator.cjs +52 -0
  295. package/runtime/node_modules/@petamoriken/float16/lib/_util/brand.cjs +27 -0
  296. package/runtime/node_modules/@petamoriken/float16/lib/_util/converter.cjs +111 -0
  297. package/runtime/node_modules/@petamoriken/float16/lib/_util/is.cjs +78 -0
  298. package/runtime/node_modules/@petamoriken/float16/lib/_util/messages.cjs +18 -0
  299. package/runtime/node_modules/@petamoriken/float16/lib/_util/primordials.cjs +150 -0
  300. package/runtime/node_modules/@petamoriken/float16/lib/_util/spec.cjs +84 -0
  301. package/runtime/node_modules/@petamoriken/float16/lib/f16round.cjs +10 -0
  302. package/runtime/node_modules/@petamoriken/float16/lib/index.cjs +16 -0
  303. package/runtime/node_modules/@petamoriken/float16/lib/isTypedArray.cjs +11 -0
  304. package/runtime/node_modules/@petamoriken/float16/package.json +115 -0
  305. package/runtime/node_modules/@petamoriken/float16/src/DataView.mjs +35 -0
  306. package/runtime/node_modules/@petamoriken/float16/src/Float16Array.mjs +1194 -0
  307. package/runtime/node_modules/@petamoriken/float16/src/_util/arrayIterator.mjs +86 -0
  308. package/runtime/node_modules/@petamoriken/float16/src/_util/brand.mjs +31 -0
  309. package/runtime/node_modules/@petamoriken/float16/src/_util/converter.mjs +173 -0
  310. package/runtime/node_modules/@petamoriken/float16/src/_util/is.mjs +151 -0
  311. package/runtime/node_modules/@petamoriken/float16/src/_util/messages.mjs +22 -0
  312. package/runtime/node_modules/@petamoriken/float16/src/_util/primordials.mjs +254 -0
  313. package/runtime/node_modules/@petamoriken/float16/src/_util/spec.mjs +137 -0
  314. package/runtime/node_modules/@petamoriken/float16/src/f16round.mjs +10 -0
  315. package/runtime/node_modules/@petamoriken/float16/src/index.mjs +7 -0
  316. package/runtime/node_modules/@petamoriken/float16/src/isTypedArray.mjs +10 -0
  317. package/runtime/node_modules/get-tsconfig/LICENSE +21 -0
  318. package/runtime/node_modules/get-tsconfig/README.md +268 -0
  319. package/runtime/node_modules/get-tsconfig/dist/index.cjs +7 -0
  320. package/runtime/node_modules/get-tsconfig/dist/index.d.cts +2116 -0
  321. package/runtime/node_modules/get-tsconfig/dist/index.d.mts +2116 -0
  322. package/runtime/node_modules/get-tsconfig/dist/index.mjs +7 -0
  323. package/runtime/node_modules/get-tsconfig/package.json +46 -0
  324. package/runtime/node_modules/jsbi/LICENSE +176 -0
  325. package/runtime/node_modules/jsbi/README.md +173 -0
  326. package/runtime/node_modules/jsbi/dist/jsbi-cjs.js +2 -0
  327. package/runtime/node_modules/jsbi/dist/jsbi-cjs.js.map +1 -0
  328. package/runtime/node_modules/jsbi/dist/jsbi-umd.js +2 -0
  329. package/runtime/node_modules/jsbi/dist/jsbi-umd.js.map +1 -0
  330. package/runtime/node_modules/jsbi/dist/jsbi.mjs +2 -0
  331. package/runtime/node_modules/jsbi/dist/jsbi.mjs.map +1 -0
  332. package/runtime/node_modules/jsbi/jsbi.d.ts +43 -0
  333. package/runtime/node_modules/jsbi/package.json +35 -0
  334. package/runtime/node_modules/oxc-parser/LICENSE +22 -0
  335. package/runtime/node_modules/oxc-parser/README.md +167 -0
  336. package/runtime/node_modules/oxc-parser/package.json +153 -0
  337. package/runtime/node_modules/oxc-parser/src-js/bindings.js +601 -0
  338. package/runtime/node_modules/oxc-parser/src-js/generated/constants.js +105 -0
  339. package/runtime/node_modules/oxc-parser/src-js/generated/deserialize/js.js +5862 -0
  340. package/runtime/node_modules/oxc-parser/src-js/generated/deserialize/js_range.js +6403 -0
  341. package/runtime/node_modules/oxc-parser/src-js/generated/deserialize/ts.js +6154 -0
  342. package/runtime/node_modules/oxc-parser/src-js/generated/deserialize/ts_range.js +6723 -0
  343. package/runtime/node_modules/oxc-parser/src-js/generated/lazy/constructors.js +13875 -0
  344. package/runtime/node_modules/oxc-parser/src-js/generated/lazy/type_ids.js +191 -0
  345. package/runtime/node_modules/oxc-parser/src-js/generated/lazy/walk.js +5810 -0
  346. package/runtime/node_modules/oxc-parser/src-js/generated/visit/keys.js +220 -0
  347. package/runtime/node_modules/oxc-parser/src-js/generated/visit/type_ids.js +177 -0
  348. package/runtime/node_modules/oxc-parser/src-js/generated/visit/visitor.d.ts +387 -0
  349. package/runtime/node_modules/oxc-parser/src-js/generated/visit/walk.js +2455 -0
  350. package/runtime/node_modules/oxc-parser/src-js/index.d.ts +312 -0
  351. package/runtime/node_modules/oxc-parser/src-js/index.js +108 -0
  352. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/common.js +301 -0
  353. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/eager.js +255 -0
  354. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/lazy-common.js +11 -0
  355. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/lazy.js +162 -0
  356. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/node-array.js +365 -0
  357. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/supported.js +52 -0
  358. package/runtime/node_modules/oxc-parser/src-js/raw-transfer/visitor.js +127 -0
  359. package/runtime/node_modules/oxc-parser/src-js/visit/index.js +41 -0
  360. package/runtime/node_modules/oxc-parser/src-js/visit/visitor.js +405 -0
  361. package/runtime/node_modules/oxc-parser/src-js/wasm.js +13 -0
  362. package/runtime/node_modules/oxc-parser/src-js/webcontainer-fallback.cjs +21 -0
  363. package/runtime/node_modules/oxc-parser/src-js/wrap.js +57 -0
  364. package/runtime/node_modules/oxc-transform/LICENSE +22 -0
  365. package/runtime/node_modules/oxc-transform/README.md +84 -0
  366. package/runtime/node_modules/oxc-transform/browser.js +1 -0
  367. package/runtime/node_modules/oxc-transform/index.d.ts +658 -0
  368. package/runtime/node_modules/oxc-transform/index.js +598 -0
  369. package/runtime/node_modules/oxc-transform/package.json +114 -0
  370. package/runtime/node_modules/oxc-transform/webcontainer-fallback.cjs +21 -0
  371. package/runtime/node_modules/resolve-pkg-maps/LICENSE +21 -0
  372. package/runtime/node_modules/resolve-pkg-maps/README.md +216 -0
  373. package/runtime/node_modules/resolve-pkg-maps/dist/index.cjs +1 -0
  374. package/runtime/node_modules/resolve-pkg-maps/dist/index.d.cts +11 -0
  375. package/runtime/node_modules/resolve-pkg-maps/dist/index.d.mts +11 -0
  376. package/runtime/node_modules/resolve-pkg-maps/dist/index.mjs +1 -0
  377. package/runtime/node_modules/resolve-pkg-maps/package.json +42 -0
  378. package/runtime/node_modules/urlpattern-polyfill/LICENSE +19 -0
  379. package/runtime/node_modules/urlpattern-polyfill/README.md +242 -0
  380. package/runtime/node_modules/urlpattern-polyfill/dist/index.d.ts +9 -0
  381. package/runtime/node_modules/urlpattern-polyfill/dist/types.d.ts +49 -0
  382. package/runtime/node_modules/urlpattern-polyfill/dist/urlpattern.cjs +1 -0
  383. package/runtime/node_modules/urlpattern-polyfill/dist/urlpattern.js +1 -0
  384. package/runtime/node_modules/urlpattern-polyfill/index.cjs +7 -0
  385. package/runtime/node_modules/urlpattern-polyfill/index.js +7 -0
  386. package/runtime/node_modules/urlpattern-polyfill/package.json +149 -0
  387. package/runtime/version.mjs +1 -1
@@ -0,0 +1,2424 @@
1
+ import * as ES from './ecmascript';
2
+ import { DefineIntrinsic } from './intrinsicclass';
3
+ import type { Temporal } from '..';
4
+ import type {
5
+ BuiltinCalendarId,
6
+ CalendarDateRecord,
7
+ CalendarFieldsRecord,
8
+ CalendarYMD,
9
+ DateDuration,
10
+ FieldKey,
11
+ ISODate,
12
+ ISODateToFieldsType,
13
+ MonthDayFromFieldsObject,
14
+ Overflow,
15
+ Resolve
16
+ } from './internaltypes';
17
+
18
+ function arrayFromSet<T>(src: Set<T>): T[] {
19
+ return [...src];
20
+ }
21
+
22
+ function calendarDateWeekOfYear(id: BuiltinCalendarId, isoDate: ISODate): { week: number; year: number } | undefined {
23
+ // Supports only Gregorian and ISO8601 calendar; can be updated to add support for other calendars.
24
+ // Returns undefined for calendars without a well-defined week calendar system.
25
+ // eslint-disable-next-line max-len
26
+ // Also see: https://github.com/unicode-org/icu/blob/ab72ab1d4a3c3f9beeb7d92b0c7817ca93dfdb04/icu4c/source/i18n/calendar.cpp#L1606
27
+ if (id !== 'gregory' && id !== 'iso8601') return undefined;
28
+ const calendar = impl[id];
29
+ let yow = isoDate.year;
30
+ const { dayOfWeek, dayOfYear, daysInYear } = calendar.isoToDate(isoDate, {
31
+ dayOfWeek: true,
32
+ dayOfYear: true,
33
+ daysInYear: true
34
+ });
35
+ const fdow = calendar.getFirstDayOfWeek();
36
+ const mdow = calendar.getMinimalDaysInFirstWeek();
37
+ ES.uncheckedAssertNarrowedType<number>(fdow, 'guaranteed to exist for iso8601/gregory');
38
+ ES.uncheckedAssertNarrowedType<number>(mdow, 'guaranteed to exist for iso8601/gregory');
39
+
40
+ // For both the input date and the first day of its calendar year, calculate the day of week
41
+ // relative to first day of week in the relevant calendar (e.g., in iso8601, relative to Monday).
42
+ let relDow = (dayOfWeek + 7 - fdow) % 7;
43
+ // Assuming the year length is less than 7000 days.
44
+ let relDowJan1 = (dayOfWeek - dayOfYear + 7001 - fdow) % 7;
45
+
46
+ let woy = Math.floor((dayOfYear - 1 + relDowJan1) / 7);
47
+ if (7 - relDowJan1 >= mdow) {
48
+ ++woy;
49
+ }
50
+
51
+ // Adjust for weeks at the year end that overlap into the previous or next calendar year.
52
+ if (woy == 0) {
53
+ // Check for last week of previous year; if true, handle the case for
54
+ // first week of next year
55
+ const prevYearCalendar = calendar.isoToDate(calendar.dateAdd(isoDate, { years: -1 }, 'constrain'), {
56
+ daysInYear: true
57
+ });
58
+ let prevDoy = dayOfYear + prevYearCalendar.daysInYear;
59
+ woy = weekNumber(fdow, mdow, prevDoy, dayOfWeek);
60
+ yow--;
61
+ } else {
62
+ // For it to be week 1 of the next year, dayOfYear must be >= lastDoy - 5
63
+ // L-5 L
64
+ // doy: 359 360 361 362 363 364 365 001
65
+ // dow: 1 2 3 4 5 6 7
66
+ let lastDoy = daysInYear;
67
+ if (dayOfYear >= lastDoy - 5) {
68
+ let lastRelDow = (relDow + lastDoy - dayOfYear) % 7;
69
+ if (lastRelDow < 0) {
70
+ lastRelDow += 7;
71
+ }
72
+ if (6 - lastRelDow >= mdow && dayOfYear + 7 - relDow > lastDoy) {
73
+ woy = 1;
74
+ yow++;
75
+ }
76
+ }
77
+ }
78
+ return { week: woy, year: yow };
79
+ }
80
+
81
+ function ISODateSurpasses(sign: -1 | 0 | 1, y1: number, m1: number, d1: number, isoDate2: ISODate) {
82
+ if (y1 !== isoDate2.year) {
83
+ if (sign * (y1 - isoDate2.year) > 0) return true;
84
+ } else if (m1 !== isoDate2.month) {
85
+ if (sign * (m1 - isoDate2.month) > 0) return true;
86
+ } else if (d1 !== isoDate2.day) {
87
+ if (sign * (d1 - isoDate2.day) > 0) return true;
88
+ }
89
+ return false;
90
+ }
91
+
92
+ type ResolveFieldsReturn<Type extends ISODateToFieldsType> = Resolve<
93
+ CalendarFieldsRecord & {
94
+ year: Type extends 'date' ? number : never;
95
+ month: number;
96
+ monthCode: string;
97
+ day: number;
98
+ }
99
+ >;
100
+
101
+ /**
102
+ * Shape of internal implementation of each built-in calendar. Note that
103
+ * parameter types are simpler than CalendarProtocol because the `Calendar`
104
+ * class performs validation and parameter normalization before handing control
105
+ * over to CalendarImpl.
106
+ *
107
+ * There are two instances of this interface: one for the ISO calendar and
108
+ * another that handles logic that's the same across all non-ISO calendars. The
109
+ * latter is cloned for each non-ISO calendar at the end of this file.
110
+ */
111
+ export interface CalendarImpl {
112
+ isoToDate<
113
+ Request extends Partial<Record<keyof CalendarDateRecord, true>>,
114
+ T extends {
115
+ [Field in keyof CalendarDateRecord]: Request extends { [K in Field]: true } ? CalendarDateRecord[Field] : never;
116
+ }
117
+ >(
118
+ isoDate: ISODate,
119
+ requestedFields: Request
120
+ ): T;
121
+ getFirstDayOfWeek(): number | undefined;
122
+ getMinimalDaysInFirstWeek(): number | undefined;
123
+ resolveFields<Type extends ISODateToFieldsType>(
124
+ fields: CalendarFieldsRecord,
125
+ type: Type
126
+ ): asserts fields is ResolveFieldsReturn<Type>;
127
+ dateToISO(fields: ResolveFieldsReturn<'date'>, overflow: Overflow): ISODate;
128
+ monthDayToISOReferenceDate(fields: ResolveFieldsReturn<'month-day'>, overflow: Overflow): ISODate;
129
+ dateAdd(date: ISODate, duration: Partial<DateDuration>, overflow: Overflow): ISODate;
130
+ dateUntil(one: ISODate, two: ISODate, largestUnit: 'year' | 'month' | 'week' | 'day'): DateDuration;
131
+ extraFields(fields: FieldKey[]): FieldKey[];
132
+ fieldKeysToIgnore(keys: FieldKey[]): FieldKey[];
133
+ }
134
+
135
+ type CalendarImplementations = {
136
+ [k in BuiltinCalendarId]: CalendarImpl;
137
+ };
138
+
139
+ /**
140
+ * Implementations for each calendar.
141
+ * Registration for each of these calendars happens throughout this file. The ISO and non-ISO calendars are registered
142
+ * separately - look for 'iso8601' for the ISO calendar registration, and all non-ISO calendar registrations happens
143
+ * at the bottom of the file.
144
+ */
145
+ const impl: CalendarImplementations = {} as unknown as CalendarImplementations;
146
+
147
+ /**
148
+ * Implementation for the ISO 8601 calendar. This is the only calendar that's
149
+ * guaranteed to be supported by all ECMAScript implementations, including those
150
+ * without Intl (ECMA-402) support.
151
+ */
152
+ impl['iso8601'] = {
153
+ resolveFields(fields, type) {
154
+ if ((type === 'date' || type === 'year-month') && fields.year === undefined) {
155
+ throw new TypeError('year is required');
156
+ }
157
+ if ((type === 'date' || type === 'month-day') && fields.day === undefined) {
158
+ throw new TypeError('day is required');
159
+ }
160
+ Object.assign(fields, resolveNonLunisolarMonth(fields));
161
+ },
162
+ dateToISO(fields, overflow) {
163
+ return ES.RegulateISODate(fields.year, fields.month, fields.day, overflow);
164
+ },
165
+ monthDayToISOReferenceDate(fields, overflow) {
166
+ const referenceISOYear = 1972;
167
+ const { month, day } = ES.RegulateISODate(fields.year ?? referenceISOYear, fields.month, fields.day, overflow);
168
+ return { month, day, year: referenceISOYear };
169
+ },
170
+ extraFields() {
171
+ return [];
172
+ },
173
+ fieldKeysToIgnore(keys) {
174
+ const result = new Set<FieldKey>();
175
+ for (let ix = 0; ix < keys.length; ix++) {
176
+ const key = keys[ix];
177
+ result.add(key);
178
+ if (key === 'month') {
179
+ result.add('monthCode');
180
+ } else if (key === 'monthCode') {
181
+ result.add('month');
182
+ }
183
+ }
184
+ return arrayFromSet(result);
185
+ },
186
+ dateAdd(isoDate, { years = 0, months = 0, weeks = 0, days = 0 }, overflow) {
187
+ let { year, month, day } = isoDate;
188
+ year += years;
189
+ month += months;
190
+ ({ year, month } = ES.BalanceISOYearMonth(year, month));
191
+ ({ year, month, day } = ES.RegulateISODate(year, month, day, overflow));
192
+ day += days + 7 * weeks;
193
+ return ES.BalanceISODate(year, month, day);
194
+ },
195
+ dateUntil(one, two, largestUnit) {
196
+ const sign = -ES.CompareISODate(one, two);
197
+ if (sign === 0) return { years: 0, months: 0, weeks: 0, days: 0 };
198
+ ES.uncheckedAssertNarrowedType<-1 | 1>(sign, "the - operator's return type is number");
199
+
200
+ let years = 0;
201
+ let months = 0;
202
+ let intermediate;
203
+ if (largestUnit === 'year' || largestUnit === 'month') {
204
+ // We can skip right to the neighbourhood of the correct number of years,
205
+ // it'll be at least one less than two.year - one.year (unless it's zero)
206
+ let candidateYears = two.year - one.year;
207
+ if (candidateYears !== 0) candidateYears -= sign;
208
+ // loops at most twice
209
+ while (!ISODateSurpasses(sign, one.year + candidateYears, one.month, one.day, two)) {
210
+ years = candidateYears;
211
+ candidateYears += sign;
212
+ }
213
+
214
+ let candidateMonths = sign;
215
+ intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + candidateMonths);
216
+ // loops at most 12 times
217
+ while (!ISODateSurpasses(sign, intermediate.year, intermediate.month, one.day, two)) {
218
+ months = candidateMonths;
219
+ candidateMonths += sign;
220
+ intermediate = ES.BalanceISOYearMonth(intermediate.year, intermediate.month + sign);
221
+ }
222
+
223
+ if (largestUnit === 'month') {
224
+ months += years * 12;
225
+ years = 0;
226
+ }
227
+ }
228
+
229
+ intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + months);
230
+ const constrained = ES.ConstrainISODate(intermediate.year, intermediate.month, one.day);
231
+
232
+ let weeks = 0;
233
+ let days =
234
+ ES.ISODateToEpochDays(two.year, two.month - 1, two.day) -
235
+ ES.ISODateToEpochDays(constrained.year, constrained.month - 1, constrained.day);
236
+
237
+ if (largestUnit === 'week') {
238
+ weeks = Math.trunc(days / 7);
239
+ days %= 7;
240
+ }
241
+
242
+ return { years, months, weeks, days };
243
+ },
244
+ isoToDate<
245
+ Request extends Partial<Record<keyof CalendarDateRecord, true>>,
246
+ T extends {
247
+ [Field in keyof CalendarDateRecord]: Request extends { [K in Field]: true } ? CalendarDateRecord[Field] : never;
248
+ }
249
+ >({ year, month, day }: ISODate, requestedFields: Request): T {
250
+ // requestedFields parameter is not part of the spec text. It's an
251
+ // illustration of one way implementations may choose to optimize this
252
+ // operation.
253
+ const date: Partial<CalendarDateRecord> = {
254
+ era: undefined,
255
+ eraYear: undefined,
256
+ year,
257
+ month,
258
+ day,
259
+ daysInWeek: 7,
260
+ monthsInYear: 12
261
+ };
262
+ if (requestedFields.monthCode) date.monthCode = buildMonthCode(month);
263
+ if (requestedFields.dayOfWeek) {
264
+ // https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Disparate_variation
265
+ const shiftedMonth = month + (month < 3 ? 10 : -2);
266
+ const shiftedYear = year - (month < 3 ? 1 : 0);
267
+
268
+ const century = Math.floor(shiftedYear / 100);
269
+ const yearInCentury = shiftedYear - century * 100;
270
+
271
+ const monthTerm = Math.floor(2.6 * shiftedMonth - 0.2);
272
+ const yearTerm = yearInCentury + Math.floor(yearInCentury / 4);
273
+ const centuryTerm = Math.floor(century / 4) - 2 * century;
274
+
275
+ const dow = (day + monthTerm + yearTerm + centuryTerm) % 7;
276
+
277
+ date.dayOfWeek = dow + (dow <= 0 ? 7 : 0);
278
+ }
279
+ if (requestedFields.dayOfYear) {
280
+ let days = day;
281
+ for (let m = month - 1; m > 0; m--) {
282
+ days += ES.ISODaysInMonth(year, m);
283
+ }
284
+ date.dayOfYear = days;
285
+ }
286
+ if (requestedFields.weekOfYear) date.weekOfYear = calendarDateWeekOfYear('iso8601', { year, month, day });
287
+ if (requestedFields.daysInMonth) date.daysInMonth = ES.ISODaysInMonth(year, month);
288
+ if (requestedFields.daysInYear || requestedFields.inLeapYear) {
289
+ date.inLeapYear = ES.LeapYear(year);
290
+ date.daysInYear = date.inLeapYear ? 366 : 365;
291
+ }
292
+ return date as T;
293
+ },
294
+ getFirstDayOfWeek() {
295
+ return 1;
296
+ },
297
+ getMinimalDaysInFirstWeek() {
298
+ return 4;
299
+ }
300
+ };
301
+
302
+ // Note: Built-in calendars other than iso8601 are not part of the Temporal
303
+ // proposal for ECMA-262. These calendars will be standardized as part of
304
+ // ECMA-402. Code below here includes an implementation of these calendars to
305
+ // validate the Temporal API and to get feedback. However, native non-ISO
306
+ // calendar behavior is at least somewhat implementation-defined, so may not
307
+ // match this polyfill's output exactly.
308
+ //
309
+ // Some ES implementations don't include ECMA-402. For this reason, it's helpful
310
+ // to ensure a clean separation between the ISO calendar implementation which is
311
+ // a part of ECMA-262 and the non-ISO calendar implementation which requires
312
+ // ECMA-402.
313
+ //
314
+ // To ensure this separation, the implementation is split. A `CalendarImpl`
315
+ // interface defines the common operations between both ISO and non-ISO
316
+ // calendars.
317
+
318
+ /**
319
+ * This type is passed through from CalendarImpl#dateFromFields().
320
+ * `monthExtra` is additional information used internally to identify lunisolar leap months.
321
+ */
322
+ type CalendarDateFields = CalendarFieldsRecord & { monthExtra?: string };
323
+
324
+ /**
325
+ * This is a "fully populated" calendar date record. It's only lacking
326
+ * `era`/`eraYear` (which may not be present in all calendars) and `monthExtra`
327
+ * which is only used in some cases.
328
+ */
329
+ type FullCalendarDate = {
330
+ era?: string;
331
+ eraYear?: number;
332
+ year: number;
333
+ month: number;
334
+ monthCode: string;
335
+ day: number;
336
+ monthExtra?: string;
337
+ };
338
+
339
+ // The types below are various subsets of calendar dates
340
+ type CalendarYM = { year: number; month: number };
341
+ type CalendarYearOnly = { year: number };
342
+ type EraAndEraYear = { era: string; eraYear: number };
343
+
344
+ function nonLeapMonthCodeNumberPart(monthCode: string) {
345
+ if (!monthCode.startsWith('M')) {
346
+ throw new RangeError(`Invalid month code: ${monthCode}. Month codes must start with M.`);
347
+ }
348
+ const month = +monthCode.slice(1);
349
+ if (Number.isNaN(month)) throw new RangeError(`Invalid month code: ${monthCode}`);
350
+ return month;
351
+ }
352
+
353
+ function buildMonthCode(month: number, leap = false) {
354
+ const digitPart = `${month}`.padStart(2, '0');
355
+ const leapMarker = leap ? 'L' : '';
356
+ return `M${digitPart}${leapMarker}`;
357
+ }
358
+
359
+ /**
360
+ * Safely merge a month, monthCode pair into an integer month.
361
+ * If both are present, make sure they match.
362
+ * This logic doesn't work for lunisolar calendars!
363
+ * */
364
+ function resolveNonLunisolarMonth<T extends { monthCode?: string; month?: number }>(
365
+ calendarDate: T,
366
+ overflow: Overflow | undefined = undefined,
367
+ monthsPerYear = 12
368
+ ) {
369
+ let { month, monthCode } = calendarDate;
370
+ if (monthCode === undefined) {
371
+ if (month === undefined) throw new TypeError('Either month or monthCode are required');
372
+ // The ISO calendar uses the default (undefined) value because it does
373
+ // constrain/reject after this method returns. Non-ISO calendars, however,
374
+ // rely on this function to constrain/reject out-of-range `month` values.
375
+ if (overflow === 'reject') ES.RejectToRange(month, 1, monthsPerYear);
376
+ if (overflow === 'constrain') month = ES.ConstrainToRange(month, 1, monthsPerYear);
377
+ monthCode = buildMonthCode(month);
378
+ } else {
379
+ const numberPart = nonLeapMonthCodeNumberPart(monthCode);
380
+ if (monthCode !== buildMonthCode(numberPart)) {
381
+ throw new RangeError(`Invalid month code: ${monthCode}`);
382
+ }
383
+ if (month !== undefined && month !== numberPart) {
384
+ throw new RangeError(`monthCode ${monthCode} and month ${month} must match if both are present`);
385
+ }
386
+ month = numberPart;
387
+ if (month < 1 || month > monthsPerYear) throw new RangeError(`Invalid monthCode: ${monthCode}`);
388
+ }
389
+ return { ...calendarDate, month, monthCode };
390
+ }
391
+
392
+ function weekNumber(firstDayOfWeek: number, minimalDaysInFirstWeek: number, desiredDay: number, dayOfWeek: number) {
393
+ let periodStartDayOfWeek = (dayOfWeek - firstDayOfWeek - desiredDay + 1) % 7;
394
+ if (periodStartDayOfWeek < 0) periodStartDayOfWeek += 7;
395
+ let weekNo = Math.floor((desiredDay + periodStartDayOfWeek - 1) / 7);
396
+ if (7 - periodStartDayOfWeek >= minimalDaysInFirstWeek) {
397
+ ++weekNo;
398
+ }
399
+ return weekNo;
400
+ }
401
+
402
+ /**
403
+ * This prototype implementation of non-ISO calendars makes many repeated calls
404
+ * to Intl APIs which may be slow (e.g. >0.2ms). This trivial cache will speed
405
+ * up these repeat accesses. Each cache instance is associated (via a WeakMap)
406
+ * to a specific Temporal object, which speeds up multiple calendar calls on the
407
+ * same Temporal object instance. No invalidation or pruning is necessary
408
+ * because each object's cache is thrown away when the object is GC-ed.
409
+ */
410
+ class OneObjectCache {
411
+ map = new Map();
412
+ calls = 0;
413
+ // now = OneObjectCache.monotonicTimestamp();
414
+ hits = 0;
415
+ misses = 0;
416
+
417
+ // static monotonicTimestamp() {
418
+ // return performance?.now() ?? Date.now();
419
+ // }
420
+
421
+ constructor(cacheToClone?: OneObjectCache) {
422
+ if (cacheToClone !== undefined) {
423
+ let i = 0;
424
+ for (const entry of cacheToClone.map.entries()) {
425
+ if (++i > OneObjectCache.MAX_CACHE_ENTRIES) break;
426
+ this.map.set(...entry);
427
+ }
428
+ }
429
+ }
430
+ get(key: string) {
431
+ const result = this.map.get(key);
432
+ if (result) {
433
+ this.hits++;
434
+ this.report();
435
+ }
436
+ this.calls++;
437
+ return result;
438
+ }
439
+ set(key: string, value: unknown) {
440
+ this.map.set(key, value);
441
+ this.misses++;
442
+ this.report();
443
+ }
444
+ report() {
445
+ // if (this.calls === 0) return;
446
+ // const ms = OneObjectCache.monotonicTimestamp() - this.now;
447
+ // const hitRate = ((100 * this.hits) / this.calls).toFixed(0);
448
+ // const t = `${ms.toFixed(2)}ms`;
449
+ // // eslint-disable-next-line no-console
450
+ // console.log(`${this.calls} calls in ${t}. Hits: ${this.hits} (${hitRate}%). Misses: ${this.misses}.`);
451
+ }
452
+ setObject(obj: ISODate) {
453
+ if (OneObjectCache.objectMap.get(obj)) throw new RangeError('object already cached');
454
+ OneObjectCache.objectMap.set(obj, this);
455
+ this.report();
456
+ }
457
+
458
+ static objectMap = new WeakMap();
459
+ static MAX_CACHE_ENTRIES = 1000;
460
+
461
+ /**
462
+ * Returns a WeakMap-backed cache that's used to store expensive results
463
+ * that are associated with a particular Temporal object instance.
464
+ *
465
+ * @param obj - object to associate with the cache
466
+ */
467
+ static getCacheForObject(obj: ISODate) {
468
+ let cache = OneObjectCache.objectMap.get(obj);
469
+ if (!cache) {
470
+ cache = new OneObjectCache();
471
+ OneObjectCache.objectMap.set(obj, cache);
472
+ }
473
+ return cache;
474
+ }
475
+ }
476
+
477
+ function toUtcIsoDateString({ isoYear, isoMonth, isoDay }: { isoYear: number; isoMonth: number; isoDay: number }) {
478
+ const yearString = ES.ISOYearString(isoYear);
479
+ const monthString = ES.ISODateTimePartString(isoMonth);
480
+ const dayString = ES.ISODateTimePartString(isoDay);
481
+ return `${yearString}-${monthString}-${dayString}T00:00Z`;
482
+ }
483
+
484
+ function simpleDateDiff(one: CalendarYMD, two: CalendarYMD) {
485
+ return {
486
+ years: one.year - two.year,
487
+ months: one.month - two.month,
488
+ days: one.day - two.day
489
+ };
490
+ }
491
+
492
+ /**
493
+ * Implementation helper that's common to all non-ISO calendars
494
+ */
495
+ abstract class HelperBase {
496
+ abstract id: BuiltinCalendarId;
497
+ abstract monthsInYear(calendarDate: CalendarYearOnly, cache?: OneObjectCache): number;
498
+ abstract maximumMonthLength(calendarDate?: CalendarYM): number;
499
+ abstract minimumMonthLength(calendarDate?: CalendarYM): number;
500
+ abstract maxLengthOfMonthCodeInAnyYear(monthCode: string): number;
501
+ abstract estimateIsoDate(calendarDate: CalendarYMD): ISODate;
502
+ abstract inLeapYear(calendarDate: CalendarYearOnly, cache?: OneObjectCache): boolean;
503
+ abstract calendarType: 'solar' | 'lunar' | 'lunisolar';
504
+ reviseIntlEra?<T extends Partial<EraAndEraYear>>(calendarDate: T, isoDate: ISODate): T;
505
+ eras: Era[] = [];
506
+ checkIcuBugs?(isoDate: ISODate): void;
507
+ private formatter?: globalThis.Intl.DateTimeFormat;
508
+ getFormatter() {
509
+ // `new Intl.DateTimeFormat()` is amazingly slow and chews up RAM. Per
510
+ // https://bugs.chromium.org/p/v8/issues/detail?id=6528#c4, we cache one
511
+ // DateTimeFormat instance per calendar. Caching is lazy so we only pay for
512
+ // calendars that are used. Note that the HelperBase class is extended to
513
+ // create each calendar's implementation before any cache is created, so
514
+ // each calendar gets its own separate cached formatter.
515
+ if (typeof this.formatter === 'undefined') {
516
+ this.formatter = new Intl.DateTimeFormat(`en-US-u-ca-${this.id}`, {
517
+ day: 'numeric',
518
+ month: 'numeric',
519
+ year: 'numeric',
520
+ era: 'short',
521
+ timeZone: 'UTC'
522
+ });
523
+ }
524
+ return this.formatter;
525
+ }
526
+ getCalendarParts(isoString: string) {
527
+ let dateTimeFormat = this.getFormatter();
528
+ let legacyDate = new Date(isoString);
529
+
530
+ // PlainDate's minimum date -271821-04-19 is one day beyond legacy Date's
531
+ // minimum -271821-04-20, because of accommodating all Instants in all time
532
+ // zones. If we have -271821-04-19, instead format -271821-04-20 in a time
533
+ // zone that pushes the result into the previous day. This is a slow path
534
+ // because we create a new Intl.DateTimeFormat.
535
+ if (isoString === '-271821-04-19T00:00Z') {
536
+ const options = dateTimeFormat.resolvedOptions();
537
+ dateTimeFormat = new Intl.DateTimeFormat(options.locale, {
538
+ ...(options as Intl.DateTimeFormatOptions),
539
+ timeZone: 'Etc/GMT+1'
540
+ });
541
+ legacyDate = new Date('-271821-04-20T00:00Z');
542
+ }
543
+
544
+ try {
545
+ return dateTimeFormat.formatToParts(legacyDate);
546
+ } catch (e) {
547
+ throw new RangeError(`Invalid ISO date: ${isoString}`);
548
+ }
549
+ }
550
+ isoToCalendarDate(isoDate: ISODate, cache: OneObjectCache): FullCalendarDate {
551
+ const { year: isoYear, month: isoMonth, day: isoDay } = isoDate;
552
+ const key = JSON.stringify({ func: 'isoToCalendarDate', isoYear, isoMonth, isoDay, id: this.id });
553
+ const cached = cache.get(key);
554
+ if (cached) return cached;
555
+
556
+ const isoString = toUtcIsoDateString({ isoYear, isoMonth, isoDay });
557
+ const parts = this.getCalendarParts(isoString);
558
+ const result: Partial<FullCalendarDate> = {};
559
+ for (let i = 0; i < parts.length; i++) {
560
+ const { type, value } = parts[i];
561
+ // TODO: remove this type annotation when `relatedYear` gets into TS lib types
562
+ if (type === 'year' || type === ('relatedYear' as Intl.DateTimeFormatPartTypes)) {
563
+ if (this.hasEra) {
564
+ result.eraYear = +value;
565
+ } else {
566
+ result.year = +value;
567
+ }
568
+ }
569
+ if (type === 'month') {
570
+ const matches = /^([0-9]*)(.*?)$/.exec(value);
571
+ if (!matches || matches.length != 3 || (!matches[1] && !matches[2])) {
572
+ throw new RangeError(`Unexpected month: ${value}`);
573
+ }
574
+ // If the month has no numeric part (should only see this for the Hebrew
575
+ // calendar with newer FF / Chromium versions; see
576
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1751833) then set a
577
+ // placeholder month index of `1` and rely on the derived class to
578
+ // calculate the correct month index from the month name stored in
579
+ // `monthExtra`.
580
+ result.month = matches[1] ? +matches[1] : 1;
581
+ if (result.month < 1) {
582
+ throw new RangeError(
583
+ `Invalid month ${value} from ${isoString}[u-ca-${this.id}]` +
584
+ ' (probably due to https://bugs.chromium.org/p/v8/issues/detail?id=10527)'
585
+ );
586
+ }
587
+ if (result.month > 13) {
588
+ throw new RangeError(
589
+ `Invalid month ${value} from ${isoString}[u-ca-${this.id}]` +
590
+ ' (probably due to https://bugs.chromium.org/p/v8/issues/detail?id=10529)'
591
+ );
592
+ }
593
+
594
+ // The ICU formats for the Hebrew calendar no longer support a numeric
595
+ // month format. So we'll rely on the derived class to interpret it.
596
+ // `monthExtra` is also used on the Chinese calendar to handle a suffix
597
+ // "bis" indicating a leap month.
598
+ if (matches[2]) result.monthExtra = matches[2];
599
+ }
600
+ if (type === 'day') result.day = +value;
601
+ if (this.hasEra && type === 'era' && value != null && value !== '') {
602
+ // The convention for Temporal era values is lowercase, so following
603
+ // that convention in this prototype. Punctuation is removed, accented
604
+ // letters are normalized, and spaces are replaced with dashes.
605
+ // E.g.: "ERA0" => "era0", "Before R.O.C." => "before-roc", "En’ō" => "eno"
606
+ // The call to normalize() and the replacement regex deals with era
607
+ // names that contain non-ASCII characters like Japanese eras. Also
608
+ // ignore extra content in parentheses like JPN era date ranges.
609
+ result.era = value
610
+ .split(' (')[0]
611
+ .normalize('NFD')
612
+ .replace(/[^-0-9 \p{L}]/gu, '')
613
+ .replace(/ /g, '-')
614
+ .toLowerCase();
615
+ }
616
+ }
617
+ if (this.hasEra && result.eraYear === undefined) {
618
+ // Node 12 has outdated ICU data that lacks the `relatedYear` field in the
619
+ // output of Intl.DateTimeFormat.formatToParts.
620
+ throw new RangeError(
621
+ `Intl.DateTimeFormat.formatToParts lacks relatedYear in ${this.id} calendar. Try Node 14+ or modern browsers.`
622
+ );
623
+ }
624
+ // Translate old ICU era codes "ERA0" etc. into canonical era names.
625
+ if (this.hasEra) {
626
+ const replacement = this.eras.find((e) => result.era === e.genericName);
627
+ if (replacement) result.era = replacement.code;
628
+ }
629
+ // Translate eras that may be handled differently by Temporal vs. by Intl
630
+ // (e.g. Japanese pre-Meiji eras). See https://github.com/tc39/proposal-temporal/issues/526.
631
+ if (this.reviseIntlEra) {
632
+ const { era, eraYear } = this.reviseIntlEra(result, isoDate);
633
+ result.era = era;
634
+ result.eraYear = eraYear;
635
+ }
636
+ if (this.checkIcuBugs) this.checkIcuBugs(isoDate);
637
+
638
+ const calendarDate = this.adjustCalendarDate(result, cache, 'constrain', true);
639
+ if (calendarDate.year === undefined) throw new RangeError(`Missing year converting ${JSON.stringify(isoDate)}`);
640
+ if (calendarDate.month === undefined) {
641
+ throw new RangeError(`Missing month converting ${JSON.stringify(isoDate)}`);
642
+ }
643
+ if (calendarDate.day === undefined) throw new RangeError(`Missing day converting ${JSON.stringify(isoDate)}`);
644
+ cache.set(key, calendarDate);
645
+ // Also cache the reverse mapping
646
+ const cacheReverse = (overflow: Overflow) => {
647
+ const keyReverse = JSON.stringify({
648
+ func: 'calendarToIsoDate',
649
+ year: calendarDate.year,
650
+ month: calendarDate.month,
651
+ day: calendarDate.day,
652
+ overflow,
653
+ id: this.id
654
+ });
655
+ cache.set(keyReverse, isoDate);
656
+ };
657
+ (['constrain', 'reject'] as const).forEach(cacheReverse);
658
+ return calendarDate;
659
+ }
660
+ validateCalendarDate(calendarDate: Partial<FullCalendarDate>): asserts calendarDate is FullCalendarDate {
661
+ const { month, year, day, eraYear, monthCode, monthExtra } = calendarDate;
662
+ // When there's a suffix (e.g. "5bis" for a leap month in Chinese calendar)
663
+ // the derived class must deal with it.
664
+ if (monthExtra !== undefined) throw new RangeError('Unexpected `monthExtra` value');
665
+ if (year === undefined && eraYear === undefined) throw new TypeError('year or eraYear is required');
666
+ if (month === undefined && monthCode === undefined) throw new TypeError('month or monthCode is required');
667
+ if (day === undefined) throw new RangeError('Missing day');
668
+ if (monthCode !== undefined) {
669
+ if (typeof monthCode !== 'string') {
670
+ throw new RangeError(`monthCode must be a string, not ${typeof monthCode}`);
671
+ }
672
+ if (!/^M([01]?\d)(L?)$/.test(monthCode)) {
673
+ throw new RangeError(`Invalid monthCode: ${monthCode}`);
674
+ }
675
+ }
676
+ if (this.hasEra) {
677
+ if ((calendarDate['era'] === undefined) !== (calendarDate['eraYear'] === undefined)) {
678
+ throw new TypeError('properties era and eraYear must be provided together');
679
+ }
680
+ }
681
+ }
682
+ /**
683
+ * Allows derived calendars to add additional fields and/or to make
684
+ * adjustments e.g. to set the era based on the date or to revise the month
685
+ * number in lunisolar calendars per
686
+ * https://github.com/tc39/proposal-temporal/issues/1203.
687
+ *
688
+ * The base implementation fills in missing values by assuming the simplest
689
+ * possible calendar:
690
+ * - no eras
691
+ * - non-lunisolar calendar (no leap months)
692
+ * */
693
+ adjustCalendarDate(
694
+ calendarDateParam: Partial<FullCalendarDate>,
695
+ cache: OneObjectCache | undefined = undefined,
696
+ overflow: Overflow = 'constrain',
697
+ // This param is only used by derived classes
698
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
699
+ fromLegacyDate = false
700
+ ): FullCalendarDate {
701
+ if (this.calendarType === 'lunisolar') throw new RangeError('Override required for lunisolar calendars');
702
+ let calendarDate = calendarDateParam;
703
+ this.validateCalendarDate(calendarDate);
704
+ const largestMonth = this.monthsInYear(calendarDate, cache);
705
+ let { month, monthCode } = calendarDate;
706
+
707
+ ({ month, monthCode } = resolveNonLunisolarMonth(calendarDate, overflow, largestMonth));
708
+ return { ...(calendarDate as typeof calendarDate & CalendarYMD), month, monthCode };
709
+ }
710
+ regulateMonthDayNaive(calendarDate: FullCalendarDate, overflow: Overflow, cache: OneObjectCache): FullCalendarDate {
711
+ const largestMonth = this.monthsInYear(calendarDate, cache);
712
+ let { month, day } = calendarDate;
713
+ if (overflow === 'reject') {
714
+ ES.RejectToRange(month, 1, largestMonth);
715
+ ES.RejectToRange(day, 1, this.maximumMonthLength(calendarDate));
716
+ } else {
717
+ month = ES.ConstrainToRange(month, 1, largestMonth);
718
+ day = ES.ConstrainToRange(day, 1, this.maximumMonthLength({ ...calendarDate, month }));
719
+ }
720
+ return { ...calendarDate, month, day };
721
+ }
722
+ calendarToIsoDate(dateParam: CalendarDateFields, overflow: Overflow = 'constrain', cache: OneObjectCache): ISODate {
723
+ const originalDate = dateParam as Partial<FullCalendarDate>;
724
+ // First, normalize the calendar date to ensure that (year, month, day)
725
+ // are all present, converting monthCode and eraYear if needed.
726
+ let date = this.adjustCalendarDate(dateParam, cache, overflow, false);
727
+
728
+ // Fix obviously out-of-bounds values. Values that are valid generally, but
729
+ // not in this particular year, may not be caught here for some calendars.
730
+ // If so, these will be handled lower below.
731
+ date = this.regulateMonthDayNaive(date, overflow, cache);
732
+
733
+ const { year, month, day } = date;
734
+ const key = JSON.stringify({ func: 'calendarToIsoDate', year, month, day, overflow, id: this.id });
735
+ let cached = cache.get(key);
736
+ if (cached) return cached;
737
+ // If YMD are present in the input but the input has been constrained
738
+ // already, then cache both the original value and the constrained value.
739
+ let keyOriginal;
740
+ if (
741
+ originalDate.year !== undefined &&
742
+ originalDate.month !== undefined &&
743
+ originalDate.day !== undefined &&
744
+ (originalDate.year !== date.year || originalDate.month !== date.month || originalDate.day !== date.day)
745
+ ) {
746
+ keyOriginal = JSON.stringify({
747
+ func: 'calendarToIsoDate',
748
+ year: originalDate.year,
749
+ month: originalDate.month,
750
+ day: originalDate.day,
751
+ overflow,
752
+ id: this.id
753
+ });
754
+ cached = cache.get(keyOriginal);
755
+ if (cached) return cached;
756
+ }
757
+
758
+ // First, try to roughly guess the result
759
+ let isoEstimate = this.estimateIsoDate({ year, month, day });
760
+ const calculateSameMonthResult = (diffDays: number) => {
761
+ // If the estimate is in the same year & month as the target, then we can
762
+ // calculate the result exactly and short-circuit any additional logic.
763
+ // This optimization assumes that months are continuous. It would break if
764
+ // a calendar skipped days, like the Julian->Gregorian switchover. But
765
+ // current ICU calendars only skip days (japanese/roc/buddhist) because of
766
+ // a bug (https://bugs.chromium.org/p/chromium/issues/detail?id=1173158)
767
+ // that's currently worked around by a custom calendarToIsoDate
768
+ // implementation in those calendars. So this optimization should be safe
769
+ // for all ICU calendars.
770
+ let testIsoEstimate = this.addDaysIso(isoEstimate, diffDays);
771
+ if (date.day > this.minimumMonthLength(date)) {
772
+ // There's a chance that the calendar date is out of range. Throw or
773
+ // constrain if so.
774
+ let testCalendarDate = this.isoToCalendarDate(testIsoEstimate, cache);
775
+ while (testCalendarDate.month !== month || testCalendarDate.year !== year) {
776
+ if (overflow === 'reject') {
777
+ throw new RangeError(`day ${day} does not exist in month ${month} of year ${year}`);
778
+ }
779
+ // Back up a day at a time until we're not hanging over the month end
780
+ testIsoEstimate = this.addDaysIso(testIsoEstimate, -1);
781
+ testCalendarDate = this.isoToCalendarDate(testIsoEstimate, cache);
782
+ }
783
+ }
784
+ return testIsoEstimate;
785
+ };
786
+ let sign = 0;
787
+ let roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
788
+ let diff = simpleDateDiff(date, roundtripEstimate);
789
+ if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0) {
790
+ const diffTotalDaysEstimate = diff.years * 365 + diff.months * 30 + diff.days;
791
+ isoEstimate = this.addDaysIso(isoEstimate, diffTotalDaysEstimate);
792
+ roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
793
+ diff = simpleDateDiff(date, roundtripEstimate);
794
+ if (diff.years === 0 && diff.months === 0) {
795
+ isoEstimate = calculateSameMonthResult(diff.days);
796
+ } else {
797
+ sign = this.compareCalendarDates(date, roundtripEstimate);
798
+ }
799
+ }
800
+ // If the initial guess is not in the same month, then bisect the
801
+ // distance to the target, starting with 8 days per step.
802
+ let increment = 8;
803
+ while (sign) {
804
+ isoEstimate = this.addDaysIso(isoEstimate, sign * increment);
805
+ const oldRoundtripEstimate = roundtripEstimate;
806
+ roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
807
+ const oldSign = sign;
808
+ sign = this.compareCalendarDates(date, roundtripEstimate);
809
+ if (sign) {
810
+ diff = simpleDateDiff(date, roundtripEstimate);
811
+ if (diff.years === 0 && diff.months === 0) {
812
+ isoEstimate = calculateSameMonthResult(diff.days);
813
+ // Signal the loop condition that there's a match.
814
+ sign = 0;
815
+ } else if (oldSign && sign !== oldSign) {
816
+ if (increment > 1) {
817
+ // If the estimate overshot the target, try again with a smaller increment
818
+ // in the reverse direction.
819
+ increment /= 2;
820
+ } else {
821
+ // Increment is 1, and neither the previous estimate nor the new
822
+ // estimate is correct. The only way that can happen is if the
823
+ // original date was an invalid value that will be constrained or
824
+ // rejected here.
825
+ if (overflow === 'reject') {
826
+ throw new RangeError(`Can't find ISO date from calendar date: ${JSON.stringify({ ...originalDate })}`);
827
+ } else {
828
+ // To constrain, pick the earliest value
829
+ const order = this.compareCalendarDates(roundtripEstimate, oldRoundtripEstimate);
830
+ // If current value is larger, then back up to the previous value.
831
+ if (order > 0) isoEstimate = this.addDaysIso(isoEstimate, -1);
832
+ sign = 0;
833
+ }
834
+ }
835
+ }
836
+ }
837
+ }
838
+ cache.set(key, isoEstimate);
839
+ if (keyOriginal) cache.set(keyOriginal, isoEstimate);
840
+ if (
841
+ date.year === undefined ||
842
+ date.month === undefined ||
843
+ date.day === undefined ||
844
+ date.monthCode === undefined ||
845
+ (this.hasEra && (date.era === undefined || date.eraYear === undefined))
846
+ ) {
847
+ throw new RangeError('Unexpected missing property');
848
+ }
849
+ return isoEstimate;
850
+ }
851
+ compareCalendarDates(date1: CalendarYMD, date2: CalendarYMD) {
852
+ if (date1.year !== date2.year) return ES.ComparisonResult(date1.year - date2.year);
853
+ if (date1.month !== date2.month) return ES.ComparisonResult(date1.month - date2.month);
854
+ if (date1.day !== date2.day) return ES.ComparisonResult(date1.day - date2.day);
855
+ return 0;
856
+ }
857
+ /** Ensure that a calendar date actually exists. If not, return the closest earlier date. */
858
+ regulateDate(calendarDate: CalendarYMD, overflow: Overflow = 'constrain', cache: OneObjectCache): FullCalendarDate {
859
+ const isoDate = this.calendarToIsoDate(calendarDate, overflow, cache);
860
+ return this.isoToCalendarDate(isoDate, cache);
861
+ }
862
+ addDaysIso(isoDate: ISODate, days: number): ISODate {
863
+ const added = ES.BalanceISODate(isoDate.year, isoDate.month, isoDate.day + days);
864
+ return added;
865
+ }
866
+ addDaysCalendar(calendarDate: CalendarYMD, days: number, cache: OneObjectCache): FullCalendarDate {
867
+ const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
868
+ const addedIso = this.addDaysIso(isoDate, days);
869
+ const addedCalendar = this.isoToCalendarDate(addedIso, cache);
870
+ return addedCalendar;
871
+ }
872
+ addMonthsCalendar(
873
+ calendarDateParam: CalendarYMD,
874
+ months: number,
875
+ overflow: Overflow,
876
+ cache: OneObjectCache
877
+ ): CalendarYMD {
878
+ let calendarDate = calendarDateParam;
879
+ const { day } = calendarDate;
880
+ for (let i = 0, absMonths = Math.abs(months); i < absMonths; i++) {
881
+ const { month } = calendarDate;
882
+ const oldCalendarDate = calendarDate;
883
+ const days =
884
+ months < 0
885
+ ? -Math.max(day, this.daysInPreviousMonth(calendarDate, cache))
886
+ : this.daysInMonth(calendarDate, cache);
887
+ const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
888
+ let addedIso = this.addDaysIso(isoDate, days);
889
+ calendarDate = this.isoToCalendarDate(addedIso, cache);
890
+
891
+ // Normally, we can advance one month by adding the number of days in the
892
+ // current month. However, if we're at the end of the current month and
893
+ // the next month has fewer days, then we rolled over to the after-next
894
+ // month. Below we detect this condition and back up until we're back in
895
+ // the desired month.
896
+ if (months > 0) {
897
+ const monthsInOldYear = this.monthsInYear(oldCalendarDate, cache);
898
+ while (calendarDate.month - 1 !== month % monthsInOldYear) {
899
+ addedIso = this.addDaysIso(addedIso, -1);
900
+ calendarDate = this.isoToCalendarDate(addedIso, cache);
901
+ }
902
+ }
903
+
904
+ if (calendarDate.day !== day) {
905
+ // try to retain the original day-of-month, if possible
906
+ calendarDate = this.regulateDate({ ...calendarDate, day }, 'constrain', cache);
907
+ }
908
+ }
909
+ if (overflow === 'reject' && calendarDate.day !== day) {
910
+ throw new RangeError(`Day ${day} does not exist in resulting calendar month`);
911
+ }
912
+ return calendarDate;
913
+ }
914
+ addCalendar(
915
+ calendarDate: CalendarYMD & { monthCode: string },
916
+ { years = 0, months = 0, weeks = 0, days = 0 },
917
+ overflow: Overflow,
918
+ cache: OneObjectCache
919
+ ): FullCalendarDate {
920
+ const { year, day, monthCode } = calendarDate;
921
+ const addedYears = this.adjustCalendarDate({ year: year + years, monthCode, day }, cache);
922
+ const addedMonths = this.addMonthsCalendar(addedYears, months, overflow, cache);
923
+ const initialDays = days + weeks * 7;
924
+ const addedDays = this.addDaysCalendar(addedMonths, initialDays, cache);
925
+ return addedDays;
926
+ }
927
+ untilCalendar(
928
+ calendarOne: FullCalendarDate,
929
+ calendarTwo: FullCalendarDate,
930
+ largestUnit: Temporal.DateUnit,
931
+ cache: OneObjectCache
932
+ ): { years: number; months: number; weeks: number; days: number } {
933
+ let days = 0;
934
+ let weeks = 0;
935
+ let months = 0;
936
+ let years = 0;
937
+ switch (largestUnit) {
938
+ case 'day':
939
+ days = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
940
+ break;
941
+ case 'week': {
942
+ const totalDays = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
943
+ days = totalDays % 7;
944
+ weeks = (totalDays - days) / 7;
945
+ break;
946
+ }
947
+ case 'month':
948
+ case 'year': {
949
+ const sign = this.compareCalendarDates(calendarTwo, calendarOne);
950
+ if (!sign) {
951
+ return { years: 0, months: 0, weeks: 0, days: 0 };
952
+ }
953
+ const diffYears = calendarTwo.year - calendarOne.year;
954
+ const diffDays = calendarTwo.day - calendarOne.day;
955
+ if (largestUnit === 'year' && diffYears) {
956
+ let diffInYearSign = 0;
957
+ if (calendarTwo.monthCode > calendarOne.monthCode) diffInYearSign = 1;
958
+ if (calendarTwo.monthCode < calendarOne.monthCode) diffInYearSign = -1;
959
+ if (!diffInYearSign) diffInYearSign = Math.sign(diffDays);
960
+ const isOneFurtherInYear = diffInYearSign * sign < 0;
961
+ years = isOneFurtherInYear ? diffYears - sign : diffYears;
962
+ }
963
+ const yearsAdded = years ? this.addCalendar(calendarOne, { years }, 'constrain', cache) : calendarOne;
964
+ // Now we have less than one year remaining. Add one month at a time
965
+ // until we go over the target, then back up one month and calculate
966
+ // remaining days and weeks.
967
+ let current;
968
+ let next: CalendarYMD = yearsAdded;
969
+ do {
970
+ months += sign;
971
+ current = next;
972
+ next = this.addMonthsCalendar(current, sign, 'constrain', cache);
973
+ if (next.day !== calendarOne.day) {
974
+ // In case the day was constrained down, try to un-constrain it
975
+ next = this.regulateDate({ ...next, day: calendarOne.day }, 'constrain', cache);
976
+ }
977
+ } while (this.compareCalendarDates(calendarTwo, next) * sign >= 0);
978
+ months -= sign; // correct for loop above which overshoots by 1
979
+ const remainingDays = this.calendarDaysUntil(current, calendarTwo, cache);
980
+ days = remainingDays;
981
+ break;
982
+ }
983
+ }
984
+ return { years, months, weeks, days };
985
+ }
986
+ daysInMonth(calendarDate: CalendarYMD, cache: OneObjectCache): number {
987
+ // Add enough days to roll over to the next month. One we're in the next
988
+ // month, we can calculate the length of the current month. NOTE: This
989
+ // algorithm assumes that months are continuous. It would break if a
990
+ // calendar skipped days, like the Julian->Gregorian switchover. But current
991
+ // ICU calendars only skip days (japanese/roc/buddhist) because of a bug
992
+ // (https://bugs.chromium.org/p/chromium/issues/detail?id=1173158) that's
993
+ // currently worked around by a custom calendarToIsoDate implementation in
994
+ // those calendars. So this code should be safe for all ICU calendars.
995
+ const { day } = calendarDate;
996
+ const max = this.maximumMonthLength(calendarDate);
997
+ const min = this.minimumMonthLength(calendarDate);
998
+ // easiest case: we already know the month length if min and max are the same.
999
+ if (min === max) return min;
1000
+
1001
+ // Add enough days to get into the next month, without skipping it
1002
+ const increment = day <= max - min ? max : min;
1003
+ const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
1004
+ const addedIsoDate = this.addDaysIso(isoDate, increment);
1005
+ const addedCalendarDate = this.isoToCalendarDate(addedIsoDate, cache);
1006
+
1007
+ // Now back up to the last day of the original month
1008
+ const endOfMonthIso = this.addDaysIso(addedIsoDate, -addedCalendarDate.day);
1009
+ const endOfMonthCalendar = this.isoToCalendarDate(endOfMonthIso, cache);
1010
+ return endOfMonthCalendar.day;
1011
+ }
1012
+ daysInPreviousMonth(calendarDate: CalendarYMD, cache: OneObjectCache): number {
1013
+ const { day, month, year } = calendarDate;
1014
+
1015
+ // Check to see if we already know the month length, and return it if so
1016
+ const previousMonthYear = month > 1 ? year : year - 1;
1017
+ let previousMonthDate = { year: previousMonthYear, month, day: 1 };
1018
+ const previousMonth = month > 1 ? month - 1 : this.monthsInYear(previousMonthDate, cache);
1019
+ previousMonthDate = { ...previousMonthDate, month: previousMonth };
1020
+ const min = this.minimumMonthLength(previousMonthDate);
1021
+ const max = this.maximumMonthLength(previousMonthDate);
1022
+ if (min === max) return max;
1023
+
1024
+ const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
1025
+ const lastDayOfPreviousMonthIso = this.addDaysIso(isoDate, -day);
1026
+ const lastDayOfPreviousMonthCalendar = this.isoToCalendarDate(lastDayOfPreviousMonthIso, cache);
1027
+ return lastDayOfPreviousMonthCalendar.day;
1028
+ }
1029
+ startOfCalendarYear(calendarDate: CalendarYearOnly): CalendarYMD & { monthCode: string } {
1030
+ return { year: calendarDate.year, month: 1, monthCode: 'M01', day: 1 };
1031
+ }
1032
+ startOfCalendarMonth(calendarDate: CalendarYM): CalendarYMD {
1033
+ return { year: calendarDate.year, month: calendarDate.month, day: 1 };
1034
+ }
1035
+ calendarDaysUntil(calendarOne: CalendarYMD, calendarTwo: CalendarYMD, cache: OneObjectCache): number {
1036
+ const oneIso = this.calendarToIsoDate(calendarOne, 'constrain', cache);
1037
+ const twoIso = this.calendarToIsoDate(calendarTwo, 'constrain', cache);
1038
+ return (
1039
+ ES.ISODateToEpochDays(twoIso.year, twoIso.month - 1, twoIso.day) -
1040
+ ES.ISODateToEpochDays(oneIso.year, oneIso.month - 1, oneIso.day)
1041
+ );
1042
+ }
1043
+ // Override if calendar uses eras
1044
+ hasEra = false;
1045
+ // See https://github.com/tc39/proposal-temporal/issues/1784
1046
+ erasBeginMidYear = false;
1047
+ // Override this to shortcut the search space if certain month codes only
1048
+ // occur long in the past
1049
+ monthDaySearchStartYear(monthCode: string, day: number) {
1050
+ void monthCode, day;
1051
+ return 1972;
1052
+ }
1053
+ monthDayFromFields(fields: MonthDayFromFieldsObject, overflow: Overflow, cache: OneObjectCache): ISODate {
1054
+ let { era, eraYear, year, month, monthCode, day } = fields;
1055
+ if (month !== undefined && year === undefined && (!this.hasEra || era === undefined || eraYear === undefined)) {
1056
+ throw new TypeError('when month is present, year (or era and eraYear) are required');
1057
+ }
1058
+ if (monthCode === undefined || year !== undefined || (this.hasEra && eraYear !== undefined)) {
1059
+ // Apply overflow behaviour to year/month/day, to get correct monthCode/day
1060
+ ({ monthCode, day } = this.isoToCalendarDate(this.calendarToIsoDate(fields, overflow, cache), cache));
1061
+ }
1062
+
1063
+ let isoYear, isoMonth, isoDay;
1064
+ let closestCalendar, closestIso;
1065
+ // Look backwards starting from one of the calendar years spanning ISO year
1066
+ // 1972, up to 20 calendar years prior, to find a year that has this month
1067
+ // and day. Normal months and days will match immediately, but for leap days
1068
+ // and leap months we may have to look for a while. For searches longer than
1069
+ // 20 years, override the start date in monthDaySearchStartYear.
1070
+ const startDateIso = {
1071
+ year: this.monthDaySearchStartYear(monthCode, day),
1072
+ month: 12,
1073
+ day: 31
1074
+ };
1075
+ const calendarOfStartDateIso = this.isoToCalendarDate(startDateIso, cache);
1076
+ // Note: relies on lexicographical ordering of monthCodes
1077
+ const calendarYear =
1078
+ calendarOfStartDateIso.monthCode > monthCode ||
1079
+ (calendarOfStartDateIso.monthCode === monthCode && calendarOfStartDateIso.day >= day)
1080
+ ? calendarOfStartDateIso.year
1081
+ : calendarOfStartDateIso.year - 1;
1082
+ for (let i = 0; i < 20; i++) {
1083
+ const testCalendarDate: FullCalendarDate = this.adjustCalendarDate(
1084
+ { day, monthCode, year: calendarYear - i },
1085
+ cache
1086
+ );
1087
+ const isoDate = this.calendarToIsoDate(testCalendarDate, 'constrain', cache);
1088
+ const roundTripCalendarDate = this.isoToCalendarDate(isoDate, cache);
1089
+ ({ year: isoYear, month: isoMonth, day: isoDay } = isoDate);
1090
+ if (roundTripCalendarDate.monthCode === monthCode && roundTripCalendarDate.day === day) {
1091
+ return { month: isoMonth, day: isoDay, year: isoYear };
1092
+ } else if (overflow === 'constrain') {
1093
+ // If the requested day is never present in any instance of this month
1094
+ // code, and the round trip date is an instance of this month code with
1095
+ // the most possible days, we are as close as we can get.
1096
+ const maxDayForMonthCode = this.maxLengthOfMonthCodeInAnyYear(roundTripCalendarDate.monthCode);
1097
+ if (
1098
+ roundTripCalendarDate.monthCode === monthCode &&
1099
+ roundTripCalendarDate.day === maxDayForMonthCode &&
1100
+ day > maxDayForMonthCode
1101
+ ) {
1102
+ return { month: isoMonth, day: isoDay, year: isoYear };
1103
+ }
1104
+ // non-ISO constrain algorithm tries to find the closest date in a matching month
1105
+ if (
1106
+ closestCalendar === undefined ||
1107
+ (roundTripCalendarDate.monthCode === closestCalendar.monthCode &&
1108
+ roundTripCalendarDate.day > closestCalendar.day)
1109
+ ) {
1110
+ closestCalendar = roundTripCalendarDate;
1111
+ closestIso = isoDate;
1112
+ }
1113
+ }
1114
+ }
1115
+ if (overflow === 'constrain' && closestIso !== undefined) return closestIso;
1116
+ throw new RangeError(`No recent ${this.id} year with monthCode ${monthCode} and day ${day}`);
1117
+ }
1118
+ getFirstDayOfWeek(): number | undefined {
1119
+ return undefined;
1120
+ }
1121
+ getMinimalDaysInFirstWeek(): number | undefined {
1122
+ return undefined;
1123
+ }
1124
+ }
1125
+
1126
+ interface HebrewMonthInfo {
1127
+ [m: string]: (
1128
+ | {
1129
+ leap: undefined;
1130
+ regular: number;
1131
+ }
1132
+ | {
1133
+ leap: number;
1134
+ regular: undefined;
1135
+ }
1136
+ | {
1137
+ leap: number;
1138
+ regular: number;
1139
+ }
1140
+ ) & {
1141
+ monthCode: string;
1142
+ days:
1143
+ | number
1144
+ | {
1145
+ min: number;
1146
+ max: number;
1147
+ };
1148
+ };
1149
+ }
1150
+
1151
+ class HebrewHelper extends HelperBase {
1152
+ id = 'hebrew' as const;
1153
+ calendarType = 'lunisolar' as const;
1154
+ inLeapYear(calendarDate: CalendarYearOnly) {
1155
+ const { year } = calendarDate;
1156
+ // FYI: In addition to adding a month in leap years, the Hebrew calendar
1157
+ // also has per-year changes to the number of days of Heshvan and Kislev.
1158
+ // Given that these can be calculated by counting the number of days in
1159
+ // those months, I assume that these DO NOT need to be exposed as
1160
+ // Hebrew-only prototype fields or methods.
1161
+ return (7 * year + 1) % 19 < 7;
1162
+ }
1163
+ monthsInYear(calendarDate: CalendarYearOnly) {
1164
+ return this.inLeapYear(calendarDate) ? 13 : 12;
1165
+ }
1166
+ minimumMonthLength(calendarDate: CalendarYM) {
1167
+ return this.minMaxMonthLength(calendarDate, 'min');
1168
+ }
1169
+ maximumMonthLength(calendarDate: CalendarYM) {
1170
+ return this.minMaxMonthLength(calendarDate, 'max');
1171
+ }
1172
+ minMaxMonthLength(calendarDate: CalendarYM, minOrMax: 'min' | 'max') {
1173
+ const { month, year } = calendarDate;
1174
+ const monthCode = this.getMonthCode(year, month);
1175
+ const monthInfo = Object.entries(this.months).find((m) => m[1].monthCode === monthCode);
1176
+ if (monthInfo === undefined) throw new RangeError(`unmatched Hebrew month: ${month}`);
1177
+ const daysInMonth = monthInfo[1].days;
1178
+ return typeof daysInMonth === 'number' ? daysInMonth : daysInMonth[minOrMax];
1179
+ }
1180
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1181
+ return ['M04', 'M06', 'M08', 'M10', 'M12'].includes(monthCode) ? 29 : 30;
1182
+ }
1183
+ /** Take a guess at what ISO date a particular calendar date corresponds to */
1184
+ estimateIsoDate(calendarDate: CalendarYMD) {
1185
+ const { year } = calendarDate;
1186
+ return { year: year - 3760, month: 1, day: 1 };
1187
+ }
1188
+ months: HebrewMonthInfo = {
1189
+ Tishri: { leap: 1, regular: 1, monthCode: 'M01', days: 30 },
1190
+ Heshvan: { leap: 2, regular: 2, monthCode: 'M02', days: { min: 29, max: 30 } },
1191
+ Kislev: { leap: 3, regular: 3, monthCode: 'M03', days: { min: 29, max: 30 } },
1192
+ Tevet: { leap: 4, regular: 4, monthCode: 'M04', days: 29 },
1193
+ Shevat: { leap: 5, regular: 5, monthCode: 'M05', days: 30 },
1194
+ Adar: { leap: undefined, regular: 6, monthCode: 'M06', days: 29 },
1195
+ 'Adar I': { leap: 6, regular: undefined, monthCode: 'M05L', days: 30 },
1196
+ 'Adar II': { leap: 7, regular: undefined, monthCode: 'M06', days: 29 },
1197
+ Nisan: { leap: 8, regular: 7, monthCode: 'M07', days: 30 },
1198
+ Iyar: { leap: 9, regular: 8, monthCode: 'M08', days: 29 },
1199
+ Sivan: { leap: 10, regular: 9, monthCode: 'M09', days: 30 },
1200
+ Tamuz: { leap: 11, regular: 10, monthCode: 'M10', days: 29 },
1201
+ Av: { leap: 12, regular: 11, monthCode: 'M11', days: 30 },
1202
+ Elul: { leap: 13, regular: 12, monthCode: 'M12', days: 29 }
1203
+ };
1204
+ getMonthCode(year: number, month: number) {
1205
+ if (this.inLeapYear({ year })) {
1206
+ return month === 6 ? buildMonthCode(5, true) : buildMonthCode(month < 6 ? month : month - 1);
1207
+ } else {
1208
+ return buildMonthCode(month);
1209
+ }
1210
+ }
1211
+ override adjustCalendarDate(
1212
+ calendarDate: Partial<FullCalendarDate>,
1213
+ cache?: OneObjectCache,
1214
+ overflow: Overflow = 'constrain',
1215
+ fromLegacyDate = false
1216
+ ): FullCalendarDate {
1217
+ let { year, month, monthCode, day, monthExtra } = calendarDate as Omit<typeof calendarDate, 'day'> & {
1218
+ day: number;
1219
+ };
1220
+ if (year === undefined) throw new TypeError('Missing property: year');
1221
+ if (fromLegacyDate) {
1222
+ // In Pre Node-14 V8, DateTimeFormat.formatToParts `month: 'numeric'`
1223
+ // output returns the numeric equivalent of `month` as a string, meaning
1224
+ // that `'6'` in a leap year is Adar I, while `'6'` in a non-leap year
1225
+ // means Adar. In this case, `month` will already be correct and no action
1226
+ // is needed. However, in Node 14 and later formatToParts returns the name
1227
+ // of the Hebrew month (e.g. "Tevet"), so we'll need to look up the
1228
+ // correct `month` using the string name as a key.
1229
+ if (monthExtra) {
1230
+ const monthInfo = this.months[monthExtra];
1231
+ if (!monthInfo) throw new RangeError(`Unrecognized month from formatToParts: ${monthExtra}`);
1232
+ month = this.inLeapYear({ year }) ? monthInfo.leap : monthInfo.regular;
1233
+ }
1234
+ // Because we're getting data from legacy Date, then `month` will always be present
1235
+ monthCode = this.getMonthCode(year, month as number);
1236
+ return { year, month: month as number, day, monthCode };
1237
+ } else {
1238
+ // When called without input coming from legacy Date output, simply ensure
1239
+ // that all fields are present.
1240
+ this.validateCalendarDate(calendarDate);
1241
+ if (month === undefined) {
1242
+ ES.assertExists(monthCode);
1243
+ if (monthCode.endsWith('L')) {
1244
+ if (monthCode !== 'M05L') {
1245
+ throw new RangeError(`Hebrew leap month must have monthCode M05L, not ${monthCode}`);
1246
+ }
1247
+ month = 6;
1248
+ if (!this.inLeapYear({ year })) {
1249
+ if (overflow === 'reject') {
1250
+ throw new RangeError(`Hebrew monthCode M05L is invalid in year ${year} which is not a leap year`);
1251
+ } else {
1252
+ // constrain to same day of next month (Adar)
1253
+ month = 6;
1254
+ monthCode = 'M06';
1255
+ }
1256
+ }
1257
+ } else {
1258
+ month = nonLeapMonthCodeNumberPart(monthCode);
1259
+ // if leap month is before this one, the month index is one more than the month code
1260
+ if (this.inLeapYear({ year }) && month >= 6) month++;
1261
+ const largestMonth = this.monthsInYear({ year });
1262
+ if (month < 1 || month > largestMonth) throw new RangeError(`Invalid monthCode: ${monthCode}`);
1263
+ }
1264
+ } else {
1265
+ if (overflow === 'reject') {
1266
+ ES.RejectToRange(month, 1, this.monthsInYear({ year }));
1267
+ ES.RejectToRange(day, 1, this.maximumMonthLength({ year, month }));
1268
+ } else {
1269
+ month = ES.ConstrainToRange(month, 1, this.monthsInYear({ year }));
1270
+ day = ES.ConstrainToRange(day, 1, this.maximumMonthLength({ year, month }));
1271
+ }
1272
+ if (monthCode === undefined) {
1273
+ monthCode = this.getMonthCode(year, month);
1274
+ } else {
1275
+ const calculatedMonthCode = this.getMonthCode(year, month);
1276
+ if (calculatedMonthCode !== monthCode) {
1277
+ throw new RangeError(`monthCode ${monthCode} doesn't correspond to month ${month} in Hebrew year ${year}`);
1278
+ }
1279
+ }
1280
+ }
1281
+ return { ...calendarDate, day, month, monthCode, year };
1282
+ }
1283
+ }
1284
+ }
1285
+
1286
+ /**
1287
+ * For Temporal purposes, the Islamic calendar is simple because it's always the
1288
+ * same 12 months in the same order.
1289
+ */
1290
+ abstract class IslamicBaseHelper extends HelperBase {
1291
+ abstract override id: BuiltinCalendarId;
1292
+ calendarType = 'lunar' as const;
1293
+ inLeapYear(calendarDate: CalendarYearOnly, cache: OneObjectCache) {
1294
+ const startOfYearCalendar = { year: calendarDate.year, month: 1, monthCode: 'M01', day: 1 };
1295
+ const startOfNextYearCalendar = { year: calendarDate.year + 1, month: 1, monthCode: 'M01', day: 1 };
1296
+ const result = this.calendarDaysUntil(startOfYearCalendar, startOfNextYearCalendar, cache);
1297
+ return result === 355;
1298
+ }
1299
+ monthsInYear(/* calendarYear, cache */) {
1300
+ return 12;
1301
+ }
1302
+ minimumMonthLength(/* calendarDate */) {
1303
+ return 29;
1304
+ }
1305
+ maximumMonthLength(/* calendarDate */) {
1306
+ return 30;
1307
+ }
1308
+ maxLengthOfMonthCodeInAnyYear(/* monthCode */) {
1309
+ return 30;
1310
+ }
1311
+ DAYS_PER_ISLAMIC_YEAR = 354 + 11 / 30;
1312
+ DAYS_PER_ISO_YEAR = 365.2425;
1313
+ estimateIsoDate(calendarDate: CalendarYMD) {
1314
+ const { year } = this.adjustCalendarDate(calendarDate);
1315
+ return { year: Math.floor((year * this.DAYS_PER_ISLAMIC_YEAR) / this.DAYS_PER_ISO_YEAR) + 622, month: 1, day: 1 };
1316
+ }
1317
+ }
1318
+
1319
+ // There are 6 Islamic calendars with the same implementation in this polyfill.
1320
+ // They vary only in their ID. They do emit different output from the underlying
1321
+ // Intl implementation, but our code for each of them is identical.
1322
+ class IslamicHelper extends IslamicBaseHelper {
1323
+ id = 'islamic' as const;
1324
+ }
1325
+ class IslamicUmalquraHelper extends IslamicBaseHelper {
1326
+ id = 'islamic-umalqura' as const;
1327
+ }
1328
+ class IslamicTblaHelper extends IslamicBaseHelper {
1329
+ id = 'islamic-tbla' as const;
1330
+ }
1331
+ class IslamicCivilHelper extends IslamicBaseHelper {
1332
+ id = 'islamic-civil' as const;
1333
+ }
1334
+ class IslamicRgsaHelper extends IslamicBaseHelper {
1335
+ id = 'islamic-rgsa' as const;
1336
+ }
1337
+ class IslamicCcHelper extends IslamicBaseHelper {
1338
+ id = 'islamicc' as const;
1339
+ }
1340
+
1341
+ class PersianHelper extends HelperBase {
1342
+ id = 'persian' as const;
1343
+ calendarType = 'solar' as const;
1344
+ inLeapYear(calendarDate: CalendarYearOnly, cache: OneObjectCache) {
1345
+ // If the last month has 30 days, it's a leap year.
1346
+ return this.daysInMonth({ year: calendarDate.year, month: 12, day: 1 }, cache) === 30;
1347
+ }
1348
+ monthsInYear(/* calendarYear, cache */) {
1349
+ return 12;
1350
+ }
1351
+ minimumMonthLength(calendarDate: CalendarYM) {
1352
+ const { month } = calendarDate;
1353
+ if (month === 12) return 29;
1354
+ return month <= 6 ? 31 : 30;
1355
+ }
1356
+ maximumMonthLength(calendarDate: CalendarYM) {
1357
+ const { month } = calendarDate;
1358
+ if (month === 12) return 30;
1359
+ return month <= 6 ? 31 : 30;
1360
+ }
1361
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1362
+ const month = nonLeapMonthCodeNumberPart(monthCode);
1363
+ return month <= 6 ? 31 : 30;
1364
+ }
1365
+ estimateIsoDate(calendarDate: CalendarYMD) {
1366
+ const { year } = this.adjustCalendarDate(calendarDate);
1367
+ return { year: year + 621, month: 1, day: 1 };
1368
+ }
1369
+ }
1370
+
1371
+ interface IndianMonthInfo {
1372
+ [month: number]: {
1373
+ length: number;
1374
+ month: number;
1375
+ day: number;
1376
+ leap?: {
1377
+ length: number;
1378
+ month: number;
1379
+ day: number;
1380
+ };
1381
+ nextYear?: true | undefined;
1382
+ };
1383
+ }
1384
+
1385
+ class IndianHelper extends HelperBase {
1386
+ id = 'indian' as const;
1387
+ calendarType = 'solar' as const;
1388
+ inLeapYear(calendarDate: CalendarYearOnly) {
1389
+ // From https://en.wikipedia.org/wiki/Indian_national_calendar:
1390
+ // Years are counted in the Saka era, which starts its year 0 in the year 78
1391
+ // of the Common Era. To determine leap years, add 78 to the Saka year – if
1392
+ // the result is a leap year in the Gregorian calendar, then the Saka year
1393
+ // is a leap year as well.
1394
+ return isGregorianLeapYear(calendarDate.year + 78);
1395
+ }
1396
+ monthsInYear(/* calendarYear, cache */) {
1397
+ return 12;
1398
+ }
1399
+ minimumMonthLength(calendarDate: CalendarYM) {
1400
+ return this.getMonthInfo(calendarDate).length;
1401
+ }
1402
+ maximumMonthLength(calendarDate: CalendarYM) {
1403
+ return this.getMonthInfo(calendarDate).length;
1404
+ }
1405
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1406
+ const month = nonLeapMonthCodeNumberPart(monthCode);
1407
+ let monthInfo = this.months[month];
1408
+ monthInfo = monthInfo.leap ?? monthInfo;
1409
+ return monthInfo.length;
1410
+ }
1411
+ // Indian months always start at the same well-known Gregorian month and
1412
+ // day. So this conversion is easy and fast. See
1413
+ // https://en.wikipedia.org/wiki/Indian_national_calendar
1414
+ months: IndianMonthInfo = {
1415
+ 1: { length: 30, month: 3, day: 22, leap: { length: 31, month: 3, day: 21 } },
1416
+ 2: { length: 31, month: 4, day: 21 },
1417
+ 3: { length: 31, month: 5, day: 22 },
1418
+ 4: { length: 31, month: 6, day: 22 },
1419
+ 5: { length: 31, month: 7, day: 23 },
1420
+ 6: { length: 31, month: 8, day: 23 },
1421
+ 7: { length: 30, month: 9, day: 23 },
1422
+ 8: { length: 30, month: 10, day: 23 },
1423
+ 9: { length: 30, month: 11, day: 22 },
1424
+ 10: { length: 30, month: 12, day: 22 },
1425
+ 11: { length: 30, month: 1, nextYear: true, day: 21 },
1426
+ 12: { length: 30, month: 2, nextYear: true, day: 20 }
1427
+ };
1428
+ getMonthInfo(calendarDate: CalendarYM) {
1429
+ const { month } = calendarDate;
1430
+ let monthInfo = this.months[month];
1431
+ if (monthInfo === undefined) throw new RangeError(`Invalid month: ${month}`);
1432
+ if (this.inLeapYear(calendarDate) && monthInfo.leap) monthInfo = monthInfo.leap;
1433
+ return monthInfo;
1434
+ }
1435
+ estimateIsoDate(calendarDateParam: CalendarYMD) {
1436
+ // FYI, this "estimate" is always the exact ISO date, which makes the Indian
1437
+ // calendar fast!
1438
+ const calendarDate = this.adjustCalendarDate(calendarDateParam);
1439
+ const monthInfo = this.getMonthInfo(calendarDate);
1440
+ const isoYear = calendarDate.year + 78 + (monthInfo.nextYear ? 1 : 0);
1441
+ const isoMonth = monthInfo.month;
1442
+ const isoDay = monthInfo.day;
1443
+ const isoDate = ES.BalanceISODate(isoYear, isoMonth, isoDay + calendarDate.day - 1);
1444
+ return isoDate;
1445
+ }
1446
+ // https://bugs.chromium.org/p/v8/issues/detail?id=10529 causes Intl's Indian
1447
+ // calendar output to fail for all dates before 0001-01-01 ISO. For example,
1448
+ // in Node 12 0000-01-01 is calculated as 6146/12/-583 instead of 10/11/-79 as
1449
+ // expected.
1450
+ vulnerableToBceBug =
1451
+ new Date('0000-01-01T00:00Z').toLocaleDateString('en-US-u-ca-indian', { timeZone: 'UTC' }) !== '10/11/-79 Saka';
1452
+ override checkIcuBugs(isoDate: ISODate) {
1453
+ if (this.vulnerableToBceBug && isoDate.year < 1) {
1454
+ throw new RangeError(
1455
+ `calendar '${this.id}' is broken for ISO dates before 0001-01-01` +
1456
+ ' (see https://bugs.chromium.org/p/v8/issues/detail?id=10529)'
1457
+ );
1458
+ }
1459
+ }
1460
+ }
1461
+
1462
+ /**
1463
+ * Era metadata defined for each calendar.
1464
+ * TODO: instead of optional properties, this should really have rules
1465
+ * encoded in the type, e.g. isoEpoch is required unless reverseOf is present.
1466
+ * */
1467
+ interface InputEra {
1468
+ /**
1469
+ * Era code, used to populate the 'era' field of Temporal instances.
1470
+ * See https://tc39.es/proposal-intl-era-monthcode/#table-eras
1471
+ */
1472
+ code: string;
1473
+
1474
+ /**
1475
+ * Names are additionally accepted as alternate era codes on input, and the
1476
+ * first name is also output in error messages (and may be the era code if
1477
+ * desired.)
1478
+ * See https://tc39.es/proposal-intl-era-monthcode/#table-eras
1479
+ * If absent, this field defaults to a single element matching the code.
1480
+ */
1481
+ names?: string[];
1482
+
1483
+ /**
1484
+ * Signed calendar year where this era begins.Will be
1485
+ * 1 (or 0 for zero-based eras) for the anchor era assuming that `year`
1486
+ * numbering starts at the beginning of the anchor era, which is true
1487
+ * for all ICU calendars except Japanese. If an era starts mid-year
1488
+ * then a calendar month and day are included. Otherwise
1489
+ * `{ month: 1, day: 1 }` is assumed.
1490
+ */
1491
+ anchorEpoch?: CalendarYearOnly | CalendarYMD;
1492
+
1493
+ /** ISO date of the first day of this era */
1494
+ isoEpoch?: { year: number; month: number; day: number };
1495
+
1496
+ /**
1497
+ * If present, then this era counts years backwards like BC
1498
+ * and this property points to the forward era. This must be
1499
+ * the last (oldest) era in the array.
1500
+ * */
1501
+ reverseOf?: string;
1502
+
1503
+ /**
1504
+ * If true, the era's years are 0-based. If omitted or false,
1505
+ * then the era's years are 1-based.
1506
+ * */
1507
+ hasYearZero?: boolean;
1508
+
1509
+ /**
1510
+ * Override if this era is the anchor. Not normally used because
1511
+ * anchor eras are inferred.
1512
+ * */
1513
+ isAnchor?: boolean;
1514
+ }
1515
+ /**
1516
+ * Transformation of the `InputEra` type with all fields filled in by
1517
+ * `adjustEras()`
1518
+ * */
1519
+ interface Era {
1520
+ /**
1521
+ * Era code, used to populate the 'era' field of Temporal instances.
1522
+ * See https://tc39.es/proposal-intl-era-monthcode/#table-eras
1523
+ */
1524
+ code: string;
1525
+
1526
+ /**
1527
+ * Names are additionally accepted as alternate era codes on input, and the
1528
+ * first name is also output in error messages (and may be the era code if
1529
+ * desired.)
1530
+ * See https://tc39.es/proposal-intl-era-monthcode/#table-eras
1531
+ * If absent, this field defaults to a single element matching the code.
1532
+ */
1533
+ names: string[];
1534
+
1535
+ /**
1536
+ * alternate name of the era used in old versions of ICU data
1537
+ * format is `era{n}` where n is the zero-based index of the era
1538
+ * with the oldest era being 0.
1539
+ * */
1540
+ genericName: string;
1541
+
1542
+ /**
1543
+ * Signed calendar year where this era begins. Will be 1 (or 0 for zero-based
1544
+ * eras) for the anchor era assuming that `year` numbering starts at the
1545
+ * beginning of the anchor era, which is true for all ICU calendars except
1546
+ * Japanese. For input, the month and day are optional. If an era starts
1547
+ * mid-year then a calendar month and day are included.
1548
+ * Otherwise `{ month: 1, day: 1 }` is assumed.
1549
+ */
1550
+ anchorEpoch: CalendarYMD;
1551
+
1552
+ /** ISO date of the first day of this era */
1553
+ isoEpoch: ISODate;
1554
+
1555
+ /**
1556
+ * If present, then this era counts years backwards like BC
1557
+ * and this property points to the forward era. This must be
1558
+ * the last (oldest) era in the array.
1559
+ * */
1560
+ reverseOf?: Era;
1561
+
1562
+ /**
1563
+ * If true, the era's years are 0-based. If omitted or false,
1564
+ * then the era's years are 1-based.
1565
+ * */
1566
+ hasYearZero?: boolean;
1567
+
1568
+ /**
1569
+ * Override if this era is the anchor. Not normally used because
1570
+ * anchor eras are inferred.
1571
+ * */
1572
+ isAnchor?: boolean;
1573
+ }
1574
+
1575
+ /**
1576
+ * This function adds additional metadata that makes it easier to work with
1577
+ * eras. Note that it mutates and normalizes the original era objects, which is
1578
+ * OK because this is non-observable, internal-only metadata.
1579
+ *
1580
+ * The result is an array of eras with the shape defined above.
1581
+ * */
1582
+ function adjustEras(erasParam: InputEra[]): { eras: Era[]; anchorEra: Era } {
1583
+ let eras: (InputEra | Era)[] = erasParam;
1584
+ if (eras.length === 0) {
1585
+ throw new RangeError('Invalid era data: eras are required');
1586
+ }
1587
+ if (eras.length === 1 && eras[0].reverseOf) {
1588
+ throw new RangeError('Invalid era data: anchor era cannot count years backwards');
1589
+ }
1590
+ if (eras.length === 1 && !eras[0].code) {
1591
+ throw new RangeError('Invalid era data: at least one named era is required');
1592
+ }
1593
+ if (eras.filter((e) => e.reverseOf != null).length > 1) {
1594
+ throw new RangeError('Invalid era data: only one era can count years backwards');
1595
+ }
1596
+
1597
+ // Find the "anchor era" which is the era used for (era-less) `year`. Reversed
1598
+ // eras can never be anchors. The era without an `anchorEpoch` property is the
1599
+ // anchor.
1600
+ let anchorEra: Era | InputEra | undefined;
1601
+ eras.forEach((e) => {
1602
+ if (e.isAnchor || (!e.anchorEpoch && !e.reverseOf)) {
1603
+ if (anchorEra) throw new RangeError('Invalid era data: cannot have multiple anchor eras');
1604
+ anchorEra = e;
1605
+ e.anchorEpoch = { year: e.hasYearZero ? 0 : 1 };
1606
+ } else if (!e.code) {
1607
+ throw new RangeError('If era name is blank, it must be the anchor era');
1608
+ }
1609
+ });
1610
+
1611
+ // If the era name is undefined, then it's an anchor that doesn't interact
1612
+ // with eras at all. For example, Japanese `year` is always the same as ISO
1613
+ // `year`. So this "era" is the anchor era but isn't used for era matching.
1614
+ // Strip it from the list that's returned.
1615
+ eras = eras.filter((e) => e.code);
1616
+
1617
+ eras.forEach((e) => {
1618
+ // Some eras are mirror images of another era e.g. B.C. is the reverse of A.D.
1619
+ // Replace the string-valued "reverseOf" property with the actual era object
1620
+ // that's reversed.
1621
+ const { reverseOf } = e;
1622
+ if (reverseOf) {
1623
+ const reversedEra = eras.find((era) => era.code === reverseOf);
1624
+ if (reversedEra === undefined) {
1625
+ throw new RangeError(`Invalid era data: unmatched reverseOf era: ${reverseOf}`);
1626
+ }
1627
+ e.reverseOf = reversedEra as Era; // genericName property added later
1628
+ e.anchorEpoch = reversedEra.anchorEpoch;
1629
+ e.isoEpoch = reversedEra.isoEpoch;
1630
+ }
1631
+ type YMD = {
1632
+ year: number;
1633
+ month: number;
1634
+ day: number;
1635
+ };
1636
+ if ((e.anchorEpoch as YMD).month === undefined) (e.anchorEpoch as YMD).month = 1;
1637
+ if ((e.anchorEpoch as YMD).day === undefined) (e.anchorEpoch as YMD).day = 1;
1638
+ });
1639
+
1640
+ // Ensure that the latest epoch is first in the array. This lets us try to
1641
+ // match eras in index order, with the last era getting the remaining older
1642
+ // years. Any reverse-signed era must be at the end.
1643
+ eras.sort((e1, e2) => {
1644
+ if (e1.reverseOf) return 1;
1645
+ if (e2.reverseOf) return -1;
1646
+ if (!e1.isoEpoch || !e2.isoEpoch) throw new RangeError('Invalid era data: missing ISO epoch');
1647
+ return e2.isoEpoch.year - e1.isoEpoch.year;
1648
+ });
1649
+
1650
+ // If there's a reversed era, then the one before it must be the era that's
1651
+ // being reversed.
1652
+ const lastEraReversed = eras[eras.length - 1].reverseOf;
1653
+ if (lastEraReversed) {
1654
+ if (lastEraReversed !== eras[eras.length - 2]) {
1655
+ throw new RangeError('Invalid era data: invalid reverse-sign era');
1656
+ }
1657
+ }
1658
+
1659
+ // Finally, add a "genericName" property in the format "era{n} where `n` is
1660
+ // zero-based index, with the oldest era being zero. This format is used by
1661
+ // older versions of ICU data.
1662
+ eras.forEach((e, i) => {
1663
+ (e as Era).genericName = `era${eras.length - 1 - i}`;
1664
+ });
1665
+
1666
+ return { eras: eras as Era[], anchorEra: (anchorEra || eras[0]) as Era };
1667
+ }
1668
+
1669
+ function isGregorianLeapYear(year: number) {
1670
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
1671
+ }
1672
+
1673
+ /** Base for all Gregorian-like calendars. */
1674
+ abstract class GregorianBaseHelperFixedEpoch extends HelperBase {
1675
+ id: BuiltinCalendarId;
1676
+ isoEpoch: ISODate;
1677
+
1678
+ constructor(id: BuiltinCalendarId, isoEpoch: ISODate) {
1679
+ super();
1680
+ this.id = id;
1681
+ this.isoEpoch = isoEpoch;
1682
+ }
1683
+ calendarType = 'solar' as const;
1684
+ inLeapYear(calendarDate: CalendarYearOnly) {
1685
+ const { year } = this.estimateIsoDate({ month: 1, day: 1, year: calendarDate.year });
1686
+ return isGregorianLeapYear(year);
1687
+ }
1688
+ monthsInYear(/* calendarDate */) {
1689
+ return 12;
1690
+ }
1691
+ minimumMonthLength(calendarDate: CalendarYM): number {
1692
+ const { month } = calendarDate;
1693
+ if (month === 2) return this.inLeapYear(calendarDate) ? 29 : 28;
1694
+ return [4, 6, 9, 11].indexOf(month) >= 0 ? 30 : 31;
1695
+ }
1696
+ maximumMonthLength(calendarDate: CalendarYM): number {
1697
+ return this.minimumMonthLength(calendarDate);
1698
+ }
1699
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1700
+ const month = nonLeapMonthCodeNumberPart(monthCode);
1701
+ return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
1702
+ }
1703
+ estimateIsoDate(calendarDateParam: CalendarYMD) {
1704
+ const calendarDate = this.adjustCalendarDate(calendarDateParam);
1705
+ return ES.RegulateISODate(
1706
+ calendarDate.year + this.isoEpoch.year,
1707
+ calendarDate.month + this.isoEpoch.month,
1708
+ calendarDate.day + this.isoEpoch.day,
1709
+ 'constrain'
1710
+ );
1711
+ }
1712
+ }
1713
+
1714
+ /** Base for Gregorian-like calendars with eras. */
1715
+ abstract class GregorianBaseHelper extends HelperBase {
1716
+ id: BuiltinCalendarId;
1717
+ anchorEra: Era;
1718
+
1719
+ constructor(id: BuiltinCalendarId, originalEras: InputEra[]) {
1720
+ super();
1721
+ this.id = id;
1722
+ const { eras, anchorEra } = adjustEras(originalEras);
1723
+ this.anchorEra = anchorEra;
1724
+ this.eras = eras;
1725
+ }
1726
+ override hasEra = true;
1727
+ calendarType = 'solar' as const;
1728
+ inLeapYear(calendarDate: CalendarYearOnly) {
1729
+ // Calendars that don't override this method use the same months and leap
1730
+ // years as Gregorian. Once we know the ISO year corresponding to the
1731
+ // calendar year, we'll know if it's a leap year or not.
1732
+ const { year } = this.estimateIsoDate({ month: 1, day: 1, year: calendarDate.year });
1733
+ return isGregorianLeapYear(year);
1734
+ }
1735
+ monthsInYear(/* calendarDate */) {
1736
+ return 12;
1737
+ }
1738
+ minimumMonthLength(calendarDate: CalendarYM): number {
1739
+ const { month } = calendarDate;
1740
+ if (month === 2) return this.inLeapYear(calendarDate) ? 29 : 28;
1741
+ return [4, 6, 9, 11].indexOf(month) >= 0 ? 30 : 31;
1742
+ }
1743
+ maximumMonthLength(calendarDate: CalendarYM): number {
1744
+ return this.minimumMonthLength(calendarDate);
1745
+ }
1746
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1747
+ const month = nonLeapMonthCodeNumberPart(monthCode);
1748
+ return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
1749
+ }
1750
+ /** Fill in missing parts of the (year, era, eraYear) tuple */
1751
+ completeEraYear(
1752
+ calendarDate: FullCalendarDate
1753
+ ): FullCalendarDate & Required<Pick<FullCalendarDate, 'era' | 'eraYear'>> {
1754
+ const checkField = (property: keyof FullCalendarDate, value: string | number | undefined, names?: string[]) => {
1755
+ const currentValue = calendarDate[property];
1756
+ if (currentValue != null && currentValue != value && !((names || []) as unknown[]).includes(currentValue)) {
1757
+ // Prefer displaying an era alias, instead of "gregory-inverse"
1758
+ const preferredName = names?.[0];
1759
+ const expected = preferredName ? `${value} (also called ${preferredName})` : value;
1760
+ throw new RangeError(`Input ${property} ${currentValue} doesn't match calculated value ${expected}`);
1761
+ }
1762
+ };
1763
+ const eraFromYear = (year: number) => {
1764
+ let eraYear;
1765
+ const adjustedCalendarDate = { ...calendarDate, year };
1766
+ const matchingEra = this.eras.find((e, i) => {
1767
+ if (i === this.eras.length - 1) {
1768
+ if (e.reverseOf) {
1769
+ // This is a reverse-sign era (like BCE) which must be the oldest
1770
+ // era. Count years backwards.
1771
+ if (year > 0) throw new RangeError(`Signed year ${year} is invalid for era ${e.code}`);
1772
+ eraYear = e.anchorEpoch.year - year;
1773
+ return true;
1774
+ }
1775
+ // last era always gets all "leftover" (older than epoch) years,
1776
+ // so no need for a comparison like below.
1777
+ eraYear = year - e.anchorEpoch.year + (e.hasYearZero ? 0 : 1);
1778
+ return true;
1779
+ }
1780
+ const comparison = this.compareCalendarDates(adjustedCalendarDate, e.anchorEpoch);
1781
+ if (comparison >= 0) {
1782
+ eraYear = year - e.anchorEpoch.year + (e.hasYearZero ? 0 : 1);
1783
+ return true;
1784
+ }
1785
+ return false;
1786
+ });
1787
+ if (!matchingEra) throw new RangeError(`Year ${year} was not matched by any era`);
1788
+ return { eraYear: eraYear as unknown as number, era: matchingEra.code, eraNames: matchingEra.names };
1789
+ };
1790
+
1791
+ let { year, eraYear, era } = calendarDate;
1792
+ if (year != null) {
1793
+ const matchData = eraFromYear(year);
1794
+ ({ eraYear, era } = matchData);
1795
+ checkField('era', era, matchData?.eraNames);
1796
+ checkField('eraYear', eraYear);
1797
+ } else if (eraYear != null) {
1798
+ if (era === undefined) throw new RangeError('era and eraYear must be provided together');
1799
+ // TS limitation: https://github.com/microsoft/TypeScript/issues/11498
1800
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1801
+ const matchingEra = this.eras.find(({ code, names = [] }) => code === era || names.includes(era!));
1802
+ if (!matchingEra) throw new RangeError(`Era ${era} (ISO year ${eraYear}) was not matched by any era`);
1803
+ if (matchingEra.reverseOf) {
1804
+ year = matchingEra.anchorEpoch.year - eraYear;
1805
+ } else {
1806
+ year = eraYear + matchingEra.anchorEpoch.year - (matchingEra.hasYearZero ? 0 : 1);
1807
+ }
1808
+ checkField('year', year);
1809
+ // We'll accept dates where the month/day is earlier than the start of
1810
+ // the era or after its end as long as it's in the same year. If that
1811
+ // happens, we'll adjust the era/eraYear pair to be the correct era for
1812
+ // the `year`.
1813
+ ({ eraYear, era } = eraFromYear(year));
1814
+ } else {
1815
+ throw new RangeError('Either year or eraYear and era are required');
1816
+ }
1817
+ return { ...calendarDate, year, eraYear, era };
1818
+ }
1819
+ override adjustCalendarDate(
1820
+ calendarDateParam: Partial<FullCalendarDate>,
1821
+ cache?: OneObjectCache,
1822
+ overflow: Overflow = 'constrain'
1823
+ ): FullCalendarDate {
1824
+ let calendarDate = calendarDateParam;
1825
+ // Because this is not a lunisolar calendar, it's safe to convert monthCode to a number
1826
+ const { month, monthCode } = calendarDate;
1827
+ if (month === undefined) calendarDate = { ...calendarDate, month: nonLeapMonthCodeNumberPart(monthCode as string) };
1828
+ this.validateCalendarDate(calendarDate);
1829
+ calendarDate = this.completeEraYear(calendarDate);
1830
+ return super.adjustCalendarDate(calendarDate, cache, overflow);
1831
+ }
1832
+ estimateIsoDate(calendarDateParam: CalendarYMD) {
1833
+ const calendarDate = this.adjustCalendarDate(calendarDateParam);
1834
+ const { year, month, day } = calendarDate;
1835
+ const { anchorEra } = this;
1836
+ const isoYearEstimate = year + anchorEra.isoEpoch.year - (anchorEra.hasYearZero ? 0 : 1);
1837
+ return ES.RegulateISODate(isoYearEstimate, month, day, 'constrain');
1838
+ }
1839
+ }
1840
+
1841
+ /**
1842
+ * Some calendars are identical to Gregorian except era and year. For these
1843
+ * calendars, we can avoid using Intl.DateTimeFormat and just calculate the
1844
+ * year, era, and eraYear. This is faster (because Intl.DateTimeFormat is slow
1845
+ * and uses a huge amount of RAM), and it avoids ICU bugs like
1846
+ * https://bugs.chromium.org/p/chromium/issues/detail?id=1173158.
1847
+ */
1848
+ abstract class SameMonthDayAsGregorianBaseHelper extends GregorianBaseHelper {
1849
+ constructor(id: BuiltinCalendarId, originalEras: InputEra[]) {
1850
+ super(id, originalEras);
1851
+ }
1852
+ override isoToCalendarDate(isoDate: ISODate): FullCalendarDate {
1853
+ // Month and day are same as ISO, so bypass Intl.DateTimeFormat and
1854
+ // calculate the year, era, and eraYear here.
1855
+ const { year: isoYear, month, day } = isoDate;
1856
+ const monthCode = buildMonthCode(month);
1857
+ const year = isoYear - this.anchorEra.isoEpoch.year + 1;
1858
+ return this.completeEraYear({ year, month, monthCode, day });
1859
+ }
1860
+ }
1861
+ const OrthodoxOps = {
1862
+ inLeapYear(calendarDate: CalendarYearOnly) {
1863
+ // Leap years happen one year before the Julian leap year. Note that this
1864
+ // calendar is based on the Julian calendar which has a leap year every 4
1865
+ // years, unlike the Gregorian calendar which doesn't have leap years on
1866
+ // years divisible by 100 except years divisible by 400.
1867
+ //
1868
+ // Note that we're assuming that leap years in before-epoch times match
1869
+ // how leap years are defined now. This is probably not accurate but I'm
1870
+ // not sure how better to do it.
1871
+ const { year } = calendarDate;
1872
+ return (year + 1) % 4 === 0;
1873
+ },
1874
+ monthsInYear(/* calendarDate */) {
1875
+ return 13;
1876
+ },
1877
+ minimumMonthLength(calendarDate: CalendarYM) {
1878
+ const { month } = calendarDate;
1879
+ // Ethiopian/Coptic calendars have 12 30-day months and an extra 5-6 day 13th month.
1880
+ if (month === 13) return this.inLeapYear(calendarDate) ? 6 : 5;
1881
+ return 30;
1882
+ },
1883
+ maximumMonthLength(calendarDate: CalendarYM) {
1884
+ return this.minimumMonthLength(calendarDate);
1885
+ },
1886
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
1887
+ return monthCode === 'M13' ? 6 : 30;
1888
+ }
1889
+ };
1890
+ abstract class OrthodoxBaseHelperFixedEpoch extends GregorianBaseHelperFixedEpoch {
1891
+ constructor(id: BuiltinCalendarId, isoEpoch: ISODate) {
1892
+ super(id, isoEpoch);
1893
+ }
1894
+ override inLeapYear = OrthodoxOps.inLeapYear;
1895
+ override monthsInYear = OrthodoxOps.monthsInYear;
1896
+ override minimumMonthLength = OrthodoxOps.minimumMonthLength;
1897
+ override maximumMonthLength = OrthodoxOps.maximumMonthLength;
1898
+ override maxLengthOfMonthCodeInAnyYear = OrthodoxOps.maxLengthOfMonthCodeInAnyYear;
1899
+ }
1900
+ abstract class OrthodoxBaseHelper extends GregorianBaseHelper {
1901
+ constructor(id: BuiltinCalendarId, originalEras: InputEra[]) {
1902
+ super(id, originalEras);
1903
+ }
1904
+ override inLeapYear = OrthodoxOps.inLeapYear;
1905
+ override monthsInYear = OrthodoxOps.monthsInYear;
1906
+ override minimumMonthLength = OrthodoxOps.minimumMonthLength;
1907
+ override maximumMonthLength = OrthodoxOps.maximumMonthLength;
1908
+ override maxLengthOfMonthCodeInAnyYear = OrthodoxOps.maxLengthOfMonthCodeInAnyYear;
1909
+ }
1910
+
1911
+ // `coptic` and `ethiopic` calendars are very similar to `ethioaa` calendar,
1912
+ // with the following differences:
1913
+ // - Coptic uses BCE-like positive numbers for years before its epoch (the other
1914
+ // two use negative year numbers before epoch)
1915
+ // - Coptic has a different epoch date
1916
+ // - Ethiopic has an additional second era that starts at the same date as the
1917
+ // zero era of ethioaa.
1918
+ class EthioaaHelper extends OrthodoxBaseHelperFixedEpoch {
1919
+ constructor() {
1920
+ super('ethioaa', { year: -5492, month: 7, day: 17 });
1921
+ }
1922
+ }
1923
+ class CopticHelper extends OrthodoxBaseHelper {
1924
+ constructor() {
1925
+ super('coptic', [
1926
+ { code: 'coptic', isoEpoch: { year: 284, month: 8, day: 29 } },
1927
+ { code: 'coptic-inverse', reverseOf: 'coptic' }
1928
+ ]);
1929
+ }
1930
+ }
1931
+
1932
+ // Anchor is currently the older era to match ethioaa, but should it be the newer era?
1933
+ // See https://github.com/tc39/ecma402/issues/534 for discussion.
1934
+ class EthiopicHelper extends OrthodoxBaseHelper {
1935
+ constructor() {
1936
+ super('ethiopic', [
1937
+ { code: 'ethioaa', names: ['ethiopic-amete-alem', 'mundi'], isoEpoch: { year: -5492, month: 7, day: 17 } },
1938
+ { code: 'ethiopic', names: ['incar'], isoEpoch: { year: 8, month: 8, day: 27 }, anchorEpoch: { year: 5501 } }
1939
+ ]);
1940
+ }
1941
+ }
1942
+
1943
+ class RocHelper extends SameMonthDayAsGregorianBaseHelper {
1944
+ constructor() {
1945
+ super('roc', [
1946
+ { code: 'roc', names: ['minguo'], isoEpoch: { year: 1912, month: 1, day: 1 } },
1947
+ { code: 'roc-inverse', names: ['before-roc'], reverseOf: 'roc' }
1948
+ ]);
1949
+ }
1950
+ }
1951
+
1952
+ class BuddhistHelper extends GregorianBaseHelperFixedEpoch {
1953
+ constructor() {
1954
+ super('buddhist', { year: -543, month: 1, day: 1 });
1955
+ }
1956
+ }
1957
+
1958
+ class GregoryHelper extends SameMonthDayAsGregorianBaseHelper {
1959
+ constructor() {
1960
+ super('gregory', [
1961
+ { code: 'gregory', names: ['ad', 'ce'], isoEpoch: { year: 1, month: 1, day: 1 } },
1962
+ { code: 'gregory-inverse', names: ['be', 'bce'], reverseOf: 'gregory' }
1963
+ ]);
1964
+ }
1965
+ override reviseIntlEra<T extends Partial<EraAndEraYear>>(calendarDate: T /*, isoDate: IsoDate*/): T {
1966
+ let { era, eraYear } = calendarDate;
1967
+ // Firefox 96 introduced a bug where the `'short'` format of the era
1968
+ // option mistakenly returns the one-letter (narrow) format instead. The
1969
+ // code below handles either the correct or Firefox-buggy format. See
1970
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1752253
1971
+ if (era === 'b') era = 'gregory-inverse';
1972
+ if (era === 'a') era = 'gregory';
1973
+ return { era, eraYear } as T;
1974
+ }
1975
+ override getFirstDayOfWeek() {
1976
+ return 1;
1977
+ }
1978
+ override getMinimalDaysInFirstWeek() {
1979
+ return 1;
1980
+ }
1981
+ }
1982
+
1983
+ // NOTE: Only the 5 modern eras (Meiji and later) are included. For dates
1984
+ // before Meiji 1, the `ce` and `bce` eras are used. Challenges with pre-Meiji
1985
+ // eras include:
1986
+ // - Start/end dates of older eras are not precisely defined, which is
1987
+ // challenging given Temporal's need for precision
1988
+ // - Some era dates and/or names are disputed by historians
1989
+ // - As historical research proceeds, new eras are discovered and existing era
1990
+ // dates are modified, leading to considerable churn which is not good for
1991
+ // Temporal use.
1992
+ // - The earliest era (in 645 CE) may not end up being the earliest depending
1993
+ // on future historical scholarship
1994
+ // - Before Meiji, Japan used a lunar (or lunisolar?) calendar but AFAIK
1995
+ // that's not reflected in the ICU implementation.
1996
+ //
1997
+ // For more discussion: https://github.com/tc39/proposal-temporal/issues/526.
1998
+ //
1999
+ // Here's a full list of CLDR/ICU eras:
2000
+ // https://github.com/unicode-org/icu/blob/master/icu4c/source/data/locales/root.txt#L1582-L1818
2001
+ // https://github.com/unicode-org/cldr/blob/master/common/supplemental/supplementalData.xml#L4310-L4546
2002
+ //
2003
+ // NOTE: Japan started using the Gregorian calendar in 6 Meiji, replacing a
2004
+ // lunisolar calendar. So the day before January 1 of 6 Meiji (1873) was not
2005
+ // December 31, but December 2, of 5 Meiji (1872). The existing Ecma-402
2006
+ // Japanese calendar doesn't seem to take this into account, so neither do we:
2007
+ // > args = ['en-ca-u-ca-japanese', { era: 'short' }]
2008
+ // > new Date('1873-01-01T12:00').toLocaleString(...args)
2009
+ // '1 1, 6 Meiji, 12:00:00 PM'
2010
+ // > new Date('1872-12-31T12:00').toLocaleString(...args)
2011
+ // '12 31, 5 Meiji, 12:00:00 PM'
2012
+ class JapaneseHelper extends SameMonthDayAsGregorianBaseHelper {
2013
+ constructor() {
2014
+ super('japanese', [
2015
+ // The Japanese calendar `year` is just the ISO year, because (unlike other
2016
+ // ICU calendars) there's no obvious "default era", we use the ISO year.
2017
+ { code: 'reiwa', isoEpoch: { year: 2019, month: 5, day: 1 }, anchorEpoch: { year: 2019, month: 5, day: 1 } },
2018
+ { code: 'heisei', isoEpoch: { year: 1989, month: 1, day: 8 }, anchorEpoch: { year: 1989, month: 1, day: 8 } },
2019
+ { code: 'showa', isoEpoch: { year: 1926, month: 12, day: 25 }, anchorEpoch: { year: 1926, month: 12, day: 25 } },
2020
+ { code: 'taisho', isoEpoch: { year: 1912, month: 7, day: 30 }, anchorEpoch: { year: 1912, month: 7, day: 30 } },
2021
+ { code: 'meiji', isoEpoch: { year: 1868, month: 9, day: 8 }, anchorEpoch: { year: 1868, month: 9, day: 8 } },
2022
+ { code: 'japanese', names: ['japanese', 'gregory', 'ad', 'ce'], isoEpoch: { year: 1, month: 1, day: 1 } },
2023
+ { code: 'japanese-inverse', names: ['japanese-inverse', 'gregory-inverse', 'bc', 'bce'], reverseOf: 'japanese' }
2024
+ ]);
2025
+ }
2026
+
2027
+ override erasBeginMidYear = true;
2028
+
2029
+ override reviseIntlEra<T extends Partial<EraAndEraYear>>(calendarDate: T, isoDate: ISODate): T {
2030
+ const { era, eraYear } = calendarDate;
2031
+ const { year: isoYear } = isoDate;
2032
+ if (this.eras.find((e) => e.code === era)) return { era, eraYear } as T;
2033
+ return (
2034
+ isoYear < 1 ? { era: 'japanese-inverse', eraYear: 1 - isoYear } : { era: 'japanese', eraYear: isoYear }
2035
+ ) as T;
2036
+ }
2037
+ }
2038
+
2039
+ interface ChineseMonthInfo {
2040
+ [key: string]: { monthIndex: number; daysInMonth: number };
2041
+ }
2042
+ interface ChineseDraftMonthInfo {
2043
+ [key: string]: { monthIndex: number; daysInMonth?: number };
2044
+ }
2045
+
2046
+ abstract class ChineseBaseHelper extends HelperBase {
2047
+ abstract override id: BuiltinCalendarId;
2048
+ calendarType = 'lunisolar' as const;
2049
+ inLeapYear(calendarDate: CalendarYearOnly, cache: OneObjectCache) {
2050
+ const months = this.getMonthList(calendarDate.year, cache);
2051
+ return Object.entries(months).length === 13;
2052
+ }
2053
+ monthsInYear(calendarDate: CalendarYearOnly, cache: OneObjectCache) {
2054
+ return this.inLeapYear(calendarDate, cache) ? 13 : 12;
2055
+ }
2056
+ minimumMonthLength(/* calendarDate */) {
2057
+ return 29;
2058
+ }
2059
+ maximumMonthLength(/* calendarDate */) {
2060
+ return 30;
2061
+ }
2062
+ maxLengthOfMonthCodeInAnyYear(monthCode: string) {
2063
+ // See note below about ICU4C vs ICU4X. It is possible this override should
2064
+ // always return 30.
2065
+ return ['M01L', 'M09L', 'M10L', 'M11L', 'M12L'].includes(monthCode) ? 29 : 30;
2066
+ }
2067
+ override monthDaySearchStartYear(monthCode: string, day: number) {
2068
+ // Note that ICU4C actually has _no_ years in which leap months M01L and
2069
+ // M09L through M12L have 30 days. The values marked with (*) here are years
2070
+ // in which the leap month occurs with 29 days. ICU4C disagrees with ICU4X
2071
+ // here and it is not clear which is correct.
2072
+ const monthMap: Record<string, [number, number]> = {
2073
+ M01L: [1651, 1651], // *
2074
+ M02L: [1947, 1765],
2075
+ M03L: [1966, 1955],
2076
+ M04L: [1963, 1944],
2077
+ M05L: [1971, 1952],
2078
+ M06L: [1960, 1941],
2079
+ M07L: [1968, 1938],
2080
+ M08L: [1957, 1718],
2081
+ M09L: [1832, 1832], // *
2082
+ M10L: [1870, 1870], // *
2083
+ M11L: [1814, 1814], // *
2084
+ M12L: [1890, 1890] // *
2085
+ };
2086
+ const years = monthMap[monthCode] ?? [1972, 1972];
2087
+ return day < 30 ? years[0] : years[1];
2088
+ }
2089
+ getMonthList(calendarYear: number, cache: OneObjectCache): ChineseMonthInfo {
2090
+ if (calendarYear === undefined) {
2091
+ throw new TypeError('Missing year');
2092
+ }
2093
+ const key = JSON.stringify({ func: 'getMonthList', calendarYear, id: this.id });
2094
+ const cached = cache.get(key);
2095
+ if (cached) return cached;
2096
+ const dateTimeFormat = this.getFormatter();
2097
+ const getCalendarDate = (isoYear: number, daysPastFeb1: number) => {
2098
+ const isoStringFeb1 = toUtcIsoDateString({ isoYear, isoMonth: 2, isoDay: 1 });
2099
+ const legacyDate = new Date(isoStringFeb1);
2100
+ // Now add the requested number of days, which may wrap to the next month.
2101
+ legacyDate.setUTCDate(daysPastFeb1 + 1);
2102
+ const newYearGuess = dateTimeFormat.formatToParts(legacyDate);
2103
+ // The 'month' and 'day' parts are guaranteed to be present because the
2104
+ // formatter was created with month and day options.
2105
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2106
+ const calendarMonthString = newYearGuess.find((tv) => tv.type === 'month')!.value;
2107
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2108
+ const calendarDay = +newYearGuess.find((tv) => tv.type === 'day')!.value;
2109
+ const calendarYearPartToVerify = newYearGuess.find((tv) => (tv.type as string) === 'relatedYear');
2110
+ let calendarYearToVerify: number | undefined;
2111
+ if (calendarYearPartToVerify !== undefined) {
2112
+ calendarYearToVerify = +calendarYearPartToVerify.value;
2113
+ } else {
2114
+ // Node 12 has outdated ICU data that lacks the `relatedYear` field in the
2115
+ // output of Intl.DateTimeFormat.formatToParts.
2116
+ throw new RangeError(
2117
+ `Intl.DateTimeFormat.formatToParts lacks relatedYear in ${this.id} calendar. Try Node 14+ or modern browsers.`
2118
+ );
2119
+ }
2120
+ return { calendarMonthString, calendarDay, calendarYearToVerify };
2121
+ };
2122
+
2123
+ // First, find a date close to Chinese New Year. Feb 17 will either be in
2124
+ // the first month or near the end of the last month of the previous year.
2125
+ let isoDaysDelta = 17;
2126
+ let { calendarMonthString, calendarDay, calendarYearToVerify } = getCalendarDate(calendarYear, isoDaysDelta);
2127
+
2128
+ // If we didn't guess the first month correctly, add (almost in some months)
2129
+ // a lunar month
2130
+ if (calendarMonthString !== '1') {
2131
+ isoDaysDelta += 29;
2132
+ ({ calendarMonthString, calendarDay } = getCalendarDate(calendarYear, isoDaysDelta));
2133
+ }
2134
+
2135
+ // Now back up to near the start of the first month, but not too near that
2136
+ // off-by-one issues matter.
2137
+ isoDaysDelta -= calendarDay - 5;
2138
+ const result = {} as ChineseDraftMonthInfo;
2139
+ let monthIndex = 1;
2140
+ let oldCalendarDay: number | undefined;
2141
+ let oldMonthString: string | undefined;
2142
+ let done = false;
2143
+ do {
2144
+ ({ calendarMonthString, calendarDay, calendarYearToVerify } = getCalendarDate(calendarYear, isoDaysDelta));
2145
+ if (oldCalendarDay) {
2146
+ result[oldMonthString as string].daysInMonth = oldCalendarDay + 30 - calendarDay;
2147
+ }
2148
+ if (calendarYearToVerify !== calendarYear) {
2149
+ done = true;
2150
+ } else {
2151
+ result[calendarMonthString] = { monthIndex: monthIndex++ };
2152
+ // Move to the next month. Because months are sometimes 29 days, the day of the
2153
+ // calendar month will move forward slowly but not enough to flip over to a new
2154
+ // month before the loop ends at 12-13 months.
2155
+ isoDaysDelta += 30;
2156
+ }
2157
+ oldCalendarDay = calendarDay;
2158
+ oldMonthString = calendarMonthString;
2159
+ } while (!done);
2160
+ result[oldMonthString].daysInMonth = oldCalendarDay + 30 - calendarDay;
2161
+
2162
+ cache.set(key, result);
2163
+ return result as ChineseMonthInfo;
2164
+ }
2165
+ estimateIsoDate(calendarDate: CalendarYMD) {
2166
+ const { year, month } = calendarDate;
2167
+ return { year, month: month >= 12 ? 12 : month + 1, day: 1 };
2168
+ }
2169
+ override adjustCalendarDate(
2170
+ calendarDate: Partial<FullCalendarDate>,
2171
+ cache: OneObjectCache,
2172
+ overflow: Overflow = 'constrain',
2173
+ fromLegacyDate = false
2174
+ ): FullCalendarDate {
2175
+ let { year, month, monthExtra, day, monthCode } = calendarDate;
2176
+ if (year === undefined) throw new TypeError('Missing property: year');
2177
+ if (fromLegacyDate) {
2178
+ // Legacy Date output returns a string that's an integer with an optional
2179
+ // "bis" suffix used only by the Chinese/Dangi calendar to indicate a leap
2180
+ // month. Below we'll normalize the output.
2181
+ if (monthExtra && monthExtra !== 'bis') throw new RangeError(`Unexpected leap month suffix: ${monthExtra}`);
2182
+ const monthCode = buildMonthCode(month as number, monthExtra !== undefined);
2183
+ const monthString = `${month}${monthExtra || ''}`;
2184
+ const months = this.getMonthList(year, cache);
2185
+ const monthInfo = months[monthString];
2186
+ if (monthInfo === undefined) throw new RangeError(`Unmatched month ${monthString} in Chinese year ${year}`);
2187
+ month = monthInfo.monthIndex;
2188
+ return { year, month, day: day as number, monthCode };
2189
+ } else {
2190
+ // When called without input coming from legacy Date output,
2191
+ // simply ensure that all fields are present.
2192
+ this.validateCalendarDate(calendarDate);
2193
+ if (month === undefined) {
2194
+ ES.assertExists(monthCode);
2195
+ const months = this.getMonthList(year, cache);
2196
+ let numberPart = monthCode.replace(/^M|L$/g, (ch) => (ch === 'L' ? 'bis' : ''));
2197
+ if (numberPart[0] === '0') numberPart = numberPart.slice(1);
2198
+ let monthInfo = months[numberPart];
2199
+ month = monthInfo && monthInfo.monthIndex;
2200
+
2201
+ // If this leap month isn't present in this year, constrain to the same
2202
+ // day of the previous month.
2203
+ if (month === undefined && monthCode.endsWith('L') && monthCode != 'M13L' && overflow === 'constrain') {
2204
+ const withoutML = +monthCode.replace(/^M0?|L$/g, '');
2205
+ monthInfo = months[withoutML];
2206
+ if (monthInfo) {
2207
+ month = monthInfo.monthIndex;
2208
+ monthCode = buildMonthCode(withoutML);
2209
+ }
2210
+ }
2211
+ if (month === undefined) {
2212
+ throw new RangeError(`Unmatched month ${monthCode} in Chinese year ${year}`);
2213
+ }
2214
+ } else if (monthCode === undefined) {
2215
+ const months = this.getMonthList(year, cache);
2216
+ const monthEntries = Object.entries(months);
2217
+ const largestMonth = monthEntries.length;
2218
+ if (overflow === 'reject') {
2219
+ ES.RejectToRange(month, 1, largestMonth);
2220
+ ES.RejectToRange(day as number, 1, this.maximumMonthLength());
2221
+ } else {
2222
+ month = ES.ConstrainToRange(month, 1, largestMonth);
2223
+ day = ES.ConstrainToRange(day, 1, this.maximumMonthLength());
2224
+ }
2225
+ const matchingMonthEntry = monthEntries.find((entry) => entry[1].monthIndex === month);
2226
+ if (matchingMonthEntry === undefined) {
2227
+ throw new RangeError(`Invalid month ${month} in Chinese year ${year}`);
2228
+ }
2229
+ monthCode = buildMonthCode(
2230
+ +matchingMonthEntry[0].replace('bis', ''),
2231
+ matchingMonthEntry[0].indexOf('bis') !== -1
2232
+ );
2233
+ } else {
2234
+ // Both month and monthCode are present. Make sure they don't conflict.
2235
+ const months = this.getMonthList(year, cache);
2236
+ let numberPart = monthCode.replace(/^M|L$/g, (ch) => (ch === 'L' ? 'bis' : ''));
2237
+ if (numberPart[0] === '0') numberPart = numberPart.slice(1);
2238
+ const monthInfo = months[numberPart];
2239
+ if (!monthInfo) throw new RangeError(`Unmatched monthCode ${monthCode} in Chinese year ${year}`);
2240
+ if (month !== monthInfo.monthIndex) {
2241
+ throw new RangeError(`monthCode ${monthCode} doesn't correspond to month ${month} in Chinese year ${year}`);
2242
+ }
2243
+ }
2244
+ return { ...calendarDate, year, month, monthCode, day: day as number };
2245
+ }
2246
+ }
2247
+ }
2248
+
2249
+ class ChineseHelper extends ChineseBaseHelper {
2250
+ id = 'chinese' as const;
2251
+ }
2252
+
2253
+ // Dangi (Korean) calendar has same implementation as Chinese
2254
+ class DangiHelper extends ChineseBaseHelper {
2255
+ id = 'dangi' as const;
2256
+ }
2257
+
2258
+ /**
2259
+ * Common implementation of all non-ISO calendars.
2260
+ * Per-calendar id and logic live in `id` and `helper` properties attached later.
2261
+ * This split allowed an easy separation between code that was similar between
2262
+ * ISO and non-ISO implementations vs. code that was very different.
2263
+ */
2264
+ class NonIsoCalendar implements CalendarImpl {
2265
+ constructor(private readonly helper: HelperBase) {}
2266
+ extraFields(fields: FieldKey[]): FieldKey[] {
2267
+ if (this.helper.hasEra && fields.includes('year')) {
2268
+ return ['era', 'eraYear'];
2269
+ }
2270
+ return [];
2271
+ }
2272
+ resolveFields(fields: CalendarFieldsRecord /* , type */) {
2273
+ if (this.helper.calendarType !== 'lunisolar') {
2274
+ const cache = new OneObjectCache();
2275
+ const largestMonth = this.helper.monthsInYear({ year: fields.year ?? 1972 }, cache);
2276
+ resolveNonLunisolarMonth(fields, undefined, largestMonth);
2277
+ }
2278
+ }
2279
+ dateToISO(fields: CalendarDateFields, overflow: Overflow) {
2280
+ const cache = new OneObjectCache();
2281
+ const result = this.helper.calendarToIsoDate(fields, overflow, cache);
2282
+ cache.setObject(result);
2283
+ return result;
2284
+ }
2285
+ monthDayToISOReferenceDate(fields: MonthDayFromFieldsObject, overflow: Overflow) {
2286
+ const cache = new OneObjectCache();
2287
+ const result = this.helper.monthDayFromFields(fields, overflow, cache);
2288
+ // result.year is a reference year where this month/day exists in this calendar
2289
+ cache.setObject(result);
2290
+ return result;
2291
+ }
2292
+ fieldKeysToIgnore(
2293
+ keys: Exclude<keyof Temporal.PlainDateLike, 'calendar'>[]
2294
+ ): Exclude<keyof Temporal.PlainDateLike, 'calendar'>[] {
2295
+ const result = new Set<(typeof keys)[number]>();
2296
+ for (let ix = 0; ix < keys.length; ix++) {
2297
+ const key = keys[ix];
2298
+ result.add(key);
2299
+ switch (key) {
2300
+ case 'era':
2301
+ result.add('eraYear');
2302
+ result.add('year');
2303
+ break;
2304
+ case 'eraYear':
2305
+ result.add('era');
2306
+ result.add('year');
2307
+ break;
2308
+ case 'year':
2309
+ result.add('era');
2310
+ result.add('eraYear');
2311
+ break;
2312
+ case 'month':
2313
+ result.add('monthCode');
2314
+ // See https://github.com/tc39/proposal-temporal/issues/1784
2315
+ if (this.helper.erasBeginMidYear) {
2316
+ result.add('era');
2317
+ result.add('eraYear');
2318
+ }
2319
+ break;
2320
+ case 'monthCode':
2321
+ result.add('month');
2322
+ if (this.helper.erasBeginMidYear) {
2323
+ result.add('era');
2324
+ result.add('eraYear');
2325
+ }
2326
+ break;
2327
+ case 'day':
2328
+ if (this.helper.erasBeginMidYear) {
2329
+ result.add('era');
2330
+ result.add('eraYear');
2331
+ }
2332
+ break;
2333
+ }
2334
+ }
2335
+ return arrayFromSet(result);
2336
+ }
2337
+ dateAdd(isoDate: ISODate, { years, months, weeks, days }: DateDuration, overflow: Overflow) {
2338
+ const cache = OneObjectCache.getCacheForObject(isoDate);
2339
+ const calendarDate = this.helper.isoToCalendarDate(isoDate, cache);
2340
+ const added = this.helper.addCalendar(calendarDate, { years, months, weeks, days }, overflow, cache);
2341
+ const isoAdded = this.helper.calendarToIsoDate(added, 'constrain', cache);
2342
+ // The new object's cache starts with the cache of the old object
2343
+ if (!OneObjectCache.getCacheForObject(isoAdded)) {
2344
+ const newCache = new OneObjectCache(cache);
2345
+ newCache.setObject(isoAdded);
2346
+ }
2347
+ return isoAdded;
2348
+ }
2349
+ dateUntil(one: ISODate, two: ISODate, largestUnit: Temporal.DateUnit) {
2350
+ const cacheOne = OneObjectCache.getCacheForObject(one);
2351
+ const cacheTwo = OneObjectCache.getCacheForObject(two);
2352
+ const calendarOne = this.helper.isoToCalendarDate(one, cacheOne);
2353
+ const calendarTwo = this.helper.isoToCalendarDate(two, cacheTwo);
2354
+ const result = this.helper.untilCalendar(calendarOne, calendarTwo, largestUnit, cacheOne);
2355
+ return result;
2356
+ }
2357
+ isoToDate<
2358
+ Request extends Partial<Record<keyof CalendarDateRecord, true>>,
2359
+ T extends {
2360
+ [Field in keyof CalendarDateRecord]: Request extends { [K in Field]: true } ? CalendarDateRecord[Field] : never;
2361
+ }
2362
+ >(isoDate: ISODate, requestedFields: Request): T {
2363
+ const cache = OneObjectCache.getCacheForObject(isoDate);
2364
+ const calendarDate: Partial<CalendarDateRecord> & FullCalendarDate = this.helper.isoToCalendarDate(isoDate, cache);
2365
+ if (requestedFields.dayOfWeek) {
2366
+ calendarDate.dayOfWeek = impl['iso8601'].isoToDate(isoDate, { dayOfWeek: true }).dayOfWeek;
2367
+ }
2368
+ if (requestedFields.dayOfYear) {
2369
+ const startOfYear = this.helper.startOfCalendarYear(calendarDate);
2370
+ const diffDays = this.helper.calendarDaysUntil(startOfYear, calendarDate, cache);
2371
+ calendarDate.dayOfYear = diffDays + 1;
2372
+ }
2373
+ if (requestedFields.weekOfYear) calendarDate.weekOfYear = calendarDateWeekOfYear(this.helper.id, isoDate);
2374
+ calendarDate.daysInWeek = 7;
2375
+ if (requestedFields.daysInMonth) calendarDate.daysInMonth = this.helper.daysInMonth(calendarDate, cache);
2376
+ if (requestedFields.daysInYear) {
2377
+ const startOfYearCalendar = this.helper.startOfCalendarYear(calendarDate);
2378
+ const startOfNextYearCalendar = this.helper.addCalendar(startOfYearCalendar, { years: 1 }, 'constrain', cache);
2379
+ calendarDate.daysInYear = this.helper.calendarDaysUntil(startOfYearCalendar, startOfNextYearCalendar, cache);
2380
+ }
2381
+ if (requestedFields.monthsInYear) calendarDate.monthsInYear = this.helper.monthsInYear(calendarDate, cache);
2382
+ if (requestedFields.inLeapYear) calendarDate.inLeapYear = this.helper.inLeapYear(calendarDate, cache);
2383
+ return calendarDate as T;
2384
+ }
2385
+ getFirstDayOfWeek(): number | undefined {
2386
+ return this.helper.getFirstDayOfWeek();
2387
+ }
2388
+ getMinimalDaysInFirstWeek(): number | undefined {
2389
+ return this.helper.getMinimalDaysInFirstWeek();
2390
+ }
2391
+ }
2392
+
2393
+ for (const Helper of [
2394
+ HebrewHelper,
2395
+ PersianHelper,
2396
+ EthiopicHelper,
2397
+ EthioaaHelper,
2398
+ CopticHelper,
2399
+ ChineseHelper,
2400
+ DangiHelper,
2401
+ RocHelper,
2402
+ IndianHelper,
2403
+ BuddhistHelper,
2404
+ GregoryHelper,
2405
+ JapaneseHelper,
2406
+ IslamicHelper,
2407
+ IslamicUmalquraHelper,
2408
+ IslamicTblaHelper,
2409
+ IslamicCivilHelper,
2410
+ IslamicRgsaHelper,
2411
+ IslamicCcHelper
2412
+ ]) {
2413
+ const helper = new Helper();
2414
+ // Construct a new NonIsoCalendar instance with the given Helper implementation that contains
2415
+ // per-calendar logic.
2416
+ impl[helper.id] = new NonIsoCalendar(helper);
2417
+ }
2418
+
2419
+ function calendarImpl(calendar: BuiltinCalendarId) {
2420
+ return impl[calendar];
2421
+ }
2422
+ // Probably not what the intrinsics mechanism was intended for, but view this as
2423
+ // an export of calendarImpl while avoiding circular dependencies
2424
+ DefineIntrinsic('calendarImpl', calendarImpl);