Skip to content

Commit

Permalink
refactor: ♻️ implement strict checks in tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 13, 2024
1 parent 423b80d commit b1515cb
Show file tree
Hide file tree
Showing 97 changed files with 568 additions and 413 deletions.
2 changes: 1 addition & 1 deletion examples/commonjs/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Run the examples using node.

Example: node ./examples/commonjs/satellite.js
Example: node ./examples/commonjs/satellite-js-migration.js
52 changes: 43 additions & 9 deletions examples/commonjs/satellite-js-migration.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { Satellite, Sgp4, GroundPosition, calcGmst } = require('../../lib/ootk-core');
const { Satellite, Sgp4, GroundPosition, calcGmst, DEG2RAD } = require('../../commonjs/index.js');

// Example Date
const exampleDate = new Date(1705109326817);

// Sample TLE
const tle1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992';
const tle2 = '2 25544 51.6433 59.2583 0008217 16.4489 347.6017 15.51174618173442';
const tle1 = '1 56006U 23042W 24012.45049317 .00000296 00000-0 36967-4 0 9992';
const tle2 = '2 56006 43.0043 13.3620 0001137 267.5965 92.4747 15.02542972 44491';

// Initialize a Satellite Object
const satellite = new Satellite({
Expand All @@ -26,8 +29,8 @@ const velocityEci = positionAndVelocity.velocity;

// Set the Observer at 122.03 West by 36.96 North, in DEGREES (because who likes working in radians?)
const observer = new GroundPosition({
lon: -122.0308,
lat: 36.9613422,
lon: -71.0308,
lat: 41.9613422,
alt: 0.37,
});

Expand All @@ -37,14 +40,17 @@ const { gmst, j } = calcGmst(new Date());
// You can get ECF, Geodetic, Look Angles, and Doppler Factor.
const positionEcf = satellite.ecf();
const observerEcf = observer.ecf();
const positionGd = satellite.lla();
const lookAngles = satellite.rae(observer);
const positionGd = satellite.lla(exampleDate);
const lookAngles = satellite.rae(observer, exampleDate);
// This never worked in satellite.js, but it does now!
const dopplerFactor = satellite.dopplerFactor(observer);
const uplinkFreq = 420e6;
const dopplerFactor = satellite.dopplerFactor(observer, exampleDate);
let dopplerShiftedFrequency = uplinkFreq * dopplerFactor;
dopplerShiftedFrequency = satellite.applyDoppler(uplinkFreq, observer, exampleDate);

// The coordinates are all stored in strongly typed key-value pairs.
// ECI and ECF are accessed by `x`, `y`, `z` properties.
const position = satellite.eci().position;
const position = satellite.eci(exampleDate).position;
const satelliteX = position.x;
const satelliteY = position.y;
const satelliteZ = position.z;
Expand All @@ -69,3 +75,31 @@ const latitudeRad = latitude * DEG2RAD;
// There is no need to use the units seen in TypeScript examples.
// const longitudeRad = (longitude * DEG2RAD) as Radians;
// const latitudeRad = (latitude * DEG2RAD) as Radians;

console.log('Satellite.js Migration Example');
console.log('======================================');
console.log('TLE: ', tle1);
console.log(' ', tle2);
console.log('======================================');
console.log('Position (ECI):');
console.log(' x: ', satelliteX);
console.log(' y: ', satelliteY);
console.log(' z: ', satelliteZ);
console.log('Position (ECF):');
console.log(' x: ', positionEcf.x);
console.log(' y: ', positionEcf.y);
console.log(' z: ', positionEcf.z);
console.log('Position (Geodetic):');
console.log(' longitude: ', longitude);
console.log(' latitude: ', latitude);
console.log(' height: ', height);
console.log('Look Angles:');
console.log(' azimuth: ', azimuthDegrees);
console.log(' elevation: ', elevationDegrees);
console.log(' rangeSat: ', rangeSat);
console.log(' rangeRate: ', rangeRate);
console.log('Doppler Factor:');
console.log(' dopplerFactor: ', dopplerFactor);
dopplerShiftedFrequency = dopplerShiftedFrequency / 1e6; // Hz to MHz
console.log(' 420MHz: ', `${dopplerShiftedFrequency.toPrecision(6)} MHz`);
console.log('======================================');
52 changes: 43 additions & 9 deletions examples/mjs/satellite-js-migration.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { calcGmst, GroundPosition, Satellite, Sgp4 } from '../../lib/ootk-core';
import { calcGmst, GroundPosition, Satellite, Sgp4 } from '../../lib/index.mjs';

// Example Date
const exampleDate = new Date(1705109326817);

// Sample TLE
const tle1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992';
const tle2 = '2 25544 51.6433 59.2583 0008217 16.4489 347.6017 15.51174618173442';
const tle1 = '1 56006U 23042W 24012.45049317 .00000296 00000-0 36967-4 0 9992';
const tle2 = '2 56006 43.0043 13.3620 0001137 267.5965 92.4747 15.02542972 44491';

// Initialize a Satellite Object
const satellite = new Satellite({
Expand All @@ -26,8 +29,8 @@ const velocityEci = positionAndVelocity.velocity;

// Set the Observer at 122.03 West by 36.96 North, in DEGREES (because who likes working in radians?)
const observer = new GroundPosition({
lon: -122.0308,
lat: 36.9613422,
lon: -71.0308,
lat: 41.9613422,
alt: 0.37,
});

Expand All @@ -37,14 +40,17 @@ const { gmst, j } = calcGmst(new Date());
// You can get ECF, Geodetic, Look Angles, and Doppler Factor.
const positionEcf = satellite.ecf();
const observerEcf = observer.ecf();
const positionGd = satellite.lla();
const lookAngles = satellite.rae(observer);
const positionGd = satellite.lla(exampleDate);
const lookAngles = satellite.rae(observer, exampleDate);
// This never worked in satellite.js, but it does now!
const dopplerFactor = satellite.dopplerFactor(observer);
const uplinkFreq = 420e6;
const dopplerFactor = satellite.dopplerFactor(observer, exampleDate);
let dopplerShiftedFrequency = uplinkFreq * dopplerFactor;
dopplerShiftedFrequency = satellite.applyDoppler(uplinkFreq, observer, exampleDate);

// The coordinates are all stored in strongly typed key-value pairs.
// ECI and ECF are accessed by `x`, `y`, `z` properties.
const position = satellite.eci().position;
const position = satellite.eci(exampleDate).position;
const satelliteX = position.x;
const satelliteY = position.y;
const satelliteZ = position.z;
Expand All @@ -69,3 +75,31 @@ const latitudeRad = latitude * DEG2RAD;
// There is no need to use the units seen in TypeScript examples.
// const longitudeRad = (longitude * DEG2RAD) as Radians;
// const latitudeRad = (latitude * DEG2RAD) as Radians;

console.log('Satellite.js Migration Example');
console.log('======================================');
console.log('TLE: ', tle1);
console.log(' ', tle2);
console.log('======================================');
console.log('Position (ECI):');
console.log(' x: ', satelliteX);
console.log(' y: ', satelliteY);
console.log(' z: ', satelliteZ);
console.log('Position (ECF):');
console.log(' x: ', positionEcf.x);
console.log(' y: ', positionEcf.y);
console.log(' z: ', positionEcf.z);
console.log('Position (Geodetic):');
console.log(' longitude: ', longitude);
console.log(' latitude: ', latitude);
console.log(' height: ', height);
console.log('Look Angles:');
console.log(' azimuth: ', azimuthDegrees);
console.log(' elevation: ', elevationDegrees);
console.log(' rangeSat: ', rangeSat);
console.log(' rangeRate: ', rangeRate);
console.log('Doppler Factor:');
console.log(' dopplerFactor: ', dopplerFactor);
dopplerShiftedFrequency = dopplerShiftedFrequency / 1e6; // Hz to MHz
console.log(' 420MHz: ', `${dopplerShiftedFrequency.toPrecision(6)} MHz`);
console.log('======================================');
2 changes: 1 addition & 1 deletion examples/typescript/satellite-js-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Sgp4,
TleLine1,
TleLine2,
} from '../../src/ootk-core';
} from '../../src/index';

// Sample TLE
const tle1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992' as TleLine1;
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/sun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Degrees, Meters, Sun } from '../../lib/ootk-core';
import { Degrees, Meters, Sun } from '../../lib/index';
/* eslint-disable no-console */

console.log(Sun.getTimes(new Date(), 41 as Degrees, -71 as Degrees, 0 as Meters));
2 changes: 1 addition & 1 deletion examples/typescript/tle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { Tle, TleLine1, TleLine2 } from '../../lib/ootk-core';
import { Tle, TleLine1, TleLine2 } from '../../lib/index';

// Sample TLE
const tle1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992' as TleLine1;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0-1",
"description": "Orbital Object Toolkit Core including SGP4 Propagator and Coordinate Transforms",
"scripts": {
"mjs": "ttsc -p tsconfig.build.json -m esnext",
"mjs": "ttsc -p tsconfig.build.json -m esnext && node ./scripts/makeMjs.mjs",
"cjs": "ttsc -p tsconfig.c.json -m commonjs",
"build": "node ./scripts/cleanup.mjs && npm run mjs && npm run cjs",
"lint": "eslint src",
Expand Down Expand Up @@ -57,4 +57,4 @@
"typescript-transform-paths": "^3.4.6"
},
"homepage": "https://github.com/thkruz/ootk"
}
}
9 changes: 8 additions & 1 deletion scripts/cleanup.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
/* eslint-disable no-console */
import rimraf from 'rimraf';

