siderust-js 0.1.0

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 (272) hide show
  1. package/.github/workflows/ci.yml +166 -0
  2. package/.gitmodules +9 -0
  3. package/CHANGELOG.md +26 -0
  4. package/LICENSE +661 -0
  5. package/README.md +138 -0
  6. package/package.json +12 -0
  7. package/qtty-js/.github/workflows/ci.yml +151 -0
  8. package/qtty-js/.gitmodules +3 -0
  9. package/qtty-js/CHANGELOG.md +31 -0
  10. package/qtty-js/LICENSE +661 -0
  11. package/qtty-js/README.md +132 -0
  12. package/qtty-js/package.json +20 -0
  13. package/qtty-js/qtty/.github/workflows/ci.yml +155 -0
  14. package/qtty-js/qtty/CHANGELOG.md +120 -0
  15. package/qtty-js/qtty/Cargo.lock +1462 -0
  16. package/qtty-js/qtty/Cargo.toml +12 -0
  17. package/qtty-js/qtty/LICENSE +661 -0
  18. package/qtty-js/qtty/README.md +9 -0
  19. package/qtty-js/qtty/qtty/Cargo.toml +41 -0
  20. package/qtty-js/qtty/qtty/README.md +8 -0
  21. package/qtty-js/qtty/qtty/examples/angles.rs +14 -0
  22. package/qtty-js/qtty/qtty/examples/astronomy.rs +17 -0
  23. package/qtty-js/qtty/qtty/examples/dimensional_arithmetic.rs +83 -0
  24. package/qtty-js/qtty/qtty/examples/python_integration.rs +61 -0
  25. package/qtty-js/qtty/qtty/examples/quickstart.rs +15 -0
  26. package/qtty-js/qtty/qtty/examples/ratios.rs +12 -0
  27. package/qtty-js/qtty/qtty/examples/serde_with_unit.rs +234 -0
  28. package/qtty-js/qtty/qtty/examples/serialization.rs +141 -0
  29. package/qtty-js/qtty/qtty/examples/serialization_advanced.rs +155 -0
  30. package/qtty-js/qtty/qtty/src/f32.rs +108 -0
  31. package/qtty-js/qtty/qtty/src/f64.rs +30 -0
  32. package/qtty-js/qtty/qtty/src/i128.rs +111 -0
  33. package/qtty-js/qtty/qtty/src/i16.rs +111 -0
  34. package/qtty-js/qtty/qtty/src/i32.rs +111 -0
  35. package/qtty-js/qtty/qtty/src/i64.rs +111 -0
  36. package/qtty-js/qtty/qtty/src/i8.rs +111 -0
  37. package/qtty-js/qtty/qtty/src/lib.rs +238 -0
  38. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.lock +83 -0
  39. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.toml +10 -0
  40. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/src/lib.rs +7 -0
  41. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.lock +83 -0
  42. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.toml +10 -0
  43. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/src/lib.rs +7 -0
  44. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.lock +83 -0
  45. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.toml +10 -0
  46. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/src/lib.rs +5 -0
  47. package/qtty-js/qtty/qtty/tests/integration_tests.rs +529 -0
  48. package/qtty-js/qtty/qtty/tests/qtty_vec_feature_matrix.rs +58 -0
  49. package/qtty-js/qtty/qtty-core/Cargo.toml +41 -0
  50. package/qtty-js/qtty/qtty-core/README.md +8 -0
  51. package/qtty-js/qtty/qtty-core/examples/diesel_integration.rs +145 -0
  52. package/qtty-js/qtty/qtty-core/examples/quantity_db_serde.rs +215 -0
  53. package/qtty-js/qtty/qtty-core/src/dimension.rs +249 -0
  54. package/qtty-js/qtty/qtty-core/src/feature_diesel.rs +318 -0
  55. package/qtty-js/qtty/qtty-core/src/feature_pyo3.rs +27 -0
  56. package/qtty-js/qtty/qtty-core/src/feature_serde.rs +203 -0
  57. package/qtty-js/qtty/qtty-core/src/feature_tiberius.rs +28 -0
  58. package/qtty-js/qtty/qtty-core/src/lib.rs +744 -0
  59. package/qtty-js/qtty/qtty-core/src/macros.rs +93 -0
  60. package/qtty-js/qtty/qtty-core/src/quantity.rs +810 -0
  61. package/qtty-js/qtty/qtty-core/src/scalar.rs +1742 -0
  62. package/qtty-js/qtty/qtty-core/src/unit.rs +332 -0
  63. package/qtty-js/qtty/qtty-core/src/units/angular.rs +1228 -0
  64. package/qtty-js/qtty/qtty-core/src/units/area.rs +243 -0
  65. package/qtty-js/qtty/qtty-core/src/units/frequency.rs +179 -0
  66. package/qtty-js/qtty/qtty-core/src/units/length.rs +1270 -0
  67. package/qtty-js/qtty/qtty-core/src/units/mass.rs +488 -0
  68. package/qtty-js/qtty/qtty-core/src/units/mod.rs +26 -0
  69. package/qtty-js/qtty/qtty-core/src/units/power.rs +324 -0
  70. package/qtty-js/qtty/qtty-core/src/units/time.rs +667 -0
  71. package/qtty-js/qtty/qtty-core/src/units/unitless.rs +212 -0
  72. package/qtty-js/qtty/qtty-core/src/units/velocity.rs +210 -0
  73. package/qtty-js/qtty/qtty-core/src/units/volume.rs +269 -0
  74. package/qtty-js/qtty/qtty-core/tests/core.rs +628 -0
  75. package/qtty-js/qtty/qtty-core/tests/diesel.rs +461 -0
  76. package/qtty-js/qtty/qtty-core/tests/integers.rs +632 -0
  77. package/qtty-js/qtty/qtty-core/tests/no_cross_unit_ops.rs +35 -0
  78. package/qtty-js/qtty/qtty-core/tests/pyo3.rs +334 -0
  79. package/qtty-js/qtty/qtty-core/tests/quantity_f32.rs +276 -0
  80. package/qtty-js/qtty/qtty-core/tests/scalar_decimal.rs +258 -0
  81. package/qtty-js/qtty/qtty-core/tests/scalar_f32.rs +286 -0
  82. package/qtty-js/qtty/qtty-core/tests/scalar_f64_real.rs +287 -0
  83. package/qtty-js/qtty/qtty-core/tests/scalar_rational.rs +260 -0
  84. package/qtty-js/qtty/qtty-core/tests/serde.rs +256 -0
  85. package/qtty-js/qtty/qtty-core/tests/tiberius.rs +208 -0
  86. package/qtty-js/qtty/qtty-derive/Cargo.toml +23 -0
  87. package/qtty-js/qtty/qtty-derive/README.md +8 -0
  88. package/qtty-js/qtty/qtty-derive/src/lib.rs +340 -0
  89. package/qtty-js/qtty/qtty-ffi/ARCHITECTURE.md +3 -0
  90. package/qtty-js/qtty/qtty-ffi/Cargo.toml +31 -0
  91. package/qtty-js/qtty/qtty-ffi/README.md +9 -0
  92. package/qtty-js/qtty/qtty-ffi/build.rs +326 -0
  93. package/qtty-js/qtty/qtty-ffi/cbindgen.toml +105 -0
  94. package/qtty-js/qtty/qtty-ffi/include/qtty_ffi.h +1126 -0
  95. package/qtty-js/qtty/qtty-ffi/src/ffi.rs +1251 -0
  96. package/qtty-js/qtty/qtty-ffi/src/ffi_serde.rs +294 -0
  97. package/qtty-js/qtty/qtty-ffi/src/helpers.rs +310 -0
  98. package/qtty-js/qtty/qtty-ffi/src/lib.rs +229 -0
  99. package/qtty-js/qtty/qtty-ffi/src/macros.rs +121 -0
  100. package/qtty-js/qtty/qtty-ffi/src/registry.rs +274 -0
  101. package/qtty-js/qtty/qtty-ffi/src/types.rs +620 -0
  102. package/qtty-js/qtty/qtty-ffi/tests/integration_tests.rs +842 -0
  103. package/qtty-js/qtty/qtty-ffi/units.csv +156 -0
  104. package/qtty-js/qtty/qtty-ffi/units.csv.md +3 -0
  105. package/qtty-js/qtty-node/.prettierignore +6 -0
  106. package/qtty-js/qtty-node/.prettierrc.json +6 -0
  107. package/qtty-js/qtty-node/README.md +250 -0
  108. package/qtty-js/qtty-node/c8.config.json +11 -0
  109. package/qtty-js/qtty-node/eslint.config.js +31 -0
  110. package/qtty-js/qtty-node/examples/arithmetic.mjs +64 -0
  111. package/qtty-js/qtty-node/examples/astronomy.mjs +90 -0
  112. package/qtty-js/qtty-node/examples/quickstart.mjs +36 -0
  113. package/qtty-js/qtty-node/examples/serialization.mjs +125 -0
  114. package/qtty-js/qtty-node/examples/unit_factories.mjs +74 -0
  115. package/qtty-js/qtty-node/index.d.ts +219 -0
  116. package/qtty-js/qtty-node/index.js +323 -0
  117. package/qtty-js/qtty-node/lib/DerivedQuantity.js +122 -0
  118. package/qtty-js/qtty-node/lib/Quantity.js +151 -0
  119. package/qtty-js/qtty-node/lib/backend.js +25 -0
  120. package/qtty-js/qtty-node/native.cjs +306 -0
  121. package/qtty-js/qtty-node/package-lock.json +3223 -0
  122. package/qtty-js/qtty-node/package.json +70 -0
  123. package/qtty-js/qtty-node/units.d.ts +299 -0
  124. package/qtty-js/qtty-node/units.js +210 -0
  125. package/qtty-js/qtty-web/Cargo.lock +767 -0
  126. package/qtty-js/qtty-web/Cargo.toml +21 -0
  127. package/qtty-js/qtty-web/index.d.ts +140 -0
  128. package/qtty-js/qtty-web/index.js +20 -0
  129. package/qtty-js/qtty-web/lib/DerivedQuantity.js +58 -0
  130. package/qtty-js/qtty-web/lib/Quantity.js +75 -0
  131. package/qtty-js/qtty-web/lib/backend.js +80 -0
  132. package/qtty-js/qtty-web/package.json +45 -0
  133. package/qtty-js/qtty-web/src/lib.rs +111 -0
  134. package/qtty-js/scripts/ci.sh +73 -0
  135. package/scripts/ci.sh +123 -0
  136. package/siderust-core/Cargo.lock +787 -0
  137. package/siderust-core/Cargo.toml +18 -0
  138. package/siderust-core/DEDUPLICATION.md +124 -0
  139. package/siderust-core/src/body.rs +120 -0
  140. package/siderust-core/src/events.rs +184 -0
  141. package/siderust-core/src/lib.rs +20 -0
  142. package/siderust-core/src/observer.rs +55 -0
  143. package/siderust-core/src/position.rs +213 -0
  144. package/siderust-node/.prettierignore +7 -0
  145. package/siderust-node/.prettierrc.json +6 -0
  146. package/siderust-node/Cargo.lock +906 -0
  147. package/siderust-node/Cargo.toml +29 -0
  148. package/siderust-node/README.md +109 -0
  149. package/siderust-node/__test__/index.test.mjs +248 -0
  150. package/siderust-node/build.rs +5 -0
  151. package/siderust-node/c8.config.json +3 -0
  152. package/siderust-node/eslint.config.js +31 -0
  153. package/siderust-node/examples/01_basic_coordinates.mjs +24 -0
  154. package/siderust-node/examples/02_coordinate_transformations.mjs +25 -0
  155. package/siderust-node/examples/03_all_frames_conversions.mjs +26 -0
  156. package/siderust-node/examples/04_all_center_conversions.mjs +24 -0
  157. package/siderust-node/examples/05_target_tracking.mjs +22 -0
  158. package/siderust-node/examples/06_night_events.mjs +18 -0
  159. package/siderust-node/examples/07_moon_properties.mjs +21 -0
  160. package/siderust-node/examples/08_solar_system.mjs +19 -0
  161. package/siderust-node/examples/09_star_observability.mjs +22 -0
  162. package/siderust-node/examples/10_time_periods.mjs +9 -0
  163. package/siderust-node/examples/11_serialization.mjs +31 -0
  164. package/siderust-node/examples/12_runtime_ephemeris.mjs +27 -0
  165. package/siderust-node/examples/13_coordinate_operations.mjs +20 -0
  166. package/siderust-node/index.d.ts +623 -0
  167. package/siderust-node/index.js +79 -0
  168. package/siderust-node/lib/Observer.js +112 -0
  169. package/siderust-node/lib/Star.js +118 -0
  170. package/siderust-node/lib/backend.js +63 -0
  171. package/siderust-node/lib/wrappers.js +566 -0
  172. package/siderust-node/main.js +20 -0
  173. package/siderust-node/native.cjs +360 -0
  174. package/siderust-node/package-lock.json +3261 -0
  175. package/siderust-node/package.json +71 -0
  176. package/siderust-node/src/body.rs +74 -0
  177. package/siderust-node/src/coordinates.rs +372 -0
  178. package/siderust-node/src/ephemeris.rs +462 -0
  179. package/siderust-node/src/events.rs +577 -0
  180. package/siderust-node/src/lib.rs +43 -0
  181. package/siderust-node/src/observer.rs +132 -0
  182. package/siderust-node/src/phase.rs +218 -0
  183. package/siderust-node/src/position.rs +292 -0
  184. package/siderust-node/src/star.rs +200 -0
  185. package/siderust-web/Cargo.lock +855 -0
  186. package/siderust-web/Cargo.toml +34 -0
  187. package/siderust-web/README.md +100 -0
  188. package/siderust-web/__test__/index.test.mjs +118 -0
  189. package/siderust-web/examples/github-pages/README.md +31 -0
  190. package/siderust-web/examples/github-pages/index.html +135 -0
  191. package/siderust-web/index.d.ts +311 -0
  192. package/siderust-web/index.js +66 -0
  193. package/siderust-web/lib/Observer.js +103 -0
  194. package/siderust-web/lib/Star.js +116 -0
  195. package/siderust-web/lib/backend.js +400 -0
  196. package/siderust-web/lib/wrappers.js +512 -0
  197. package/siderust-web/package.json +55 -0
  198. package/siderust-web/src/body.rs +69 -0
  199. package/siderust-web/src/coordinates.rs +302 -0
  200. package/siderust-web/src/ephemeris.rs +456 -0
  201. package/siderust-web/src/events.rs +520 -0
  202. package/siderust-web/src/lib.rs +51 -0
  203. package/siderust-web/src/observer.rs +117 -0
  204. package/siderust-web/src/phase.rs +190 -0
  205. package/siderust-web/src/position.rs +291 -0
  206. package/siderust-web/src/star.rs +178 -0
  207. package/tempoch-js/.github/workflows/ci.yml +142 -0
  208. package/tempoch-js/.gitmodules +3 -0
  209. package/tempoch-js/CHANGELOG.md +25 -0
  210. package/tempoch-js/LICENSE +661 -0
  211. package/tempoch-js/README.md +126 -0
  212. package/tempoch-js/package.json +20 -0
  213. package/tempoch-js/scripts/ci.sh +73 -0
  214. package/tempoch-js/tempoch/.github/workflows/ci.yml +113 -0
  215. package/tempoch-js/tempoch/CHANGELOG.md +82 -0
  216. package/tempoch-js/tempoch/Cargo.lock +947 -0
  217. package/tempoch-js/tempoch/Cargo.toml +3 -0
  218. package/tempoch-js/tempoch/LICENSE +661 -0
  219. package/tempoch-js/tempoch/README.md +76 -0
  220. package/tempoch-js/tempoch/tempoch/Cargo.toml +27 -0
  221. package/tempoch-js/tempoch/tempoch/examples/periods.rs +45 -0
  222. package/tempoch-js/tempoch/tempoch/examples/quickstart.rs +13 -0
  223. package/tempoch-js/tempoch/tempoch/src/lib.rs +49 -0
  224. package/tempoch-js/tempoch/tempoch/tests/integration.rs +57 -0
  225. package/tempoch-js/tempoch/tempoch-core/Cargo.toml +24 -0
  226. package/tempoch-js/tempoch/tempoch-core/src/delta_t.rs +345 -0
  227. package/tempoch-js/tempoch/tempoch-core/src/instant.rs +811 -0
  228. package/tempoch-js/tempoch/tempoch-core/src/julian_date_ext.rs +142 -0
  229. package/tempoch-js/tempoch/tempoch-core/src/lib.rs +81 -0
  230. package/tempoch-js/tempoch/tempoch-core/src/period.rs +1168 -0
  231. package/tempoch-js/tempoch/tempoch-core/src/scales.rs +779 -0
  232. package/tempoch-js/tempoch/tempoch-ffi/Cargo.lock +889 -0
  233. package/tempoch-js/tempoch/tempoch-ffi/Cargo.toml +26 -0
  234. package/tempoch-js/tempoch/tempoch-ffi/build.rs +24 -0
  235. package/tempoch-js/tempoch/tempoch-ffi/cbindgen.toml +30 -0
  236. package/tempoch-js/tempoch/tempoch-ffi/src/error.rs +19 -0
  237. package/tempoch-js/tempoch/tempoch-ffi/src/lib.rs +82 -0
  238. package/tempoch-js/tempoch/tempoch-ffi/src/period.rs +101 -0
  239. package/tempoch-js/tempoch/tempoch-ffi/src/time.rs +711 -0
  240. package/tempoch-js/tempoch/tempoch-ffi/tests/ffi.rs +265 -0
  241. package/tempoch-js/tempoch-node/.prettierignore +6 -0
  242. package/tempoch-js/tempoch-node/.prettierrc.json +6 -0
  243. package/tempoch-js/tempoch-node/Cargo.lock +496 -0
  244. package/tempoch-js/tempoch-node/Cargo.toml +29 -0
  245. package/tempoch-js/tempoch-node/README.md +265 -0
  246. package/tempoch-js/tempoch-node/__test__/index.test.mjs +598 -0
  247. package/tempoch-js/tempoch-node/build.rs +5 -0
  248. package/tempoch-js/tempoch-node/c8.config.json +3 -0
  249. package/tempoch-js/tempoch-node/eslint.config.js +31 -0
  250. package/tempoch-js/tempoch-node/examples/periods.mjs +79 -0
  251. package/tempoch-js/tempoch-node/examples/quickstart.mjs +71 -0
  252. package/tempoch-js/tempoch-node/examples/timescales.mjs +92 -0
  253. package/tempoch-js/tempoch-node/index.d.ts +280 -0
  254. package/tempoch-js/tempoch-node/index.js +32 -0
  255. package/tempoch-js/tempoch-node/lib/JulianDate.js +176 -0
  256. package/tempoch-js/tempoch-node/lib/ModifiedJulianDate.js +156 -0
  257. package/tempoch-js/tempoch-node/lib/Period.js +133 -0
  258. package/tempoch-js/tempoch-node/lib/backend.js +38 -0
  259. package/tempoch-js/tempoch-node/lib/qttyCompat.js +92 -0
  260. package/tempoch-js/tempoch-node/native.cjs +317 -0
  261. package/tempoch-js/tempoch-node/package-lock.json +3223 -0
  262. package/tempoch-js/tempoch-node/package.json +56 -0
  263. package/tempoch-js/tempoch-node/src/lib.rs +573 -0
  264. package/tempoch-js/tempoch-web/Cargo.toml +23 -0
  265. package/tempoch-js/tempoch-web/index.d.ts +95 -0
  266. package/tempoch-js/tempoch-web/index.js +27 -0
  267. package/tempoch-js/tempoch-web/lib/JulianDate.js +170 -0
  268. package/tempoch-js/tempoch-web/lib/ModifiedJulianDate.js +145 -0
  269. package/tempoch-js/tempoch-web/lib/Period.js +121 -0
  270. package/tempoch-js/tempoch-web/lib/backend.js +118 -0
  271. package/tempoch-js/tempoch-web/package.json +46 -0
  272. package/tempoch-js/tempoch-web/src/lib.rs +184 -0
