@johnmorrisdotca/wakusei-planets 0.41.0 → 0.43.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.
@@ -10,6 +10,13 @@ export { SPACECRAFT, SPACECRAFT_IDS } from "./spacecraft-facts.js";
10
10
  const AU_KM = 149_597_870.7;
11
11
  /** The speed of light in kilometres a second. */
12
12
  const LIGHT_KM_PER_SECOND = 299_792.458;
13
+ /** The Roadster module, kept once `loadSpacecraft("roadster")` has imported it. Importing it is what brings in the 20 kB table. */
14
+ let roadsterMod;
15
+ const roadsterReady = () => {
16
+ if (roadsterMod === undefined)
17
+ throw new Error(`the path of Starman is not loaded: await loadSpacecraft("roadster") first`);
18
+ return roadsterMod;
19
+ };
13
20
  const LOADERS = {
14
21
  voyager1: () => import("./craft/voyager1.js"),
15
22
  voyager2: () => import("./craft/voyager2.js"),
@@ -29,6 +36,18 @@ const LOADERS = {
29
36
  hayabusa2: () => import("./craft/hayabusa2.js"),
30
37
  osirisapex: () => import("./craft/osirisapex.js"),
31
38
  solarorbiter: () => import("./craft/solarorbiter.js"),
39
+ roadster: async () => {
40
+ roadsterMod = await import("./roadster.js");
41
+ const craft = SPACECRAFT.roadster;
42
+ craft.record.start = roadsterMod.ROADSTER.record.start;
43
+ craft.record.end = roadsterMod.ROADSTER.record.end;
44
+ const events = roadsterMod.roadsterEvents().map((event) => {
45
+ const body = event.kind === "earth-approach" ? "earth" : event.kind === "mars-approach" ? "mars" : undefined;
46
+ const kind = event.kind === "earth-approach" || event.kind === "mars-approach" ? "flyby" : event.kind;
47
+ return { kind, date: event.date, precision: "minute", name: event.name, ...(body === undefined ? {} : { body }), ...(event.distanceKm === undefined ? {} : { distanceKm: event.distanceKm }) };
48
+ });
49
+ craft.events = events;
50
+ },
32
51
  };
33
52
  function facts(id) {
34
53
  const found = SPACECRAFT[id];
@@ -40,7 +59,7 @@ function facts(id) {
40
59
  * Load the fitted paths of some spacecraft (or all of them), each a table of its own that nothing else imports: about 6 kB gzipped for the Voyagers, the Pioneers, New Horizons and JWST, and more for the craft that orbited a planet or fly past many.
41
60
  * Call it once before the other functions; it is safe to call again.
42
61
  *
43
- * @param ids - The craft to load; none means all eighteen.
62
+ * @param ids - The craft to load; none means all nineteen.
44
63
  * @returns A promise that settles when every table is registered.
45
64
  *
46
65
  * @example
@@ -89,6 +108,8 @@ function onPath(id, when) {
89
108
  * ```
90
109
  */
91
110
  export function spacecraftPosition(id, when) {
111
+ if (id === "roadster")
112
+ return roadsterReady().roadsterPosition(when);
92
113
  const { tt, table } = onPath(id, when);
93
114
  return craftState(table, tt).position;
94
115
  }
@@ -108,6 +129,8 @@ export function spacecraftPosition(id, when) {
108
129
  * ```
109
130
  */
110
131
  export function spacecraftVelocity(id, when) {
132
+ if (id === "roadster")
133
+ return roadsterReady().roadsterVelocity(when);
111
134
  const { tt, table } = onPath(id, when);
112
135
  return craftState(table, tt).velocity;
113
136
  }
@@ -199,6 +222,8 @@ export function spacecraftLightTime(id, when) {
199
222
  * ```
200
223
  */
201
224
  export function spacecraftErrorKm(id) {
225
+ if (id === "roadster")
226
+ return roadsterReady().ROADSTER_ACCURACY.interpolationKm;
202
227
  const table = craftTable(id);
203
228
  if (table === undefined)
204
229
  throw new Error(`the path of ${facts(id).name.en} is not loaded: await loadSpacecraft(${JSON.stringify(id)}) first`);
@@ -239,3 +264,27 @@ export function spacecraftStats(id, when) {
239
264
  daysSinceLaunch: (ms - facts(id).launch.date) / 86_400_000,
240
265
  };
241
266
  }
267
+ /** Fold case, full-width letters, spaces and punctuation so `tesla` matches `Tesla`, `スターマン` matches `スターマン` and `europa-clipper` matches `Europa Clipper`. */
268
+ const fold = (text) => text.normalize("NFKC").toLowerCase().replace(/[\s\p{P}\p{S}]/gu, "");
269
+ /**
270
+ * The craft a search matches: the id, either name, or an `otherNames` entry, with case, letter width, spaces and punctuation folded. `voyager` matches both Voyagers. An empty string matches nothing.
271
+ *
272
+ * @param text - What was typed.
273
+ * @returns The matching ids, in the order of `SPACECRAFT_IDS`.
274
+ *
275
+ * @example
276
+ * ```ts
277
+ * import { findSpacecraft } from "@johnmorrisdotca/wakusei-planets/spacecraft";
278
+ *
279
+ * console.log(findSpacecraft("tesla").join(","), findSpacecraft("スターマン").join(","));
280
+ * ```
281
+ */
282
+ export function findSpacecraft(text) {
283
+ const needle = fold(text.trim());
284
+ if (needle.length === 0)
285
+ return [];
286
+ return SPACECRAFT_IDS.filter((id) => {
287
+ const craft = SPACECRAFT[id];
288
+ return [id, craft.name.en, craft.name.ja, ...(craft.otherNames ?? [])].some((part) => fold(part).includes(needle));
289
+ });
290
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.41.0";
1
+ export declare const VERSION = "0.43.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.41.0";
1
+ export const VERSION = "0.43.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",