// eslint-disable-next-line no-console
console.log('Removing ./lib...');
try {
rimraf.sync('./lib');
} catch (error) {
// Intentionally left blank
}

console.log('Removing ./commonjs...');
try {
rimraf.sync('./commonjs');
} catch (error) {
// Intentionally left blank
}
5 changes: 5 additions & 0 deletions scripts/makeMjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cpSync } from 'fs';

cpSync('./lib/index.js', './lib/index.mjs');
cpSync('./lib/index.js.map', './lib/index.mjs.map');
cpSync('./lib/index.d.ts', './lib/index.d.mts');
5 changes: 0 additions & 5 deletions scripts/moveMjs.mjs

This file was deleted.

3 changes: 2 additions & 1 deletion src/body/Celestial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AzEl, Degrees, RaDec, Radians } from '..';
import { AzEl, Degrees, Kilometers, RaDec, Radians } from '..';
import { RAD2DEG } from '../utils/constants';
import { Sun } from './Sun';

Expand All @@ -11,6 +11,7 @@ export class Celestial {
const c: RaDec = {
ra,
dec,
dist: 0 as Kilometers,
};
const azEl = Sun.azEl(date, lat, lon, c);

Expand Down
18 changes: 11 additions & 7 deletions src/body/Moon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class Moon {
*/
const next = Math.min(nextNewMoon, nextFirstQuarter, nextFullMoon, nextThirdQuarter);
// eslint-disable-next-line init-declarations
let phase;
let phase: (typeof Moon.moonCycles_)[0] | null = null;

for (const moonCycle of Moon.moonCycles_) {
if (phaseValue >= moonCycle.from && phaseValue <= moonCycle.to) {
Expand All @@ -232,6 +232,10 @@ export class Moon {
}
}

if (!phase) {
throw new Error('Moon phase not found');
}

let type = '';

if (next === nextNewMoon) {
Expand Down Expand Up @@ -315,12 +319,12 @@ export class Moon {
const { rise, set, ye } = Moon.calculateRiseSetTimes_(date, lat, lon);

const result = {
rise: null,
set: null,
ye: null,
alwaysUp: null,
alwaysDown: null,
highest: null,
rise: NaN as Date | number,
set: NaN as Date | number,
ye: null as number | null,
alwaysUp: null as boolean | null,
alwaysDown: null as boolean | null,
highest: null as Date | null,
};

if (rise) {
Expand Down
11 changes: 7 additions & 4 deletions src/body/Sun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,20 @@ export class Sun {
const result = {
solarNoon: Sun.julian2date(Jnoon),
nadir: Sun.julian2date(Jnoon + 0.5), // https://github.com/mourner/suncalc/pull/125
};
} as SunTime;

// Add all other unique times using Jnoon as a reference
for (let i = 0, len = Sun.times_.length; i < len; i += 1) {
time = Sun.times_[i];
h0 = <Meters>((time[0] + dh) * DEG2RAD);
const angle = time[0] as Degrees;

h0 = <Meters>((angle + dh) * DEG2RAD);

Jset = Sun.getSetJ_(h0, lw, phi, dec, n, M, L);
Jrise = Jnoon - (Jset - Jnoon);

result[time[1]] = Sun.julian2date(Jrise);
result[time[2]] = Sun.julian2date(Jset);
result[time[1] as string] = Sun.julian2date(Jrise);
result[time[2] as string] = Sun.julian2date(Jset);
}

return result;
Expand Down Expand Up @@ -481,6 +483,7 @@ export class Sun {
return {
dec: Celestial.declination(L, 0),
ra: Celestial.rightAscension(L, 0),
dist: 0 as Kilometers,
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/coordinate/TLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
* DEALINGS IN THE SOFTWARE.
*/

import { ClassicalElements, FormatTle, TEME } from '.';
import { Sgp4, Vector3D } from '..';
import { Earth } from '../body';
import { ClassicalElements, FormatTle, TEME } from '../coordinate';
import { Sgp4OpsMode } from '../enums/Sgp4OpsMode';
import { Sgp4GravConstants } from '../sgp4/sgp4';
import { EpochUTC } from '../time/EpochUTC';
Expand Down Expand Up @@ -945,7 +945,9 @@ export class Tle {
const values = sccNum.toUpperCase().split('');

if (values[0] in Tle.alpha5_) {
values[0] = Tle.alpha5_[values[0]];
const firstLetter = values[0] as keyof typeof Tle.alpha5_;

values[0] = Tle.alpha5_[firstLetter];
}

return values.join('');
Expand Down
2 changes: 1 addition & 1 deletion src/coordinate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * from './RelativeState';
export * from './RIC';
export * from './StateVector';
export * from './TEME';
export * from './TLE';
export * from './Tle';
Loading

0 comments on commit b1515cb

Please sign in to comment.