@@ -0,0 +1,125 @@
1
+ /**
2
+ * serialization.mjs — JSON serialization of quantities.
3
+ *
4
+ * Demonstrates:
5
+ * - `quantity.toJson()` → plain object `{ value, unit }`
6
+ * - `JSON.stringify()` → JSON string
7
+ * - Restoring a quantity from a JSON object via `new Quantity(...)`
8
+ * - `derivedQuantity.toJson()` → `{ value, numerator, denominator }`
9
+ * - A practical "API payload" round-trip pattern
10
+ *
11
+ * Run: node examples/serialization.mjs
12
+ */
13
+
14
+ import { Quantity, DerivedQuantity } from '../index.js';
15
+
16
+ const line = (label = '') =>
17
+ console.log(
18
+ label ? `─── ${label} ${`─`.repeat(Math.max(0, 52 - label.length))}` : `─`.repeat(56),
19
+ );
20
+
21
+ // ─── Quantity → plain object → JSON string ───────────────────────────────
22
+ line('Quantity.toJson()');
23
+
24
+ const distance = new Quantity(42_195, 'Meter'); // marathon distance
25
+ const time = new Quantity(2.01389, 'Hour'); // world-record approx
26
+ const mass = new Quantity(70, 'Kilogram');
27
+
28
+ for (const q of [distance, time, mass]) {
29
+ const plain = q.toJson(); // { value, unit }
30
+ const json = JSON.stringify(plain);
31
+ console.log(` ${String(q).padEnd(22)} → ${json}`);
32
+ }
33
+
34
+ // ─── JSON string → Quantity (round-trip) ─────────────────────────────────
35
+ line('Round-trip: JSON → Quantity');
36
+
37
+ const marathonJson = JSON.stringify(distance.toJson()); // "{"value":42195,"unit":"Meter"}"
38
+ const parsed = JSON.parse(marathonJson); // { value: 42195, unit: 'Meter' }
39
+ const restored = new Quantity(parsed.value, parsed.unit);
40
+
41
+ console.log(` original : ${distance}`);
42
+ console.log(` JSON text : ${marathonJson}`);
43
+ console.log(` restored : ${restored}`);
44
+ console.log(` equal : ${Math.abs(restored.value - distance.value) < 1e-9}`);
45
+
46
+ // ─── DerivedQuantity → JSON ───────────────────────────────────────────────
47
+ line('DerivedQuantity.toJson()');
48
+
49
+ const soundSpeed = new DerivedQuantity(343, 'Meter', 'Second'); // air at 20 °C
50
+ const earthVelocity = new DerivedQuantity(29.783, 'Kilometer', 'Second');
51
+ const lightSpeed = new DerivedQuantity(299_792.458, 'Kilometer', 'Second');
52
+
53
+ for (const dq of [soundSpeed, earthVelocity, lightSpeed]) {
54
+ const plain = dq.toJson(); // { value, numerator, denominator }
55
+ const json = JSON.stringify(plain);
56
+ console.log(` ${String(dq).padEnd(28)} → ${json}`);
57
+ }
58
+
59
+ // ─── DerivedQuantity round-trip ────────────────────────────────────────────
60
+ line('Round-trip: JSON → DerivedQuantity');
61
+
62
+ const velJson = JSON.stringify(earthVelocity.toJson());
63
+ const velObj = JSON.parse(velJson);
64
+ const velRt = new DerivedQuantity(velObj.value, velObj.numerator, velObj.denominator);
65
+
66
+ console.log(` original : ${earthVelocity}`);
67
+ console.log(` JSON text : ${velJson}`);
68
+ console.log(` restored : ${velRt}`);
69
+ console.log(` in km/h : ${velRt.to('Kilometer', 'Hour').format(2)}`);
70
+
71
+ // ─── API payload pattern ──────────────────────────────────────────────────
72
+ line('API payload: serialize / deserialize');
73
+
74
+ /**
75
+ * Simulated server response with a list of physical measurements.
76
+ */
77
+ const serverPayload = JSON.stringify([
78
+ { value: 1.496e11, unit: 'Meter' }, // 1 AU
79
+ { value: 9.461e15, unit: 'Meter' }, // 1 light-year
80
+ { value: 3.086e16, unit: 'Meter' }, // 1 parsec
81
+ ]);
82
+
83
+ const measurements = JSON.parse(serverPayload).map(({ value, unit }) => new Quantity(value, unit));
84
+
85
+ const names = ['1 AU', '1 ly', '1 pc'];
86
+ console.log(' Distances received from API:');
87
+ for (const [name, q] of names.map((n, i) => [n, measurements[i]])) {
88
+ const km = q.to('Kilometer');
89
+ console.log(
90
+ ` ${name.padEnd(5)} = ${q.value.toExponential(3)} m = ${km.value.toExponential(3)} km`,
91
+ );
92
+ }
93
+
94
+ // ─── Derived payload ──────────────────────────────────────────────────────
95
+ line('Derived API payload');
96
+
97
+ const derivedPayload = JSON.stringify([
98
+ { value: 11.2, numerator: 'Kilometer', denominator: 'Second' }, // Earth escape velocity
99
+ { value: 617.7, numerator: 'Kilometer', denominator: 'Second' }, // Solar escape velocity
100
+ ]);
101
+
102
+ const velocities = JSON.parse(derivedPayload).map(
103
+ ({ value, numerator, denominator }) => new DerivedQuantity(value, numerator, denominator),
104
+ );
105
+
106
+ const velNames = ['Earth escape', 'Solar escape'];
107
+ for (const [name, v] of velNames.map((n, i) => [n, velocities[i]])) {
108
+ const kmh = v.to('Kilometer', 'Hour');
109
+ console.log(` ${name.padEnd(14)}: ${String(v).padEnd(16)} = ${kmh.format(0)}`);
110
+ }
111
+
112
+ // ─── listUnits introspection via JSON ─────────────────────────────────────
113
+ line('JSON catalog of available units');
114
+
115
+ import { listUnits } from '../index.js';
116
+ const allUnits = listUnits();
117
+ const byDim = {};
118
+ for (const u of allUnits) {
119
+ (byDim[u.dimension] ??= []).push(u.name);
120
+ }
121
+ for (const [dim, names_] of Object.entries(byDim).sort()) {
122
+ console.log(
123
+ ` ${dim.padEnd(10)} (${names_.length}): ${names_.slice(0, 5).join(', ')}${names_.length > 5 ? ', …' : ''}`,
124
+ );
125
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * unit_factories.mjs — Arithmetic-style construction with unit factories.
3
+ *
4
+ * Unit factories let you write Degrees(180) instead of new Quantity(180, 'Degree'),
5
+ * giving a concise, readable style that reads like "180 Degrees".
6
+ *
7
+ * Run: node examples/unit_factories.mjs
8
+ */
9
+
10
+ import {
11
+ Meters,
12
+ Kilometers,
13
+ LightYears,
14
+ Hours,
15
+ Degrees,
16
+ Kilograms,
17
+ Kilowatts,
18
+ unit,
19
+ units,
20
+ } from '../units.js';
21
+
22
+ console.log('─── Unit factories (Degrees(180) ≡ new Quantity(180, "Degree")) ───');
23
+
24
+ const angle = Degrees(180); // same as: new Quantity(180, 'Degree')
25
+ const dist = Kilometers(2.5); // same as: new Quantity(2.5, 'Kilometer')
26
+ const time = Hours(1.5); // same as: new Quantity(1.5, 'Hour')
27
+ const mass = Kilograms(70); // same as: new Quantity(70, 'Kilogram')
28
+ const power = Kilowatts(100); // same as: new Quantity(100, 'Kilowatt')
29
+
30
+ console.log(` Degrees(180) → ${angle}`);
31
+ console.log(` Kilometers(2.5) → ${dist}`);
32
+ console.log(` Hours(1.5) → ${time}`);
33
+ console.log(` Kilograms(70) → ${mass}`);
34
+ console.log(` Kilowatts(100) → ${power}`);
35
+
36
+ console.log('\n─── Factory metadata ──────────────────────────────────────────────');
37
+ console.log(` Meters.unit = ${Meters.unit}`); // Meter
38
+ console.log(` Meters.symbol = ${Meters.symbol}`); // m
39
+ console.log(` Meters.dimension = ${Meters.dimension}`); // Length
40
+ console.log(` Degrees.symbol = ${Degrees.symbol}`); // °
41
+
42
+ console.log('\n─── Convert using factories ───────────────────────────────────────');
43
+
44
+ const rad = Degrees(180).to('Radian');
45
+ console.log(` Degrees(180) → ${rad.format(6)}`); // 3.141593 rad
46
+
47
+ const inMeters = Kilometers(2.5).to('Meter');
48
+ console.log(` Kilometers(2.5) → ${inMeters}`); // 2500 m
49
+
50
+ const inMiles = Kilometers(1.609344).to('Mile');
51
+ console.log(` Kilometers(1.609344) → ${inMiles.format(4)}`); // 1.0000 mi
52
+
53
+ const inParsecs = LightYears(3.26156).to('Parsec');
54
+ console.log(` LightYears(3.26156) → ${inParsecs.format(4)}`); // 1.0000 pc
55
+
56
+ const inSeconds = Hours(2).to('Second');
57
+ console.log(` Hours(2) → ${inSeconds}`); // 7200 s
58
+
59
+ const inPounds = Kilograms(1).to('Pound');
60
+ console.log(` Kilograms(1) → ${inPounds.format(6)}`); // 2.204623 lb
61
+
62
+ console.log('\n─── Dynamic lookup with unit() ────────────────────────────────────');
63
+ const factory = unit('SolarMass');
64
+ if (factory) {
65
+ const sun = factory(1);
66
+ console.log(` unit('SolarMass')(1) → ${sun}`); // 1 M_☉
67
+ console.log(` Dimension: ${sun.dimension}`); // Mass
68
+ }
69
+
70
+ console.log('\n─── Browse all units for a dimension ──────────────────────────────');
71
+ const lengthUnits = Object.values(units)
72
+ .filter((f) => f.dimension === 'Length')
73
+ .map((f) => `${f.unit} (${f.symbol})`);
74
+ console.log(` Length units (${lengthUnits.length}): ${lengthUnits.slice(0, 8).join(', ')}, …`);
@@ -0,0 +1,219 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ /** Plain JSON-serializable representation of a quantity. */
7
+ export interface QuantityJson {
8
+ value: number
9
+ unit: string
10
+ }
11
+ /** Plain JSON-serializable representation of a derived quantity. */
12
+ export interface DerivedQuantityJson {
13
+ value: number
14
+ numerator: string
15
+ denominator: string
16
+ }
17
+ /**
18
+ * Converts a numeric value from one unit to another.
19
+ *
20
+ * This is a convenience function when you only need the converted number.
21
+ *
22
+ * ```js
23
+ * const { convert } = require('@siderust/qtty');
24
+ * const km = convert(1000, 'Meter', 'Kilometer'); // 1.0
25
+ * ```
26
+ *
27
+ * @param value - The numeric value.
28
+ * @param fromUnit - Source unit name.
29
+ * @param toUnit - Target unit name.
30
+ * @returns The converted value.
31
+ * @throws If units are unknown or have different dimensions.
32
+ */
33
+ export declare function convert(value: number, fromUnit: string, toUnit: string): number
34
+ /**
35
+ * Checks whether two unit names refer to compatible units (same dimension).
36
+ *
37
+ * ```js
38
+ * const { isCompatible } = require('@siderust/qtty');
39
+ * isCompatible('Meter', 'Kilometer'); // true
40
+ * isCompatible('Meter', 'Second'); // false
41
+ * ```
42
+ */
43
+ export declare function isCompatible(unitA: string, unitB: string): boolean
44
+ /**
45
+ * Returns the dimension name for a unit, e.g. `"Length"`, `"Time"`.
46
+ *
47
+ * ```js
48
+ * const { unitDimension } = require('@siderust/qtty');
49
+ * unitDimension('Meter'); // "Length"
50
+ * unitDimension('Second'); // "Time"
51
+ * ```
52
+ */
53
+ export declare function unitDimension(unit: string): string
54
+ /**
55
+ * Returns the symbol for a unit, e.g. `"m"`, `"km"`, `"s"`.
56
+ *
57
+ * ```js
58
+ * const { unitSymbol } = require('@siderust/qtty');
59
+ * unitSymbol('Meter'); // "m"
60
+ * unitSymbol('Kilometer'); // "km"
61
+ * ```
62
+ */
63
+ export declare function unitSymbol(unit: string): string
64
+ /**
65
+ * Checks whether a unit name is valid (recognized by the registry).
66
+ *
67
+ * ```js
68
+ * const { isValidUnit } = require('@siderust/qtty');
69
+ * isValidUnit('Meter'); // true
70
+ * isValidUnit('Foobar'); // false
71
+ * ```
72
+ */
73
+ export declare function isValidUnit(unit: string): boolean
74
+ /** Returns the FFI ABI version number. */
75
+ export declare function ffiVersion(): number
76
+ /** Metadata about a single unit returned by `listUnits`. */
77
+ export interface UnitInfo {
78
+ /** Unit name, e.g. `"Meter"`. */
79
+ name: string
80
+ /** Unit symbol, e.g. `"m"`. */
81
+ symbol: string
82
+ /** Dimension name, e.g. `"Length"`. */
83
+ dimension: string
84
+ }
85
+ /**
86
+ * Returns an array of all registered units with their name, symbol, and dimension.
87
+ *
88
+ * This is useful for building dynamic UIs, documenting available units,
89
+ * or generating unit factory collections at runtime.
90
+ *
91
+ * ```js
92
+ * const { listUnits } = require('@siderust/qtty');
93
+ * const units = listUnits();
94
+ * units.filter(u => u.dimension === 'Length').map(u => u.name);
95
+ * ```
96
+ */
97
+ export declare function listUnits(): Array<UnitInfo>
98
+ /**
99
+ * A physical quantity: a numeric value paired with a unit.
100
+ *
101
+ * Create quantities with `new Quantity(value, unit)` and convert between
102
+ * compatible units with `.to(unit)`.
103
+ *
104
+ * ```js
105
+ * const { Quantity } = require('@siderust/qtty');
106
+ *
107
+ * const distance = new Quantity(1000, 'Meter');
108
+ * const km = distance.to('Kilometer');
109
+ * console.log(km.value); // 1.0
110
+ * console.log(km.unit); // "Kilometer"
111
+ * ```
112
+ */
113
+ export declare class Quantity {
114
+ /**
115
+ * Creates a new quantity with the given value and unit name.
116
+ *
117
+ * @param value - The numeric value.
118
+ * @param unit - Unit name, e.g. `"Meter"`, `"Kilometer"`, `"Second"`.
119
+ * @throws If the unit name is not recognized.
120
+ */
121
+ constructor(value: number, unit: string)
122
+ /** The numeric value of this quantity. */
123
+ get value(): number
124
+ /** The unit name, e.g. `"Meter"`. */
125
+ get unit(): string
126
+ /** The unit symbol, e.g. `"m"`, `"km"`, `"s"`. */
127
+ get symbol(): string
128
+ /** The physical dimension of this quantity, e.g. `"Length"`, `"Time"`. */
129
+ get dimension(): string
130
+ /**
131
+ * Converts this quantity to a different unit.
132
+ *
133
+ * @param unit - Target unit name, e.g. `"Kilometer"`.
134
+ * @returns A new `Quantity` in the target unit.
135
+ * @throws If the target unit is unknown or has a different dimension.
136
+ */
137
+ to(unit: string): Quantity
138
+ /** Checks whether this quantity is compatible (same dimension) with another. */
139
+ compatible(other: Quantity): boolean
140
+ /**
141
+ * Adds another quantity to this one. Both must share the same dimension.
142
+ * The result uses this quantity's unit.
143
+ *
144
+ * @throws If the quantities have different dimensions.
145
+ */
146
+ add(other: Quantity): Quantity
147
+ /**
148
+ * Subtracts another quantity from this one. Both must share the same dimension.
149
+ * The result uses this quantity's unit.
150
+ *
151
+ * @throws If the quantities have different dimensions.
152
+ */
153
+ sub(other: Quantity): Quantity
154
+ /** Multiplies this quantity by a scalar. */
155
+ mul(scalar: number): Quantity
156
+ /** Divides this quantity by a scalar. */
157
+ div(scalar: number): Quantity
158
+ /** Negates the quantity. */
159
+ neg(): Quantity
160
+ /**
161
+ * Formats the quantity as a human-readable string, e.g. `"1000 m"`.
162
+ *
163
+ * @param precision - Number of decimal digits (omit for default).
164
+ */
165
+ format(precision?: number | undefined | null): string
166
+ /** Returns the quantity as a plain object `{ value, unit }` suitable for JSON. */
167
+ toJson(): QuantityJson
168
+ /** Returns `"<value> <symbol>"`, e.g. `"1000 m"`. */
169
+ toString(): string
170
+ }
171
+ /**
172
+ * A derived (compound) quantity like velocity (m/s) or angular velocity (rad/s).
173
+ *
174
+ * ```js
175
+ * const { DerivedQuantity } = require('@siderust/qtty');
176
+ *
177
+ * const velocity = new DerivedQuantity(100, 'Meter', 'Second');
178
+ * const kmh = velocity.to('Kilometer', 'Hour');
179
+ * console.log(kmh.value); // 360
180
+ * ```
181
+ */
182
+ export declare class DerivedQuantity {
183
+ /**
184
+ * Creates a new derived quantity.
185
+ *
186
+ * @param value - The numeric value.
187
+ * @param numerator - Numerator unit name, e.g. `"Meter"`.
188
+ * @param denominator - Denominator unit name, e.g. `"Second"`.
189
+ */
190
+ constructor(value: number, numerator: string, denominator: string)
191
+ /** The numeric value. */
192
+ get value(): number
193
+ /** The numerator unit name. */
194
+ get numerator(): string
195
+ /** The denominator unit name. */
196
+ get denominator(): string
197
+ /** The compound symbol, e.g. `"m/s"`. */
198
+ get symbol(): string
199
+ /**
200
+ * Converts this derived quantity to different units.
201
+ *
202
+ * @param numerator - Target numerator unit.
203
+ * @param denominator - Target denominator unit.
204
+ * @throws If the dimensions are incompatible.
205
+ */
206
+ to(numerator: string, denominator: string): DerivedQuantity
207
+ /** Multiplies by a scalar. */
208
+ mul(scalar: number): DerivedQuantity
209
+ /** Divides by a scalar. */
210
+ div(scalar: number): DerivedQuantity
211
+ /** Negates the value. */
212
+ neg(): DerivedQuantity
213
+ /** Formats as `"<value> <num_symbol>/<den_symbol>"`. */
214
+ format(precision?: number | undefined | null): string
215
+ /** Returns the derived quantity as a plain object. */
216
+ toJson(): DerivedQuantityJson
217
+ /** Returns `"<value> <num_symbol>/<den_symbol>"`. */
218
+ toString(): string
219
+ }