@zodiacs/engine 0.1.1-rc.14
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.
- package/CHANGELOG.md +502 -0
- package/LICENSE +21 -0
- package/LICENSING.md +99 -0
- package/NOTICE +58 -0
- package/README.md +785 -0
- package/dist/chunk-4OSOCZY2.js +340 -0
- package/dist/chunk-AECKV73C.js +231 -0
- package/dist/chunk-CPALRSPC.js +35 -0
- package/dist/chunk-GFT24GFR.js +25 -0
- package/dist/chunk-NAJ735VN.js +219 -0
- package/dist/chunk-ONBZ4HK7.js +285 -0
- package/dist/chunk-PFGUCNOK.js +87 -0
- package/dist/chunk-RETEVD4F.js +681 -0
- package/dist/crossings.d.ts +91 -0
- package/dist/crossings.js +8 -0
- package/dist/deltat.d.ts +73 -0
- package/dist/deltat.js +12 -0
- package/dist/geo.d.ts +58 -0
- package/dist/geo.js +312 -0
- package/dist/index.d.ts +478 -0
- package/dist/index.js +682 -0
- package/dist/internal-math-BE2hcSHd.d.ts +271 -0
- package/dist/internal-math.d.ts +3 -0
- package/dist/internal-math.js +76 -0
- package/dist/internal.d.ts +9 -0
- package/dist/internal.js +16 -0
- package/dist/receipt.d.ts +224 -0
- package/dist/receipt.js +824 -0
- package/dist/types-CeafogFp.d.ts +197 -0
- package/package.json +94 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { B as BodyName } from './types-CeafogFp.js';
|
|
2
|
+
import './deltat.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The instants a body sits exactly on an ecliptic longitude.
|
|
6
|
+
*
|
|
7
|
+
* This is the engine's one crossing solver. It imports no ephemeris: the
|
|
8
|
+
* caller passes the longitude function. `findLongitudeCrossings` and
|
|
9
|
+
* `searchLongitudeCrossings` in the root entry point run it on the engine's
|
|
10
|
+
* own longitudes, and the `@zodiacs/engine/crossings` entry point carries it
|
|
11
|
+
* without the ephemeris, for scans that bring their own.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** One pass of a body over a longitude. */
|
|
15
|
+
interface LongitudeCrossing {
|
|
16
|
+
/** The instant, to within the step divided by 2^24. */
|
|
17
|
+
at: Date;
|
|
18
|
+
/** True when the body was moving backward through the degree. */
|
|
19
|
+
retrograde: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** A longitude source: the ecliptic longitude of `body` at `date`, in degrees. */
|
|
22
|
+
type BodyLongitudeAt = (body: BodyName, date: Date) => number;
|
|
23
|
+
interface CrossingSearchOptions {
|
|
24
|
+
/** Days between coarse samples. Defaults to 5. */
|
|
25
|
+
stepDays?: number;
|
|
26
|
+
/**
|
|
27
|
+
* The most longitude evaluations the search may make, refinements
|
|
28
|
+
* included: a positive integer, or `Infinity`. Defaults to no limit.
|
|
29
|
+
*/
|
|
30
|
+
maxSamples?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A search either completes, with every crossing it found, or is refused
|
|
34
|
+
* whole: a refused search returns no crossings, never part of a result.
|
|
35
|
+
*/
|
|
36
|
+
type CrossingSearchResult = {
|
|
37
|
+
status: "complete";
|
|
38
|
+
crossings: LongitudeCrossing[];
|
|
39
|
+
/** Longitude evaluations made. */
|
|
40
|
+
samples: number;
|
|
41
|
+
} | {
|
|
42
|
+
status: "refused";
|
|
43
|
+
reason: "sample-budget";
|
|
44
|
+
/**
|
|
45
|
+
* Longitude evaluations made before the search stopped: 0 when the
|
|
46
|
+
* coarse scan alone needs more than `maxSamples`, which is decided
|
|
47
|
+
* before sampling; otherwise `maxSamples`.
|
|
48
|
+
*/
|
|
49
|
+
samples: number;
|
|
50
|
+
maxSamples: number;
|
|
51
|
+
crossings: [];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Every instant in (from, to] when `body` sits exactly on `targetLongitude`,
|
|
55
|
+
* in time order, from the longitudes `longitudeAt` returns.
|
|
56
|
+
*
|
|
57
|
+
* The scan samples `from`, every `stepDays` after it, and `to`, and bisects
|
|
58
|
+
* each sign change of the offset from the target 24 times, to the step
|
|
59
|
+
* divided by 2^24. A root exactly at `from` is left to the window that ends
|
|
60
|
+
* there. A sample exactly on the target is reported once, at that sample, in
|
|
61
|
+
* the direction of the sample before it. Offsets of 90° or more are read as
|
|
62
|
+
* the far side of the circle, not a crossing.
|
|
63
|
+
*
|
|
64
|
+
* A plain scan loses both crossings when a station falls between two samples
|
|
65
|
+
* just past the target: at a 5-day step, a Saturn station within 0.0103° of
|
|
66
|
+
* it, or a Jupiter station within 0.0205°. Where the sampled motion turns
|
|
67
|
+
* without the offset changing sign, and the turning sample is within reach of
|
|
68
|
+
* the local curvature, a golden-section search finds the extremum. Both
|
|
69
|
+
* crossings are bisected when it passes the target, and one is reported, as
|
|
70
|
+
* direct, when it only touches. A turn in the first or last cell is found by a
|
|
71
|
+
* probe one minute inside the window; no sample falls outside [from, to].
|
|
72
|
+
* Crossings less than a second after another are the same crossing. The
|
|
73
|
+
* search assumes smooth motion with at most one station in two steps; it is
|
|
74
|
+
* tested, not proven complete.
|
|
75
|
+
*
|
|
76
|
+
* There is no sample budget: the work grows with (to − from) / step, and
|
|
77
|
+
* {@link searchLongitudeCrossingsWith} bounds it. Throws RangeError for an
|
|
78
|
+
* invalid window, target or step, and for a non-finite longitude.
|
|
79
|
+
*/
|
|
80
|
+
declare function findLongitudeCrossingsWith(longitudeAt: BodyLongitudeAt, body: BodyName, targetLongitude: number, from: Date, to: Date, stepDays?: number): LongitudeCrossing[];
|
|
81
|
+
/**
|
|
82
|
+
* The search {@link findLongitudeCrossingsWith} makes, with the step as
|
|
83
|
+
* `options.stepDays` (default 5), under an optional budget of longitude
|
|
84
|
+
* evaluations, `options.maxSamples`. It is refused before any sampling when
|
|
85
|
+
* the coarse scan alone needs more than the budget, and otherwise when a
|
|
86
|
+
* refinement would pass it; either way it returns a typed refusal with no
|
|
87
|
+
* crossings rather than throwing. Invalid input still throws RangeError.
|
|
88
|
+
*/
|
|
89
|
+
declare function searchLongitudeCrossingsWith(longitudeAt: BodyLongitudeAt, body: BodyName, targetLongitude: number, from: Date, to: Date, options?: CrossingSearchOptions): CrossingSearchResult;
|
|
90
|
+
|
|
91
|
+
export { type BodyLongitudeAt, type CrossingSearchOptions, type CrossingSearchResult, type LongitudeCrossing, findLongitudeCrossingsWith, searchLongitudeCrossingsWith };
|
package/dist/deltat.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ΔT = TT − UT1 in seconds, with a 1-σ band: the model "zodiacs-deltat/1".
|
|
3
|
+
*
|
|
4
|
+
* The argument is astronomy-engine's `ut`, days since 2000-01-01T12:00Z, with
|
|
5
|
+
* the instant read as UT1 (UTC is taken as UT1; UT1 − UTC and leap seconds
|
|
6
|
+
* belong to the time-scale work, M3). Years are y = 2000 + ut / 365.25, so
|
|
7
|
+
* y = 2027 falls on 2027-01-01T06:00Z.
|
|
8
|
+
*
|
|
9
|
+
* - y < −720, "long-term": the integral of Stephenson, Morrison & Hohenkerk
|
|
10
|
+
* 2016 (SMH, Proc. R. Soc. A 472: 20160404, CC BY 4.0), eq. (5.1),
|
|
11
|
+
* lod = 1.78t − 4.0 sin(2πt/15) ms with t in centuries from 1825, joined in
|
|
12
|
+
* value at −720.
|
|
13
|
+
* - −720 ≤ y < 1941, "reconstructed": SMH's Table S15 as the C2 cubic spline
|
|
14
|
+
* through its knots from −720 to 1945, rebuilt on first use. Their lunar
|
|
15
|
+
* tidal acceleration, −25.82″/cy², is kept: no tidal term is applied.
|
|
16
|
+
* - 1941 ≤ y < 1956, "reconstructed": whole-year knots from USNO's
|
|
17
|
+
* historic_deltat.data.
|
|
18
|
+
* - 1956 ≤ y ≤ the last observed day, "observed": whole-year knots from USNO
|
|
19
|
+
* to 1961, IERS 20 C04 to 1972 and IERS finals2000A (rows flagged I) after,
|
|
20
|
+
* then a knot at the last observed day. Linear between knots.
|
|
21
|
+
* - To the last predicted day, "predicted": four evenly spaced knots from
|
|
22
|
+
* Bulletin A's predictions (finals2000A rows flagged P).
|
|
23
|
+
* - After it, "extrapolated": the prediction window's slope, damped with
|
|
24
|
+
* τ = 15 years, plus the curvature of the long-term curve.
|
|
25
|
+
*
|
|
26
|
+
* σ: after the last observed day it depends only on h, the years since that
|
|
27
|
+
* day: 0.03 + 0.09·h^0.75 up to one year, 0.12·h^1.5 up to ten, then 0.61 s a
|
|
28
|
+
* year more; continuous and never decreasing. Observed: 0.03. Before 1956 it
|
|
29
|
+
* is a band built from measured indicators, and before 1620 an estimate, not
|
|
30
|
+
* a calibrated 1-σ. Sources, licences and every number:
|
|
31
|
+
* docs/platform/evidence/deltat-2026-09-25/ in the Zodiacs site repository.
|
|
32
|
+
*/
|
|
33
|
+
type DeltaTSegment = "long-term" | "reconstructed" | "observed" | "predicted" | "extrapolated" | "pinned";
|
|
34
|
+
/** ΔT with its band and provenance. A caller's pin has sigma, table and tableDigest null. */
|
|
35
|
+
interface DeltaT {
|
|
36
|
+
seconds: number;
|
|
37
|
+
/** 1-σ band in seconds; null for a pin. */
|
|
38
|
+
sigma: number | null;
|
|
39
|
+
model: "zodiacs-deltat/1" | "pinned";
|
|
40
|
+
/** Date of the last observed IERS day behind the table; null for a pin. */
|
|
41
|
+
table: string | null;
|
|
42
|
+
/** DELTA_T_TABLE.digest; null for a pin. */
|
|
43
|
+
tableDigest: string | null;
|
|
44
|
+
segment: DeltaTSegment;
|
|
45
|
+
}
|
|
46
|
+
/** The part of the model refreshed from IERS. It changes only with an engine release. */
|
|
47
|
+
interface DeltaTTable {
|
|
48
|
+
/** Date of the last observed IERS day (YYYY-MM-DD). */
|
|
49
|
+
readonly version: string;
|
|
50
|
+
/** First 16 hex digits of SHA-256 over the JSON text [from, observedTo, predictedTo, ...knots]. */
|
|
51
|
+
readonly digest: string;
|
|
52
|
+
/** Year of the first whole-year knot. */
|
|
53
|
+
readonly from: number;
|
|
54
|
+
/** MJD of the last observed day. */
|
|
55
|
+
readonly observedTo: number;
|
|
56
|
+
/** MJD of the last predicted day. */
|
|
57
|
+
readonly predictedTo: number;
|
|
58
|
+
/**
|
|
59
|
+
* Knots in 0.01 s, the first absolute and the rest differences: every whole
|
|
60
|
+
* year from `from` before observedTo, then observedTo, then four evenly
|
|
61
|
+
* spaced up to predictedTo.
|
|
62
|
+
*/
|
|
63
|
+
readonly knots: readonly number[];
|
|
64
|
+
}
|
|
65
|
+
declare const DELTA_T_MODEL = "zodiacs-deltat/1";
|
|
66
|
+
/** This release's table: IERS files of 2026-09-24. Frozen. */
|
|
67
|
+
declare const DELTA_T_TABLE: DeltaTTable;
|
|
68
|
+
/** ΔT at astronomy-engine's UT (days since J2000.0), with its band and the part of the model that answered. */
|
|
69
|
+
declare function deltaTAt(ut: number): DeltaT;
|
|
70
|
+
/** For astronomy-engine's SetDeltaTFunction: UT days in, seconds out. Allocation-free. */
|
|
71
|
+
declare function deltaT(ut: number): number;
|
|
72
|
+
|
|
73
|
+
export { DELTA_T_MODEL, DELTA_T_TABLE, type DeltaT, type DeltaTSegment, type DeltaTTable, deltaT, deltaTAt };
|
package/dist/deltat.js
ADDED
package/dist/geo.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { H as HouseSystem, c as ChartFlag, d as BirthInput } from './types-CeafogFp.js';
|
|
2
|
+
import './deltat.js';
|
|
3
|
+
|
|
4
|
+
interface City {
|
|
5
|
+
name: string;
|
|
6
|
+
admin1: string;
|
|
7
|
+
country: string;
|
|
8
|
+
latitude: number;
|
|
9
|
+
longitude: number;
|
|
10
|
+
timeZone: string;
|
|
11
|
+
population: number;
|
|
12
|
+
}
|
|
13
|
+
interface GeoNamesIndexMetadata {
|
|
14
|
+
version: number;
|
|
15
|
+
source: string;
|
|
16
|
+
count: number;
|
|
17
|
+
timeZones: readonly string[];
|
|
18
|
+
shards: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
interface GeoNamesClientOptions {
|
|
21
|
+
/** URL of a compatible directory containing index.json and shard JSON. */
|
|
22
|
+
baseUrl: string;
|
|
23
|
+
fetch?: typeof globalThis.fetch;
|
|
24
|
+
}
|
|
25
|
+
interface GeoNamesClient {
|
|
26
|
+
/** Returns metadata snapshots; mutating an array cannot change the client's cache. */
|
|
27
|
+
preload(): Promise<GeoNamesIndexMetadata>;
|
|
28
|
+
searchCities(query: string, limit?: number): Promise<City[]>;
|
|
29
|
+
}
|
|
30
|
+
declare function createGeoNamesClient(options: GeoNamesClientOptions): GeoNamesClient;
|
|
31
|
+
|
|
32
|
+
interface LocalTimeResolution {
|
|
33
|
+
utc: Date;
|
|
34
|
+
/** Minutes east of UTC; may be fractional for historical local mean time. */
|
|
35
|
+
offsetMinutes: number;
|
|
36
|
+
flags: Extract<ChartFlag, "dst-gap" | "dst-fold" | "lmt">[];
|
|
37
|
+
}
|
|
38
|
+
interface LocalBirthInput {
|
|
39
|
+
date: string;
|
|
40
|
+
/** Local 24-hour wall time. Defaults to noon when `timeKnown` is false. */
|
|
41
|
+
time?: string;
|
|
42
|
+
timeZone: string;
|
|
43
|
+
latitude?: number;
|
|
44
|
+
longitude?: number;
|
|
45
|
+
houseSystem?: HouseSystem;
|
|
46
|
+
timeKnown?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** UTC offset for an IANA timezone at a UTC instant. */
|
|
49
|
+
declare function offsetAt(timeZone: string, utcMilliseconds: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* Resolve a local wall time against the host runtime's IANA timezone data.
|
|
52
|
+
* Folds choose the earlier instant; gaps shift forward by the clock change.
|
|
53
|
+
*/
|
|
54
|
+
declare function resolveLocalToUtc(date: string, time: string, timeZone: string): LocalTimeResolution;
|
|
55
|
+
/** Convert a local birth form into the explicit UTC input accepted by the core. */
|
|
56
|
+
declare function resolveBirth(input: LocalBirthInput): BirthInput;
|
|
57
|
+
|
|
58
|
+
export { type City, type GeoNamesClient, type GeoNamesClientOptions, type GeoNamesIndexMetadata, type LocalBirthInput, type LocalTimeResolution, createGeoNamesClient, offsetAt, resolveBirth, resolveLocalToUtc };
|
package/dist/geo.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateBirthSettings
|
|
3
|
+
} from "./chunk-PFGUCNOK.js";
|
|
4
|
+
|
|
5
|
+
// src/geo/geonames.ts
|
|
6
|
+
var fold = (value) => value.normalize("NFD").replace(/[\u0300-\u036f]/gu, "").toLowerCase();
|
|
7
|
+
function joinedUrl(baseUrl, file) {
|
|
8
|
+
return `${baseUrl.replace(/\/$/u, "")}/${file}`;
|
|
9
|
+
}
|
|
10
|
+
function invalidData(resource) {
|
|
11
|
+
throw new TypeError(`Invalid GeoNames ${resource} data.`);
|
|
12
|
+
}
|
|
13
|
+
function nonemptyString(value) {
|
|
14
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
15
|
+
}
|
|
16
|
+
function nonnegativeInteger(value) {
|
|
17
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
18
|
+
}
|
|
19
|
+
function stringTable(value, nonempty = false) {
|
|
20
|
+
if (!Array.isArray(value)) invalidData("index");
|
|
21
|
+
const table = [];
|
|
22
|
+
for (const item of value) {
|
|
23
|
+
if (typeof item !== "string" || nonempty && !nonemptyString(item)) invalidData("index");
|
|
24
|
+
table.push(item);
|
|
25
|
+
}
|
|
26
|
+
return table;
|
|
27
|
+
}
|
|
28
|
+
function validateIndex(value) {
|
|
29
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) invalidData("index");
|
|
30
|
+
const own = (key) => Object.getOwnPropertyDescriptor(value, key)?.value;
|
|
31
|
+
const version = own("version");
|
|
32
|
+
const source = own("source");
|
|
33
|
+
const count = own("count");
|
|
34
|
+
if (version !== 1 || !nonemptyString(source) || !nonnegativeInteger(count)) invalidData("index");
|
|
35
|
+
const tz = stringTable(own("tz"), true);
|
|
36
|
+
const admin1 = stringTable(own("admin1"));
|
|
37
|
+
const countries = stringTable(own("countries"));
|
|
38
|
+
const shards = stringTable(own("shards"));
|
|
39
|
+
if (shards.some((key) => key.length !== 1 || !/^[a-z0]$/u.test(key)) || new Set(shards).size !== shards.length) {
|
|
40
|
+
invalidData("index");
|
|
41
|
+
}
|
|
42
|
+
return { version, source, count, tz, admin1, countries, shards };
|
|
43
|
+
}
|
|
44
|
+
function tableIndex(value, table) {
|
|
45
|
+
return nonnegativeInteger(value) && value < table.length;
|
|
46
|
+
}
|
|
47
|
+
function coordinate(value, maximum) {
|
|
48
|
+
return typeof value === "number" && Number.isSafeInteger(value) && Math.abs(value) <= maximum;
|
|
49
|
+
}
|
|
50
|
+
function validateShard(value, index) {
|
|
51
|
+
if (!Array.isArray(value)) invalidData("shard");
|
|
52
|
+
const rows = [];
|
|
53
|
+
for (const row of value) {
|
|
54
|
+
if (!Array.isArray(row) || row.length !== 8) invalidData("shard");
|
|
55
|
+
const [name, ascii, admin1, country, latitude, longitude, timeZone, population] = row;
|
|
56
|
+
if (!nonemptyString(name) || ascii !== 0 && !nonemptyString(ascii) || !tableIndex(admin1, index.admin1) || !tableIndex(country, index.countries) || !coordinate(latitude, 9e3) || !coordinate(longitude, 18e3) || !tableIndex(timeZone, index.tz) || !nonnegativeInteger(population)) {
|
|
57
|
+
invalidData("shard");
|
|
58
|
+
}
|
|
59
|
+
rows.push([name, ascii, admin1, country, latitude, longitude, timeZone, population]);
|
|
60
|
+
}
|
|
61
|
+
return rows;
|
|
62
|
+
}
|
|
63
|
+
function createGeoNamesClient(options) {
|
|
64
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
65
|
+
if (!fetcher) throw new Error("A fetch implementation is required.");
|
|
66
|
+
if (!options.baseUrl.trim()) throw new RangeError("baseUrl is required.");
|
|
67
|
+
let indexPromise;
|
|
68
|
+
const shardCache = /* @__PURE__ */ new Map();
|
|
69
|
+
async function fetchJson(url) {
|
|
70
|
+
const response = await fetcher(url);
|
|
71
|
+
if (!response.ok) throw new Error(`GeoNames fetch failed: ${response.status}`);
|
|
72
|
+
return response.json();
|
|
73
|
+
}
|
|
74
|
+
function index() {
|
|
75
|
+
if (!indexPromise) {
|
|
76
|
+
const request = fetchJson(joinedUrl(options.baseUrl, "index.json")).then(validateIndex).catch((error) => {
|
|
77
|
+
if (indexPromise === request) indexPromise = void 0;
|
|
78
|
+
throw error;
|
|
79
|
+
});
|
|
80
|
+
indexPromise = request;
|
|
81
|
+
}
|
|
82
|
+
return indexPromise;
|
|
83
|
+
}
|
|
84
|
+
function shard(key, loaded) {
|
|
85
|
+
let request = shardCache.get(key);
|
|
86
|
+
if (!request) {
|
|
87
|
+
request = fetchJson(joinedUrl(options.baseUrl, `${key}.json`)).then((value) => validateShard(value, loaded)).catch((error) => {
|
|
88
|
+
if (shardCache.get(key) === request) shardCache.delete(key);
|
|
89
|
+
throw error;
|
|
90
|
+
});
|
|
91
|
+
shardCache.set(key, request);
|
|
92
|
+
}
|
|
93
|
+
return request;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
async preload() {
|
|
97
|
+
const loaded = await index();
|
|
98
|
+
return {
|
|
99
|
+
version: loaded.version,
|
|
100
|
+
source: loaded.source,
|
|
101
|
+
count: loaded.count,
|
|
102
|
+
timeZones: [...loaded.tz],
|
|
103
|
+
shards: [...loaded.shards]
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
async searchCities(query, limit = 8) {
|
|
107
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
108
|
+
throw new RangeError("limit must be a positive integer.");
|
|
109
|
+
}
|
|
110
|
+
const needle = fold(query.trim());
|
|
111
|
+
if (needle.length < 2) return [];
|
|
112
|
+
const loaded = await index();
|
|
113
|
+
const key = /^[a-z]/u.test(needle) ? needle[0] ?? "0" : "0";
|
|
114
|
+
if (!loaded.shards.includes(key)) return [];
|
|
115
|
+
const rows = await shard(key, loaded);
|
|
116
|
+
const starts = [];
|
|
117
|
+
const contains = [];
|
|
118
|
+
for (const row of rows) {
|
|
119
|
+
const searchable = fold(typeof row[1] === "string" ? row[1] : row[0]);
|
|
120
|
+
if (searchable.startsWith(needle)) starts.push(row);
|
|
121
|
+
else if (needle.length >= 3 && searchable.includes(needle)) {
|
|
122
|
+
contains.push(row);
|
|
123
|
+
}
|
|
124
|
+
if (starts.length >= limit * 3) break;
|
|
125
|
+
}
|
|
126
|
+
return [...starts, ...contains].slice(0, limit).flatMap((row) => {
|
|
127
|
+
const timeZone = loaded.tz[row[6]];
|
|
128
|
+
if (!timeZone) return [];
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
name: row[0],
|
|
132
|
+
admin1: loaded.admin1[row[2]] ?? "",
|
|
133
|
+
country: loaded.countries[row[3]] ?? "",
|
|
134
|
+
latitude: row[4] / 100,
|
|
135
|
+
longitude: row[5] / 100,
|
|
136
|
+
timeZone,
|
|
137
|
+
population: row[7]
|
|
138
|
+
}
|
|
139
|
+
];
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/geo/timezone.ts
|
|
146
|
+
var offsetFormatters = /* @__PURE__ */ new Map();
|
|
147
|
+
var wallFormatters = /* @__PURE__ */ new Map();
|
|
148
|
+
function validateTimeZone(timeZone) {
|
|
149
|
+
if (typeof timeZone !== "string" || timeZone.trim().length === 0) {
|
|
150
|
+
throw new RangeError("timeZone must be an explicit nonempty timezone string.");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function offsetFormatter(timeZone) {
|
|
154
|
+
validateTimeZone(timeZone);
|
|
155
|
+
let formatter = offsetFormatters.get(timeZone);
|
|
156
|
+
if (!formatter) {
|
|
157
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
158
|
+
timeZone,
|
|
159
|
+
calendar: "gregory",
|
|
160
|
+
numberingSystem: "latn",
|
|
161
|
+
timeZoneName: "longOffset"
|
|
162
|
+
});
|
|
163
|
+
offsetFormatters.set(timeZone, formatter);
|
|
164
|
+
}
|
|
165
|
+
return formatter;
|
|
166
|
+
}
|
|
167
|
+
function wallFormatter(timeZone) {
|
|
168
|
+
validateTimeZone(timeZone);
|
|
169
|
+
let formatter = wallFormatters.get(timeZone);
|
|
170
|
+
if (!formatter) {
|
|
171
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
172
|
+
timeZone,
|
|
173
|
+
calendar: "gregory",
|
|
174
|
+
numberingSystem: "latn",
|
|
175
|
+
era: "short",
|
|
176
|
+
year: "numeric",
|
|
177
|
+
month: "2-digit",
|
|
178
|
+
day: "2-digit",
|
|
179
|
+
hour: "2-digit",
|
|
180
|
+
minute: "2-digit",
|
|
181
|
+
second: "2-digit",
|
|
182
|
+
fractionalSecondDigits: 3,
|
|
183
|
+
hourCycle: "h23"
|
|
184
|
+
});
|
|
185
|
+
wallFormatters.set(timeZone, formatter);
|
|
186
|
+
}
|
|
187
|
+
return formatter;
|
|
188
|
+
}
|
|
189
|
+
function offsetAt(timeZone, utcMilliseconds) {
|
|
190
|
+
if (typeof utcMilliseconds !== "number" || !Number.isFinite(utcMilliseconds)) {
|
|
191
|
+
throw new RangeError("utcMilliseconds must be a finite epoch-millisecond timestamp.");
|
|
192
|
+
}
|
|
193
|
+
const name = offsetFormatter(timeZone).formatToParts(utcMilliseconds).find((part) => part.type === "timeZoneName")?.value;
|
|
194
|
+
const match = (name ?? "GMT").match(/^GMT(?:([+-])(\d{1,2})(?::(\d{2}))?(?::(\d{2}))?)?$/u);
|
|
195
|
+
if (!match) {
|
|
196
|
+
throw new RangeError(`Could not read UTC offset for timezone: ${timeZone}`);
|
|
197
|
+
}
|
|
198
|
+
const sign = match[1] === "-" ? -1 : 1;
|
|
199
|
+
return sign * (Number(match[2] ?? 0) * 60 + Number(match[3] ?? 0) + Number(match[4] ?? 0) / 60);
|
|
200
|
+
}
|
|
201
|
+
function wallStringAt(timeZone, utcMilliseconds) {
|
|
202
|
+
const parts = wallFormatter(timeZone).formatToParts(utcMilliseconds);
|
|
203
|
+
const part = (type) => parts.find((candidate) => candidate.type === type)?.value ?? "";
|
|
204
|
+
const era = part("era");
|
|
205
|
+
if (era !== "AD" && era !== "BC") {
|
|
206
|
+
throw new RangeError(`Could not read Gregorian era for timezone: ${timeZone}`);
|
|
207
|
+
}
|
|
208
|
+
const year = era === "BC" ? 1 - Number(part("year")) : Number(part("year"));
|
|
209
|
+
const isoYear = year >= 0 && year <= 9999 ? String(year).padStart(4, "0") : `${year < 0 ? "-" : "+"}${String(Math.abs(year)).padStart(6, "0")}`;
|
|
210
|
+
return `${isoYear}-${part("month").padStart(2, "0")}-${part("day").padStart(2, "0")}T${part("hour").padStart(2, "0")}:${part("minute").padStart(2, "0")}:${part("second").padStart(2, "0")}.${part("fractionalSecond").padStart(3, "0")}`;
|
|
211
|
+
}
|
|
212
|
+
function wallMilliseconds(date, time) {
|
|
213
|
+
if (typeof date !== "string" || typeof time !== "string") {
|
|
214
|
+
throw new RangeError("Local date/time must use YYYY-MM-DD and HH:MM strings.");
|
|
215
|
+
}
|
|
216
|
+
const dateMatch = date.match(/^(\d{4})-(\d{2})-(\d{2})$/u);
|
|
217
|
+
const timeMatch = time.match(/^(\d{2}):(\d{2})$/u);
|
|
218
|
+
if (!dateMatch || dateMatch[0] !== date || !timeMatch || timeMatch[0] !== time) {
|
|
219
|
+
throw new RangeError("Local date/time must use YYYY-MM-DD and HH:MM.");
|
|
220
|
+
}
|
|
221
|
+
const year = Number(dateMatch[1]);
|
|
222
|
+
const month = Number(dateMatch[2]);
|
|
223
|
+
const day = Number(dateMatch[3]);
|
|
224
|
+
const hour = Number(timeMatch[1]);
|
|
225
|
+
const minute = Number(timeMatch[2]);
|
|
226
|
+
if (month < 1 || month > 12 || hour > 23 || minute > 59) {
|
|
227
|
+
throw new RangeError("Local date/time contains an out-of-range field.");
|
|
228
|
+
}
|
|
229
|
+
const check = /* @__PURE__ */ new Date(0);
|
|
230
|
+
check.setUTCFullYear(year, month - 1, day);
|
|
231
|
+
check.setUTCHours(hour, minute, 0, 0);
|
|
232
|
+
if (check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day) {
|
|
233
|
+
throw new RangeError("Local date is not a calendar date.");
|
|
234
|
+
}
|
|
235
|
+
return check.getTime();
|
|
236
|
+
}
|
|
237
|
+
function resolveLocalToUtc(date, time, timeZone) {
|
|
238
|
+
const localMilliseconds = wallMilliseconds(date, time);
|
|
239
|
+
const wall = `${date}T${time}:00.000`;
|
|
240
|
+
const sampledOffsets = [
|
|
241
|
+
offsetAt(timeZone, localMilliseconds - 36 * 36e5),
|
|
242
|
+
offsetAt(timeZone, localMilliseconds),
|
|
243
|
+
offsetAt(timeZone, localMilliseconds + 36 * 36e5)
|
|
244
|
+
];
|
|
245
|
+
const candidates = [...new Set(sampledOffsets)];
|
|
246
|
+
const matches = [];
|
|
247
|
+
for (const offset of candidates) {
|
|
248
|
+
const utcMilliseconds = localMilliseconds - Math.round(offset * 6e4);
|
|
249
|
+
if (wallStringAt(timeZone, utcMilliseconds) === wall) {
|
|
250
|
+
matches.push({
|
|
251
|
+
utcMilliseconds,
|
|
252
|
+
offset: offsetAt(timeZone, utcMilliseconds)
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const flags = [];
|
|
257
|
+
let chosen;
|
|
258
|
+
if (matches.length === 1) {
|
|
259
|
+
const match = matches[0];
|
|
260
|
+
if (!match) throw new Error("Timezone match disappeared.");
|
|
261
|
+
chosen = match;
|
|
262
|
+
} else if (matches.length > 1) {
|
|
263
|
+
matches.sort((a, b) => a.utcMilliseconds - b.utcMilliseconds);
|
|
264
|
+
const match = matches[0];
|
|
265
|
+
if (!match) throw new Error("Timezone fold match disappeared.");
|
|
266
|
+
chosen = match;
|
|
267
|
+
flags.push("dst-fold");
|
|
268
|
+
} else {
|
|
269
|
+
const before = offsetAt(timeZone, localMilliseconds - 36 * 36e5);
|
|
270
|
+
const utcMilliseconds = localMilliseconds - Math.round(before * 6e4);
|
|
271
|
+
chosen = {
|
|
272
|
+
utcMilliseconds,
|
|
273
|
+
offset: offsetAt(timeZone, utcMilliseconds)
|
|
274
|
+
};
|
|
275
|
+
flags.push("dst-gap");
|
|
276
|
+
}
|
|
277
|
+
if (Math.abs(chosen.offset % 1) > 1e-9) flags.push("lmt");
|
|
278
|
+
return {
|
|
279
|
+
utc: new Date(chosen.utcMilliseconds),
|
|
280
|
+
offsetMinutes: chosen.offset,
|
|
281
|
+
flags
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function resolveBirth(input) {
|
|
285
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
286
|
+
throw new RangeError("birth must be an object containing a local date and timezone.");
|
|
287
|
+
}
|
|
288
|
+
const settings = validateBirthSettings(input);
|
|
289
|
+
const time = input.time;
|
|
290
|
+
const timeKnown = settings.timeKnown ?? time !== void 0;
|
|
291
|
+
if (timeKnown && time === void 0) {
|
|
292
|
+
throw new RangeError("time is required when timeKnown is true.");
|
|
293
|
+
}
|
|
294
|
+
const resolution = resolveLocalToUtc(
|
|
295
|
+
input.date,
|
|
296
|
+
time === void 0 ? "12:00" : time,
|
|
297
|
+
input.timeZone
|
|
298
|
+
);
|
|
299
|
+
return {
|
|
300
|
+
utc: resolution.utc,
|
|
301
|
+
timeKnown,
|
|
302
|
+
houseSystem: settings.houseSystem ?? "whole",
|
|
303
|
+
flags: resolution.flags,
|
|
304
|
+
...settings.latitude === void 0 ? {} : { latitude: settings.latitude, longitude: settings.longitude }
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
export {
|
|
308
|
+
createGeoNamesClient,
|
|
309
|
+
offsetAt,
|
|
310
|
+
resolveBirth,
|
|
311
|
+
resolveLocalToUtc
|
|
312
|
+
};
|